Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchDatabase
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSearchText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSearchTextInDB
n/a
0 / 0
n/a
0 / 0
0
 doSearchTitle
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 doSearchTitleInDB
n/a
0 / 0
n/a
0 / 0
0
 filter
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 extractNamespacePrefix
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Database 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
24use MediaWiki\Status\Status;
25use Wikimedia\Rdbms\DBQueryError;
26use Wikimedia\Rdbms\IConnectionProvider;
27
28/**
29 * Base search engine base class for database-backed searches
30 * @stable to extend
31 * @ingroup Search
32 * @since 1.23
33 */
34abstract class SearchDatabase extends SearchEngine {
35    /**
36     * @var string[] search terms
37     */
38    protected $searchTerms = [];
39    protected IConnectionProvider $dbProvider;
40
41    /**
42     * @param IConnectionProvider $dbProvider
43     */
44    public function __construct( IConnectionProvider $dbProvider ) {
45        $this->dbProvider = $dbProvider;
46    }
47
48    /**
49     * @param string $term
50     * @return ISearchResultSet|Status|null
51     */
52    final public function doSearchText( $term ) {
53        return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
54    }
55
56    /**
57     * Perform a full text search query and return a result set.
58     *
59     * @param string $term Raw search term
60     * @return SqlSearchResultSet|null
61     */
62    abstract protected function doSearchTextInDB( $term );
63
64    /**
65     * @param string $term
66     * @return ISearchResultSet|null
67     */
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__
79                    );
80            } else {
81                throw $dqe;
82            }
83        }
84    }
85
86    /**
87     * Perform a title-only search query and return a result set.
88     *
89     * @param string $term Raw search term
90     * @return SqlSearchResultSet|null
91     */
92    abstract protected function doSearchTitleInDB( $term );
93
94    /**
95     * Return a 'cleaned up' search string
96     *
97     * @param string $text
98     * @return string
99     */
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 ) );
106    }
107
108    /**
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
113     */
114    final protected function extractNamespacePrefix( $term ) {
115        $queryAndNs = self::parseNamespacePrefixes( $term );
116        if ( $queryAndNs === false ) {
117            return $term;
118        }
119        $this->namespaces = $queryAndNs[1];
120        return $queryAndNs[0];
121    }
122}