Added search module
authorDan
Thu, 31 Jan 2008 21:35:46 -0500
changeset 2 4e7762863437
parent 1 6e76ca311f2d
child 3 a050ff3d4509
Added search module
plugins/Nuggie.php
plugins/nuggie/postbit.php
plugins/nuggie/search.php
--- a/plugins/Nuggie.php	Tue Dec 11 19:05:43 2007 -0500
+++ b/plugins/Nuggie.php	Thu Jan 31 21:35:46 2008 -0500
@@ -42,6 +42,10 @@
     {
       require( ENANO_ROOT . "/plugins/nuggie/usercp.php" );
     }
+    else if ( $page_id == "Search" && $namespace == "Special" )
+    {
+      require( ENANO_ROOT . "/plugins/nuggie/search.php" );
+    }
   ');
 
 $plugins->attachHook('acl_rule_init', 'nuggie_namespace_setup();');
--- a/plugins/nuggie/postbit.php	Tue Dec 11 19:05:43 2007 -0500
+++ b/plugins/nuggie/postbit.php	Thu Jan 31 21:35:46 2008 -0500
@@ -315,6 +315,7 @@
     $template->header();
     echo '&lt; <a href="' . makeUrlNS('Blog', $row['username']) . '">' . htmlspecialchars($row['blog_name']) . '</a>';
     echo $postbit->render_post();
+    display_page_footers();
     $template->footer();
     
     return true;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugins/nuggie/search.php	Thu Jan 31 21:35:46 2008 -0500
@@ -0,0 +1,128 @@
+<?php
+
+/*
+ * Nuggie
+ * Version 0.1
+ * Copyright (C) 2007 Dan Fuhry
+ *
+ * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ */
+
+$plugins->attachHook('search_global_inner', 'nuggie_search($query, $query_phrase, $scores, $page_data, $case_sensitive, $word_list);');
+
+/**
+ * Searches the forums for the specified search terms. Called from a hook.
+ * @access private
+ */
+
+function nuggie_search(&$query, &$query_phrase, &$scores, &$page_data, &$case_sensitive, &$word_list)
+{
+  global $db, $session, $paths, $template, $plugins; // Common objects
+  
+  require_once( ENANO_ROOT . '/plugins/nuggie/postbit.php' );
+  
+  // Based on the search functions from Snapr and Decir
+  
+  // 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 ) ? 'p.post_title' : 'lcase(p.post_title)';
+  $desc_col = ( $case_sensitive ) ? 'p.post_text' : 'lcase(p.post_text)';
+  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 p.post_id, p.post_title, p.post_title_clean, p.post_text, p.post_author, u.username, p.post_timestamp, u.username\n"
+         . "    FROM " . table_prefix . "blog_posts AS p\n"
+         . "  LEFT JOIN " . table_prefix . "users AS u\n"
+         . "    ON ( u.user_id = p.post_author )\n"
+         . "  WHERE ( $where )\n"
+         . "  GROUP BY p.post_id;";
+  
+  if ( !($q = $db->sql_unbuffered_query($sql)) )
+  {
+    $db->_die('Error is in auto-generated SQL query in the Nuggie plugin search module');
+  }
+  
+  if ( $row = $db->fetchrow() )
+  {
+    do
+    {
+      $day = enano_date('d', $row['post_timestamp']);
+      $year = enano_date('Y', $row['post_timestamp']);
+      $month = enano_date('n', $row['post_timestamp']);
+      $username = sanitize_page_id($row['username']);
+      $post_url = "{$username}/$year/$month/$day/{$row['post_title_clean']}";
+      $idstring = "ns=Blog;pid=$post_url";
+      foreach ( $word_list as $term )
+      {
+        $func = ( $case_sensitive ) ? 'strstr' : 'stristr';
+        $inc = ( $func($row['post_title'], $term) ? 1.5 : ( $func($row['post_text'], $term) ? 1 : 0 ) );
+        ( isset($scores[$idstring]) ) ? $scores[$idstring] = $scores[$idstring] + $inc : $scores[$idstring] = $inc;
+      }
+      // Generate text...
+      $post_text = highlight_and_clip_search_result($row['post_text'], $word_list);
+      $post_length = strlen($post_text);
+      
+      // Inject result
+      
+      if ( isset($scores[$idstring]) )
+      {
+        // echo('adding image "' . $row['img_title'] . '" to results<br />');
+        $page_data[$idstring] = array(
+          'page_name' => highlight_search_result(htmlspecialchars($row['post_title']), $word_list),
+          'page_text' => $post_text,
+          'score' => $scores[$idstring],
+          'page_note' => '[Blog post]',
+          'page_id' => $post_url,
+          'namespace' => 'Blog',
+          'page_length' => $post_length,
+        );
+      }
+    }
+    while ( $row = $db->fetchrow() );
+  }
+}