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