]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/search_engines.php
a distributed -> the distributed
[quix0rs-gnu-social.git] / lib / search_engines.php
index e96570d63bd6a6ffa04fc4c6afb95253f52939a5..2dd7bb49a207d58da577e8e25c99389b2bf9d08c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, Controlez-Vous, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -19,7 +19,8 @@
 
 if (!defined('LACONICA')) { exit(1); }
 
-class SearchEngine {
+class SearchEngine
+{
     protected $target;
     protected $table;
 
@@ -45,7 +46,8 @@ class SearchEngine {
     }
 }
 
-class SphinxSearch extends SearchEngine {
+class SphinxSearch extends SearchEngine
+{
     private $sphinx;
     private $connected;
 
@@ -72,7 +74,7 @@ class SphinxSearch extends SearchEngine {
     {
         //FIXME without LARGEST_POSSIBLE, the most recent results aren't returned
         //      this probably has a large impact on performance
-        $LARGEST_POSSIBLE = 1e6; 
+        $LARGEST_POSSIBLE = 1e6;
 
         if ($rss) {
             $this->sphinx->setLimits($offset, $count, $count, $LARGEST_POSSIBLE);
@@ -103,25 +105,76 @@ class SphinxSearch extends SearchEngine {
     }
 }
 
-class MySQLSearch extends SearchEngine {
+class MySQLSearch extends SearchEngine
+{
     function query($q)
     {
-        if ('identica_people' === $this->table)
-            return $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
-                           'against (\''.addslashes($q).'\')');
-        if ('identica_notices' === $this->table)
-            return $this->target->whereAdd('MATCH(content) ' .
-                           'against (\''.addslashes($q).'\')');
+        if ('identica_people' === $this->table) {
+            $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
+                                    'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
+            if (strtolower($q) != $q) {
+                $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
+                                        'AGAINST (\''.addslashes(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR');
+            }
+            return true;
+        } else if ('identica_notices' === $this->table) {
+
+            // Don't show imported notices
+            $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
+
+            if (strtolower($q) != $q) {
+                $this->target->whereAdd("( MATCH(content) AGAINST ('" . addslashes($q) .
+                    "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
+                    "AGAINST ('"  . addslashes(strtolower($q)) .
+                    "' IN BOOLEAN MODE))");
+            } else {
+                $this->target->whereAdd('MATCH(content) ' .
+                                         'AGAINST (\''.addslashes($q).'\' IN BOOLEAN MODE)');
+            }
+
+            return true;
+        } else {
+            throw new ServerException('Unknown table: ' . $this->table);
+        }
     }
 }
 
-class PGSearch extends SearchEngine {
+class MySQLLikeSearch extends SearchEngine
+{
     function query($q)
     {
-        if ('identica_people' === $this->table)
+        if ('identica_people' === $this->table) {
+            $qry = sprintf('(nickname LIKE "%%%1$s%%" OR '.
+                           ' fullname LIKE "%%%1$s%%" OR '.
+                           ' location LIKE "%%%1$s%%" OR '.
+                           ' bio      LIKE "%%%1$s%%" OR '.
+                           ' homepage LIKE "%%%1$s%%")', addslashes($q));
+        } else if ('identica_notices' === $this->table) {
+            $qry = sprintf('content LIKE "%%%1$s%%"', addslashes($q));
+        } else {
+            throw new ServerException('Unknown table: ' . $this->table);
+        }
+
+        $this->target->whereAdd($qry);
+
+        return true;
+    }
+}
+
+class PGSearch extends SearchEngine
+{
+    function query($q)
+    {
+        if ('identica_people' === $this->table) {
             return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.addslashes($q).'\')');
-        if ('identica_notices' === $this->table)
+        } else if ('identica_notices' === $this->table) {
+
+            // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach
+
             return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
+        } else {
+            throw new ServerException('Unknown table: ' . $this->table);
+        }
     }
 }