MediaWiki  1.33.0
SearchMssql.php
Go to the documentation of this file.
1 <?php
26 
31 class SearchMssql extends SearchDatabase {
38  protected function doSearchTextInDB( $term ) {
39  $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), true ) );
40  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
41  }
42 
49  protected function doSearchTitleInDB( $term ) {
50  $resultSet = $this->db->query( $this->getQuery( $this->filter( $term ), false ) );
51  return new SqlSearchResultSet( $resultSet, $this->searchTerms );
52  }
53 
59  private function queryNamespaces() {
60  $namespaces = implode( ',', $this->namespaces );
61  if ( $namespaces == '' ) {
62  $namespaces = '0';
63  }
64  return 'AND page_namespace IN (' . $namespaces . ')';
65  }
66 
74  private function queryLimit( $sql ) {
75  return $this->db->limitResult( $sql, $this->limit, $this->offset );
76  }
77 
86  function queryRanking( $filteredTerm, $fulltext ) {
87  return ' ORDER BY ftindex.[RANK] DESC'; // return ' ORDER BY score(1)';
88  }
89 
98  private function getQuery( $filteredTerm, $fulltext ) {
99  return $this->queryLimit( $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
100  $this->queryNamespaces() . ' ' .
101  $this->queryRanking( $filteredTerm, $fulltext ) . ' ' );
102  }
103 
110  function getIndexField( $fulltext ) {
111  return $fulltext ? 'si_text' : 'si_title';
112  }
113 
121  private function queryMain( $filteredTerm, $fulltext ) {
122  $match = $this->parseQuery( $filteredTerm, $fulltext );
123  $page = $this->db->tableName( 'page' );
124  $searchindex = $this->db->tableName( 'searchindex' );
125 
126  return 'SELECT page_id, page_namespace, page_title, ftindex.[RANK]' .
127  "FROM $page,FREETEXTTABLE($searchindex , $match, LANGUAGE 'English') as ftindex " .
128  'WHERE page_id=ftindex.[KEY] ';
129  }
130 
136  private function parseQuery( $filteredText, $fulltext ) {
137  $lc = $this->legalSearchChars( self::CHARS_NO_SYNTAX );
138  $this->searchTerms = [];
139 
140  # @todo FIXME: This doesn't handle parenthetical expressions.
141  $m = [];
142  $q = [];
143 
144  if ( preg_match_all( '/([-+<>~]?)(([' . $lc . ']+)(\*?)|"[^"]*")/',
145  $filteredText, $m, PREG_SET_ORDER ) ) {
146  foreach ( $m as $terms ) {
147  $q[] = $terms[1] . MediaWikiServices::getInstance()->getContentLanguage()->
148  normalizeForSearch( $terms[2] );
149 
150  if ( !empty( $terms[3] ) ) {
151  $regexp = preg_quote( $terms[3], '/' );
152  if ( $terms[4] ) {
153  $regexp .= "[0-9A-Za-z_]+";
154  }
155  } else {
156  $regexp = preg_quote( str_replace( '"', '', $terms[2] ), '/' );
157  }
158  $this->searchTerms[] = $regexp;
159  }
160  }
161 
162  $searchon = $this->db->addQuotes( implode( ',', $q ) );
163  $field = $this->getIndexField( $fulltext );
164  return "$field, $searchon";
165  }
166 
176  function update( $id, $title, $text ) {
177  // We store the column data as UTF-8 byte order marked binary stream
178  // because we are invoking the plain text IFilter on it so that, and we want it
179  // to properly decode the stream as UTF-8. SQL doesn't support UTF8 as a data type
180  // but the indexer will correctly handle it by this method. Since all we are doing
181  // is passing this data to the indexer and never retrieving it via PHP, this will save space
182  $table = $this->db->tableName( 'searchindex' );
183  $utf8bom = '0xEFBBBF';
184  $si_title = $utf8bom . bin2hex( $title );
185  $si_text = $utf8bom . bin2hex( $text );
186  $sql = "DELETE FROM $table WHERE si_page = $id;";
187  $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, $si_text)";
188  return $this->db->query( $sql, 'SearchMssql::update' );
189  }
190 
199  function updateTitle( $id, $title ) {
200  $table = $this->db->tableName( 'searchindex' );
201 
202  // see update for why we are using the utf8bom
203  $utf8bom = '0xEFBBBF';
204  $si_title = $utf8bom . bin2hex( $title );
205  $sql = "DELETE FROM $table WHERE si_page = $id;";
206  $sql .= "INSERT INTO $table (si_page, si_title, si_text) VALUES ($id, $si_title, 0x00)";
207  return $this->db->query( $sql, 'SearchMssql::updateTitle' );
208  }
209 }
SearchEngine\legalSearchChars
static legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search NOTE: usage as static is deprecated and preserved only as BC measure.
Definition: SearchEngine.php:291
SearchMssql
Search engine hook base class for Mssql (ConText).
Definition: SearchMssql.php:31
SearchMssql\queryLimit
queryLimit( $sql)
Return a LIMIT clause to limit results on the query.
Definition: SearchMssql.php:74
SearchMssql\queryMain
queryMain( $filteredTerm, $fulltext)
Get the base part of the search query.
Definition: SearchMssql.php:121
SearchEngine\$namespaces
int[] null $namespaces
Definition: SearchEngine.php:41
SearchMssql\doSearchTitleInDB
doSearchTitleInDB( $term)
Perform a title-only search query and return a result set.
Definition: SearchMssql.php:49
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
SearchDatabase
Base search engine base class for database-backed searches.
Definition: SearchDatabase.php:31
namespaces
to move a page</td >< td > &*You are moving the page across namespaces
Definition: All_system_messages.txt:2670
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
SearchMssql\getQuery
getQuery( $filteredTerm, $fulltext)
Construct the full SQL query to do the search.
Definition: SearchMssql.php:98
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
SearchMssql\updateTitle
updateTitle( $id, $title)
Update a search index record's title only.
Definition: SearchMssql.php:199
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$term
whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2878
SearchMssql\parseQuery
parseQuery( $filteredText, $fulltext)
Definition: SearchMssql.php:136
SqlSearchResultSet
This class is used for different SQL-based search engines shipped with MediaWiki.
Definition: SqlSearchResultSet.php:9
SearchMssql\doSearchTextInDB
doSearchTextInDB( $term)
Perform a full text search query and return a result set.
Definition: SearchMssql.php:38
SearchMssql\queryRanking
queryRanking( $filteredTerm, $fulltext)
Does not do anything for generic search engine subclasses may define this though.
Definition: SearchMssql.php:86
SearchMssql\update
update( $id, $title, $text)
Create or update the search index record for the given page.
Definition: SearchMssql.php:176
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
SearchMssql\getIndexField
getIndexField( $fulltext)
Picks which field to index on, depending on what type of query.
Definition: SearchMssql.php:110
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
SearchDatabase\filter
filter( $text)
Return a 'cleaned up' search string.
Definition: SearchDatabase.php:86
SearchMssql\queryNamespaces
queryNamespaces()
Return a partial WHERE clause to limit the search to the given namespaces.
Definition: SearchMssql.php:59