# HG changeset patch
# User Dan
# Date 1201833346 18000
# Node ID 4e7762863437ff469b2f413888d7c9cf2d79595a
# Parent 6e76ca311f2d5a9f35cf71d0a46c77cb7b05c707
Added search module
diff -r 6e76ca311f2d -r 4e7762863437 plugins/Nuggie.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();');
diff -r 6e76ca311f2d -r 4e7762863437 plugins/nuggie/postbit.php
--- 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 '< ' . htmlspecialchars($row['blog_name']) . '';
echo $postbit->render_post();
+ display_page_footers();
$template->footer();
return true;
diff -r 6e76ca311f2d -r 4e7762863437 plugins/nuggie/search.php
--- /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 @@
+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
');
+ $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() );
+ }
+}