Merge "Change mid-upload 'cancel' button to 'back'"
[lhc/web/wiklou.git] / includes / 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 /**
24 * Handles searching prefixes of titles and finding any page
25 * names that match. Used largely by the OpenSearch implementation.
26 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
27 *
28 * @ingroup Search
29 */
30 abstract class PrefixSearch {
31 /**
32 * Do a prefix search of titles and return a list of matching page names.
33 * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
34 *
35 * @param string $search
36 * @param int $limit
37 * @param array $namespaces Used if query is not explicitly prefixed
38 * @param int $offset How many results to offset from the beginning
39 * @return array Array of strings
40 */
41 public static function titleSearch( $search, $limit, $namespaces = [], $offset = 0 ) {
42 $prefixSearch = new StringPrefixSearch;
43 return $prefixSearch->search( $search, $limit, $namespaces, $offset );
44 }
45
46 /**
47 * Do a prefix search of titles and return a list of matching page names.
48 *
49 * @param string $search
50 * @param int $limit
51 * @param array $namespaces Used if query is not explicitly prefixed
52 * @param int $offset How many results to offset from the beginning
53 * @return array Array of strings or Title objects
54 */
55 public function search( $search, $limit, $namespaces = [], $offset = 0 ) {
56 $search = trim( $search );
57 if ( $search == '' ) {
58 return []; // Return empty result
59 }
60
61 $hasNamespace = $this->extractNamespace( $search );
62 if ( $hasNamespace ) {
63 list( $namespace, $search ) = $hasNamespace;
64 $namespaces = [ $namespace ];
65 } else {
66 $namespaces = $this->validateNamespaces( $namespaces );
67 Hooks::run( 'PrefixSearchExtractNamespace', [ &$namespaces, &$search ] );
68 }
69
70 return $this->searchBackend( $namespaces, $search, $limit, $offset );
71 }
72
73 /**
74 * Figure out if given input contains an explicit namespace.
75 *
76 * @param string $input
77 * @return false|array Array of namespace and remaining text, or false if no namespace given.
78 */
79 protected function extractNamespace( $input ) {
80 if ( strpos( $input, ':' ) === false ) {
81 return false;
82 }
83
84 // Namespace prefix only
85 $title = Title::newFromText( $input . 'Dummy' );
86 if (
87 $title &&
88 $title->getText() === 'Dummy' &&
89 !$title->inNamespace( NS_MAIN ) &&
90 !$title->isExternal()
91 ) {
92 return [ $title->getNamespace(), '' ];
93 }
94
95 // Namespace prefix with additional input
96 $title = Title::newFromText( $input );
97 if (
98 $title &&
99 !$title->inNamespace( NS_MAIN ) &&
100 !$title->isExternal()
101 ) {
102 // getText provides correct capitalization
103 return [ $title->getNamespace(), $title->getText() ];
104 }
105
106 return false;
107 }
108
109 /**
110 * Do a prefix search for all possible variants of the prefix
111 * @param string $search
112 * @param int $limit
113 * @param array $namespaces
114 * @param int $offset How many results to offset from the beginning
115 *
116 * @return array
117 */
118 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
119 $searches = $this->search( $search, $limit, $namespaces, $offset );
120
121 // if the content language has variants, try to retrieve fallback results
122 $fallbackLimit = $limit - count( $searches );
123 if ( $fallbackLimit > 0 ) {
124 global $wgContLang;
125
126 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
127 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $search ] );
128
129 foreach ( $fallbackSearches as $fbs ) {
130 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
131 $searches = array_merge( $searches, $fallbackSearchResult );
132 $fallbackLimit -= count( $fallbackSearchResult );
133
134 if ( $fallbackLimit == 0 ) {
135 break;
136 }
137 }
138 }
139 return $searches;
140 }
141
142 /**
143 * When implemented in a descendant class, receives an array of Title objects and returns
144 * either an unmodified array or an array of strings corresponding to titles passed to it.
145 *
146 * @param array $titles
147 * @return array
148 */
149 abstract protected function titles( array $titles );
150
151 /**
152 * When implemented in a descendant class, receives an array of titles as strings and returns
153 * either an unmodified array or an array of Title objects corresponding to strings received.
154 *
155 * @param array $strings
156 *
157 * @return array
158 */
159 abstract protected function strings( array $strings );
160
161 /**
162 * Do a prefix search of titles and return a list of matching page names.
163 * @param array $namespaces
164 * @param string $search
165 * @param int $limit
166 * @param int $offset How many results to offset from the beginning
167 * @return array Array of strings
168 */
169 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
170 if ( count( $namespaces ) == 1 ) {
171 $ns = $namespaces[0];
172 if ( $ns == NS_MEDIA ) {
173 $namespaces = [ NS_FILE ];
174 } elseif ( $ns == NS_SPECIAL ) {
175 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
176 }
177 }
178 $srchres = [];
179 if ( Hooks::run(
180 'PrefixSearchBackend',
181 [ $namespaces, $search, $limit, &$srchres, $offset ]
182 ) ) {
183 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
184 }
185 return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
186 }
187
188 private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) {
189 $rescorer = new SearchExactMatchRescorer();
190 return $rescorer->rescore( $search, $namespaces, $srchres, $limit );
191 }
192
193 /**
194 * Prefix search special-case for Special: namespace.
195 *
196 * @param string $search Term
197 * @param int $limit Max number of items to return
198 * @param int $offset Number of items to offset
199 * @return array
200 */
201 protected function specialSearch( $search, $limit, $offset ) {
202 global $wgContLang;
203
204 $searchParts = explode( '/', $search, 2 );
205 $searchKey = $searchParts[0];
206 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
207
208 // Handle subpage search separately.
209 if ( $subpageSearch !== null ) {
210 // Try matching the full search string as a page name
211 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
212 if ( !$specialTitle ) {
213 return [];
214 }
215 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
216 if ( $special ) {
217 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
218 return array_map( function ( $sub ) use ( $specialTitle ) {
219 return $specialTitle->getSubpage( $sub );
220 }, $subpages );
221 } else {
222 return [];
223 }
224 }
225
226 # normalize searchKey, so aliases with spaces can be found - bug 25675
227 $searchKey = str_replace( ' ', '_', $searchKey );
228 $searchKey = $wgContLang->caseFold( $searchKey );
229
230 // Unlike SpecialPage itself, we want the canonical forms of both
231 // canonical and alias title forms...
232 $keys = [];
233 foreach ( SpecialPageFactory::getNames() as $page ) {
234 $keys[$wgContLang->caseFold( $page )] = $page;
235 }
236
237 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
238 if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# bug 20885
239 continue;
240 }
241
242 foreach ( $aliases as $alias ) {
243 $keys[$wgContLang->caseFold( $alias )] = $alias;
244 }
245 }
246 ksort( $keys );
247
248 $srchres = [];
249 $skipped = 0;
250 foreach ( $keys as $pageKey => $page ) {
251 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
252 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
253 // localizes its input leading to searches for e.g. Special:All
254 // returning Spezial:MediaWiki-Systemnachrichten and returning
255 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
256 if ( $offset > 0 && $skipped < $offset ) {
257 $skipped++;
258 continue;
259 }
260 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
261 }
262
263 if ( count( $srchres ) >= $limit ) {
264 break;
265 }
266 }
267
268 return $srchres;
269 }
270
271 /**
272 * Unless overridden by PrefixSearchBackend hook...
273 * This is case-sensitive (First character may
274 * be automatically capitalized by Title::secureAndSpit()
275 * later on depending on $wgCapitalLinks)
276 *
277 * @param array|null $namespaces Namespaces to search in
278 * @param string $search Term
279 * @param int $limit Max number of items to return
280 * @param int $offset Number of items to skip
281 * @return Title[] Array of Title objects
282 */
283 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
284 // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
285 if ( $namespaces === null ) {
286 $namespaces = [];
287 }
288 if ( !$namespaces ) {
289 $namespaces[] = NS_MAIN;
290 }
291
292 // Construct suitable prefix for each namespace. They differ in cases where
293 // some namespaces always capitalize and some don't.
294 $prefixes = [];
295 foreach ( $namespaces as $namespace ) {
296 // For now, if special is included, ignore the other namespaces
297 if ( $namespace == NS_SPECIAL ) {
298 return $this->specialSearch( $search, $limit, $offset );
299 }
300
301 $title = Title::makeTitleSafe( $namespace, $search );
302 // Why does the prefix default to empty?
303 $prefix = $title ? $title->getDBkey() : '';
304 $prefixes[$prefix][] = $namespace;
305 }
306
307 $dbr = wfGetDB( DB_REPLICA );
308 // Often there is only one prefix that applies to all requested namespaces,
309 // but sometimes there are two if some namespaces do not always capitalize.
310 $conds = [];
311 foreach ( $prefixes as $prefix => $namespaces ) {
312 $condition = [
313 'page_namespace' => $namespaces,
314 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
315 ];
316 $conds[] = $dbr->makeList( $condition, LIST_AND );
317 }
318
319 $table = 'page';
320 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
321 $conds = $dbr->makeList( $conds, LIST_OR );
322 $options = [
323 'LIMIT' => $limit,
324 'ORDER BY' => [ 'page_title', 'page_namespace' ],
325 'OFFSET' => $offset
326 ];
327
328 $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
329
330 return iterator_to_array( TitleArray::newFromResult( $res ) );
331 }
332
333 /**
334 * Validate an array of numerical namespace indexes
335 *
336 * @param array $namespaces
337 * @return array (default: contains only NS_MAIN)
338 */
339 protected function validateNamespaces( $namespaces ) {
340 global $wgContLang;
341
342 // We will look at each given namespace against wgContLang namespaces
343 $validNamespaces = $wgContLang->getNamespaces();
344 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
345 $valid = [];
346 foreach ( $namespaces as $ns ) {
347 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
348 $valid[] = $ns;
349 }
350 }
351 if ( count( $valid ) > 0 ) {
352 return $valid;
353 }
354 }
355
356 return [ NS_MAIN ];
357 }
358 }
359
360 /**
361 * Performs prefix search, returning Title objects
362 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
363 * @ingroup Search
364 */
365 class TitlePrefixSearch extends PrefixSearch {
366
367 protected function titles( array $titles ) {
368 return $titles;
369 }
370
371 protected function strings( array $strings ) {
372 $titles = array_map( 'Title::newFromText', $strings );
373 $lb = new LinkBatch( $titles );
374 $lb->setCaller( __METHOD__ );
375 $lb->execute();
376 return $titles;
377 }
378 }
379
380 /**
381 * Performs prefix search, returning strings
382 * @deprecated Since 1.27, Use SearchEngine::prefixSearchSubpages or SearchEngine::completionSearch
383 * @ingroup Search
384 */
385 class StringPrefixSearch extends PrefixSearch {
386
387 protected function titles( array $titles ) {
388 return array_map( function ( Title $t ) {
389 return $t->getPrefixedText();
390 }, $titles );
391 }
392
393 protected function strings( array $strings ) {
394 return $strings;
395 }
396 }