' : ' <'). ($search['fields'][$key]['inc'] ? '=' : ''). " '".$search['fields'][$key]['date']."'" ); } } } if (isset($search['fields']['cat'])) { if ($search['fields']['cat']['sub_inc']) { // searching all the categories id of sub-categories $cat_ids = get_subcat_ids($search['fields']['cat']['words']); } else { $cat_ids = $search['fields']['cat']['words']; } $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; array_push($clauses, $local_clause); } // adds brackets around where clauses $clauses = prepend_append_array_items($clauses, '(', ')'); $where_separator = implode( "\n ".$search['mode'].' ', $clauses ); $search_clause = $where_separator; return $search_clause; } /** * returns the list of items corresponding to the advanced search array * * @param array search * @return array */ function get_regular_search_results($search, $images_where) { global $conf; $forbidden = get_sql_condition_FandF( array ( 'forbidden_categories' => 'category_id', 'visible_categories' => 'category_id', 'visible_images' => 'id' ), "\n AND" ); $items = array(); $tag_items = array(); if (isset($search['fields']['tags'])) { $tag_items = get_image_ids_for_tags( $search['fields']['tags']['words'], $search['fields']['tags']['mode'] ); } $search_clause = get_sql_search_clause($search); if (!empty($search_clause)) { $query = ' SELECT DISTINCT(id) FROM '.IMAGES_TABLE.' i INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id WHERE '.$search_clause; if (!empty($images_where)) { $query .= "\n AND ".$images_where; } $query .= $forbidden.' '.$conf['order_by']; $items = array_from_query($query, 'id'); } if ( !empty($tag_items) ) { switch ($search['mode']) { case 'AND': if (empty($search_clause)) { $items = $tag_items; } else { $items = array_values( array_intersect($items, $tag_items) ); } break; case 'OR': $before_count = count($items); $items = array_unique( array_merge( $items, $tag_items ) ); break; } } return $items; } function is_word_char($ch) { return ($ch>='0' && $ch<='9') || ($ch>='a' && $ch<='z') || ($ch>='A' && $ch<='Z') || ord($ch)>127; } function is_odd_wbreak_begin($ch) { return strpos('[{<=*+', $ch)===false ? false:true; } function is_odd_wbreak_end($ch) { return strpos(']}>=*+', $ch)===false ? false:true; } define('QST_QUOTED', 0x01); define('QST_NOT', 0x02); define('QST_WILDCARD_BEGIN',0x04); define('QST_WILDCARD_END', 0x08); define('QST_WILDCARD', QST_WILDCARD_BEGIN|QST_WILDCARD_END); /** * analyzes and splits the quick/query search query $q into tokens * q='john bill' => 2 tokens 'john' 'bill' * Special characters for MySql full text search (+,<,>,~) appear in the token modifiers. * The query can contain a phrase: 'Pierre "New York"' will return 'pierre' qnd 'new york'. */ function analyse_qsearch($q, &$qtokens, &$qtoken_modifiers) { $q = stripslashes($q); $tokens = array(); $token_modifiers = array(); $crt_token = ""; $crt_token_modifier = 0; for ($i=0; $i<~')==0 ) { //special full text modifier if (strlen($crt_token)) { $crt_token .= $ch; } else { if ( $ch=='*' ) $crt_token_modifier |= QST_WILDCARD_BEGIN; if ( $ch=='-' ) $crt_token_modifier |= QST_NOT; } } elseif (preg_match('/[\s,.;!\?]+/', $ch)) { // white space if (strlen($crt_token)) { $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; $crt_token = ""; } $crt_token_modifier = 0; } else { $crt_token .= $ch; } } else // qualified with quotes { if ($ch=='"') { if ($i+1 < strlen($q) && $q[$i+1]=='*') { $crt_token_modifier |= QST_WILDCARD_END; $i++; } $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; $crt_token = ""; $crt_token_modifier = 0; $state=0; break; } else $crt_token .= $ch; } } if (strlen($crt_token)) { $tokens[] = $crt_token; $token_modifiers[] = $crt_token_modifier; } $qtokens = array(); $qtoken_modifiers = array(); for ($i=0; $i array(85,68,79...) * 'qs' => array( * 'matching_tags' => array of matching tags * 'matching_cats' => array of matching categories * 'matching_cats_no_images' =>array(99) - matching categories without images * )) * * @param string q * @param bool super_order_by * @param string images_where optional aditional restriction on images table * @return array */ function get_quick_search_results($q, $super_order_by, $images_where='') { global $user, $conf; $search_results = array( 'items' => array(), 'qs' => array('q'=>stripslashes($q)), ); $q = trim($q); analyse_qsearch($q, $tokens, $token_modifiers); if (count($tokens)==0) { return $search_results; } $debug[] = ''; global $template; $template->append('footer_elements', implode(', ', $debug) ); if ( $super_order_by or empty($by_weights) ) { $search_results['items'] = $allowed_images; return $search_results; } $allowed_images = array_flip( $allowed_images ); $divisor = 5.0 * count($allowed_images); foreach ($allowed_images as $id=>$rank ) { $weight = isset($by_weights[$id]) ? $by_weights[$id] : 1; $weight -= $rank/$divisor; $allowed_images[$id] = $weight; } arsort($allowed_images, SORT_NUMERIC); $search_results['items'] = array_keys($allowed_images); return $search_results; } /** * returns an array of 'items' corresponding to the search id * * @param int search id * @param string images_where optional aditional restriction on images table * @return array */ function get_search_results($search_id, $super_order_by, $images_where='') { $search = get_search_array($search_id); if ( !isset($search['q']) ) { $result['items'] = get_regular_search_results($search, $images_where); return $result; } else { return get_quick_search_results($search['q'], $super_order_by, $images_where); } } ?>