MediaWiki  1.23.14
PrefixSearch.php
Go to the documentation of this file.
1 <?php
29 abstract class PrefixSearch {
39  public static function titleSearch( $search, $limit, $namespaces = array() ) {
40  $search = new StringPrefixSearch;
41  return $search->search( $search, $limit, $namespaces );
42  }
43 
52  public function search( $search, $limit, $namespaces = array() ) {
53  $search = trim( $search );
54  if ( $search == '' ) {
55  return array(); // Return empty result
56  }
58 
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  if ( $ns[0] == NS_MAIN ) {
64  $ns = $namespaces; // no explicit prefix, use default namespaces
65  }
66  return $this->searchBackend(
67  $ns, $title->getText(), $limit );
68  }
69 
70  // Is this a namespace prefix?
71  $title = Title::newFromText( $search . 'Dummy' );
72  if ( $title && $title->getText() == 'Dummy'
73  && $title->getNamespace() != NS_MAIN
74  && !$title->isExternal() )
75  {
76  $namespaces = array( $title->getNamespace() );
77  $search = '';
78  }
79 
80  return $this->searchBackend( $namespaces, $search, $limit );
81  }
82 
91  public function searchWithVariants( $search, $limit, array $namespaces ) {
92  wfProfileIn( __METHOD__ );
93  $searches = $this->search( $search, $limit, $namespaces );
94 
95  // if the content language has variants, try to retrieve fallback results
96  $fallbackLimit = $limit - count( $searches );
97  if ( $fallbackLimit > 0 ) {
99 
100  $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
101  $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
102 
103  foreach ( $fallbackSearches as $fbs ) {
104  $fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
105  $searches = array_merge( $searches, $fallbackSearchResult );
106  $fallbackLimit -= count( $fallbackSearchResult );
107 
108  if ( $fallbackLimit == 0 ) {
109  break;
110  }
111  }
112  }
113  wfProfileOut( __METHOD__ );
114  return $searches;
115  }
116 
124  protected abstract function titles( array $titles );
125 
134  protected abstract function strings( array $strings );
135 
143  protected function searchBackend( $namespaces, $search, $limit ) {
144  if ( count( $namespaces ) == 1 ) {
145  $ns = $namespaces[0];
146  if ( $ns == NS_MEDIA ) {
148  } elseif ( $ns == NS_SPECIAL ) {
149  return $this->titles( $this->specialSearch( $search, $limit ) );
150  }
151  }
152  $srchres = array();
153  if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
154  return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
155  }
156  return $this->strings( $srchres );
157  }
158 
166  protected function specialSearch( $search, $limit ) {
168 
169  # normalize searchKey, so aliases with spaces can be found - bug 25675
170  $search = str_replace( ' ', '_', $search );
171 
172  $searchKey = $wgContLang->caseFold( $search );
173 
174  // Unlike SpecialPage itself, we want the canonical forms of both
175  // canonical and alias title forms...
176  $keys = array();
177  foreach ( SpecialPageFactory::getList() as $page => $class ) {
178  $keys[$wgContLang->caseFold( $page )] = $page;
179  }
180 
181  foreach ( $wgContLang->getSpecialPageAliases() as $page => $aliases ) {
182  if ( !array_key_exists( $page, SpecialPageFactory::getList() ) ) {# bug 20885
183  continue;
184  }
185 
186  foreach ( $aliases as $alias ) {
187  $keys[$wgContLang->caseFold( $alias )] = $alias;
188  }
189  }
190  ksort( $keys );
191 
192  $srchres = array();
193  foreach ( $keys as $pageKey => $page ) {
194  if ( $searchKey === '' || strpos( $pageKey, $searchKey ) === 0 ) {
195  // bug 27671: Don't use SpecialPage::getTitleFor() here because it
196  // localizes its input leading to searches for e.g. Special:All
197  // returning Spezial:MediaWiki-Systemnachrichten and returning
198  // Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
199  $srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
200  }
201 
202  if ( count( $srchres ) >= $limit ) {
203  break;
204  }
205  }
206 
207  return $srchres;
208  }
209 
221  protected function defaultSearchBackend( $namespaces, $search, $limit ) {
222  $ns = array_shift( $namespaces ); // support only one namespace
223  if ( in_array( NS_MAIN, $namespaces ) ) {
224  $ns = NS_MAIN; // if searching on many always default to main
225  }
226 
227  $t = Title::newFromText( $search, $ns );
228  $prefix = $t ? $t->getDBkey() : '';
229  $dbr = wfGetDB( DB_SLAVE );
230  $res = $dbr->select( 'page',
231  array( 'page_id', 'page_namespace', 'page_title' ),
232  array(
233  'page_namespace' => $ns,
234  'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
235  ),
236  __METHOD__,
237  array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
238  );
239  $srchres = array();
240  foreach ( $res as $row ) {
241  $srchres[] = Title::newFromRow( $row );
242  }
243  return $srchres;
244  }
245 
252  protected function validateNamespaces( $namespaces ) {
254 
255  // We will look at each given namespace against wgContLang namespaces
256  $validNamespaces = $wgContLang->getNamespaces();
257  if ( is_array( $namespaces ) && count( $namespaces ) > 0 ) {
258  $valid = array();
259  foreach ( $namespaces as $ns ) {
260  if ( is_numeric( $ns ) && array_key_exists( $ns, $validNamespaces ) ) {
261  $valid[] = $ns;
262  }
263  }
264  if ( count( $valid ) > 0 ) {
265  return $valid;
266  }
267  }
268 
269  return array( NS_MAIN );
270  }
271 }
272 
278 
279  protected function titles( array $titles ) {
280  return $titles;
281  }
282 
283  protected function strings( array $strings ) {
284  $titles = array_map( 'Title::newFromText', $strings );
285  $lb = new LinkBatch( $titles );
286  $lb->setCaller( __METHOD__ );
287  $lb->execute();
288  return $titles;
289  }
290 }
291 
297 
298  protected function titles( array $titles ) {
299  return array_map( function( Title $t ) { return $t->getPrefixedText(); }, $titles );
300  }
301 
302  protected function strings( array $strings ) {
303  return $strings;
304  }
305 }
SpecialPageFactory\getList
static getList()
Get the special page list.
Definition: SpecialPageFactory.php:182
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:30
TitlePrefixSearch
Performs prefix search, returning Title objects.
Definition: PrefixSearch.php:277
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3714
StringPrefixSearch\titles
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
Definition: PrefixSearch.php:298
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
NS_FILE
const NS_FILE
Definition: Defines.php:85
$limit
if( $sleep) $limit
Definition: importImages.php:99
PrefixSearch\specialSearch
specialSearch( $search, $limit)
Prefix search special-case for Special: namespace.
Definition: PrefixSearch.php:166
$wgContLang
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 content language as $wgContLang
Definition: design.txt:56
$dbr
$dbr
Definition: testCompression.php:48
TitlePrefixSearch\strings
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
Definition: PrefixSearch.php:283
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:68
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
PrefixSearch\titleSearch
static titleSearch( $search, $limit, $namespaces=array())
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:39
$titles
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
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:345
PrefixSearch\strings
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4066
PrefixSearch\searchBackend
searchBackend( $namespaces, $search, $limit)
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:143
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
PrefixSearch\searchWithVariants
searchWithVariants( $search, $limit, array $namespaces)
Do a prefix search for all possible variants of the prefix.
Definition: PrefixSearch.php:91
PrefixSearch\defaultSearchBackend
defaultSearchBackend( $namespaces, $search, $limit)
Unless overridden by PrefixSearchBackend hook...
Definition: PrefixSearch.php:221
TitlePrefixSearch\titles
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...
Definition: PrefixSearch.php:279
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:422
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:67
PrefixSearch\validateNamespaces
validateNamespaces( $namespaces)
Validate an array of numerical namespace indexes.
Definition: PrefixSearch.php:252
StringPrefixSearch\strings
strings(array $strings)
When implemented in a descendant class, receives an array of titles as strings and returns either an ...
Definition: PrefixSearch.php:302
PrefixSearch\search
search( $search, $limit, $namespaces=array())
Do a prefix search of titles and return a list of matching page names.
Definition: PrefixSearch.php:52
DB_SLAVE
const DB_SLAVE
Definition: Defines.php:55
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$namespaces
namespace and then decline to actually register it & $namespaces
Definition: hooks.txt:815
PrefixSearch
Handles searching prefixes of titles and finding any page names that match.
Definition: PrefixSearch.php:29
as
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
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:63
$t
$t
Definition: testCompression.php:65
$res
$res
Definition: database.txt:21
StringPrefixSearch
Performs prefix search, returning strings.
Definition: PrefixSearch.php:296
PrefixSearch\titles
titles(array $titles)
When implemented in a descendant class, receives an array of Title objects and returns either an unmo...