plugins/gallery/search.php
changeset 14 e47e4be549ad
parent 13 f6ca7cead82c
child 15 ac7d3dedcc44
equal deleted inserted replaced
12:3893b99cd443 14:e47e4be549ad
    15 //
    15 //
    16 // Search results hook
    16 // Search results hook
    17 //
    17 //
    18 
    18 
    19 $plugins->attachHook('search_results', 'gal_searcher($q, $offset);');
    19 $plugins->attachHook('search_results', 'gal_searcher($q, $offset);');
       
    20 $plugins->attachHook('search_global_inner', 'snapr_search_new_api($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);');
    20 
    21 
    21 $plugins->attachHook('compile_template', '
    22 $plugins->attachHook('compile_template', '
    22   // CSS for gallery browser
    23   // CSS for gallery browser
    23   $template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />\');
    24   $template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/browser.css" type="text/css" />\');
    24   $template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />\');
    25   $template->add_header(\'<link rel="stylesheet" href="' . scriptPath . '/plugins/gallery/dropdown.css" type="text/css" />\');
    25   ');
    26   ');
    26 
    27 
    27 function gal_searcher($q, $offset)
    28 function gal_searcher($q, $offset)
    28 {
    29 {
    29   global $db, $session, $paths, $template, $plugins; // Common objects
    30   global $db, $session, $paths, $template, $plugins; // Common objects
       
    31   if ( defined('SNAPR_SEARCH_USING_NEW_API') )
       
    32     return false;
    30   
    33   
    31   $fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)';
    34   $fulltext_col = 'MATCH(img_title, img_desc) AGAINST (\'' . $db->escape($q) . '\' IN BOOLEAN MODE)';
    32   $sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery
    35   $sql = "SELECT img_id, img_title, img_desc, is_folder, $fulltext_col AS score, CHAR_LENGTH(img_desc) AS length FROM ".table_prefix."gallery
    33               WHERE $fulltext_col > 0
    36               WHERE $fulltext_col > 0
    34                 AND ( ( is_folder=1 AND folder_parent IS NULL ) OR is_folder!=1 )
    37                 AND ( ( is_folder=1 AND folder_parent IS NULL ) OR is_folder!=1 )
    39     return false;
    42     return false;
    40   }
    43   }
    41   echo "<h3>Image results</h3>";
    44   echo "<h3>Image results</h3>";
    42   if ( $row = $db->fetchrow() )
    45   if ( $row = $db->fetchrow() )
    43   {
    46   {
    44     echo '<table border="0" cellspacing="8"><tr>';
    47     echo '<ul class="snapr-gallery">';
    45     $renderer = new SnaprFormatter();
    48     $renderer = new SnaprFormatter();
    46     $fullpage = $paths->fullpage;
    49     $fullpage = $paths->fullpage;
    47     $paths->fullpage = $paths->nslist['Special'] . 'Gallery';
    50     $paths->fullpage = $paths->nslist['Special'] . 'Gallery';
    48     do
    51     do
    49     {
    52     {
    50       echo $renderer->render(false, $row, false);
    53       echo $renderer->render(false, $row, false);
    51     }
    54     }
    52     while ( $row = $db->fetchrow() );
    55     while ( $row = $db->fetchrow() );
    53     $paths->fullpage = $fullpage;
    56     $paths->fullpage = $fullpage;
    54     echo '</tr></table>';
    57     echo '</ul><span class="menuclear"></span>';
    55   }
    58   }
    56   else
    59   else
    57   {
    60   {
    58     echo '<p>No image results.</p>';
    61     echo '<p>No image results.</p>';
    59   }
    62   }
    60 }
    63 }
    61 
    64 
       
    65 function snapr_search_new_api(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list)
       
    66 {
       
    67   global $db, $session, $paths, $template, $plugins; // Common objects
       
    68   
       
    69   if ( !defined('SNAPR_SEARCH_USING_NEW_API') )
       
    70     define('SNAPR_SEARCH_USING_NEW_API', 1);
       
    71   
       
    72   // Let's do this all in one query
       
    73   $terms = array(
       
    74       'any' => array_merge($query['any'], $query_phrase['any']),
       
    75       'req' => array_merge($query['req'], $query_phrase['req']),
       
    76       'not' => $query['not']
       
    77     );
       
    78   $where = array('any' => array(), 'req' => array(), 'not' => array());
       
    79   $where_any =& $where['any'];
       
    80   $where_req =& $where['req'];
       
    81   $where_not =& $where['not'];
       
    82   $title_col = ( $case_sensitive ) ? 'img_title' : 'lcase(img_title)';
       
    83   $desc_col = ( $case_sensitive ) ? 'img_desc' : 'lcase(img_desc)';
       
    84   foreach ( $terms['any'] as $term )
       
    85   {
       
    86     $term = escape_string_like($term);
       
    87     if ( !$case_sensitive )
       
    88       $term = strtolower($term);
       
    89     $where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
       
    90   }
       
    91   foreach ( $terms['req'] as $term )
       
    92   {
       
    93     $term = escape_string_like($term);
       
    94     if ( !$case_sensitive )
       
    95       $term = strtolower($term);
       
    96     $where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
       
    97   }
       
    98   foreach ( $terms['not'] as $term )
       
    99   {
       
   100     $term = escape_string_like($term);
       
   101     if ( !$case_sensitive )
       
   102       $term = strtolower($term);
       
   103     $where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'";
       
   104   }
       
   105   if ( empty($where_any) )
       
   106     unset($where_any, $where['any']);
       
   107   if ( empty($where_req) )
       
   108     unset($where_req, $where['req']);
       
   109   if ( empty($where_not) )
       
   110     unset($where_not, $where['not']);
       
   111   
       
   112   $where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')';
       
   113   
       
   114   if ( isset($where_req) )
       
   115     $where_req = implode(' AND ', $where_req);
       
   116   if ( isset($where_not) )
       
   117   $where_not = implode( 'AND ', $where_not);
       
   118   
       
   119   $where = implode(' AND ', $where);
       
   120   $sql = "SELECT img_id, img_title, img_desc FROM " . table_prefix . "gallery WHERE ( $where ) AND is_folder = 0;";
       
   121   
       
   122   if ( !($q = $db->sql_unbuffered_query($sql)) )
       
   123   {
       
   124     $db->_die('Error is in auto-generated SQL query in the Snapr plugin search module');
       
   125   }
       
   126   
       
   127   if ( $row = $db->fetchrow() )
       
   128   {
       
   129     do
       
   130     {
       
   131       $idstring = 'ns=Image;pid=' . $row['img_id'];
       
   132       foreach ( $word_list as $term )
       
   133       {
       
   134         if ( $case_sensitive )
       
   135         {
       
   136           if ( strstr($row['img_title'], $term) || strstr($row['img_desc'], $term) )
       
   137           {
       
   138             ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1;
       
   139           }
       
   140         }
       
   141         else
       
   142         {
       
   143           if ( strstr(strtolower($row['img_title']), strtolower($term)) || strstr(strtolower($row['img_desc']), strtolower($term)) )
       
   144           {
       
   145             ( isset($scores[$idstring]) ) ? $scores[$idstring]++ : $scores[$idstring] = 1;
       
   146           }
       
   147         }
       
   148       }
       
   149       // Generate text...
       
   150       $text = highlight_and_clip_search_result(htmlspecialchars($row['img_desc']), $word_list);
       
   151       
       
   152       $preview_and_text = '
       
   153         <table border="0" width="100%" cellspacing="0" cellpadding="0">
       
   154           <tr>
       
   155             <td valign="top">
       
   156               ' . $text . '
       
   157             </td>
       
   158             <td valign="top" style="text-align: right; width: 80px; padding-left: 10px;">
       
   159               <a href="' . makeUrlNS('Image', $row['img_id']) . '"><img alt="[thumbnail]" src="' . makeUrlNS('Special', "GalleryFetcher/thumb/{$row['img_id']}") . '" /></a>
       
   160             </td>
       
   161           </tr>
       
   162         </table>
       
   163       ';
       
   164       
       
   165       // Inject result
       
   166       
       
   167       if ( isset($scores[$idstring]) )
       
   168       {
       
   169         // echo('adding image "' . $row['img_title'] . '" to results<br />');
       
   170         $page_data[$idstring] = array(
       
   171           'page_name' => highlight_search_result(htmlspecialchars($row['img_title']), $word_list),
       
   172           'page_text' => $preview_and_text,
       
   173           'score' => $scores[$idstring],
       
   174           'page_note' => '[Gallery image]',
       
   175           'page_id' => strval($row['img_id']),
       
   176           'namespace' => 'Image',
       
   177           'page_length' => strlen($row['img_desc']),
       
   178         );
       
   179       }
       
   180     }
       
   181     while ( $row = $db->fetchrow() );
       
   182     
       
   183   }
       
   184 }
       
   185 
    62 ?>
   186 ?>