SearchMySQL.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 for MySQL 4+
  25. * @ingroup Search
  26. */
  27. class SearchMySQL extends SearchEngine {
  28. var $strictMatching = true;
  29. /** @todo document */
  30. function __construct( $db ) {
  31. $this->db = $db;
  32. }
  33. /**
  34. * Parse the user's query and transform it into an SQL fragment which will
  35. * become part of a WHERE clause
  36. */
  37. function parseQuery( $filteredText, $fulltext ) {
  38. global $wgContLang;
  39. $lc = SearchEngine::legalSearchChars(); // Minus format chars
  40. $searchon = '';
  41. $this->searchTerms = array();
  42. # FIXME: This doesn't handle parenthetical expressions.
  43. $m = array();
  44. if( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
  45. $filteredText, $m, PREG_SET_ORDER ) ) {
  46. foreach( $m as $terms ) {
  47. if( $searchon !== '' ) $searchon .= ' ';
  48. if( $this->strictMatching && ($terms[1] == '') ) {
  49. $terms[1] = '+';
  50. }
  51. $searchon .= $terms[1] . $wgContLang->stripForSearch( $terms[2] );
  52. if( !empty( $terms[3] ) ) {
  53. // Match individual terms in result highlighting...
  54. $regexp = preg_quote( $terms[3], '/' );
  55. if( $terms[4] ) {
  56. $regexp = "\b$regexp"; // foo*
  57. } else {
  58. $regexp = "\b$regexp\b";
  59. }
  60. } else {
  61. // Match the quoted term in result highlighting...
  62. $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
  63. }
  64. $this->searchTerms[] = $regexp;
  65. }
  66. wfDebug( "Would search with '$searchon'\n" );
  67. wfDebug( 'Match with /' . implode( '|', $this->searchTerms ) . "/\n" );
  68. } else {
  69. wfDebug( "Can't understand search query '{$filteredText}'\n" );
  70. }
  71. $searchon = $this->db->strencode( $searchon );
  72. $field = $this->getIndexField( $fulltext );
  73. return " MATCH($field) AGAINST('$searchon' IN BOOLEAN MODE) ";
  74. }
  75. public static function legalSearchChars() {
  76. return "\"*" . parent::legalSearchChars();
  77. }
  78. /**
  79. * Perform a full text search query and return a result set.
  80. *
  81. * @param string $term - Raw search term
  82. * @return MySQLSearchResultSet
  83. * @access public
  84. */
  85. function searchText( $term ) {
  86. $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
  87. return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
  88. }
  89. /**
  90. * Perform a title-only search query and return a result set.
  91. *
  92. * @param string $term - Raw search term
  93. * @return MySQLSearchResultSet
  94. * @access public
  95. */
  96. function searchTitle( $term ) {
  97. $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
  98. return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
  99. }
  100. /**
  101. * Return a partial WHERE clause to exclude redirects, if so set
  102. * @return string
  103. * @private
  104. */
  105. function queryRedirect() {
  106. if( $this->showRedirects ) {
  107. return '';
  108. } else {
  109. return 'AND page_is_redirect=0';
  110. }
  111. }
  112. /**
  113. * Return a partial WHERE clause to limit the search to the given namespaces
  114. * @return string
  115. * @private
  116. */
  117. function queryNamespaces() {
  118. if( is_null($this->namespaces) )
  119. return ''; # search all
  120. if ( !count( $this->namespaces ) ) {
  121. $namespaces = '0';
  122. } else {
  123. $namespaces = $this->db->makeList( $this->namespaces );
  124. }
  125. return 'AND page_namespace IN (' . $namespaces . ')';
  126. }
  127. /**
  128. * Return a LIMIT clause to limit results on the query.
  129. * @return string
  130. * @private
  131. */
  132. function queryLimit() {
  133. return $this->db->limitResult( '', $this->limit, $this->offset );
  134. }
  135. /**
  136. * Does not do anything for generic search engine
  137. * subclasses may define this though
  138. * @return string
  139. * @private
  140. */
  141. function queryRanking( $filteredTerm, $fulltext ) {
  142. return '';
  143. }
  144. /**
  145. * Construct the full SQL query to do the search.
  146. * The guts shoulds be constructed in queryMain()
  147. * @param string $filteredTerm
  148. * @param bool $fulltext
  149. * @private
  150. */
  151. function getQuery( $filteredTerm, $fulltext ) {
  152. return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
  153. $this->queryRedirect() . ' ' .
  154. $this->queryNamespaces() . ' ' .
  155. $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
  156. $this->queryLimit();
  157. }
  158. /**
  159. * Picks which field to index on, depending on what type of query.
  160. * @param bool $fulltext
  161. * @return string
  162. */
  163. function getIndexField( $fulltext ) {
  164. return $fulltext ? 'si_text' : 'si_title';
  165. }
  166. /**
  167. * Get the base part of the search query.
  168. * The actual match syntax will depend on the server
  169. * version; MySQL 3 and MySQL 4 have different capabilities
  170. * in their fulltext search indexes.
  171. *
  172. * @param string $filteredTerm
  173. * @param bool $fulltext
  174. * @return string
  175. * @private
  176. */
  177. function queryMain( $filteredTerm, $fulltext ) {
  178. $match = $this->parseQuery( $filteredTerm, $fulltext );
  179. $page = $this->db->tableName( 'page' );
  180. $searchindex = $this->db->tableName( 'searchindex' );
  181. return 'SELECT page_id, page_namespace, page_title ' .
  182. "FROM $page,$searchindex " .
  183. 'WHERE page_id=si_page AND ' . $match;
  184. }
  185. /**
  186. * Create or update the search index record for the given page.
  187. * Title and text should be pre-processed.
  188. *
  189. * @param int $id
  190. * @param string $title
  191. * @param string $text
  192. */
  193. function update( $id, $title, $text ) {
  194. $dbw = wfGetDB( DB_MASTER );
  195. $dbw->replace( 'searchindex',
  196. array( 'si_page' ),
  197. array(
  198. 'si_page' => $id,
  199. 'si_title' => $title,
  200. 'si_text' => $text
  201. ), __METHOD__ );
  202. }
  203. /**
  204. * Update a search index record's title only.
  205. * Title should be pre-processed.
  206. *
  207. * @param int $id
  208. * @param string $title
  209. */
  210. function updateTitle( $id, $title ) {
  211. $dbw = wfGetDB( DB_MASTER );
  212. $dbw->update( 'searchindex',
  213. array( 'si_title' => $title ),
  214. array( 'si_page' => $id ),
  215. __METHOD__,
  216. array( $dbw->lowPriorityOption() ) );
  217. }
  218. }
  219. /**
  220. * @ingroup Search
  221. */
  222. class MySQLSearchResultSet extends SearchResultSet {
  223. function MySQLSearchResultSet( $resultSet, $terms ) {
  224. $this->mResultSet = $resultSet;
  225. $this->mTerms = $terms;
  226. }
  227. function termMatches() {
  228. return $this->mTerms;
  229. }
  230. function numRows() {
  231. return $this->mResultSet->numRows();
  232. }
  233. function next() {
  234. $row = $this->mResultSet->fetchObject();
  235. if( $row === false ) {
  236. return false;
  237. } else {
  238. return new SearchResult( $row );
  239. }
  240. }
  241. function free() {
  242. $this->mResultSet->free();
  243. }
  244. }