PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/search/SearchMssql.php

https://github.com/daevid/MWFork
PHP | 252 lines | 110 code | 27 blank | 115 comment | 9 complexity | ea012530823e9f9dc8dc81fdc5afac94 MD5 | raw file
  1. <?php
  2. /**
  3. * Mssql search engine
  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 Mssql (ConText).
  25. * @ingroup Search
  26. */
  27. class SearchMssql extends SearchEngine {
  28. /**
  29. * Creates an instance of this class
  30. * @param $db DatabaseMssql: database object
  31. */
  32. function __construct( $db ) {
  33. parent::__construct( $db );
  34. }
  35. /**
  36. * Perform a full text search query and return a result set.
  37. *
  38. * @param $term String: raw search term
  39. * @return MssqlSearchResultSet
  40. * @access public
  41. */
  42. function searchText( $term ) {
  43. $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
  44. return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
  45. }
  46. /**
  47. * Perform a title-only search query and return a result set.
  48. *
  49. * @param $term String: raw search term
  50. * @return MssqlSearchResultSet
  51. * @access public
  52. */
  53. function searchTitle( $term ) {
  54. $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
  55. return new MssqlSearchResultSet( $resultSet, $this->searchTerms );
  56. }
  57. /**
  58. * Return a partial WHERE clause to exclude redirects, if so set
  59. *
  60. * @return String
  61. * @private
  62. */
  63. function queryRedirect() {
  64. if ( $this->showRedirects ) {
  65. return '';
  66. } else {
  67. return 'AND page_is_redirect=0';
  68. }
  69. }
  70. /**
  71. * Return a partial WHERE clause to limit the search to the given namespaces
  72. *
  73. * @return String
  74. * @private
  75. */
  76. function queryNamespaces() {
  77. $namespaces = implode( ',', $this->namespaces );
  78. if ( $namespaces == '' ) {
  79. $namespaces = '0';
  80. }
  81. return 'AND page_namespace IN (' . $namespaces . ')';
  82. }
  83. /**
  84. * Return a LIMIT clause to limit results on the query.
  85. *
  86. * @param $sql string
  87. *
  88. * @return String
  89. */
  90. function queryLimit( $sql ) {
  91. return $this->db->limitResult( $sql, $this->limit, $this->offset );
  92. }
  93. /**
  94. * Does not do anything for generic search engine
  95. * subclasses may define this though
  96. *
  97. * @return String
  98. */
  99. function queryRanking( $filteredTerm, $fulltext ) {
  100. return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
  101. }
  102. /**
  103. * Construct the full SQL query to do the search.
  104. * The guts shoulds be constructed in queryMain()
  105. *
  106. * @param $filteredTerm String
  107. * @param $fulltext Boolean
  108. */
  109. function getQuery( $filteredTerm, $fulltext ) {
  110. return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
  111. $this->queryRedirect() . ' ' .
  112. $this->queryNamespaces() . ' ' .
  113. $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
  114. }
  115. /**
  116. * Picks which field to index on, depending on what type of query.
  117. *
  118. * @param $fulltext Boolean
  119. * @return string
  120. */
  121. function getIndexField( $fulltext ) {
  122. return $fulltext ? 'si_text' : 'si_title';
  123. }
  124. /**
  125. * Get the base part of the search query.
  126. *
  127. * @param $filteredTerm String
  128. * @param $fulltext Boolean
  129. * @return String
  130. * @private
  131. */
  132. function queryMain( $filteredTerm, $fulltext ) {
  133. $match = $this->parseQuery( $filteredTerm, $fulltext );
  134. $page = $this->db->tableName( 'page' );
  135. $searchindex = $this->db->tableName( 'searchindex' );
  136. return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
  137. "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
  138. 'WHERE page_id=ftindex.[KEY] ';
  139. }
  140. /** @todo document */
  141. function parseQuery( $filteredText, $fulltext ) {
  142. global $wgContLang;
  143. $lc = SearchEngine::legalSearchChars();
  144. $this->searchTerms = array();
  145. # @todo FIXME: This doesn't handle parenthetical expressions.
  146. $m = array();
  147. $q = array();
  148. if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
  149. $filteredText, $m, PREG_SET_ORDER ) ) {
  150. foreach ( $m as $terms ) {
  151. $q[] = $terms[1] . $wgContLang->normalizeForSearch( $terms[2] );
  152. if ( !empty( $terms[3] ) ) {
  153. $regexp = preg_quote( $terms[3], '/' );
  154. if ( $terms[4] )
  155. $regexp .= "[0-9A-Za-z_]+";
  156. } else {
  157. $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
  158. }
  159. $this->searchTerms[] = $regexp;
  160. }
  161. }
  162. $searchon = $this->db->strencode( join( ',', $q ) );
  163. $field = $this->getIndexField( $fulltext );
  164. return "$field, '$searchon'";
  165. }
  166. /**
  167. * Create or update the search index record for the given page.
  168. * Title and text should be pre-processed.
  169. *
  170. * @param $id Integer
  171. * @param $title String
  172. * @param $text String
  173. */
  174. function update( $id, $title, $text ) {
  175. // We store the column data as UTF-8 byte order marked binary stream
  176. // because we are invoking the plain text IFilter on it so that, and we want it
  177. // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
  178. // but the indexer will correctly handle it by this method. Since all we are doing
  179. // is passing this data to the indexer and never retrieving it via PHP, this will save space
  180. $table = $this->db->tableName( 'searchindex' );
  181. $utf8bom = '0xEFBBBF';
  182. $si_title = $utf8bom . bin2hex( $title );
  183. $si_text = $utf8bom . bin2hex( $text );
  184. $sql = "DELETE FROM $table WHERE si_page = $id;";
  185. $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
  186. return $this->db->query( $sql, 'SearchMssql::update' );
  187. }
  188. /**
  189. * Update a search index record's title only.
  190. * Title should be pre-processed.
  191. *
  192. * @param $id Integer
  193. * @param $title String
  194. */
  195. function updateTitle( $id, $title ) {
  196. $table = $this->db->tableName( 'searchindex' );
  197. // see update for why we are using the utf8bom
  198. $utf8bom = '0xEFBBBF';
  199. $si_title = $utf8bom . bin2hex( $title );
  200. $sql = "DELETE FROM $table WHERE si_page = $id;";
  201. $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
  202. return $this->db->query( $sql, 'SearchMssql::updateTitle' );
  203. }
  204. }
  205. /**
  206. * @ingroup Search
  207. */
  208. class MssqlSearchResultSet extends SearchResultSet {
  209. function __construct( $resultSet, $terms ) {
  210. $this->mResultSet = $resultSet;
  211. $this->mTerms = $terms;
  212. }
  213. function termMatches() {
  214. return $this->mTerms;
  215. }
  216. function numRows() {
  217. return $this->mResultSet->numRows();
  218. }
  219. function next() {
  220. $row = $this->mResultSet->fetchObject();
  221. if ( $row === false )
  222. return false;
  223. return new SearchResult( $row );
  224. }
  225. }