diff options
Diffstat (limited to 'include/functions_search.inc.php')
-rw-r--r-- | include/functions_search.inc.php | 184 |
1 files changed, 108 insertions, 76 deletions
diff --git a/include/functions_search.inc.php b/include/functions_search.inc.php index 66be23bb4..9cf50d602 100644 --- a/include/functions_search.inc.php +++ b/include/functions_search.inc.php @@ -2,7 +2,7 @@ // +-----------------------------------------------------------------------+ // | Piwigo - a PHP based photo gallery | // +-----------------------------------------------------------------------+ -// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | +// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | // +-----------------------------------------------------------------------+ @@ -21,12 +21,16 @@ // | USA. | // +-----------------------------------------------------------------------+ +/** + * @package functions\search + */ + /** - * returns search rules stored into a serialized array in "search" + * Returns search rules stored into a serialized array in "search" * table. Each search rules set is numericaly identified. * - * @param int search_id + * @param int $search_id * @return array */ function get_search_array($search_id) @@ -47,12 +51,10 @@ SELECT rules } /** - * returns the SQL clause from a search identifier + * Returns the SQL clause for a search. + * Transforms the array returned by get_search_array() into SQL sub-query. * - * Search rules are stored in search table as a serialized array. This array - * need to be transformed into an SQL clause to be used in queries. - * - * @param array search + * @param array $search * @return string */ function get_sql_search_clause($search) @@ -68,18 +70,15 @@ function get_sql_search_clause($search) $local_clauses = array(); foreach ($search['fields'][$textfield]['words'] as $word) { - array_push($local_clauses, $textfield." LIKE '%".$word."%'"); + $local_clauses[] = $textfield." LIKE '%".$word."%'"; } // adds brackets around where clauses $local_clauses = prepend_append_array_items($local_clauses, '(', ')'); - array_push( - $clauses, - implode( - ' '.$search['fields'][$textfield]['mode'].' ', - $local_clauses - ) + $clauses[] = implode( + ' '.$search['fields'][$textfield]['mode'].' ', + $local_clauses ); } } @@ -100,15 +99,12 @@ function get_sql_search_clause($search) $field_clauses = array(); foreach ($fields as $field) { - array_push($field_clauses, $field." LIKE '%".$word."%'"); + $field_clauses[] = $field." LIKE '%".$word."%'"; } // adds brackets around where clauses - array_push( - $word_clauses, - implode( - "\n OR ", - $field_clauses - ) + $word_clauses[] = implode( + "\n OR ", + $field_clauses ); } @@ -117,26 +113,24 @@ function get_sql_search_clause($search) create_function('&$s','$s="(".$s.")";') ); - array_push( - $clauses, - "\n ". + // make sure the "mode" is either OR or AND + if ($search['fields']['allwords']['mode'] != 'AND' and $search['fields']['allwords']['mode'] != 'OR') + { + $search['fields']['allwords']['mode'] = 'AND'; + } + + $clauses[] = "\n ". implode( - "\n ". - $search['fields']['allwords']['mode']. - "\n ", + "\n ". $search['fields']['allwords']['mode']. "\n ", $word_clauses - ) - ); + ); } foreach (array('date_available', 'date_creation') as $datefield) { if (isset($search['fields'][$datefield])) { - array_push( - $clauses, - $datefield." = '".$search['fields'][$datefield]['date']."'" - ); + $clauses[] = $datefield." = '".$search['fields'][$datefield]['date']."'"; } foreach (array('after','before') as $suffix) @@ -145,15 +139,10 @@ function get_sql_search_clause($search) if (isset($search['fields'][$key])) { - array_push( - $clauses, - - $datefield. + $clauses[] = $datefield. ($suffix == 'after' ? ' >' : ' <'). ($search['fields'][$key]['inc'] ? '=' : ''). - " '".$search['fields'][$key]['date']."'" - - ); + " '".$search['fields'][$key]['date']."'"; } } } @@ -171,7 +160,7 @@ function get_sql_search_clause($search) } $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; - array_push($clauses, $local_clause); + $clauses[] = $local_clause; } // adds brackets around where clauses @@ -189,12 +178,13 @@ function get_sql_search_clause($search) } /** - * returns the list of items corresponding to the advanced search array + * Returns the list of items corresponding to the advanced search array. * - * @param array search + * @param array $search + * @param string $images_where optional additional restriction on images table * @return array */ -function get_regular_search_results($search, $images_where) +function get_regular_search_results($search, $images_where='') { global $conf; $forbidden = get_sql_condition_FandF( @@ -265,34 +255,55 @@ SELECT DISTINCT(id) return $items; } - +/** + * Finds if a char is a letter, a figure or any char of the extended ASCII table (>127). + * + * @param char $ch + * @return bool + */ function is_word_char($ch) { return ($ch>='0' && $ch<='9') || ($ch>='a' && $ch<='z') || ($ch>='A' && $ch<='Z') || ord($ch)>127; } +/** + * Finds if a char is a special token for word start: [{<=*+ + * + * @param char $ch + * @return bool + */ function is_odd_wbreak_begin($ch) { return strpos('[{<=*+', $ch)===false ? false:true; } +/** + * Finds if a char is a special token for word end: ]}>=*+ + * + * @param char $ch + * @return bool + */ 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); +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 + * 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'. + * + * @param string $q + * @param array &$qtokens + * @param array &$qtoken_modifiers */ function analyse_qsearch($q, &$qtokens, &$qtoken_modifiers) { @@ -372,12 +383,12 @@ function analyse_qsearch($q, &$qtokens, &$qtoken_modifiers) $qtoken_modifiers = array(); for ($i=0; $i<count($tokens); $i++) { - if (strstr($token_modifiers[$i], 'q')===false) + if ( !($token_modifiers[$i] & QST_QUOTED) ) { if ( substr($tokens[$i], -1)=='*' ) { $tokens[$i] = rtrim($tokens[$i], '*'); - $token_modifiers[$i] .= '*'; + $token_modifiers[$i] |= QST_WILDCARD_END; } } if ( strlen($tokens[$i])==0) @@ -387,11 +398,15 @@ function analyse_qsearch($q, &$qtokens, &$qtoken_modifiers) } } - /** - * returns the LIKE sql clause corresponding to the quick search query - * that has been split into tokens + * Returns the LIKE SQL clause corresponding to the quick search query + * that has been split into tokens. * for example file LIKE '%john%' OR file LIKE '%bill%'. + * + * @param array $tokens + * @param array $token_modifiers + * @param string $field + * @return string|null */ function get_qsearch_like_clause($tokens, $token_modifiers, $field) { @@ -412,7 +427,14 @@ function get_qsearch_like_clause($tokens, $token_modifiers, $field) } /** -*/ + * Returns tags corresponding to the quick search query that has been split into tokens. + * + * @param array $tokens + * @param array $token_modifiers + * @param array &$token_tag_ids + * @param array &$not_tag_ids + * @param array &$all_tags + */ function get_qsearch_tags($tokens, $token_modifiers, &$token_tag_ids, &$not_tag_ids, &$all_tags) { $token_tag_ids = array_fill(0, count($tokens), array() ); @@ -549,6 +571,8 @@ SELECT t.*, COUNT(image_id) AS counter { array_splice($token_tag_ids[$i], $j, 1); array_splice($token_tag_scores[$i], $j, 1); + $j--; + continue; } $counter += $all_tags[$tag_id]['counter']; @@ -560,27 +584,30 @@ SELECT t.*, COUNT(image_id) AS counter } } } - + usort($all_tags, 'tag_alpha_compare'); foreach ( $all_tags as &$tag ) - $tag['name'] = trigger_event('render_tag_name', $tag['name']); + { + $tag['name'] = trigger_event('render_tag_name', $tag['name'], $tag); + } } /** - * returns the search results corresponding to a quick/query search. + * Returns the search results corresponding to a quick/query search. * A quick/query search returns many items (search is not strict), but results * are sorted by relevance unless $super_order_by is true. Returns: - * array ( - * 'items' => 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 - * )) + * array ( + * 'items' => array of matching images + * '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 + * @param string $q + * @param bool $super_order_by + * @param string $images_where optional additional restriction on images table * @return array */ function get_quick_search_results($q, $super_order_by, $images_where='') @@ -766,22 +793,26 @@ SELECT DISTINCT(id) $allowed_images = array_flip( $allowed_images ); $divisor = 5.0 * count($allowed_images); - foreach ($allowed_images as $id=>$rank ) + foreach ($allowed_images as $id=> &$rank ) { $weight = isset($by_weights[$id]) ? $by_weights[$id] : 1; $weight -= $rank/$divisor; - $allowed_images[$id] = $weight; + $rank = $weight; } + unset($rank); + 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 + * Returns an array of 'items' corresponding to the search id. + * It can be either a quick search or a regular search. * - * @param int search id - * @param string images_where optional aditional restriction on images table + * @param int $search_id + * @param bool $super_order_by + * @param string $images_where optional aditional restriction on images table * @return array */ function get_search_results($search_id, $super_order_by, $images_where='') @@ -797,4 +828,5 @@ function get_search_results($search_id, $super_order_by, $images_where='') return get_quick_search_results($search['q'], $super_order_by, $images_where); } } + ?>
\ No newline at end of file |