SearchOracle.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
  3. # http://www.mediawiki.org/
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # http://www.gnu.org/copyleft/gpl.html
  19. /**
  20. * @file
  21. * @ingroup Search
  22. */
  23. /**
  24. * Search engine hook base class for Oracle (ConText).
  25. * @ingroup Search
  26. */
  27. class SearchOracle extends SearchEngine {
  28. function __construct($db) {
  29. $this->db = $db;
  30. }
  31. /**
  32. * Perform a full text search query and return a result set.
  33. *
  34. * @param string $term - Raw search term
  35. * @return OracleSearchResultSet
  36. * @access public
  37. */
  38. function searchText( $term ) {
  39. $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), true)));
  40. return new OracleSearchResultSet($resultSet, $this->searchTerms);
  41. }
  42. /**
  43. * Perform a title-only search query and return a result set.
  44. *
  45. * @param string $term - Raw search term
  46. * @return ORacleSearchResultSet
  47. * @access public
  48. */
  49. function searchTitle($term) {
  50. $resultSet = $this->db->resultObject($this->db->query($this->getQuery($this->filter($term), false)));
  51. return new MySQLSearchResultSet($resultSet, $this->searchTerms);
  52. }
  53. /**
  54. * Return a partial WHERE clause to exclude redirects, if so set
  55. * @return string
  56. * @private
  57. */
  58. function queryRedirect() {
  59. if ($this->showRedirects) {
  60. return '';
  61. } else {
  62. return 'AND page_is_redirect=0';
  63. }
  64. }
  65. /**
  66. * Return a partial WHERE clause to limit the search to the given namespaces
  67. * @return string
  68. * @private
  69. */
  70. function queryNamespaces() {
  71. if( is_null($this->namespaces) )
  72. return '';
  73. if ( !count( $this->namespaces ) ) {
  74. $namespaces = '0';
  75. } else {
  76. $namespaces = $this->db->makeList( $this->namespaces );
  77. }
  78. return 'AND page_namespace IN (' . $namespaces . ')';
  79. }
  80. /**
  81. * Return a LIMIT clause to limit results on the query.
  82. * @return string
  83. * @private
  84. */
  85. function queryLimit($sql) {
  86. return $this->db->limitResult($sql, $this->limit, $this->offset);
  87. }
  88. /**
  89. * Does not do anything for generic search engine
  90. * subclasses may define this though
  91. * @return string
  92. * @private
  93. */
  94. function queryRanking($filteredTerm, $fulltext) {
  95. return ' ORDER BY score(1)';
  96. }
  97. /**
  98. * Construct the full SQL query to do the search.
  99. * The guts shoulds be constructed in queryMain()
  100. * @param string $filteredTerm
  101. * @param bool $fulltext
  102. * @private
  103. */
  104. function getQuery( $filteredTerm, $fulltext ) {
  105. return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
  106. $this->queryRedirect() . ' ' .
  107. $this->queryNamespaces() . ' ' .
  108. $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
  109. }
  110. /**
  111. * Picks which field to index on, depending on what type of query.
  112. * @param bool $fulltext
  113. * @return string
  114. */
  115. function getIndexField($fulltext) {
  116. return $fulltext ? 'si_text' : 'si_title';
  117. }
  118. /**
  119. * Get the base part of the search query.
  120. *
  121. * @param string $filteredTerm
  122. * @param bool $fulltext
  123. * @return string
  124. * @private
  125. */
  126. function queryMain( $filteredTerm, $fulltext ) {
  127. $match = $this->parseQuery($filteredTerm, $fulltext);
  128. $page = $this->db->tableName('page');
  129. $searchindex = $this->db->tableName('searchindex');
  130. return 'SELECT page_id, page_namespace, page_title ' .
  131. "FROM $page,$searchindex " .
  132. 'WHERE page_id=si_page AND ' . $match;
  133. }
  134. /**
  135. * Parse a user input search string, and return an SQL fragment to be used
  136. * as part of a WHERE clause
  137. */
  138. function parseQuery($filteredText, $fulltext) {
  139. global $wgContLang;
  140. $lc = SearchEngine::legalSearchChars();
  141. $this->searchTerms = array();
  142. # FIXME: This doesn't handle parenthetical expressions.
  143. $m = array();
  144. $q = array();
  145. if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
  146. $filteredText, $m, PREG_SET_ORDER)) {
  147. foreach($m as $terms) {
  148. $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
  149. if (!empty($terms[3])) {
  150. $regexp = preg_quote( $terms[3], '/' );
  151. if ($terms[4])
  152. $regexp .= "[0-9A-Za-z_]+";
  153. } else {
  154. $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
  155. }
  156. $this->searchTerms[] = $regexp;
  157. }
  158. }
  159. $searchon = $this->db->addQuotes(join(',', $q));
  160. $field = $this->getIndexField($fulltext);
  161. return " CONTAINS($field, $searchon, 1) > 0 ";
  162. }
  163. /**
  164. * Create or update the search index record for the given page.
  165. * Title and text should be pre-processed.
  166. *
  167. * @param int $id
  168. * @param string $title
  169. * @param string $text
  170. */
  171. function update($id, $title, $text) {
  172. $dbw = wfGetDB(DB_MASTER);
  173. $dbw->replace('searchindex',
  174. array('si_page'),
  175. array(
  176. 'si_page' => $id,
  177. 'si_title' => $title,
  178. 'si_text' => $text
  179. ), 'SearchOracle::update' );
  180. $dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
  181. $dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
  182. }
  183. /**
  184. * Update a search index record's title only.
  185. * Title should be pre-processed.
  186. *
  187. * @param int $id
  188. * @param string $title
  189. */
  190. function updateTitle($id, $title) {
  191. $dbw = wfGetDB(DB_MASTER);
  192. $dbw->update('searchindex',
  193. array('si_title' => $title),
  194. array('si_page' => $id),
  195. 'SearchOracle::updateTitle',
  196. array());
  197. }
  198. }
  199. /**
  200. * @ingroup Search
  201. */
  202. class OracleSearchResultSet extends SearchResultSet {
  203. function __construct($resultSet, $terms) {
  204. $this->mResultSet = $resultSet;
  205. $this->mTerms = $terms;
  206. }
  207. function termMatches() {
  208. return $this->mTerms;
  209. }
  210. function numRows() {
  211. return $this->mResultSet->numRows();
  212. }
  213. function next() {
  214. $row = $this->mResultSet->fetchObject();
  215. if ($row === false)
  216. return false;
  217. return new SearchResult($row);
  218. }
  219. }