We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

search_engines.php 5.07 KB
Newer Older
millette's avatar
millette committed
1 2
<?php
/*
3
 * StatusNet - the distributed open-source microblogging tool
4
 * Copyright (C) 2008, 2009, StatusNet, Inc.
millette's avatar
millette committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
 * the Free Software Foundation, either version 3 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
millette's avatar
millette committed
21

22 23
class SearchEngine
{
24
    protected $target;
millette's avatar
millette committed
25
    protected $table;
millette's avatar
millette committed
26

27 28
    function __construct($target, $table)
    {
29
        $this->target = $target;
millette's avatar
millette committed
30
        $this->table = $table;
millette's avatar
millette committed
31 32
    }

33 34
    function query($q)
    {
millette's avatar
millette committed
35 36
    }

37 38
    function limit($offset, $count, $rss = false)
    {
39 40 41
        return $this->target->limit($offset, $count);
    }

42 43
    function set_sort_mode($mode)
    {
44 45 46 47 48 49 50 51 52 53 54 55 56
        switch ($mode) {
        case 'chron':
            return $this->target->orderBy('created DESC');
            break;
        case 'reverse_chron':
            return $this->target->orderBy('created ASC');
            break;
        case 'nickname_desc':
            if ($this->table != 'profile') {
                throw new Exception(
                    'nickname_desc sort mode can only be use when searching profile.'
                );
            } else {
57
                return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
58 59 60 61 62 63 64 65
            }
            break;
        case 'nickname_asc':
            if ($this->table != 'profile') {
                throw new Exception(
                    'nickname_desc sort mode can only be use when searching profile.'
                );
            } else {
66
                return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
67 68 69 70 71 72
            }
            break;
        default:
            return $this->target->orderBy('created DESC');
            break;
        }
millette's avatar
millette committed
73 74 75
    }
}

76 77
class MySQLSearch extends SearchEngine
{
78 79
    function query($q)
    {
80
        if ('profile' === $this->table) {
81
            $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
82
                                    'AGAINST (\''.$this->target->escape($q).'\' IN BOOLEAN MODE)');
83 84
            if (strtolower($q) != $q) {
                $this->target->whereAdd('MATCH(nickname, fullname, location, bio, homepage) ' .
85
                                        'AGAINST (\''.$this->target->escape(strtolower($q)).'\' IN BOOLEAN MODE)', 'OR');
86 87
            }
            return true;
88
        } else if ('notice' === $this->table) {
89

90
            // Don't show imported notices
91
            $this->target->whereAdd('notice.is_local != ' . Notice::GATEWAY);
92

93
            if (strtolower($q) != $q) {
94
                $this->target->whereAdd("( MATCH(content) AGAINST ('" . $this->target->escape($q) .
95
                    "' IN BOOLEAN MODE)) OR ( MATCH(content) " .
96
                    "AGAINST ('"  . $this->target->escape(strtolower($q)) .
97 98
                    "' IN BOOLEAN MODE))");
            } else {
99
                $this->target->whereAdd('MATCH(content) ' .
100
                                         'AGAINST (\''.$this->target->escape($q).'\' IN BOOLEAN MODE)');
101
            }
102

103 104 105 106
            return true;
        } else {
            throw new ServerException('Unknown table: ' . $this->table);
        }
millette's avatar
millette committed
107 108 109
    }
}

Evan Prodromou's avatar
Evan Prodromou committed
110 111 112 113
class MySQLLikeSearch extends SearchEngine
{
    function query($q)
    {
114
        if ('profile' === $this->table) {
115 116 117 118 119 120 121
            $qry = sprintf('(%2$s.nickname LIKE "%%%1$s%%" OR '.
                            ' %2$s.fullname LIKE "%%%1$s%%" OR '.
                            ' %2$s.location LIKE "%%%1$s%%" OR '.
                            ' %2$s.bio      LIKE "%%%1$s%%" OR '.
                            ' %2$s.homepage LIKE "%%%1$s%%")',
                           $this->target->escape($q, true),
                           $this->table);
122
        } else if ('notice' === $this->table) {
123
            $qry = sprintf('content LIKE "%%%1$s%%"', $this->target->escape($q, true));
Evan Prodromou's avatar
Evan Prodromou committed
124 125 126 127 128 129 130 131 132 133
        } else {
            throw new ServerException('Unknown table: ' . $this->table);
        }

        $this->target->whereAdd($qry);

        return true;
    }
}

134 135
class PGSearch extends SearchEngine
{
136 137
    function query($q)
    {
138
        if ('profile' === $this->table) {
139
            return $this->target->whereAdd('textsearch @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
140
        } else if ('notice' === $this->table) {
141 142 143

            // XXX: We need to filter out gateway notices (notice.is_local = -2) --Zach

144
            return $this->target->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.$this->target->escape($q).'\')');
145 146 147
        } else {
            throw new ServerException('Unknown table: ' . $this->table);
        }
millette's avatar
millette committed
148 149 150
    }
}