Followup cf5f641: pass $params by reference again
[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 *
27 * @ingroup Search
28 */
29 abstract class PrefixSearch {
30 /**
31 * Do a prefix search of titles and return a list of matching page names.
32 * @deprecated Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
33 *
34 * @param string $search
35 * @param int $limit
36 * @param array $namespaces Used if query is not explicitly prefixed
37 * @param int $offset How many results to offset from the beginning
38 * @return array Array of strings
39 */
40 public static function titleSearch( $search, $limit, $namespaces = array(), $offset = 0 ) {
41 $prefixSearch = new StringPrefixSearch;
42 return $prefixSearch->search( $search, $limit, $namespaces, $offset );
43 }
44
45 /**
46 * Do a prefix search of titles and return a list of matching page names.
47 *
48 * @param string $search
49 * @param int $limit
50 * @param array $namespaces Used if query is not explicitly prefixed
51 * @param int $offset How many results to offset from the beginning
52 * @return array Array of strings or Title objects
53 */
54 public function search( $search, $limit, $namespaces = array(), $offset = 0 ) {
55 $search = trim( $search );
56 if ( $search == '' ) {
57 return array(); // Return empty result
58 }
59 $namespaces = $this->validateNamespaces( $namespaces );
60
61 // Find a Title which is not an interwiki and is in NS_MAIN
62 $title = Title::newFromText( $search );
63 if ( $title && !$title->isExternal() ) {
64 $ns = array( $title->getNamespace() );
65 $search = $title->getText();
66 if ( $ns[0] == NS_MAIN ) {
67 $ns = $namespaces; // no explicit prefix, use default namespaces
68 Hooks::run( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) );
69 }
70 return $this->searchBackend( $ns, $search, $limit, $offset );
71 }
72
73 // Is this a namespace prefix?
74 $title = Title::newFromText( $search . 'Dummy' );
75 if ( $title && $title->getText() == 'Dummy'
76 && $title->getNamespace() != NS_MAIN
77 && !$title->isExternal() )
78 {
79 $namespaces = array( $title->getNamespace() );
80 $search = '';
81 } else {
82 Hooks::run( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) );
83 }
84
85 return $this->searchBackend( $namespaces, $search, $limit, $offset );
86 }
87
88 /**
89 * Do a prefix search for all possible variants of the prefix
90 * @param string $search
91 * @param int $limit
92 * @param array $namespaces
93 * @param int $offset How many results to offset from the beginning
94 *
95 * @return array
96 */
97 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
98 wfProfileIn( __METHOD__ );
99 $searches = $this->search( $search, $limit, $namespaces, $offset );
100
101 // if the content language has variants, try to retrieve fallback results
102 $fallbackLimit = $limit - count( $searches );
103 if ( $fallbackLimit > 0 ) {
104 global $wgContLang;
105
106 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
107 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
108
109 foreach ( $fallbackSearches as $fbs ) {
110 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
111 $searches = array_merge( $searches, $fallbackSearchResult );
112 $fallbackLimit -= count( $fallbackSearchResult );
113
114 if ( $fallbackLimit == 0 ) {
115 break;
116 }
117 }
118 }
119 wfProfileOut( __METHOD__ );
120 return $searches;
121 }
122
123 /**
124 * When implemented in a descendant class, receives an array of Title objects and returns
125 * either an unmodified array or an array of strings corresponding to titles passed to it.
126 *
127 * @param array $titles
128 * @return array
129 */
130 abstract protected function titles( array $titles );
131
132 /**
133 * When implemented in a descendant class, receives an array of titles as strings and returns
134 * either an unmodified array or an array of Title objects corresponding to strings received.
135 *
136 * @param array $strings
137 *
138 * @return array
139 */
140 abstract protected function strings( array $strings );
141
142 /**
143 * Do a prefix search of titles and return a list of matching page names.
144 * @param array $namespaces
145 * @param string $search
146 * @param int $limit
147 * @param int $offset How many results to offset from the beginning
148 * @return array Array of strings
149 */
150 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
151 if ( count( $namespaces ) == 1 ) {
152 $ns = $namespaces[0];
153 if ( $ns == NS_MEDIA ) {
154 $namespaces = array( NS_FILE );
155 } elseif ( $ns == NS_SPECIAL ) {
156 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
157 }
158 }
159 $srchres = array();
160 if ( Hooks::run( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres, $offset ) ) ) {
161 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
162 }
163 return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
164 }
165
166 /**
167 * Default search backend does proper prefix searching, but custom backends
168 * may sort based on other algorythms that may cause the exact title match
169 * to not be in the results or be lower down the list.
170 * @param array $srchres results from the hook
171 * @return array munged results from the hook
172 */
173 private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) {
174 // Pick namespace (based on PrefixSearch::defaultSearchBackend)
175 $ns = in_array( NS_MAIN, $namespaces ) ? NS_MAIN : $namespaces[0];
176 $t = Title::newFromText( $search, $ns );
177 if ( !$t || !$t->exists() ) {
178 // No exact match so just return the search results
179 return $srchres;
180 }
181 $string = $t->getPrefixedText();
182 $key = array_search( $string, $srchres );
183 if ( $key !== false ) {
184 // Exact match was in the results so just move it to the front
185 return $this->pullFront( $key, $srchres );
186 }
187 // Exact match not in the search results so check for some redirect handling cases
188 if ( $t->isRedirect() ) {
189 $target = $this->getRedirectTarget( $t );
190 $key = array_search( $target, $srchres );
191 if ( $key !== false ) {
192 // Exact match is a redirect to one of the returned matches so pull the
193 // returned match to the front. This might look odd but the alternative
194 // is to put the redirect in front and drop the match. The name of the
195 // found match is often more descriptive/better formed than the name of
196 // the redirect AND by definition they share a prefix. Hopefully this
197 // choice is less confusing and more helpful. But it might not be. But
198 // it is the choice we're going with for now.
199 return $this->pullFront( $key, $srchres );
200 }
201 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
202 if ( isset( $redirectTargetsToRedirect[$target] ) ) {
203 // The exact match and something in the results list are both redirects
204 // to the same thing! In this case we'll pull the returned match to the
205 // top following the same logic above. Again, it might not be a perfect
206 // choice but it'll do.
207 return $this->pullFront( $redirectTargetsToRedirect[$target], $srchres );
208 }
209 } else {
210 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
211 if ( isset( $redirectTargetsToRedirect[$string] ) ) {
212 // The exact match is the target of a redirect already in the results list so remove
213 // the redirect from the results list and push the exact match to the front
214 array_splice( $srchres, $redirectTargetsToRedirect[$string], 1 );
215 array_unshift( $srchres, $string );
216 return $srchres;
217 }
218 }
219
220 // Exact match is totally unique from the other results so just add it to the front
221 array_unshift( $srchres, $string );
222 // And roll one off the end if the results are too long
223 if ( count( $srchres ) > $limit ) {
224 array_pop( $srchres );
225 }
226 return $srchres;
227 }
228
229 /**
230 * @param Array(string) $titles as strings
231 * @return Array(string => int) redirect target prefixedText to index of title in titles
232 * that is a redirect to it.
233 */
234 private function redirectTargetsToRedirect( $titles ) {
235 $result = array();
236 foreach ( $titles as $key => $titleText ) {
237 $title = Title::newFromText( $titleText );
238 if ( !$title || !$title->isRedirect() ) {
239 continue;
240 }
241 $target = $this->getRedirectTarget( $title );
242 if ( !$target ) {
243 continue;
244 }
245 $result[$target] = $key;
246 }
247 return $result;
248 }
249
250 /**
251 * @param int $key key to pull to the front
252 * @return array $array with the item at $key pulled to the front
253 */
254 private function pullFront( $key, $array ) {
255 $cut = array_splice( $array, $key, 1 );
256 array_unshift( $array, $cut[0] );
257 return $array;
258 }
259
260 private function getRedirectTarget( $title ) {
261 $page = WikiPage::factory( $title );
262 if ( !$page->exists() ) {
263 return null;
264 }
265 return $page->getRedirectTarget()->getPrefixedText();
266 }
267
268 /**
269 * Prefix search special-case for Special: namespace.
270 *
271 * @param string $search Term
272 * @param int $limit Max number of items to return
273 * @param int $offset Number of items to offset
274 * @return array
275 */
276 protected function specialSearch( $search, $limit, $offset ) {
277 global $wgContLang;
278
279 $searchParts = explode( '/', $search, 2 );
280 $searchKey = $searchParts[0];
281 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
282
283 // Handle subpage search separately.
284 if ( $subpageSearch !== null ) {
285 // Try matching the full search string as a page name
286 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
287 if ( !$specialTitle ) {
288 return array();
289 }
290 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
291 if ( $special ) {
292 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
293 return array_map( function ( $sub ) use ( $specialTitle ) {
294 return $specialTitle->getSubpage( $sub );
295 }, $subpages );
296 } else {
297 return array();
298 }
299 }
300
301 # normalize searchKey, so aliases with spaces can be found - bug 25675
302 $searchKey = str_replace( ' ', '_', $searchKey );
303 $searchKey = $wgContLang->caseFold( $searchKey );
304
305 // Unlike SpecialPage itself, we want the canonical forms of both
306 // canonical and alias title forms...
307 $keys = array();
308 foreach ( SpecialPageFactory::getNames() as $page ) {
309 $keys[$wgContLang->caseFold( $page )] = $page;
310 }
311
312 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
313 if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# bug 20885
314 continue;
315 }
316
317 foreach ( $aliases as $alias ) {
318 $keys[$wgContLang->caseFold( $alias )] = $alias;
319 }
320 }
321 ksort( $keys );
322
323 $srchres = array();
324 $skipped = 0;
325 foreach ( $keys as $pageKey => $page ) {
326 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
327 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
328 // localizes its input leading to searches for e.g. Special:All
329 // returning Spezial:MediaWiki-Systemnachrichten and returning
330 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
331 if ( $offset > 0 && $skipped < $offset ) {
332 $skipped++;
333 continue;
334 }
335 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
336 }
337
338 if ( count( $srchres ) >= $limit ) {
339 break;
340 }
341 }
342
343 return $srchres;
344 }
345
346 /**
347 * Unless overridden by PrefixSearchBackend hook...
348 * This is case-sensitive (First character may
349 * be automatically capitalized by Title::secureAndSpit()
350 * later on depending on $wgCapitalLinks)
351 *
352 * @param array $namespaces Namespaces to search in
353 * @param string $search Term
354 * @param int $limit Max number of items to return
355 * @param int $offset Number of items to skip
356 * @return array Array of Title objects
357 */
358 protected function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
359 $ns = array_shift( $namespaces ); // support only one namespace
360 if ( in_array( NS_MAIN, $namespaces ) ) {
361 $ns = NS_MAIN; // if searching on many always default to main
362 }
363
364 $t = Title::newFromText( $search, $ns );
365 $prefix = $t ? $t->getDBkey() : '';
366 $dbr = wfGetDB( DB_SLAVE );
367 $res = $dbr->select( 'page',
368 array( 'page_id', 'page_namespace', 'page_title' ),
369 array(
370 'page_namespace' => $ns,
371 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
372 ),
373 __METHOD__,
374 array(
375 'LIMIT' => $limit,
376 'ORDER BY' => 'page_title',
377 'OFFSET' => $offset
378 )
379 );
380 $srchres = array();
381 foreach ( $res as $row ) {
382 $srchres[] = Title::newFromRow( $row );
383 }
384 return $srchres;
385 }
386
387 /**
388 * Validate an array of numerical namespace indexes
389 *
390 * @param array $namespaces
391 * @return array (default: contains only NS_MAIN)
392 */
393 protected function validateNamespaces( $namespaces ) {
394 global $wgContLang;
395
396 // We will look at each given namespace against wgContLang namespaces
397 $validNamespaces = $wgContLang->getNamespaces();
398 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
399 $valid = array();
400 foreach ( $namespaces as $ns ) {
401 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
402 $valid[] = $ns;
403 }
404 }
405 if ( count( $valid ) > 0 ) {
406 return $valid;
407 }
408 }
409
410 return array( NS_MAIN );
411 }
412 }
413
414 /**
415 * Performs prefix search, returning Title objects
416 * @ingroup Search
417 */
418 class TitlePrefixSearch extends PrefixSearch {
419
420 protected function titles( array $titles ) {
421 return $titles;
422 }
423
424 protected function strings( array $strings ) {
425 $titles = array_map( 'Title::newFromText', $strings );
426 $lb = new LinkBatch( $titles );
427 $lb->setCaller( __METHOD__ );
428 $lb->execute();
429 return $titles;
430 }
431 }
432
433 /**
434 * Performs prefix search, returning strings
435 * @ingroup Search
436 */
437 class StringPrefixSearch extends PrefixSearch {
438
439 protected function titles( array $titles ) {
440 return array_map( function ( Title $t ) {
441 return $t->getPrefixedText();
442 }, $titles );
443 }
444
445 protected function strings( array $strings ) {
446 return $strings;
447 }
448 }