3 * Prefix search of page names.
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.
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.
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
24 * Handles searching prefixes of titles and finding any page
25 * names that match. Used largely by the OpenSearch implementation.
29 abstract class PrefixSearch
{
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
34 * @param string $search
36 * @param array $namespaces Used if query is not explicitly prefixed
37 * @return array Array of strings
39 public static function titleSearch( $search, $limit, $namespaces = array() ) {
40 $prefixSearch = new StringPrefixSearch
;
41 return $prefixSearch->search( $search, $limit, $namespaces );
45 * Do a prefix search of titles and return a list of matching page names.
47 * @param string $search
49 * @param array $namespaces Used if query is not explicitly prefixed
50 * @return array Array of strings or Title objects
52 public function search( $search, $limit, $namespaces = array() ) {
53 $search = trim( $search );
54 if ( $search == '' ) {
55 return array(); // Return empty result
57 $namespaces = $this->validateNamespaces( $namespaces );
59 // Find a Title which is not an interwiki and is in NS_MAIN
60 $title = Title
::newFromText( $search );
61 if ( $title && !$title->isExternal() ) {
62 $ns = array( $title->getNamespace() );
63 $search = $title->getText();
64 if ( $ns[0] == NS_MAIN
) {
65 $ns = $namespaces; // no explicit prefix, use default namespaces
66 wfRunHooks( 'PrefixSearchExtractNamespace', array( &$ns, &$search ) );
68 return $this->searchBackend( $ns, $search, $limit );
71 // Is this a namespace prefix?
72 $title = Title
::newFromText( $search . 'Dummy' );
73 if ( $title && $title->getText() == 'Dummy'
74 && $title->getNamespace() != NS_MAIN
75 && !$title->isExternal() )
77 $namespaces = array( $title->getNamespace() );
80 wfRunHooks( 'PrefixSearchExtractNamespace', array( &$namespaces, &$search ) );
83 return $this->searchBackend( $namespaces, $search, $limit );
87 * Do a prefix search for all possible variants of the prefix
88 * @param string $search
90 * @param array $namespaces
94 public function searchWithVariants( $search, $limit, array $namespaces ) {
95 wfProfileIn( __METHOD__
);
96 $searches = $this->search( $search, $limit, $namespaces );
98 // if the content language has variants, try to retrieve fallback results
99 $fallbackLimit = $limit - count( $searches );
100 if ( $fallbackLimit > 0 ) {
103 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
104 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
106 foreach ( $fallbackSearches as $fbs ) {
107 $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
108 $searches = array_merge( $searches, $fallbackSearchResult );
109 $fallbackLimit -= count( $fallbackSearchResult );
111 if ( $fallbackLimit == 0 ) {
116 wfProfileOut( __METHOD__
);
121 * When implemented in a descendant class, receives an array of Title objects and returns
122 * either an unmodified array or an array of strings corresponding to titles passed to it.
124 * @param array $titles
127 abstract protected function titles( array $titles );
130 * When implemented in a descendant class, receives an array of titles as strings and returns
131 * either an unmodified array or an array of Title objects corresponding to strings received.
133 * @param array $strings
137 abstract protected function strings( array $strings );
140 * Do a prefix search of titles and return a list of matching page names.
141 * @param array $namespaces
142 * @param string $search
144 * @return array Array of strings
146 protected function searchBackend( $namespaces, $search, $limit ) {
147 if ( count( $namespaces ) == 1 ) {
148 $ns = $namespaces[0];
149 if ( $ns == NS_MEDIA
) {
150 $namespaces = array( NS_FILE
);
151 } elseif ( $ns == NS_SPECIAL
) {
152 return $this->titles( $this->specialSearch( $search, $limit ) );
156 if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
157 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
159 return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
163 * Default search backend does proper prefix searching, but custom backends
164 * may sort based on other algorythms that may cause the exact title match
165 * to not be in the results or be lower down the list.
166 * @param array $srchres results from the hook
167 * @return array munged results from the hook
169 private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) {
170 // Pick namespace (based on PrefixSearch::defaultSearchBackend)
171 $ns = in_array( NS_MAIN
, $namespaces ) ? NS_MAIN
: $namespaces[0];
172 $t = Title
::newFromText( $search, $ns );
173 if ( !$t ||
!$t->exists() ) {
174 // No exact match so just return the search results
177 $string = $t->getPrefixedText();
178 $key = array_search( $string, $srchres );
179 if ( $key !== false ) {
180 // Exact match was in the results so just move it to the front
181 return $this->pullFront( $key, $srchres );
183 // Exact match not in the search results so check for some redirect handling cases
184 if ( $t->isRedirect() ) {
185 $target = $this->getRedirectTarget( $t );
186 $key = array_search( $target, $srchres );
187 if ( $key !== false ) {
188 // Exact match is a redirect to one of the returned matches so pull the
189 // returned match to the front. This might look odd but the alternative
190 // is to put the redirect in front and drop the match. The name of the
191 // found match is often more descriptive/better formed than the name of
192 // the redirec AND by definition they share a prefix. Hopefully this
193 // choice is less confusing and more helpful. But it might now be. But
194 // it is the choice we're going with for now.
195 return $this->pullFront( $key, $srchres );
197 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
198 if ( isset( $redirectTargetsToRedirect[ $target ] ) ) {
199 // The exact match and something in the results list are both redirects
200 // to the same thing! In this case we'll pull the returned match to the
201 // top following the same logic above. Again, it might not be a perfect
202 // choice but it'll do.
203 return $this->pullFront( $redirectTargetsToRedirect[ $target ], $srchres );
206 $redirectTargetsToRedirect = $this->redirectTargetsToRedirect( $srchres );
207 if ( isset( $redirectTargetsToRedirect[ $string ] ) ) {
208 // The exact match is the target of a redirect already in the results list so remove
209 // the redirect from the results list and push the exact match to the front
210 array_splice( $srchres, $redirectTargetsToRedirect[ $string ], 1 );
211 array_unshift( $srchres, $string );
216 // Exact match is totally unique from the other results so just add it to the front
217 array_unshift( $srchres, $string );
218 // And roll one off the end if the results are too long
219 if ( count( $srchres ) > $limit ) {
220 array_pop( $srchres );
226 * @param Array(string) $titles as strings
227 * @return Array(string => int) redirect target prefixedText to index of title in titles
228 * that is a redirect to it.
230 private function redirectTargetsToRedirect( $titles ) {
232 foreach ( $titles as $key => $titleText ) {
233 $title = Title
::newFromText( $titleText );
234 if ( !$title ||
!$title->isRedirect() ) {
237 $target = $this->getRedirectTarget( $title );
241 $result[ $target ] = $key;
247 * @param int $key key to pull to the front
248 * @return array $array with the item at $key pulled to the front
250 private function pullFront( $key, $array ) {
251 $cut = array_splice( $array, $key, 1 );
252 array_unshift( $array, $cut[0] );
256 private function getRedirectTarget( $title ) {
257 $page = WikiPage
::factory( $title );
258 if ( !$page->exists() ) {
261 return $page->getRedirectTarget()->getPrefixedText();
265 * Prefix search special-case for Special: namespace.
267 * @param string $search Term
268 * @param int $limit Max number of items to return
271 protected function specialSearch( $search, $limit ) {
274 $searchParts = explode( '/', $search, 2 );
275 $searchKey = $searchParts[0];
276 $subpageSearch = isset( $searchParts[1] ) ?
$searchParts[1] : null;
278 // Handle subpage search separately.
279 if ( $subpageSearch !== null ) {
280 // Try matching the full search string as a page name
281 $specialTitle = Title
::makeTitleSafe( NS_SPECIAL
, $searchKey );
282 if ( !$specialTitle ) {
285 $special = SpecialPageFactory
::getPage( $specialTitle->getText() );
287 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit );
288 return array_map( function ( $sub ) use ( $specialTitle ) {
289 return $specialTitle->getSubpage( $sub );
296 # normalize searchKey, so aliases with spaces can be found - bug 25675
297 $searchKey = str_replace( ' ', '_', $searchKey );
298 $searchKey = $wgContLang->caseFold( $searchKey );
300 // Unlike SpecialPage itself, we want the canonical forms of both
301 // canonical and alias title forms...
303 foreach ( SpecialPageFactory
::getNames() as $page ) {
304 $keys[$wgContLang->caseFold( $page )] = $page;
307 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
308 if ( !in_array( $page, SpecialPageFactory
::getNames() ) ) {# bug 20885
312 foreach ( $aliases as $alias ) {
313 $keys[$wgContLang->caseFold( $alias )] = $alias;
319 foreach ( $keys as $pageKey => $page ) {
320 if ( $searchKey === '' ||
strpos( $pageKey, $searchKey ) === 0 ) {
321 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
322 // localizes its input leading to searches for e.g. Special:All
323 // returning Spezial:MediaWiki-Systemnachrichten and returning
324 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
325 $srchres[] = Title
::makeTitleSafe( NS_SPECIAL
, $page );
328 if ( count( $srchres ) >= $limit ) {
337 * Unless overridden by PrefixSearchBackend hook...
338 * This is case-sensitive (First character may
339 * be automatically capitalized by Title::secureAndSpit()
340 * later on depending on $wgCapitalLinks)
342 * @param array $namespaces Namespaces to search in
343 * @param string $search Term
344 * @param int $limit Max number of items to return
345 * @return array Array of Title objects
347 protected function defaultSearchBackend( $namespaces, $search, $limit ) {
348 $ns = array_shift( $namespaces ); // support only one namespace
349 if ( in_array( NS_MAIN
, $namespaces ) ) {
350 $ns = NS_MAIN
; // if searching on many always default to main
353 $t = Title
::newFromText( $search, $ns );
354 $prefix = $t ?
$t->getDBkey() : '';
355 $dbr = wfGetDB( DB_SLAVE
);
356 $res = $dbr->select( 'page',
357 array( 'page_id', 'page_namespace', 'page_title' ),
359 'page_namespace' => $ns,
360 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
363 array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
366 foreach ( $res as $row ) {
367 $srchres[] = Title
::newFromRow( $row );
373 * Validate an array of numerical namespace indexes
375 * @param array $namespaces
376 * @return array (default: contains only NS_MAIN)
378 protected function validateNamespaces( $namespaces ) {
381 // We will look at each given namespace against wgContLang namespaces
382 $validNamespaces = $wgContLang->getNamespaces();
383 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
385 foreach ( $namespaces as $ns ) {
386 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
390 if ( count( $valid ) > 0 ) {
395 return array( NS_MAIN
);
400 * Performs prefix search, returning Title objects
403 class TitlePrefixSearch
extends PrefixSearch
{
405 protected function titles( array $titles ) {
409 protected function strings( array $strings ) {
410 $titles = array_map( 'Title::newFromText', $strings );
411 $lb = new LinkBatch( $titles );
412 $lb->setCaller( __METHOD__
);
419 * Performs prefix search, returning strings
422 class StringPrefixSearch
extends PrefixSearch
{
424 protected function titles( array $titles ) {
425 return array_map( function ( Title
$t ) {
426 return $t->getPrefixedText();
430 protected function strings( array $strings ) {