Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / search / PrefixSearch.php
1 <?php
2 /**
3 * Prefix search of page names.
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 */
22
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Handles searching prefixes of titles and finding any page
27 * names that match. Used largely by the OpenSearch implementation.
28 * @deprecated Since 1.27, Use SearchEngine::defaultPrefixSearch or SearchEngine::completionSearch
29 *
30 * @ingroup Search
31 */
32 abstract class PrefixSearch {
33 /**
34 * Do a prefix search of titles and return a list of matching page names.
35 *
36 * @param string $search
37 * @param int $limit
38 * @param array $namespaces Used if query is not explicitly prefixed
39 * @param int $offset How many results to offset from the beginning
40 * @return array Array of strings or Title objects
41 */
42 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
43 $search = trim( $search );
44 if ( $search == '' ) {
45 return []; // Return empty result
46 }
47
48 $hasNamespace = SearchEngine::parseNamespacePrefixes( $search, false, true );
49 if ( $hasNamespace !== false ) {
50 list( $search, $namespaces ) = $hasNamespace;
51 }
52
53 return $this->searchBackend( $namespaces, $search, $limit, $offset );
54 }
55
56 /**
57 * Do a prefix search for all possible variants of the prefix
58 * @param string $search
59 * @param int $limit
60 * @param array $namespaces
61 * @param int $offset How many results to offset from the beginning
62 *
63 * @return array
64 */
65 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
66 $searches = $this->search( $search, $limit, $namespaces, $offset );
67
68 // if the content language has variants, try to retrieve fallback results
69 $fallbackLimit = $limit - count( $searches );
70 if ( $fallbackLimit > 0 ) {
71 $fallbackSearches = MediaWikiServices::getInstance()->getContentLanguage()->
72 autoConvertToAllVariants( $search );
73 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
74
75 foreach ( $fallbackSearches as $fbs ) {
76 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
77 $searches = array_merge( $searches, $fallbackSearchResult );
78 $fallbackLimit -= count( $fallbackSearchResult );
79
80 if ( $fallbackLimit == 0 ) {
81 break;
82 }
83 }
84 }
85 return $searches;
86 }
87
88 /**
89 * When implemented in a descendant class, receives an array of Title objects and returns
90 * either an unmodified array or an array of strings corresponding to titles passed to it.
91 *
92 * @param array $titles
93 * @return array
94 */
95 abstract protected function titles( array $titles );
96
97 /**
98 * When implemented in a descendant class, receives an array of titles as strings and returns
99 * either an unmodified array or an array of Title objects corresponding to strings received.
100 *
101 * @param array $strings
102 *
103 * @return array
104 */
105 abstract protected function strings( array $strings );
106
107 /**
108 * Do a prefix search of titles and return a list of matching page names.
109 * @param array $namespaces
110 * @param string $search
111 * @param int $limit
112 * @param int $offset How many results to offset from the beginning
113 * @return array Array of strings
114 */
115 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
116 if ( count( $namespaces ) == 1 ) {
117 $ns = $namespaces[0];
118 if ( $ns == NS_MEDIA ) {
119 $namespaces = [ NS_FILE ];
120 } elseif ( $ns == NS_SPECIAL ) {
121 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
122 }
123 }
124 $srchres = [];
125 if ( Hooks::run(
126 'PrefixSearchBackend',
127 [ $namespaces, $search, $limit, &$srchres, $offset ]
128 ) ) {
129 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
130 }
131 return $this->strings(
132 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
133 }
134
135 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
136 if ( $offset === 0 ) {
137 // Only perform exact db match if offset === 0
138 // This is still far from perfect but at least we avoid returning the
139 // same title afain and again when the user is scrolling with a query
140 // that matches a title in the db.
141 $rescorer = new SearchExactMatchRescorer();
142 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
143 }
144 return $srchres;
145 }
146
147 /**
148 * Prefix search special-case for Special: namespace.
149 *
150 * @param string $search Term
151 * @param int $limit Max number of items to return
152 * @param int $offset Number of items to offset
153 * @return array
154 */
155 protected function specialSearch( $search, $limit, $offset ) {
156 $searchParts = explode( '/', $search, 2 );
157 $searchKey = $searchParts[0];
158 $subpageSearch = $searchParts[1] ?? null;
159
160 // Handle subpage search separately.
161 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
162 if ( $subpageSearch !== null ) {
163 // Try matching the full search string as a page name
164 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
165 if ( !$specialTitle ) {
166 return [];
167 }
168 $special = $spFactory->getPage( $specialTitle->getText() );
169 if ( $special ) {
170 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
171 return array_map( function ( $sub ) use ( $specialTitle ) {
172 return $specialTitle->getSubpage( $sub );
173 }, $subpages );
174 } else {
175 return [];
176 }
177 }
178
179 # normalize searchKey, so aliases with spaces can be found - T27675
180 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
181 $searchKey = str_replace( ' ', '_', $searchKey );
182 $searchKey = $contLang->caseFold( $searchKey );
183
184 // Unlike SpecialPage itself, we want the canonical forms of both
185 // canonical and alias title forms...
186 $keys = [];
187 foreach ( $spFactory->getNames() as $page ) {
188 $keys[$contLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
189 }
190
191 foreach ( $contLang->getSpecialPageAliases() as $page => $aliases ) {
192 if ( !in_array( $page, $spFactory->getNames() ) ) {# T22885
193 continue;
194 }
195
196 foreach ( $aliases as $key => $alias ) {
197 $keys[$contLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
198 }
199 }
200 ksort( $keys );
201
202 $matches = [];
203 foreach ( $keys as $pageKey => $page ) {
204 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
205 // T29671: Don't use SpecialPage::getTitleFor() here because it
206 // localizes its input leading to searches for e.g. Special:All
207 // returning Spezial:MediaWiki-Systemnachrichten and returning
208 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
209 $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] );
210
211 if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) {
212 // We have enough items in primary rank, no use to continue
213 break;
214 }
215 }
216
217 }
218
219 // Ensure keys are in order
220 ksort( $matches );
221 // Flatten the array
222 $matches = array_reduce( $matches, 'array_merge', [] );
223
224 return array_slice( $matches, $offset, $limit );
225 }
226
227 /**
228 * Unless overridden by PrefixSearchBackend hook...
229 * This is case-sensitive (First character may
230 * be automatically capitalized by Title::secureAndSpit()
231 * later on depending on $wgCapitalLinks)
232 *
233 * @param array|null $namespaces Namespaces to search in
234 * @param string $search Term
235 * @param int $limit Max number of items to return
236 * @param int $offset Number of items to skip
237 * @return Title[] Array of Title objects
238 */
239 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
240 // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
241 if ( $namespaces === null ) {
242 $namespaces = [];
243 }
244 if ( !$namespaces ) {
245 $namespaces[] = NS_MAIN;
246 }
247
248 // Construct suitable prefix for each namespace. They differ in cases where
249 // some namespaces always capitalize and some don't.
250 $prefixes = [];
251 foreach ( $namespaces as $namespace ) {
252 // For now, if special is included, ignore the other namespaces
253 if ( $namespace == NS_SPECIAL ) {
254 return $this->specialSearch( $search, $limit, $offset );
255 }
256
257 $title = Title::makeTitleSafe( $namespace, $search );
258 // Why does the prefix default to empty?
259 $prefix = $title ? $title->getDBkey() : '';
260 $prefixes[$prefix][] = $namespace;
261 }
262
263 $dbr = wfGetDB( DB_REPLICA );
264 // Often there is only one prefix that applies to all requested namespaces,
265 // but sometimes there are two if some namespaces do not always capitalize.
266 $conds = [];
267 foreach ( $prefixes as $prefix => $namespaces ) {
268 $condition = [
269 'page_namespace' => $namespaces,
270 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
271 ];
272 $conds[] = $dbr->makeList( $condition, LIST_AND );
273 }
274
275 $table = 'page';
276 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
277 $conds = $dbr->makeList( $conds, LIST_OR );
278 $options = [
279 'LIMIT' => $limit,
280 'ORDER BY' => [ 'page_title', 'page_namespace' ],
281 'OFFSET' => $offset
282 ];
283
284 $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
285
286 return iterator_to_array( TitleArray::newFromResult( $res ) );
287 }
288
289 /**
290 * Validate an array of numerical namespace indexes
291 *
292 * @param array $namespaces
293 * @return array (default: contains only NS_MAIN)
294 */
295 protected function validateNamespaces( $namespaces ) {
296 // We will look at each given namespace against content language namespaces
297 $validNamespaces = MediaWikiServices::getInstance()->getContentLanguage()->getNamespaces();
298 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
299 $valid = [];
300 foreach ( $namespaces as $ns ) {
301 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
302 $valid[] = $ns;
303 }
304 }
305 if ( count( $valid ) > 0 ) {
306 return $valid;
307 }
308 }
309
310 return [ NS_MAIN ];
311 }
312 }