123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- defined('GNUSOCIAL') || die();
- class SearchEngine
- {
- protected $target;
- protected $table;
- public function __construct($target, $table)
- {
- $this->target = $target;
- $this->table = $table;
- }
- public function query($q)
- {
- }
- public function limit($offset, $count, $rss = false)
- {
- return $this->target->limit($offset, $count);
- }
- public function set_sort_mode($mode)
- {
- switch ($mode) {
- case 'nickname_desc':
- if ($this->table != 'profile') {
- throw new Exception(
- 'nickname_desc sort mode can only be use when searching profile.'
- );
- } else {
- return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
- }
- break;
- case 'nickname_asc':
- if ($this->table != 'profile') {
- throw new Exception(
- 'nickname_desc sort mode can only be use when searching profile.'
- );
- } else {
- return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
- }
- break;
- case 'reverse_chron':
- return $this->target->orderBy('created, id');
- break;
- case 'chron':
- default:
- return $this->target->orderBy('created DESC, id DESC');
- break;
- }
- }
- }
- class PostgreSQLSearch extends SearchEngine
- {
- public function query($q)
- {
- if ($this->table === 'profile') {
- $cols = implode(" || ' ' || ", array_map(
- function ($col) {
- return sprintf(
- 'COALESCE(%s."%s", \'\')',
- common_database_tablename($this->table),
- $col
- );
- },
- ['nickname', 'fullname', 'location', 'bio', 'homepage']
- ));
- $this->target->whereAdd(sprintf(
- 'to_tsvector(\'english\', %2$s) @@ websearch_to_tsquery(\'%1$s\')',
- $this->target->escape($q, true),
- $cols
- ));
- return true;
- } elseif ($this->table === 'notice') {
-
- $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
-
- $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
- $this->target->whereAdd(sprintf(
- 'to_tsvector(\'english\', "content") @@ websearch_to_tsquery(\'%1$s\')',
- $this->target->escape($q, true)
- ));
- return true;
- } else {
- throw new ServerException('Unknown table: ' . $this->table);
- }
- }
- }
- class MySQLSearch extends SearchEngine
- {
-
- private function websearchToBoolean(string $input): string
- {
- $split = [];
- preg_match_all('/(?:[^\s"]|["][^"]*["])+/', $input, $split);
- $phrases = [];
- $or_cond = false;
- foreach ($split[0] as $phrase) {
- if (strtoupper($phrase) === 'OR') {
- $last = &$phrases[array_key_last($phrases)];
- $last['op'] = '';
- $or_cond = true;
- continue;
- }
- if (substr($phrase, 0, 1) === '-') {
- $phrases[] = ['op' => '-', 'text' => substr($phrase, 1)];
- } elseif ($or_cond) {
- $phrases[] = ['op' => '', 'text' => $phrase];
- } else {
- $phrases[] = ['op' => '+', 'text' => $phrase];
- }
- $or_cond = false;
- }
- return array_reduce(
- $phrases,
- function (string $carry, array $item): string {
-
- $text = '"' . str_replace('"', '', $item['text']) . '"';
- return $carry . ' ' . $item['op'] . $text;
- },
- ''
- );
- }
- public function query($q)
- {
- if ($this->table === 'profile') {
- $tables = sprintf(
- '%1$s.nickname, %1$s.fullname, %1$s.location, %1$s.bio, %1$s.homepage',
- $this->table
- );
- } elseif ($this->table === 'notice') {
-
- $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
-
- $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
- $tables = 'notice.content';
- } else {
- throw new ServerException('Unknown table: ' . $this->table);
- }
- $boolean_query = $this->websearchToBoolean($q);
- $this->target->whereAdd(sprintf(
- 'MATCH (%1$s) AGAINST (\'%2$s\' IN BOOLEAN MODE)',
- $tables,
- $this->target->escape($boolean_query)
- ));
- return true;
- }
- }
- class SQLLikeSearch extends SearchEngine
- {
- public function query($q)
- {
- $q_escaped = $this->target->escape(mb_strtolower($q), true);
- $cols = [];
- if ($this->table === 'profile') {
- $cols = ['nickname', 'fullname', 'location', 'bio', 'homepage'];
- } elseif ($this->table === 'notice') {
-
- $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
-
- $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
- $cols = ['content'];
- } else {
- throw new ServerException('Unknown table: ' . $this->table);
- }
- $conds = [];
- foreach ($cols as $col) {
- switch (common_config('db', 'type')) {
- case 'pgsql':
-
- $cond = "{$this->table}.{$col} ILIKE";
- break;
- case 'mysql':
-
- $cond = "{$this->table}.{$col} LIKE";
- break;
- default:
- $cond = "LOWER({$this->table}.{$col}) LIKE";
- }
- $conds[] = $cond . " '%" . $q_escaped . "%'";
- }
- $qry = '(' . implode(' OR ', $conds) . ')';
- $this->target->whereAdd($qry);
- return true;
- }
- }
|