MediaWiki REL1_31
PrefixSearch.php
Go to the documentation of this file.
1<?php
30abstract class PrefixSearch {
41 public static function titleSearch( $search, $limit, $namespaces = [], $offset = 0 ) {
42 $prefixSearch = new StringPrefixSearch;
43 return $prefixSearch->search( $search, $limit, $namespaces, $offset );
44 }
45
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 {
67 Hooks::run( 'PrefixSearchExtractNamespace', [ &$namespaces, &$search ] );
68 }
69
70 return $this->searchBackend( $namespaces, $search, $limit, $offset );
71 }
72
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
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 ) {
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
149 abstract protected function titles( array $titles );
150
159 abstract protected function strings( array $strings );
160
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(
186 $this->handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) );
187 }
188
189 private function handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset ) {
190 if ( $offset === 0 ) {
191 // Only perform exact db match if offset === 0
192 // This is still far from perfect but at least we avoid returning the
193 // same title afain and again when the user is scrolling with a query
194 // that matches a title in the db.
195 $rescorer = new SearchExactMatchRescorer();
196 $srchres = $rescorer->rescore( $search, $namespaces, $srchres, $limit );
197 }
198 return $srchres;
199 }
200
209 protected function specialSearch( $search, $limit, $offset ) {
211
212 $searchParts = explode( '/', $search, 2 );
213 $searchKey = $searchParts[0];
214 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
215
216 // Handle subpage search separately.
217 if ( $subpageSearch !== null ) {
218 // Try matching the full search string as a page name
219 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
220 if ( !$specialTitle ) {
221 return [];
222 }
223 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
224 if ( $special ) {
225 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
226 return array_map( function ( $sub ) use ( $specialTitle ) {
227 return $specialTitle->getSubpage( $sub );
228 }, $subpages );
229 } else {
230 return [];
231 }
232 }
233
234 # normalize searchKey, so aliases with spaces can be found - T27675
235 $searchKey = str_replace( ' ', '_', $searchKey );
236 $searchKey = $wgContLang->caseFold( $searchKey );
237
238 // Unlike SpecialPage itself, we want the canonical forms of both
239 // canonical and alias title forms...
240 $keys = [];
241 foreach ( SpecialPageFactory::getNames() as $page ) {
242 $keys[$wgContLang->caseFold( $page )] = [ 'page' => $page, 'rank' => 0 ];
243 }
244
245 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
246 if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# T22885
247 continue;
248 }
249
250 foreach ( $aliases as $key => $alias ) {
251 $keys[$wgContLang->caseFold( $alias )] = [ 'page' => $alias, 'rank' => $key ];
252 }
253 }
254 ksort( $keys );
255
256 $matches = [];
257 foreach ( $keys as $pageKey => $page ) {
258 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
259 // T29671: Don't use SpecialPage::getTitleFor() here because it
260 // localizes its input leading to searches for e.g. Special:All
261 // returning Spezial:MediaWiki-Systemnachrichten and returning
262 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
263 $matches[$page['rank']][] = Title::makeTitleSafe( NS_SPECIAL, $page['page'] );
264
265 if ( isset( $matches[0] ) && count( $matches[0] ) >= $limit + $offset ) {
266 // We have enough items in primary rank, no use to continue
267 break;
268 }
269 }
270
271 }
272
273 // Ensure keys are in order
274 ksort( $matches );
275 // Flatten the array
276 $matches = array_reduce( $matches, 'array_merge', [] );
277
278 return array_slice( $matches, $offset, $limit );
279 }
280
293 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
294 // Backwards compatability with old code. Default to NS_MAIN if no namespaces provided.
295 if ( $namespaces === null ) {
296 $namespaces = [];
297 }
298 if ( !$namespaces ) {
300 }
301
302 // Construct suitable prefix for each namespace. They differ in cases where
303 // some namespaces always capitalize and some don't.
304 $prefixes = [];
305 foreach ( $namespaces as $namespace ) {
306 // For now, if special is included, ignore the other namespaces
307 if ( $namespace == NS_SPECIAL ) {
308 return $this->specialSearch( $search, $limit, $offset );
309 }
310
311 $title = Title::makeTitleSafe( $namespace, $search );
312 // Why does the prefix default to empty?
313 $prefix = $title ? $title->getDBkey() : '';
314 $prefixes[$prefix][] = $namespace;
315 }
316
318 // Often there is only one prefix that applies to all requested namespaces,
319 // but sometimes there are two if some namespaces do not always capitalize.
320 $conds = [];
321 foreach ( $prefixes as $prefix => $namespaces ) {
322 $condition = [
323 'page_namespace' => $namespaces,
324 'page_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
325 ];
326 $conds[] = $dbr->makeList( $condition, LIST_AND );
327 }
328
329 $table = 'page';
330 $fields = [ 'page_id', 'page_namespace', 'page_title' ];
331 $conds = $dbr->makeList( $conds, LIST_OR );
332 $options = [
333 'LIMIT' => $limit,
334 'ORDER BY' => [ 'page_title', 'page_namespace' ],
335 'OFFSET' => $offset
336 ];
337
338 $res = $dbr->select( $table, $fields, $conds, __METHOD__, $options );
339
340 return iterator_to_array( TitleArray::newFromResult( $res ) );
341 }
342
349 protected function validateNamespaces( $namespaces ) {
351
352 // We will look at each given namespace against wgContLang namespaces
353 $validNamespaces = $wgContLang->getNamespaces();
354 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
355 $valid = [];
356 foreach ( $namespaces as $ns ) {
357 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
358 $valid[] = $ns;
359 }
360 }
361 if ( count( $valid ) > 0 ) {
362 return $valid;
363 }
364 }
365
366 return [ NS_MAIN ];
367 }
368}
369
376
377 protected function titles( array $titles ) {
378 return $titles;
379 }
380
381 protected function strings( array $strings ) {
382 $titles = array_map( 'Title::newFromText', $strings );
383 $lb = new LinkBatch( $titles );
384 $lb->setCaller( __METHOD__ );
385 $lb->execute();
386 return $titles;
387 }
388}
389
396
397 protected function titles( array $titles ) {
398 return array_map( function ( Title $t ) {
399 return $t->getPrefixedText();
400 }, $titles );
401 }
402
403 protected function strings( array $strings ) {
404 return $strings;
405 }
406}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
Handles searching prefixes of titles and finding any page names that match.
searchWithVariants( $search, $limit, array $namespaces, $offset=0)
Do a prefix search for all possible variants of the prefix.
search( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
specialSearch( $search, $limit, $offset)
Prefix search special-case for Special: namespace.
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
extractNamespace( $input)
Figure out if given input contains an explicit namespace.
defaultSearchBackend( $namespaces, $search, $limit, $offset)
Unless overridden by PrefixSearchBackend hook... This is case-sensitive (First character may be autom...
static titleSearch( $search, $limit, $namespaces=[], $offset=0)
Do a prefix search of titles and return a list of matching page names.
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
handleResultFromHook( $srchres, $namespaces, $search, $limit, $offset)
searchBackend( $namespaces, $search, $limit, $offset)
Do a prefix search of titles and return a list of matching page names.
An utility class to rescore search results by looking for an exact match in the db and add the page f...
static getPage( $name)
Find the object with a given name and return it (or NULL)
static getNames()
Returns a list of canonical special page names.
Performs prefix search, returning strings.
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
static newFromResult( $res)
Performs prefix search, returning Title objects.
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
Represents a title within MediaWiki.
Definition Title.php:39
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
it sets a lot of them automatically from query strings
Definition design.txt:93
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const NS_FILE
Definition Defines.php:80
const NS_MAIN
Definition Defines.php:74
const NS_SPECIAL
Definition Defines.php:63
const LIST_OR
Definition Defines.php:56
const NS_MEDIA
Definition Defines.php:62
const LIST_AND
Definition Defines.php:53
the array() calling protocol came about after MediaWiki 1.4rc1.
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:934
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
this hook is for auditing only RecentChangesLinked and Watchlist $special
Definition hooks.txt:998
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
Definition linkcache.txt:17
if(is_array($mode)) switch( $mode) $input
const DB_REPLICA
Definition defines.php:25