plugins/SpecialSearch.php
changeset 334 c72b545f1304
parent 317 f8356d9c3481
child 335 67bd3121a12e
equal deleted inserted replaced
333:32429702305e 334:c72b545f1304
    18  *
    18  *
    19  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    19  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
    20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
    21  */
    21  */
    22 
    22 
    23 $plugins->attachHook('base_classes_initted', '
    23 $plugins->attachHook('session_started', '
    24   global $paths;
    24   global $paths;
    25     $paths->add_page(Array(
    25     $paths->add_page(Array(
    26       \'name\'=>\'Rebuild search index\',
    26       \'name\'=>\'specialpage_search_rebuild\',
    27       \'urlname\'=>\'SearchRebuild\',
    27       \'urlname\'=>\'SearchRebuild\',
    28       \'namespace\'=>\'Special\',
    28       \'namespace\'=>\'Special\',
    29       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    29       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    30       ));
    30       ));
    31     
    31     
    32     $paths->add_page(Array(
    32     $paths->add_page(Array(
    33       \'name\'=>\'Search\',
    33       \'name\'=>\'specialpage_search\',
    34       \'urlname\'=>\'Search\',
    34       \'urlname\'=>\'Search\',
    35       \'namespace\'=>\'Special\',
    35       \'namespace\'=>\'Special\',
    36       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    36       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    37       ));
    37       ));
    38     ');
    38     ');
    39 
    39 
    40 function page_Special_SearchRebuild()
    40 function page_Special_SearchRebuild()
    41 {
    41 {
    42   global $db, $session, $paths, $template, $plugins; // Common objects
    42   global $db, $session, $paths, $template, $plugins; // Common objects
    43   if(!$session->get_permissions('mod_misc')) die_friendly('Unauthorized', '<p>You need to be an administrator to rebuild the search index</p>');
    43   if ( !$session->get_permissions('mod_misc') )
       
    44   {
       
    45     die_friendly('Unauthorized', '<p>You need to be an administrator to rebuild the search index</p>');
       
    46   }
    44   $template->header();
    47   $template->header();
    45   @set_time_limit(0);
    48   @set_time_limit(0);
    46   if($paths->rebuild_search_index(true))
    49   if($paths->rebuild_search_index(true))
    47     echo '<p>Index rebuilt!</p>';
    50     echo '<p>Index rebuilt!</p>';
    48   else
    51   else
    52 
    55 
    53 function page_Special_Search()
    56 function page_Special_Search()
    54 {
    57 {
    55   global $db, $session, $paths, $template, $plugins; // Common objects
    58   global $db, $session, $paths, $template, $plugins; // Common objects
    56   global $aggressive_optimize_html;
    59   global $aggressive_optimize_html;
       
    60   global $lang;
       
    61   
    57   $aggressive_optimize_html = false;
    62   $aggressive_optimize_html = false;
    58   
    63   
    59   if ( !$q = $paths->getParam(0) )
    64   if ( !$q = $paths->getParam(0) )
    60     $q = ( isset($_GET['q']) ) ? $_GET['q'] : '';
    65     $q = ( isset($_GET['q']) ) ? $_GET['q'] : '';
    61   
    66   
    93   
    98   
    94   $template->header();
    99   $template->header();
    95   
   100   
    96   $qin = ( isset($q) ) ? str_replace('"', '\"', htmlspecialchars($q)) : '';
   101   $qin = ( isset($q) ) ? str_replace('"', '\"', htmlspecialchars($q)) : '';
    97   $search_form = '<form action="' . makeUrlNS('Special', 'Search') . '">
   102   $search_form = '<form action="' . makeUrlNS('Special', 'Search') . '">
    98   <input type="text" tabindex="1" name="q" size="50" value="' . $qin . '" />&nbsp;<input tabindex="2" type="submit" value="Search" />&nbsp;<a href="' . makeUrlNS('Special', 'Search') . '">Advanced search</a>
   103   <input type="text" tabindex="1" name="q" size="50" value="' . $qin . '" />&nbsp;<input tabindex="2" type="submit" value="' . $lang->get('search_btn_search') . '" />&nbsp;<a href="' . makeUrlNS('Special', 'Search') . '">' . $lang->get('search_btn_advanced_search') . '</a>
    99   ' . ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ) . '
   104   ' . ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' ) . '
   100   </form>';
   105   </form>';
   101   
   106   
   102   if ( !empty($q) )
   107   if ( !empty($q) )
   103   {
   108   {
   104     $search_start = microtime_float();
   109     $search_start = microtime_float();
   105     
   110     
   106     $results = perform_search($q, $warn, ( isset($_GET['match_case']) ));
   111     $results = perform_search($q, $warn, ( isset($_GET['match_case']) ), $word_list);
   107     $warn = array_unique($warn);
   112     $warn = array_unique($warn);
   108     
   113     
   109     if ( file_exists( ENANO_ROOT . '/themes/' . $template->theme . '/search-result.tpl' ) )
   114     if ( file_exists( ENANO_ROOT . '/themes/' . $template->theme . '/search-result.tpl' ) )
   110     {
   115     {
   111       $parser = $template->makeParser('search-result.tpl');
   116       $parser = $template->makeParser('search-result.tpl');
   120         <p>
   125         <p>
   121          <h3><a href="{RESULT_URL}"><span class="search-result-annotation">{PAGE_NOTE}</span>{PAGE_TITLE}</a></h3>
   126          <h3><a href="{RESULT_URL}"><span class="search-result-annotation">{PAGE_NOTE}</span>{PAGE_TITLE}</a></h3>
   122           {PAGE_TEXT}
   127           {PAGE_TEXT}
   123           <span class="search-result-url">{PAGE_URL}</span> - 
   128           <span class="search-result-url">{PAGE_URL}</span> - 
   124           <!-- BEGINNOT special_page --><span class="search-result-info">{PAGE_LENGTH} {PAGE_LENGTH_UNIT}</span> -<!-- END special_page --> 
   129           <!-- BEGINNOT special_page --><span class="search-result-info">{PAGE_LENGTH} {PAGE_LENGTH_UNIT}</span> -<!-- END special_page --> 
   125           <span class="search-result-info">Relevance: {RELEVANCE_SCORE}%</span>
   130           <span class="search-result-info">{lang:search_lbl_relevance} {RELEVANCE_SCORE}%</span>
   126         </p>
   131         </p>
   127       </div>
   132       </div>
   128       
   133       
   129       <!-- Finish search result -->
   134       <!-- Finish search result -->
   130       
   135       
   136       $result =& $results[$i];
   141       $result =& $results[$i];
   137       $result['page_text'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="search-term">', '</span>'), $result['page_text']);
   142       $result['page_text'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="search-term">', '</span>'), $result['page_text']);
   138       if ( !empty($result['page_text']) )
   143       if ( !empty($result['page_text']) )
   139         $result['page_text'] .= '<br />';
   144         $result['page_text'] .= '<br />';
   140       $result['page_name'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="title-search-term">', '</span>'), $result['page_name']);
   145       $result['page_name'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="title-search-term">', '</span>'), $result['page_name']);
       
   146       $result['url_highlight'] = str_replace(array('<highlight>', '</highlight>'), array('<span class="url-search-term">', '</span>'), $result['url_highlight']);
   141       if ( $result['page_length'] >= 1048576 )
   147       if ( $result['page_length'] >= 1048576 )
   142       {
   148       {
   143         $result['page_length'] = round($result['page_length'] / 1048576, 1);
   149         $result['page_length'] = round($result['page_length'] / 1048576, 1);
   144         $length_unit = 'MB';
   150         $length_unit = $lang->get('etc_unit_megabytes_short');
   145       }
   151       }
   146       else if ( $result['page_length'] >= 1024 )
   152       else if ( $result['page_length'] >= 1024 )
   147       {
   153       {
   148         $result['page_length'] = round($result['page_length'] / 1024, 1);
   154         $result['page_length'] = round($result['page_length'] / 1024, 1);
   149         $length_unit = 'KB';
   155         $length_unit = $lang->get('etc_unit_kilobytes_short');
   150       }
   156       }
   151       else
   157       else
   152       {
   158       {
   153         $length_unit = 'bytes';
   159         $length_unit = $lang->get('etc_unit_bytes');
   154       }
   160       }
   155       $url = makeUrlComplete($result['namespace'], $result['page_id']);
   161       //$url = makeUrlComplete($result['namespace'], $result['page_id']);
   156       $url = preg_replace('/\?.+$/', '', $url);
   162       //$url = preg_replace('/\?.+$/', '', $url);
       
   163       $url = $result['url_highlight'];
       
   164       
   157       $parser->assign_vars(array(
   165       $parser->assign_vars(array(
   158          'PAGE_TITLE' => $result['page_name'],
   166          'PAGE_TITLE' => $result['page_name'],
   159          'PAGE_TEXT' => $result['page_text'],
   167          'PAGE_TEXT' => $result['page_text'],
   160          'PAGE_LENGTH' => $result['page_length'],
   168          'PAGE_LENGTH' => $result['page_length'],
   161          'RELEVANCE_SCORE' => $result['score'],
   169          'RELEVANCE_SCORE' => $result['score'],
   191     $search_time = round($search_time, 3);
   199     $search_time = round($search_time, 3);
   192     
   200     
   193     $q_trim = ( strlen($q) > 30 ) ? substr($q, 0, 27) . '...' : $q;
   201     $q_trim = ( strlen($q) > 30 ) ? substr($q, 0, 27) . '...' : $q;
   194     $q_trim = htmlspecialchars($q_trim);
   202     $q_trim = htmlspecialchars($q_trim);
   195     
   203     
   196     $result_string = ( count($results) > 0 ) ? "Results <b>$start_string</b> - <b>$per_string</b> of about <b>$num_results</b> for <b>" . $q_trim . "</b> in {$search_time}s." : 'No results.';
   204     $result_detail = $lang->get('search_msg_result_detail', array(
       
   205         'start_string' => $start_string,
       
   206         'per_string' => $per_string,
       
   207         'q_trim' => $q_trim,
       
   208         'num_results' => $num_results,
       
   209         'search_time' => $search_time
       
   210       ));
       
   211     $result_string = ( count($results) > 0 ) ? $result_detail : $lang->get('search_msg_no_results');
   197     
   212     
   198     echo '<div class="search-hibar">
   213     echo '<div class="search-hibar">
   199             <div style="float: right;">
   214             <div style="float: right;">
   200               ' . $result_string . '
   215               ' . $result_string . '
   201             </div>
   216             </div>
   202             <b>Site search</b>
   217             <b>' . $lang->get('search_lbl_site_search') . '</b>
   203           </div>
   218           </div>
   204           <div class="search-lobar">
   219           <div class="search-lobar">
   205             ' . $search_form . '
   220             ' . $search_form . '
   206           </div>';
   221           </div>';
   207           
   222           
   208     if ( count($warn) > 0 )
   223     if ( count($warn) > 0 )
   209     {
   224     {
   210       echo '<div class="warning-box" style="margin: 10px 0 0 0;">';
   225       echo '<div class="warning-box" style="margin: 10px 0 0 0;">';
   211       echo '<b>Some problems were encountered during your search.</b><br />
   226       echo '<b>' . $lang->get('search_err_query_title') . '</b><br />
   212             There was a problem with your search query, and as a result there may be a reduced number of search results.';
   227             ' . $lang->get('search_err_query_body');
   213       echo '<ul><li>' . implode('</li><li>', $warn) . '</li></ul>';
   228       echo '<ul><li>' . implode('</li><li>', $warn) . '</li></ul>';
   214       echo '</div>';
   229       echo '</div>';
   215     }
   230     }
   216   
   231   
   217     if ( count($results) > 0 )
   232     if ( count($results) > 0 )
   252       <?php 
   267       <?php 
   253       echo ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' );
   268       echo ( $session->auth_level > USER_LEVEL_MEMBER ? '<input type="hidden" name="auth" value="' . $session->sid_super . '" />' : '' );
   254       endif; ?>
   269       endif; ?>
   255       <div class="tblholder">
   270       <div class="tblholder">
   256         <table border="0" style="width: 100%;" cellspacing="1" cellpadding="4">
   271         <table border="0" style="width: 100%;" cellspacing="1" cellpadding="4">
   257           <tr><th colspan="2">Advanced Search</th></tr>
   272           <tr><th colspan="2"><?php echo $lang->get('search_th_advanced_search'); ?></th></tr>
   258           <tr>
   273           <tr>
   259             <td class="row1">Search for pages with <b>any of these words</b>:</td>
   274             <td class="row1"><?php echo $lang->get('search_lbl_field_any'); ?></td>
   260             <td class="row1"><input type="text" name="words_any" size="40" /></td>
   275             <td class="row1"><input type="text" name="words_any" size="40" /></td>
   261           </tr>
   276           </tr>
   262           <tr>
   277           <tr>
   263             <td class="row2">with <b>this exact phrase</b>:</td>
   278             <td class="row2"><?php echo $lang->get('search_lbl_field_exact'); ?></td>
   264             <td class="row2"><input type="text" name="exact_phrase" size="40" /></td>
   279             <td class="row2"><input type="text" name="exact_phrase" size="40" /></td>
   265           </tr>
   280           </tr>
   266           <tr>
   281           <tr>
   267             <td class="row1">with <b>none of these words</b>:</td>
   282             <td class="row1"><?php echo $lang->get('search_lbl_field_none'); ?></td>
   268             <td class="row1"><input type="text" name="exclude_words" size="40" /></td>
   283             <td class="row1"><input type="text" name="exclude_words" size="40" /></td>
   269           </tr>
   284           </tr>
   270           <tr>
   285           <tr>
   271             <td class="row2">with <b>all of these words</b>:</td>
   286             <td class="row2"><?php echo $lang->get('search_lbl_field_all'); ?></td>
   272             <td class="row2"><input type="text" name="require_words" size="40" /></td>
   287             <td class="row2"><input type="text" name="require_words" size="40" /></td>
   273           </tr>
   288           </tr>
   274           <tr>
   289           <tr>
   275             <td class="row1">
   290             <td class="row1">
   276               <label for="chk_case">Case-sensitive search:</label>
   291               <label for="chk_case"><?php echo $lang->get('search_lbl_field_casesensitive'); ?></label>
   277             </td>
   292             </td>
   278             <td class="row1">
   293             <td class="row1">
   279               <input type="checkbox" name="match_case" id="chk_case" />
   294               <input type="checkbox" name="match_case" id="chk_case" />
   280             </td>
   295             </td>
   281           </tr>
   296           </tr>
   282           <tr>
   297           <tr>
   283             <th colspan="2" class="subhead">
   298             <th colspan="2" class="subhead">
   284               <input type="submit" name="do_search" value="Search" />
   299               <input type="submit" name="do_search" value="<?php echo $lang->get('search_btn_search'); ?>" />
   285             </td>
   300             </td>
   286           </tr>
   301           </tr>
   287         </table>
   302         </table>
   288       </div>
   303       </div>
   289     </form>
   304     </form>