SearchIBM_DB2.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 IBM DB2
  25. * @ingroup Search
  26. */
  27. class SearchIBM_DB2 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 IBM_DB2SearchResultSet
  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 IBM_DB2SearchResultSet($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 IBM_DB2SearchResultSet
  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. $namespaces = implode(',', $this->namespaces);
  74. if ($namespaces == '') {
  75. $namespaces = '0';
  76. }
  77. return 'AND page_namespace IN (' . $namespaces . ')';
  78. }
  79. /**
  80. * Return a LIMIT clause to limit results on the query.
  81. * @return string
  82. * @private
  83. */
  84. function queryLimit($sql) {
  85. return $this->db->limitResult($sql, $this->limit, $this->offset);
  86. }
  87. /**
  88. * Does not do anything for generic search engine
  89. * subclasses may define this though
  90. * @return string
  91. * @private
  92. */
  93. function queryRanking($filteredTerm, $fulltext) {
  94. // requires Net Search Extender or equivalent
  95. // return ' ORDER BY score(1)';
  96. return '';
  97. }
  98. /**
  99. * Construct the full SQL query to do the search.
  100. * The guts shoulds be constructed in queryMain()
  101. * @param string $filteredTerm
  102. * @param bool $fulltext
  103. * @private
  104. */
  105. function getQuery( $filteredTerm, $fulltext ) {
  106. return $this->queryLimit($this->queryMain($filteredTerm, $fulltext) . ' ' .
  107. $this->queryRedirect() . ' ' .
  108. $this->queryNamespaces() . ' ' .
  109. $this->queryRanking( $filteredTerm, $fulltext ) . ' ');
  110. }
  111. /**
  112. * Picks which field to index on, depending on what type of query.
  113. * @param bool $fulltext
  114. * @return string
  115. */
  116. function getIndexField($fulltext) {
  117. return $fulltext ? 'si_text' : 'si_title';
  118. }
  119. /**
  120. * Get the base part of the search query.
  121. *
  122. * @param string $filteredTerm
  123. * @param bool $fulltext
  124. * @return string
  125. * @private
  126. */
  127. function queryMain( $filteredTerm, $fulltext ) {
  128. $match = $this->parseQuery($filteredTerm, $fulltext);
  129. $page = $this->db->tableName('page');
  130. $searchindex = $this->db->tableName('searchindex');
  131. return 'SELECT page_id, page_namespace, page_title ' .
  132. "FROM $page,$searchindex " .
  133. 'WHERE page_id=si_page AND ' . $match;
  134. }
  135. /** @todo document */
  136. function parseQuery($filteredText, $fulltext) {
  137. global $wgContLang;
  138. $lc = SearchEngine::legalSearchChars();
  139. $this->searchTerms = array();
  140. # FIXME: This doesn't handle parenthetical expressions.
  141. $m = array();
  142. $q = array();
  143. if (preg_match_all('/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
  144. $filteredText, $m, PREG_SET_ORDER)) {
  145. foreach($m as $terms) {
  146. $q[] = $terms[1] . $wgContLang->stripForSearch($terms[2]);
  147. if (!empty($terms[3])) {
  148. $regexp = preg_quote( $terms[3], '/' );
  149. if ($terms[4])
  150. $regexp .= "[0-9A-Za-z_]+";
  151. } else {
  152. $regexp = preg_quote(str_replace('"', '', $terms[2]), '/');
  153. }
  154. $this->searchTerms[] = $regexp;
  155. }
  156. }
  157. $searchon = $this->db->strencode(join(',', $q));
  158. $field = $this->getIndexField($fulltext);
  159. // requires Net Search Extender or equivalent
  160. //return " CONTAINS($field, '$searchon') > 0 ";
  161. return " lcase($field) LIKE lcase('%$searchon%')";
  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. ), 'SearchIBM_DB2::update' );
  180. // ?
  181. //$dbw->query("CALL ctx_ddl.sync_index('si_text_idx')");
  182. //$dbw->query("CALL ctx_ddl.sync_index('si_title_idx')");
  183. }
  184. /**
  185. * Update a search index record's title only.
  186. * Title should be pre-processed.
  187. *
  188. * @param int $id
  189. * @param string $title
  190. */
  191. function updateTitle($id, $title) {
  192. $dbw = wfGetDB(DB_MASTER);
  193. $dbw->update('searchindex',
  194. array('si_title' => $title),
  195. array('si_page' => $id),
  196. 'SearchIBM_DB2::updateTitle',
  197. array());
  198. }
  199. }
  200. /**
  201. * @ingroup Search
  202. */
  203. class IBM_DB2SearchResultSet extends SearchResultSet {
  204. function __construct($resultSet, $terms) {
  205. $this->mResultSet = $resultSet;
  206. $this->mTerms = $terms;
  207. }
  208. function termMatches() {
  209. return $this->mTerms;
  210. }
  211. function numRows() {
  212. return $this->mResultSet->numRows();
  213. }
  214. function next() {
  215. $row = $this->mResultSet->fetchObject();
  216. if ($row === false)
  217. return false;
  218. return new SearchResult($row);
  219. }
  220. }