search_engines.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 'chron':
  37. return $this->target->orderBy('created DESC');
  38. break;
  39. case 'reverse_chron':
  40. return $this->target->orderBy('created ASC');
  41. break;
  42. case 'nickname_desc':
  43. if ($this->table != 'profile') {
  44. throw new Exception(
  45. 'nickname_desc sort mode can only be use when searching profile.'
  46. );
  47. } else {
  48. return $this->target->orderBy(sprintf('%1$s.nickname DESC', $this->table));
  49. }
  50. break;
  51. case 'nickname_asc':
  52. if ($this->table != 'profile') {
  53. throw new Exception(
  54. 'nickname_desc sort mode can only be use when searching profile.'
  55. );
  56. } else {
  57. return $this->target->orderBy(sprintf('%1$s.nickname ASC', $this->table));
  58. }
  59. break;
  60. default:
  61. return $this->target->orderBy('created DESC');
  62. break;
  63. }
  64. }
  65. }
  66. class PostgreSQLSearch extends SearchEngine
  67. {
  68. public function query($q)
  69. {
  70. if ($this->table === 'profile') {
  71. $cols = implode(" || ' ' || ", array_map(
  72. function ($col) {
  73. return sprintf(
  74. 'COALESCE(%s."%s", \'\')',
  75. common_database_tablename($this->table),
  76. $col
  77. );
  78. },
  79. ['nickname', 'fullname', 'location', 'bio', 'homepage']
  80. ));
  81. $this->target->whereAdd(sprintf(
  82. 'to_tsvector(\'english\', %2$s) @@ websearch_to_tsquery(\'%1$s\')',
  83. $this->target->escape($q, true),
  84. $cols
  85. ));
  86. return true;
  87. } elseif ($this->table === 'notice') {
  88. // Don't show direct messages.
  89. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  90. // Don't show imported notices
  91. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  92. $this->target->whereAdd(sprintf(
  93. 'to_tsvector(\'english\', "content") @@ websearch_to_tsquery(\'%1$s\')',
  94. $this->target->escape($q, true)
  95. ));
  96. return true;
  97. } else {
  98. throw new ServerException('Unknown table: ' . $this->table);
  99. }
  100. }
  101. }
  102. class MySQLSearch extends SearchEngine
  103. {
  104. /*
  105. * Creates a full-text MATCH IN BOOLEAN MODE from the query format
  106. * analogous to PostgreSQL's websearch_to_tsquery.
  107. * The resulting boolean search query should never raise syntax errors
  108. * regardless of the kind of input this method receives.
  109. *
  110. * The syntax is as follows:
  111. * - unquoted text: text not inside quote marks will be converted to
  112. * individual quoted words with "+" operators each.
  113. * - "quoted text": text inside quote marks will have the "+" operator
  114. * prepended.
  115. * - OR: causes the two adjoined words to lose the "+" operator.
  116. * - "-": words prepended with the "-" operator will retain it unquoted.
  117. */
  118. private function websearchToBoolean(string $input): string
  119. {
  120. $split = [];
  121. preg_match_all('/(?:[^\s"]|["][^"]*["])+/', $input, $split);
  122. $phrases = [];
  123. $or_cond = false;
  124. foreach ($split[0] as $phrase) {
  125. if (strtoupper($phrase) === 'OR') {
  126. $last = &$phrases[array_key_last($phrases)];
  127. $last['op'] = '';
  128. $or_cond = true;
  129. continue;
  130. }
  131. if (substr($phrase, 0, 1) === '-') {
  132. $phrases[] = ['op' => '-', 'text' => substr($phrase, 1)];
  133. } elseif ($or_cond) {
  134. $phrases[] = ['op' => '', 'text' => $phrase];
  135. } else {
  136. $phrases[] = ['op' => '+', 'text' => $phrase];
  137. }
  138. $or_cond = false;
  139. }
  140. return array_reduce(
  141. $phrases,
  142. function (string $carry, array $item): string {
  143. // Strip all double quote marks and wrap with them around
  144. $text = '"' . str_replace('"', '', $item['text']) . '"';
  145. return $carry . ' ' . $item['op'] . $text;
  146. },
  147. ''
  148. );
  149. }
  150. public function query($q)
  151. {
  152. if ($this->table === 'profile') {
  153. $tables = sprintf(
  154. '%1$s.nickname, %1$s.fullname, %1$s.location, %1$s.bio, %1$s.homepage',
  155. $this->table
  156. );
  157. } elseif ($this->table === 'notice') {
  158. // Don't show direct messages.
  159. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  160. // Don't show imported notices
  161. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  162. $tables = 'notice.content';
  163. } else {
  164. throw new ServerException('Unknown table: ' . $this->table);
  165. }
  166. $boolean_query = $this->websearchToBoolean($q);
  167. $this->target->whereAdd(sprintf(
  168. 'MATCH (%1$s) AGAINST (\'%2$s\' IN BOOLEAN MODE)',
  169. $tables,
  170. $this->target->escape($boolean_query)
  171. ));
  172. return true;
  173. }
  174. }
  175. class SQLLikeSearch extends SearchEngine
  176. {
  177. public function query($q)
  178. {
  179. $q_escaped = $this->target->escape(mb_strtolower($q), true);
  180. $cols = [];
  181. if ($this->table === 'profile') {
  182. $cols = ['nickname', 'fullname', 'location', 'bio', 'homepage'];
  183. } elseif ($this->table === 'notice') {
  184. // Don't show direct messages.
  185. $this->target->whereAdd('notice.scope <> ' . Notice::MESSAGE_SCOPE);
  186. // Don't show imported notices
  187. $this->target->whereAdd('notice.is_local <> ' . Notice::GATEWAY);
  188. $cols = ['content'];
  189. } else {
  190. throw new ServerException('Unknown table: ' . $this->table);
  191. }
  192. $conds = [];
  193. foreach ($cols as $col) {
  194. switch (common_config('db', 'type')) {
  195. case 'pgsql':
  196. // Faster than with the LOWER function
  197. $cond = "{$this->table}.{$col} ILIKE";
  198. break;
  199. case 'mysql':
  200. // Case-insensitive collation
  201. $cond = "{$this->table}.{$col} LIKE";
  202. break;
  203. default:
  204. $cond = "LOWER({$this->table}.{$col}) LIKE";
  205. }
  206. $conds[] = $cond . " '%" . $q_escaped . "%'";
  207. }
  208. $qry = '(' . implode(' OR ', $conds) . ')';
  209. $this->target->whereAdd($qry);
  210. return true;
  211. }
  212. }