Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchDatabase.php
blob1efa77623743d71682f995c817c70862689db39e
1 <?php
2 /**
3 * Database search engine
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.
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.
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
20 * @file
21 * @ingroup Search
24 use MediaWiki\Status\Status;
25 use Wikimedia\Rdbms\DBQueryError;
26 use Wikimedia\Rdbms\IConnectionProvider;
28 /**
29 * Base search engine base class for database-backed searches
30 * @stable to extend
31 * @ingroup Search
32 * @since 1.23
34 abstract class SearchDatabase extends SearchEngine {
35 /**
36 * @var string[] search terms
38 protected $searchTerms = [];
39 protected IConnectionProvider $dbProvider;
41 /**
42 * @param IConnectionProvider $dbProvider
44 public function __construct( IConnectionProvider $dbProvider ) {
45 $this->dbProvider = $dbProvider;
48 /**
49 * @param string $term
50 * @return ISearchResultSet|Status|null
52 final public function doSearchText( $term ) {
53 return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
56 /**
57 * Perform a full text search query and return a result set.
59 * @param string $term Raw search term
60 * @return SqlSearchResultSet|null
62 abstract protected function doSearchTextInDB( $term );
64 /**
65 * @param string $term
66 * @return ISearchResultSet|null
68 final public function doSearchTitle( $term ) {
69 try {
70 return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
71 } catch ( DBQueryError $dqe ) {
72 if ( $dqe->errno == 1064 ) {
73 throw new DBQueryError(
74 $dqe->db,
75 "Query incompatible with database engine. For more information: " .
76 "https://bugs.mysql.com/bug.php?id=78485 https://jira.mariadb.org/browse/MDEV-21750 / " .
77 "https://phabricator.wikimedia.org/T355096",
78 1064, $dqe->sql, __METHOD__
80 } else {
81 throw $dqe;
86 /**
87 * Perform a title-only search query and return a result set.
89 * @param string $term Raw search term
90 * @return SqlSearchResultSet|null
92 abstract protected function doSearchTitleInDB( $term );
94 /**
95 * Return a 'cleaned up' search string
97 * @param string $text
98 * @return string
100 protected function filter( $text ) {
101 // List of chars allowed in the search query.
102 // This must include chars used in the search syntax.
103 // Usually " (phrase) or * (wildcards) if supported by the engine
104 $lc = $this->legalSearchChars( self::CHARS_ALL );
105 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
109 * Extract the optional namespace prefix and set self::namespaces
110 * accordingly and return the query string
111 * @param string $term
112 * @return string the query string without any namespace prefix
114 final protected function extractNamespacePrefix( $term ) {
115 $queryAndNs = self::parseNamespacePrefixes( $term );
116 if ( $queryAndNs === false ) {
117 return $term;
119 $this->namespaces = $queryAndNs[1];
120 return $queryAndNs[0];