search_engines.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. class SearchEngine
  18. {
  19. protected $target;
  20. protected $table;
  21. public function __construct($target, $table)
  22. {
  23. $this->target = $target;
  24. $this->table = $table;
  25. }
  26. public function query($q)
  27. {
  28. }
  29. public function limit($offset, $count, $rss = false)
  30. {
  31. return $this->target->limit($offset, $count);
  32. }
  33. public function set_sort_mode($mode)
  34. {
  35. switch ($mode) {
  36. case 'nickname_desc':
  37. if ($this->table != 'profile') {
  38. throw new Exception(
  39. 'nickname_desc sort mode can only be use when searching profile.'
  40. );
  41. } else {
  42. return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
  43. }
  44. break;
  45. case 'nickname_asc':
  46. if ($this->table != 'profile') {
  47. throw new Exception(
  48. 'nickname_desc sort mode can only be use when searching profile.'
  49. );
  50. } else {
  51. return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
  52. }
  53. break;
  54. case 'reverse_chron':
  55. return $this->target->orderBy('created, id');
  56. break;
  57. case 'chron':
  58. default:
  59. return $this->target->orderBy('created DESC, id DESC');
  60. break;
  61. }
  62. }
  63. }
  64. class PostgreSQLSearch extends SearchEngine
  65. {
  66. public function query($q)
  67. {
  68. if ($this->table === 'profile') {
  69. $cols = implode(" || ' ' || ", array_map(
  70. function ($col) {
  71. return sprintf(
  72. 'COALESCE(%s."%s", \'\')',
  73. common_database_tablename($this->table),
  74. $col
  75. );
  76. },
  77. ['nickname', 'fullname', 'location', 'bio', 'homepage']
  78. ));
  79. $this->target->whereAdd(sprintf(
  80. 'to_tsvector(\'english\', %2$s) @@ websearch_to_tsquery(\'%1$s\')',
  81. $this->target->escape($q, true),
  82. $cols
  83. ));
  84. return true;
  85. } elseif ($this->table === 'notice') {
  86. // Don't show direct messages.
  87. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  88. // Don't show imported notices
  89. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  90. $this->target->whereAdd(sprintf(
  91. 'to_tsvector(\'english\', "content") @@ websearch_to_tsquery(\'%1$s\')',
  92. $this->target->escape($q, true)
  93. ));
  94. return true;
  95. } else {
  96. throw new ServerException('Unknown table: ' . $this->table);
  97. }
  98. }
  99. }
  100. class MySQLSearch extends SearchEngine
  101. {
  102. /*
  103. * Creates a full-text MATCH IN BOOLEAN MODE from the query format
  104. * analogous to PostgreSQL's websearch_to_tsquery.
  105. * The resulting boolean search query should never raise syntax errors
  106. * regardless of the kind of input this method receives.
  107. *
  108. * The syntax is as follows:
  109. * - unquoted text: text not inside quote marks will be converted to
  110. * individual quoted words with "+" operators each.
  111. * - "quoted text": text inside quote marks will have the "+" operator
  112. * prepended.
  113. * - OR: causes the two adjoined words to lose the "+" operator.
  114. * - "-": words prepended with the "-" operator will retain it unquoted.
  115. */
  116. private function websearchToBoolean(string $input): string
  117. {
  118. $split = [];
  119. preg_match_all('/(?:[^\s"]|["][^"]*["])+/', $input, $split);
  120. $phrases = [];
  121. $or_cond = false;
  122. foreach ($split[0] as $phrase) {
  123. if (strtoupper($phrase) === 'OR') {
  124. $last = &$phrases[array_key_last($phrases)];
  125. $last['op'] = '';
  126. $or_cond = true;
  127. continue;
  128. }
  129. if (substr($phrase, 0, 1) === '-') {
  130. $phrases[] = ['op' => '-', 'text' => substr($phrase, 1)];
  131. } elseif ($or_cond) {
  132. $phrases[] = ['op' => '', 'text' => $phrase];
  133. } else {
  134. $phrases[] = ['op' => '+', 'text' => $phrase];
  135. }
  136. $or_cond = false;
  137. }
  138. return array_reduce(
  139. $phrases,
  140. function (string $carry, array $item): string {
  141. // Strip all double quote marks and wrap with them around
  142. $text = '"' . str_replace('"', '', $item['text']) . '"';
  143. return $carry . ' ' . $item['op'] . $text;
  144. },
  145. ''
  146. );
  147. }
  148. public function query($q)
  149. {
  150. if ($this->table === 'profile') {
  151. $tables = sprintf(
  152. '%1$s.nickname, %1$s.fullname, %1$s.location, %1$s.bio, %1$s.homepage',
  153. $this->table
  154. );
  155. } elseif ($this->table === 'notice') {
  156. // Don't show direct messages.
  157. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  158. // Don't show imported notices
  159. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  160. $tables = 'notice.content';
  161. } else {
  162. throw new ServerException('Unknown table: ' . $this->table);
  163. }
  164. $boolean_query = $this->websearchToBoolean($q);
  165. $this->target->whereAdd(sprintf(
  166. 'MATCH (%1$s) AGAINST (\'%2$s\' IN BOOLEAN MODE)',
  167. $tables,
  168. $this->target->escape($boolean_query)
  169. ));
  170. return true;
  171. }
  172. }
  173. class SQLLikeSearch extends SearchEngine
  174. {
  175. public function query($q)
  176. {
  177. $q_escaped = $this->target->escape(mb_strtolower($q), true);
  178. $cols = [];
  179. if ($this->table === 'profile') {
  180. $cols = ['nickname', 'fullname', 'location', 'bio', 'homepage'];
  181. } elseif ($this->table === 'notice') {
  182. // Don't show direct messages.
  183. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  184. // Don't show imported notices
  185. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  186. $cols = ['content'];
  187. } else {
  188. throw new ServerException('Unknown table: ' . $this->table);
  189. }
  190. $conds = [];
  191. foreach ($cols as $col) {
  192. switch (common_config('db', 'type')) {
  193. case 'pgsql':
  194. // Faster than with the LOWER function
  195. $cond = "{$this->table}.{$col} ILIKE";
  196. break;
  197. case 'mysql':
  198. // Case-insensitive collation
  199. $cond = "{$this->table}.{$col} LIKE";
  200. break;
  201. default:
  202. $cond = "LOWER({$this->table}.{$col}) LIKE";
  203. }
  204. $conds[] = $cond . " '%" . $q_escaped . "%'";
  205. }
  206. $qry = '(' . implode(' OR ', $conds) . ')';
  207. $this->target->whereAdd($qry);
  208. return true;
  209. }
  210. }