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