MediaWiki REL1_27
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 }
61
62 // Find a Title which is not an interwiki and is in NS_MAIN
63 $title = Title::newFromText( $search );
64 if ( $title && !$title->isExternal() ) {
65 $ns = [ $title->getNamespace() ];
66 $search = $title->getText();
67 if ( $ns[0] == NS_MAIN ) {
68 $ns = $namespaces; // no explicit prefix, use default namespaces
69 Hooks::run( 'PrefixSearchExtractNamespace', [ &$ns, &$search ] );
70 }
71 return $this->searchBackend( $ns, $search, $limit, $offset );
72 }
73
74 // Is this a namespace prefix?
75 $title = Title::newFromText( $search . 'Dummy' );
76 if ( $title && $title->getText() == 'Dummy'
77 && $title->getNamespace() != NS_MAIN
78 && !$title->isExternal() )
79 {
80 $namespaces = [ $title->getNamespace() ];
81 $search = '';
82 } else {
83 Hooks::run( 'PrefixSearchExtractNamespace', [ &$namespaces, &$search ] );
84 }
85
86 return $this->searchBackend( $namespaces, $search, $limit, $offset );
87 }
88
98 public function searchWithVariants( $search, $limit, array $namespaces, $offset = 0 ) {
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 ) {
105
106 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
107 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), [ $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 return $searches;
120 }
121
129 abstract protected function titles( array $titles );
130
139 abstract protected function strings( array $strings );
140
149 protected function searchBackend( $namespaces, $search, $limit, $offset ) {
150 if ( count( $namespaces ) == 1 ) {
151 $ns = $namespaces[0];
152 if ( $ns == NS_MEDIA ) {
153 $namespaces = [ NS_FILE ];
154 } elseif ( $ns == NS_SPECIAL ) {
155 return $this->titles( $this->specialSearch( $search, $limit, $offset ) );
156 }
157 }
158 $srchres = [];
159 if ( Hooks::run(
160 'PrefixSearchBackend',
161 [ $namespaces, $search, $limit, &$srchres, $offset ]
162 ) ) {
163 return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit, $offset ) );
164 }
165 return $this->strings( $this->handleResultFromHook( $srchres, $namespaces, $search, $limit ) );
166 }
167
168 private function handleResultFromHook( $srchres, $namespaces, $search, $limit ) {
169 $rescorer = new SearchExactMatchRescorer();
170 return $rescorer->rescore( $search, $namespaces, $srchres, $limit );
171 }
172
181 protected function specialSearch( $search, $limit, $offset ) {
183
184 $searchParts = explode( '/', $search, 2 );
185 $searchKey = $searchParts[0];
186 $subpageSearch = isset( $searchParts[1] ) ? $searchParts[1] : null;
187
188 // Handle subpage search separately.
189 if ( $subpageSearch !== null ) {
190 // Try matching the full search string as a page name
191 $specialTitle = Title::makeTitleSafe( NS_SPECIAL, $searchKey );
192 if ( !$specialTitle ) {
193 return [];
194 }
195 $special = SpecialPageFactory::getPage( $specialTitle->getText() );
196 if ( $special ) {
197 $subpages = $special->prefixSearchSubpages( $subpageSearch, $limit, $offset );
198 return array_map( function ( $sub ) use ( $specialTitle ) {
199 return $specialTitle->getSubpage( $sub );
200 }, $subpages );
201 } else {
202 return [];
203 }
204 }
205
206 # normalize searchKey, so aliases with spaces can be found - bug 25675
207 $searchKey = str_replace( ' ', '_', $searchKey );
208 $searchKey = $wgContLang->caseFold( $searchKey );
209
210 // Unlike SpecialPage itself, we want the canonical forms of both
211 // canonical and alias title forms...
212 $keys = [];
214 $keys[$wgContLang->caseFold( $page )] = $page;
215 }
216
217 foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
218 if ( !in_array( $page, SpecialPageFactory::getNames() ) ) {# bug 20885
219 continue;
220 }
221
222 foreach ( $aliases as $alias ) {
223 $keys[$wgContLang->caseFold( $alias )] = $alias;
224 }
225 }
226 ksort( $keys );
227
228 $srchres = [];
229 $skipped = 0;
230 foreach ( $keys as $pageKey => $page ) {
231 if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
232 // bug 27671: Don't use SpecialPage::getTitleFor() here because it
233 // localizes its input leading to searches for e.g. Special:All
234 // returning Spezial:MediaWiki-Systemnachrichten and returning
235 // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
236 if ( $offset > 0 && $skipped < $offset ) {
237 $skipped++;
238 continue;
239 }
240 $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
241 }
242
243 if ( count( $srchres ) >= $limit ) {
244 break;
245 }
246 }
247
248 return $srchres;
249 }
250
263 public function defaultSearchBackend( $namespaces, $search, $limit, $offset ) {
264 $ns = array_shift( $namespaces ); // support only one namespace
265 if ( is_null( $ns ) || in_array( NS_MAIN, $namespaces ) ) {
266 $ns = NS_MAIN; // if searching on many always default to main
267 }
268
269 if ( $ns == NS_SPECIAL ) {
270 return $this->specialSearch( $search, $limit, $offset );
271 }
272
273 $t = Title::newFromText( $search, $ns );
274 $prefix = $t ? $t->getDBkey() : '';
275 $dbr = wfGetDB( DB_SLAVE );
276 $res = $dbr->select( 'page',
277 [ 'page_id', 'page_namespace', 'page_title' ],
278 [
279 'page_namespace' => $ns,
280 'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
281 ],
282 __METHOD__,
283 [
284 'LIMIT' => $limit,
285 'ORDER BY' => 'page_title',
286 'OFFSET' => $offset
287 ]
288 );
289 $srchres = [];
290 foreach ( $res as $row ) {
291 $srchres[] = Title::newFromRow( $row );
292 }
293 return $srchres;
294 }
295
302 protected function validateNamespaces( $namespaces ) {
304
305 // We will look at each given namespace against wgContLang namespaces
306 $validNamespaces = $wgContLang->getNamespaces();
307 if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
308 $valid = [];
309 foreach ( $namespaces as $ns ) {
310 if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
311 $valid[] = $ns;
312 }
313 }
314 if ( count( $valid ) > 0 ) {
315 return $valid;
316 }
317 }
318
319 return [ NS_MAIN ];
320 }
321}
322
329
330 protected function titles( array $titles ) {
331 return $titles;
332 }
333
334 protected function strings( array $strings ) {
335 $titles = array_map( 'Title::newFromText', $strings );
336 $lb = new LinkBatch( $titles );
337 $lb->setCaller( __METHOD__ );
338 $lb->execute();
339 return $titles;
340 }
341}
342
349
350 protected function titles( array $titles ) {
351 return array_map( function ( Title $t ) {
352 return $t->getPrefixedText();
353 }, $titles );
354 }
355
356 protected function strings( array $strings ) {
357 return $strings;
358 }
359}
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:31
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...
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.
handleResultFromHook( $srchres, $namespaces, $search, $limit)
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
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...
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:34
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:277
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition Title.php:548
static newFromRow( $row)
Make a Title object from a DB row.
Definition Title.php:465
$res
Definition database.txt:21
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:76
const NS_MAIN
Definition Defines.php:70
const NS_SPECIAL
Definition Defines.php:59
const NS_MEDIA
Definition Defines.php:58
const DB_SLAVE
Definition Defines.php:47
the array() calling protocol came about after MediaWiki 1.4rc1.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2379
namespace and then decline to actually register it & $namespaces
Definition hooks.txt:915
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:944
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition hooks.txt:1081
this hook is for auditing only RecentChangesLinked and Watchlist $special
Definition hooks.txt:976
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