<commit_before><?php

function _log( $message ){
	$fp = fopen( "/tmp/candlelights.log", "a+" );
	fputs( $fp, time() . ' - ' . $message );
	fclose( $fp );
}

function make_plaintext( $string ) {
	$text = strip_tags( $string );
	$text = str_replace( array( "\n", "\t", '&nbsp;' ), ' ', $text );
	$text = preg_replace( '/ +/', ' ', $text );
	$text = trim( $text );
	return $text;

}
function make_needle( $needle ) {
	return "%{$needle}%";
}

function make_defined_needle( $needle ) {
	return "__{$needle}__";
}

function make_escape( $string ) {
	if( is_array( $string ) || is_object( $string ) ) return $string;
	/*
	$pattern = array(
		"'" => "\'",
		'"' => '\"',
	);
	return str_replace( array_keys( $pattern ), array_values( $pattern ), $string );
	*/
	return esc_sql( $string );
}

function apply_pattern_to_structure( $pattern, $structure, $escape = array(), $key_filter = 'make_needle', $value_filter = 'make_escape' ) {
	if( !$pattern ) return $structure;
	if( is_object( $pattern ) ) $pattern = (array) $pattern;
	if( !is_array( $pattern ) ) return $pattern;

	$escape = $escape ? $escape : array(
		'post_title', 'post_excerpt', 'post_content', 'post_content_filtered',
	);

	$original = array();
	$escaped = array();
	foreach( $pattern as $key => $value ) {
		if( is_array( $value ) || is_object( $value ) ) continue;
		$key = call_user_func( $key_filter, $key );
		$value = $value;
		$original[$key] = $value;
		if( in_array( $key, $escape ) ) {
			$key = call_user_func( $key_filter, $key . '_escaped' );
			$value = call_user_func( $value_filter, $value );
			$escaped[$key] = $value;
		}
	}
	$pattern = array_merge( $original, $escaped );
	$result = str_replace( array_keys( $pattern ), array_values( $pattern ), $structure );
	return $result;
}

function apply_defined_pattern_to_structure( $structure, $escape = array(), $category = 'user' ) {
	$pattern = array();
	$all = get_defined_constants( true );
	if( $category == 'all' ) {
		foreach( $all as $categorized ) {
			$pattern = array_merge( $pattern, $categorized );
		}
	} else {
		$pattern = $all[$category];
	}
	$result = apply_pattern_to_structure( $pattern, $structure, $escape, 'make_defined_needle', 'make_escape' );
	return $result;
}

function filter_date( $format, $time = '' ){
	$time = $time?$time:time();
	$pattern = array(
		'am' => __( 'am', 'candlelights' ),
		'pm' => __( 'pm', 'candlelights' ),
		'AM' => __( 'AM', 'candlelights' ),
		'PM' => __( 'PM', 'candlelights' ),
	);
	setlocale( LC_ALL, WP_LANG );
	$timezone_backup = date_default_timezone_get();
	date_default_timezone_set( get_option( 'timezone_string' ) );
	$date = date( $format, $time );
	$date = str_replace( array_keys( $pattern ), array_values( $pattern ), $date );
	date_default_timezone_set($timezone_backup);
	return $date;
}

function filter_recurrence_rules( $event ){
	if( $event->recurrence_rules ) {
		$event->event_rules = array();
		$rules = explode( ';', $event->recurrence_rules );
		foreach( $rules as $rule ) {
			list( $key, $value ) = explode( '=', $rule );
			$key = trim( strtolower( $key ) );
			$value = trim( $value );
			$event->event_rules[$key] = $value;
		}
		$event->event_date_start = filter_date( get_option( 'date_format' ), $event->start );
		$event->event_date_end = !$event->instant_event&&$event->event_rules[until] ? filter_date( get_option( 'date_format' ), strtotime( str_replace( array( 'T', 'Z' ), ' ', $event->event_rules[until] ) ) ) : '';
		$event->event_date = sprintf( __( '%s ~ %s', 'candlelights' ), $event->event_date_start, $event->event_date_end );

		if( $event->event_rules[byday] ) {
			$event->event_byday_raw = explode( ',', $event->event_rules[byday] );
			$event->event_byday_array = null;
			foreach( $event->event_byday_raw as $day ) {
				$event->event_byday_array[] = __( trim($day), 'candlelights' );
			}
			$event->event_byday = implode( ',', $event->event_byday_array );
		}
		$event->event_rule = __(
				sprintf(
					__( 'Every %s %s', 'candlelights' ),
					( $event->event_rules[interval] && $event->event_rules[interval] > 1 ? $event->event_rules[interval] : '' ),
					( $event->event_rules[freq] ? __( trim( $event->event_rules[freq] ), 'candlelights' ) : '' )
				),
				'candlelights'
			);
		$event->event_rule .= ( $event->event_rules[byday] ? ' ' . __( $event->event_byday, 'candlelights' ) : '' );
		$event->event_rules = array_filter( $event->event_rules, 'trim' );
	} else {
		$current_year = date('Y', time());
		$event_year = date('Y', $event->start);
		$format = ( strpos( $current_year, $event_year ) !== false && defined('ALT_DATE_FORMAT') ) ? ALT_DATE_FORMAT : get_option( 'date_format' );
		$event->event_date = filter_date( $format, $event->start );
		$event->event_rule = '';
	}
	$event->event_time_start = filter_date( get_option( 'time_format' ), $event->start );
	$event->event_time_end = !$event->instant_event?filter_date( get_option( 'time_format' ), $event->end ):'';
	if( $event->allday ) {
		$event->event_time = __( 'All day', 'candlelights' );
	} else {
		$event->event_time = sprintf( __( '<span class="from">%s</span> <span class="split">~</span> <span class="to">%s</span>', 'candlelights' ), $event->event_time_start, $event->event_time_end );
	}
	return $event;
}

function get_event_posts( $ids ){
	global $wpdb;
	$ida = implode(',', $ids);
	$query = "SELECT * FROM {$wpdb->posts} WHERE ID IN ({$ida})";
	$results = $wpdb->get_results( $query );
	if( $results ){
		foreach( $results as $post ){
			$posts[$post->ID] = $post;
		}
		return $posts;
	} else {
		return false;
	}
}
function get_event_category($entry){
	global $wpdb,$table_prefix;
	$ttid = $entry->term_taxonomy_id;
	if($ttid){
		$db = $table_prefix.'ai1ec_event_category_meta';
		$general = get_term_by( 'term_taxonomy_id', $ttid, 'events_categories' );
		$meta = $wpdb->get_row("SELECT * FROM {$db} WHERE term_id='{$general->term_id}'");
		$category = array_merge((array)$general,(array)$meta);
	}else{
		$category = array();
	}
	return (object)$category;
}
function determine_icon($name){
	$basenamePattern = array(
//		'-' => '_',
	);
	$icon = (object)array('basename' => str_replace(array_keys($basenamePattern),array_values($basenamePattern),$name),);
	$icon->basename = $icon->basename?$icon->basename:ICON_SLUG_DEFAULT;
	$icon->slug = $icon->basename;
	$icon->normal = ICON_PREFIX.$icon->slug.ICON_SUFFIX_NORMAL.ICON_EXTENSION;
	$icon->hover = ICON_PREFIX.$icon->slug.ICON_SUFFIX_HOVER.ICON_EXTENSION;<commit_msg>fix category basename processor<commit_after><?php

function _log( $message ){
	$fp = fopen( "/tmp/candlelights.log", "a+" );
	fputs( $fp, time() . ' - ' . $message );
	fclose( $fp );
}

function make_plaintext( $string ) {
	$text = strip_tags( $string );
	$text = str_replace( array( "\n", "\t", '&nbsp;' ), ' ', $text );
	$text = preg_replace( '/ +/', ' ', $text );
	$text = trim( $text );
	return $text;

}
function make_needle( $needle ) {
	return "%{$needle}%";
}

function make_defined_needle( $needle ) {
	return "__{$needle}__";
}

function make_escape( $string ) {
	if( is_array( $string ) || is_object( $string ) ) return $string;
	/*
	$pattern = array(
		"'" => "\'",
		'"' => '\"',
	);
	return str_replace( array_keys( $pattern ), array_values( $pattern ), $string );
	*/
	return esc_sql( $string );
}

function apply_pattern_to_structure( $pattern, $structure, $escape = array(), $key_filter = 'make_needle', $value_filter = 'make_escape' ) {
	if( !$pattern ) return $structure;
	if( is_object( $pattern ) ) $pattern = (array) $pattern;
	if( !is_array( $pattern ) ) return $pattern;

	$escape = $escape ? $escape : array(
		'post_title', 'post_excerpt', 'post_content', 'post_content_filtered',
	);

	$original = array();
	$escaped = array();
	foreach( $pattern as $key => $value ) {
		if( is_array( $value ) || is_object( $value ) ) continue;
		$key = call_user_func( $key_filter, $key );
		$value = $value;
		$original[$key] = $value;
		if( in_array( $key, $escape ) ) {
			$key = call_user_func( $key_filter, $key . '_escaped' );
			$value = call_user_func( $value_filter, $value );
			$escaped[$key] = $value;
		}
	}
	$pattern = array_merge( $original, $escaped );
	$result = str_replace( array_keys( $pattern ), array_values( $pattern ), $structure );
	return $result;
}

function apply_defined_pattern_to_structure( $structure, $escape = array(), $category = 'user' ) {
	$pattern = array();
	$all = get_defined_constants( true );
	if( $category == 'all' ) {
		foreach( $all as $categorized ) {
			$pattern = array_merge( $pattern, $categorized );
		}
	} else {
		$pattern = $all[$category];
	}
	$result = apply_pattern_to_structure( $pattern, $structure, $escape, 'make_defined_needle', 'make_escape' );
	return $result;
}

function filter_date( $format, $time = '' ){
	$time = $time?$time:time();
	$pattern = array(
		'am' => __( 'am', 'candlelights' ),
		'pm' => __( 'pm', 'candlelights' ),
		'AM' => __( 'AM', 'candlelights' ),
		'PM' => __( 'PM', 'candlelights' ),
	);
	setlocale( LC_ALL, WP_LANG );
	$timezone_backup = date_default_timezone_get();
	date_default_timezone_set( get_option( 'timezone_string' ) );
	$date = date( $format, $time );
	$date = str_replace( array_keys( $pattern ), array_values( $pattern ), $date );
	date_default_timezone_set($timezone_backup);
	return $date;
}

function filter_recurrence_rules( $event ){
	if( $event->recurrence_rules ) {
		$event->event_rules = array();
		$rules = explode( ';', $event->recurrence_rules );
		foreach( $rules as $rule ) {
			list( $key, $value ) = explode( '=', $rule );
			$key = trim( strtolower( $key ) );
			$value = trim( $value );
			$event->event_rules[$key] = $value;
		}
		$event->event_date_start = filter_date( get_option( 'date_format' ), $event->start );
		$event->event_date_end = !$event->instant_event&&$event->event_rules[until] ? filter_date( get_option( 'date_format' ), strtotime( str_replace( array( 'T', 'Z' ), ' ', $event->event_rules[until] ) ) ) : '';
		$event->event_date = sprintf( __( '%s ~ %s', 'candlelights' ), $event->event_date_start, $event->event_date_end );

		if( $event->event_rules[byday] ) {
			$event->event_byday_raw = explode( ',', $event->event_rules[byday] );
			$event->event_byday_array = null;
			foreach( $event->event_byday_raw as $day ) {
				$event->event_byday_array[] = __( trim($day), 'candlelights' );
			}
			$event->event_byday = implode( ',', $event->event_byday_array );
		}
		$event->event_rule = __(
				sprintf(
					__( 'Every %s %s', 'candlelights' ),
					( $event->event_rules[interval] && $event->event_rules[interval] > 1 ? $event->event_rules[interval] : '' ),
					( $event->event_rules[freq] ? __( trim( $event->event_rules[freq] ), 'candlelights' ) : '' )
				),
				'candlelights'
			);
		$event->event_rule .= ( $event->event_rules[byday] ? ' ' . __( $event->event_byday, 'candlelights' ) : '' );
		$event->event_rules = array_filter( $event->event_rules, 'trim' );
	} else {
		$current_year = date('Y', time());
		$event_year = date('Y', $event->start);
		$format = ( strpos( $current_year, $event_year ) !== false && defined('ALT_DATE_FORMAT') ) ? ALT_DATE_FORMAT : get_option( 'date_format' );
		$event->event_date = filter_date( $format, $event->start );
		$event->event_rule = '';
	}
	$event->event_time_start = filter_date( get_option( 'time_format' ), $event->start );
	$event->event_time_end = !$event->instant_event?filter_date( get_option( 'time_format' ), $event->end ):'';
	if( $event->allday ) {
		$event->event_time = __( 'All day', 'candlelights' );
	} else {
		$event->event_time = sprintf( __( '<span class="from">%s</span> <span class="split">~</span> <span class="to">%s</span>', 'candlelights' ), $event->event_time_start, $event->event_time_end );
	}
	return $event;
}

function get_event_posts( $ids ){
	global $wpdb;
	$ida = implode(',', $ids);
	$query = "SELECT * FROM {$wpdb->posts} WHERE ID IN ({$ida})";
	$results = $wpdb->get_results( $query );
	if( $results ){
		foreach( $results as $post ){
			$posts[$post->ID] = $post;
		}
		return $posts;
	} else {
		return false;
	}
}
function get_event_category($entry){
	global $wpdb,$table_prefix;
	$ttid = $entry->term_taxonomy_id;
	if($ttid){
		$db = $table_prefix.'ai1ec_event_category_meta';
		$general = get_term_by( 'term_taxonomy_id', $ttid, 'events_categories' );
		$meta = $wpdb->get_row("SELECT * FROM {$db} WHERE term_id='{$general->term_id}'");
		$category = array_merge((array)$general,(array)$meta);
	}else{
		$category = array();
	}
	return (object)$category;
}
function determine_icon($name){
	$icon = (object)array('basename' => 'c'.md5($name),);
	$icon->basename = $icon->basename?$icon->basename:ICON_SLUG_DEFAULT;
	$icon->slug = $icon->basename;
	$icon->normal = ICON_PREFIX.$icon->slug.ICON_SUFFIX_NORMAL.ICON_EXTENSION;
	$icon->hover = ICON_PREFIX.$icon->slug.ICON_SUFFIX_HOVER.ICON_EXTENSION;
{'fraction_non_alphanumeric': 0.20236140433184768, 'fraction_numerical': 0.000390960982093987, 'mean_word_length': 4.826879271070615, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 156, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4002470', 'n_tokens_mistral': 5010, 'n_tokens_neox': 4826, 'n_words': 888}