Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
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 * @license GPL-2.0-or-later
6 * @file
7 * @ingroup Search
8 */
9
10namespace MediaWiki\Search;
11
12use MediaWiki\Status\Status;
13use Wikimedia\Rdbms\DBQueryError;
14use Wikimedia\Rdbms\IConnectionProvider;
15
16/**
17 * Base search engine base class for database-backed searches
18 * @stable to extend
19 * @ingroup Search
20 * @since 1.23
21 */
22abstract class SearchDatabase extends SearchEngine {
23    /**
24     * @var string[] search terms
25     */
26    protected $searchTerms = [];
27    protected IConnectionProvider $dbProvider;
28
29    public function __construct( IConnectionProvider $dbProvider ) {
30        $this->dbProvider = $dbProvider;
31    }
32
33    /**
34     * @param string $term
35     * @return ISearchResultSet|Status|null
36     */
37    final public function doSearchText( $term ) {
38        return $this->doSearchTextInDB( $this->extractNamespacePrefix( $term ) );
39    }
40
41    /**
42     * Perform a full text search query and return a result set.
43     *
44     * @param string $term Raw search term
45     * @return SqlSearchResultSet|null
46     */
47    abstract protected function doSearchTextInDB( $term );
48
49    /**
50     * @param string $term
51     * @return ISearchResultSet|null
52     */
53    final public function doSearchTitle( $term ) {
54        try {
55            return $this->doSearchTitleInDB( $this->extractNamespacePrefix( $term ) );
56        } catch ( DBQueryError $dqe ) {
57            if ( $dqe->errno == 1064 ) {
58                throw new DBQueryError(
59                    $dqe->db,
60                    "Query incompatible with database engine. For more information: " .
61                    "https://bugs.mysql.com/bug.php?id=78485 https://jira.mariadb.org/browse/MDEV-21750 / " .
62                    "https://phabricator.wikimedia.org/T355096",
63                    1064, $dqe->sql, __METHOD__
64                    );
65            } else {
66                throw $dqe;
67            }
68        }
69    }
70
71    /**
72     * Perform a title-only search query and return a result set.
73     *
74     * @param string $term Raw search term
75     * @return SqlSearchResultSet|null
76     */
77    abstract protected function doSearchTitleInDB( $term );
78
79    /**
80     * Return a 'cleaned up' search string
81     *
82     * @param string $text
83     * @return string
84     */
85    protected function filter( $text ) {
86        // List of chars allowed in the search query.
87        // This must include chars used in the search syntax.
88        // Usually " (phrase) or * (wildcards) if supported by the engine
89        $lc = $this->legalSearchChars( self::CHARS_ALL );
90        return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
91    }
92
93    /**
94     * Extract the optional namespace prefix and set self::namespaces
95     * accordingly and return the query string
96     * @param string $term
97     * @return string the query string without any namespace prefix
98     */
99    final protected function extractNamespacePrefix( $term ) {
100        $queryAndNs = self::parseNamespacePrefixes( $term );
101        if ( $queryAndNs === false ) {
102            return $term;
103        }
104        $this->namespaces = $queryAndNs[1];
105        return $queryAndNs[0];
106    }
107}
108
109/** @deprecated class alias since 1.46 */
110class_alias( SearchDatabase::class, 'SearchDatabase' );