MediaWiki master
SearchDatabase.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Search;
11
16
23abstract class SearchDatabase extends SearchEngine {
27 protected $searchTerms = [];
29
31 $this->dbProvider = $dbProvider;
32 }
33
38 final public function doSearchText( $term ) {
39 return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
40 }
41
48 abstract protected function doSearchTextInDB( $term );
49
54 final public function doSearchTitle( $term ) {
55 try {
56 return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
57 } catch ( DBQueryError $dqe ) {
58 if ( $dqe->errno == 1064 ) {
59 throw new DBQueryError(
60 $dqe->db,
61 "Query incompatible with database engine. For more information: " .
62 "https://bugs.mysql.com/bug.php?id=78485 https://jira.mariadb.org/browse/MDEV-21750 / " .
63 "https://phabricator.wikimedia.org/T355096",
64 1064, $dqe->sql, __METHOD__
65 );
66 } else {
67 throw $dqe;
68 }
69 }
70 }
71
78 abstract protected function doSearchTitleInDB( $term );
79
86 protected function filter( $text ) {
87 // List of chars allowed in the search query.
88 // This must include chars used in the search syntax.
89 // Usually " (phrase) or * (wildcards) if supported by the engine
90 $lc = $this->legalSearchChars( self::CHARS_ALL );
91 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
92 }
93
100 final protected function extractNamespacePrefix( $term ) {
101 $queryAndNs = self::parseNamespacePrefixes( $term );
102 if ( $queryAndNs === false ) {
103 return $term;
104 }
105 $this->namespaces = $queryAndNs[1];
106 return $queryAndNs[0];
107 }
108
109 protected function regexTerm( string $string, ?string $wildcard = null ): string {
110 $regex = preg_quote( $string, '/' );
111 if ( MediaWikiServices::getInstance()->getContentLanguage()->hasWordBreaks() ) {
112 if ( $wildcard ) {
113 // Don't cut off the final bit!
114 $regex = "\b$regex";
115 } else {
116 $regex = "\b$regex\b";
117 }
118 } else {
119 // For Chinese, words may legitimately abut other words in the text literal.
120 // Don't add \b boundary checks... note this could cause false positives
121 // for Latin chars.
122 }
123 return $regex;
124 }
125}
126
128class_alias( SearchDatabase::class, 'SearchDatabase' );
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Base search engine base class for database-backed searches.
extractNamespacePrefix( $term)
Extract the optional namespace prefix and set self::namespaces accordingly and return the query strin...
doSearchTextInDB( $term)
Perform a full text search query and return a result set.
doSearchTitleInDB( $term)
Perform a title-only search query and return a result set.
__construct(IConnectionProvider $dbProvider)
regexTerm(string $string, ?string $wildcard=null)
string[] $searchTerms
search terms
filter( $text)
Return a 'cleaned up' search string.
Contain a class for special pages.
legalSearchChars( $type=self::CHARS_ALL)
Get chars legal for search.
static parseNamespacePrefixes( $query, $withAllKeyword=true, $withPrefixSearchExtractNamespaceHook=false)
Parse some common prefixes: all (search everything) or namespace names.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Provide primary and replica IDatabase connections.
Definition of a mapping for the search index field.