plugins/nuggie/search.php
changeset 2 4e7762863437
child 6 c51809bdf6af
equal deleted inserted replaced
1:6e76ca311f2d 2:4e7762863437
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Nuggie
       
     5  * Version 0.1
       
     6  * Copyright (C) 2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 $plugins->attachHook('search_global_inner', 'nuggie_search($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);');
       
    16 
       
    17 /**
       
    18  * Searches the forums for the specified search terms. Called from a hook.
       
    19  * @access private
       
    20  */
       
    21 
       
    22 function nuggie_search(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list)
       
    23 {
       
    24   global $db, $session, $paths, $template, $plugins; // Common objects
       
    25   
       
    26   require_once( ENANO_ROOT . '/plugins/nuggie/postbit.php' );
       
    27   
       
    28   // Based on the search functions from Snapr and Decir
       
    29   
       
    30   // Let's do this all in one query
       
    31   $terms = array(
       
    32       'any' => array_merge($query['any'], $query_phrase['any']),
       
    33       'req' => array_merge($query['req'], $query_phrase['req']),
       
    34       'not' => $query['not']
       
    35     );
       
    36   $where = array('any' => array(), 'req' => array(), 'not' => array());
       
    37   $where_any =& $where['any'];
       
    38   $where_req =& $where['req'];
       
    39   $where_not =& $where['not'];
       
    40   $title_col = ( $case_sensitive ) ? 'p.post_title' : 'lcase(p.post_title)';
       
    41   $desc_col = ( $case_sensitive ) ? 'p.post_text' : 'lcase(p.post_text)';
       
    42   foreach ( $terms['any'] as $term )
       
    43   {
       
    44     $term = escape_string_like($term);
       
    45     if ( !$case_sensitive )
       
    46       $term = strtolower($term);
       
    47     $where_any[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
       
    48   }
       
    49   foreach ( $terms['req'] as $term )
       
    50   {
       
    51     $term = escape_string_like($term);
       
    52     if ( !$case_sensitive )
       
    53       $term = strtolower($term);
       
    54     $where_req[] = "( $title_col LIKE '%{$term}%' OR $desc_col LIKE '%{$term}%' )";
       
    55   }
       
    56   foreach ( $terms['not'] as $term )
       
    57   {
       
    58     $term = escape_string_like($term);
       
    59     if ( !$case_sensitive )
       
    60       $term = strtolower($term);
       
    61     $where_not[] = "$title_col NOT LIKE '%{$term}%' AND $desc_col NOT LIKE '%{$term}%'";
       
    62   }
       
    63   if ( empty($where_any) )
       
    64     unset($where_any, $where['any']);
       
    65   if ( empty($where_req) )
       
    66     unset($where_req, $where['req']);
       
    67   if ( empty($where_not) )
       
    68     unset($where_not, $where['not']);
       
    69   
       
    70   $where_any = '(' . implode(' OR ', $where_any) . '' . ( isset($where['req']) || isset($where['not']) ? ' OR 1 = 1' : '' ) . ')';
       
    71   
       
    72   if ( isset($where_req) )
       
    73     $where_req = implode(' AND ', $where_req);
       
    74   if ( isset($where_not) )
       
    75   $where_not = implode( 'AND ', $where_not);
       
    76   
       
    77   $where = implode(' AND ', $where);
       
    78   $sql = "SELECT p.post_id, p.post_title, p.post_title_clean, p.post_text, p.post_author, u.username, p.post_timestamp, u.username\n"
       
    79          . "    FROM " . table_prefix . "blog_posts AS p\n"
       
    80          . "  LEFT JOIN " . table_prefix . "users AS u\n"
       
    81          . "    ON ( u.user_id = p.post_author )\n"
       
    82          . "  WHERE ( $where )\n"
       
    83          . "  GROUP BY p.post_id;";
       
    84   
       
    85   if ( !($q = $db->sql_unbuffered_query($sql)) )
       
    86   {
       
    87     $db->_die('Error is in auto-generated SQL query in the Nuggie plugin search module');
       
    88   }
       
    89   
       
    90   if ( $row = $db->fetchrow() )
       
    91   {
       
    92     do
       
    93     {
       
    94       $day = enano_date('d', $row['post_timestamp']);
       
    95       $year = enano_date('Y', $row['post_timestamp']);
       
    96       $month = enano_date('n', $row['post_timestamp']);
       
    97       $username = sanitize_page_id($row['username']);
       
    98       $post_url = "{$username}/$year/$month/$day/{$row['post_title_clean']}";
       
    99       $idstring = "ns=Blog;pid=$post_url";
       
   100       foreach ( $word_list as $term )
       
   101       {
       
   102         $func = ( $case_sensitive ) ? 'strstr' : 'stristr';
       
   103         $inc = ( $func($row['post_title'], $term) ? 1.5 : ( $func($row['post_text'], $term) ? 1 : 0 ) );
       
   104         ( isset($scores[$idstring]) ) ? $scores[$idstring] = $scores[$idstring] + $inc : $scores[$idstring] = $inc;
       
   105       }
       
   106       // Generate text...
       
   107       $post_text = highlight_and_clip_search_result($row['post_text'], $word_list);
       
   108       $post_length = strlen($post_text);
       
   109       
       
   110       // Inject result
       
   111       
       
   112       if ( isset($scores[$idstring]) )
       
   113       {
       
   114         // echo('adding image "' . $row['img_title'] . '" to results<br />');
       
   115         $page_data[$idstring] = array(
       
   116           'page_name' => highlight_search_result(htmlspecialchars($row['post_title']), $word_list),
       
   117           'page_text' => $post_text,
       
   118           'score' => $scores[$idstring],
       
   119           'page_note' => '[Blog post]',
       
   120           'page_id' => $post_url,
       
   121           'namespace' => 'Blog',
       
   122           'page_length' => $post_length,
       
   123         );
       
   124       }
       
   125     }
       
   126     while ( $row = $db->fetchrow() );
       
   127   }
       
   128 }