diff -r 0944c9354e9c -r 7c6e2e97aa08 plugins/gallery/search.php --- a/plugins/gallery/search.php Sat Aug 21 23:25:41 2010 -0400 +++ b/plugins/gallery/search.php Sat Aug 21 23:32:06 2010 -0400 @@ -20,167 +20,167 @@ $plugins->attachHook('search_global_inner', 'snapr_search_new_api($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);'); $plugins->attachHook('compile_template', ' - // CSS for gallery browser - $template->add_header(\'\'); - $template->add_header(\'\'); - '); + // CSS for gallery browser + $template->add_header(\'\'); + $template->add_header(\'\'); + '); function gal_searcher($q, $offset) { - global $db, $session, $paths, $template, $plugins; // Common objects - if ( defined('SNAPR_SEARCH_USING_NEW_API') || version_compare(enano_version(true), '1.0.2', '>=') ) - return false; - - $fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)'; - $sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery - WHERE $fulltext_col > 0 - AND ( ( is_folder=1 AND folder_parent IS NULL ) OR is_folder!=1 ) - ORDER BY is_folder DESC, score DESC, img_title ASC;"; - if ( !$db->sql_unbuffered_query($sql) ) - { - echo $db->get_error(); - return false; - } - echo "

Image results

"; - if ( $row = $db->fetchrow() ) - { - echo ''; - } - else - { - echo '

No image results.

'; - } + global $db, $session, $paths, $template, $plugins; // Common objects + if ( defined('SNAPR_SEARCH_USING_NEW_API') || version_compare(enano_version(true), '1.0.2', '>=') ) + return false; + + $fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)'; + $sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery + WHERE $fulltext_col > 0 + AND ( ( is_folder=1 AND folder_parent IS NULL ) OR is_folder!=1 ) + ORDER BY is_folder DESC, score DESC, img_title ASC;"; + if ( !$db->sql_unbuffered_query($sql) ) + { + echo $db->get_error(); + return false; + } + echo "

Image results

"; + if ( $row = $db->fetchrow() ) + { + echo ''; + } + else + { + echo '

No image results.

'; + } } function snapr_search_new_api(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list) { - global $db, $session, $paths, $template, $plugins; // Common objects - - if ( !defined('SNAPR_SEARCH_USING_NEW_API') ) - define('SNAPR_SEARCH_USING_NEW_API', 1); - - // Let's do this all in one query - $terms = array( - 'any' => array_merge($query['any'], $query_phrase['any']), - 'req' => array_merge($query['req'], $query_phrase['req']), - 'not' => $query['not'] - ); - $where = array('any' => array(), 'req' => array(), 'not' => array()); - $where_any =& $where['any']; - $where_req =& $where['req']; - $where_not =& $where['not']; - $title_col = ( $case_sensitive ) ? 'img_title' : 'lcase(img_title)'; - $desc_col = ( $case_sensitive ) ? 'img_desc' : 'lcase(img_desc)'; - foreach ( $terms['any'] as $term ) - { - $term = escape_string_like($term); - if ( !$case_sensitive ) - $term = strtolower($term); - $where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; - } - foreach ( $terms['req'] as $term ) - { - $term = escape_string_like($term); - if ( !$case_sensitive ) - $term = strtolower($term); - $where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; - } - foreach ( $terms['not'] as $term ) - { - $term = escape_string_like($term); - if ( !$case_sensitive ) - $term = strtolower($term); - $where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'"; - } - if ( empty($where_any) ) - unset($where_any, $where['any']); - if ( empty($where_req) ) - unset($where_req, $where['req']); - if ( empty($where_not) ) - unset($where_not, $where['not']); - - $where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')'; - - if ( isset($where_req) ) - $where_req = implode(' AND ', $where_req); - if ( isset($where_not) ) - $where_not = implode( 'AND ', $where_not); - - $where = implode(' AND ', $where); - $sql = "SELECT img_id, img_title, img_desc FROM " . table_prefix . "gallery WHERE ( $where ) AND is_folder = 0;"; - - if ( !($q = $db->sql_unbuffered_query($sql)) ) - { - $db->_die('Error is in auto-generated SQL query in the Snapr plugin search module'); - } - - if ( $row = $db->fetchrow() ) - { - do - { - $idstring = 'ns=Gallery;pid=' . $row['img_id']; - foreach ( $word_list as $term ) - { - if ( $case_sensitive ) - { - if ( strstr($row['img_title'], $term) || strstr($row['img_desc'], $term) ) - { - ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; - } - } - else - { - if ( strstr(strtolower($row['img_title']), strtolower($term)) || strstr(strtolower($row['img_desc']), strtolower($term)) ) - { - ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; - } - } - } - // Generate text... - $text = highlight_and_clip_search_result(htmlspecialchars($row['img_desc']), $word_list); - - $preview_and_text = ' - - - - - -
- ' . $text . ' - - [thumbnail] -
- '; - - // Inject result - - if ( isset($scores[$idstring]) ) - { - // echo('adding image "' . $row['img_title'] . '" to results
'); - $page_data[$idstring] = array( - 'page_name' => highlight_search_result(htmlspecialchars($row['img_title']), $word_list), - 'page_text' => $preview_and_text, - 'score' => $scores[$idstring], - 'page_note' => '[Gallery image]', - 'page_id' => strval($row['img_id']), - 'namespace' => 'Gallery', - 'page_length' => strlen($row['img_desc']), - ); - } - } - while ( $row = $db->fetchrow() ); - - } + global $db, $session, $paths, $template, $plugins; // Common objects + + if ( !defined('SNAPR_SEARCH_USING_NEW_API') ) + define('SNAPR_SEARCH_USING_NEW_API', 1); + + // Let's do this all in one query + $terms = array( + 'any' => array_merge($query['any'], $query_phrase['any']), + 'req' => array_merge($query['req'], $query_phrase['req']), + 'not' => $query['not'] + ); + $where = array('any' => array(), 'req' => array(), 'not' => array()); + $where_any =& $where['any']; + $where_req =& $where['req']; + $where_not =& $where['not']; + $title_col = ( $case_sensitive ) ? 'img_title' : 'lcase(img_title)'; + $desc_col = ( $case_sensitive ) ? 'img_desc' : 'lcase(img_desc)'; + foreach ( $terms['any'] as $term ) + { + $term = escape_string_like($term); + if ( !$case_sensitive ) + $term = strtolower($term); + $where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; + } + foreach ( $terms['req'] as $term ) + { + $term = escape_string_like($term); + if ( !$case_sensitive ) + $term = strtolower($term); + $where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )"; + } + foreach ( $terms['not'] as $term ) + { + $term = escape_string_like($term); + if ( !$case_sensitive ) + $term = strtolower($term); + $where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'"; + } + if ( empty($where_any) ) + unset($where_any, $where['any']); + if ( empty($where_req) ) + unset($where_req, $where['req']); + if ( empty($where_not) ) + unset($where_not, $where['not']); + + $where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')'; + + if ( isset($where_req) ) + $where_req = implode(' AND ', $where_req); + if ( isset($where_not) ) + $where_not = implode( 'AND ', $where_not); + + $where = implode(' AND ', $where); + $sql = "SELECT img_id, img_title, img_desc FROM " . table_prefix . "gallery WHERE ( $where ) AND is_folder = 0;"; + + if ( !($q = $db->sql_unbuffered_query($sql)) ) + { + $db->_die('Error is in auto-generated SQL query in the Snapr plugin search module'); + } + + if ( $row = $db->fetchrow() ) + { + do + { + $idstring = 'ns=Gallery;pid=' . $row['img_id']; + foreach ( $word_list as $term ) + { + if ( $case_sensitive ) + { + if ( strstr($row['img_title'], $term) || strstr($row['img_desc'], $term) ) + { + ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; + } + } + else + { + if ( strstr(strtolower($row['img_title']), strtolower($term)) || strstr(strtolower($row['img_desc']), strtolower($term)) ) + { + ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1; + } + } + } + // Generate text... + $text = highlight_and_clip_search_result(htmlspecialchars($row['img_desc']), $word_list); + + $preview_and_text = ' + + + + + +
+ ' . $text . ' + + [thumbnail] +
+ '; + + // Inject result + + if ( isset($scores[$idstring]) ) + { + // echo('adding image "' . $row['img_title'] . '" to results
'); + $page_data[$idstring] = array( + 'page_name' => highlight_search_result(htmlspecialchars($row['img_title']), $word_list), + 'page_text' => $preview_and_text, + 'score' => $scores[$idstring], + 'page_note' => '[Gallery image]', + 'page_id' => strval($row['img_id']), + 'namespace' => 'Gallery', + 'page_length' => strlen($row['img_desc']), + ); + } + } + while ( $row = $db->fetchrow() ); + + } } ?>