* Remove manual query building in search mysql
[lhc/web/wiklou.git] / includes / SpecialPageFactory.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
4 *
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
8 *
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
15 *
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @defgroup SpecialPage SpecialPage
23 */
24
25 /**
26 * Factory for handling the special page list and generating SpecialPage objects
27 * @ingroup SpecialPage
28 */
29 class SpecialPageFactory {
30
31 /**
32 * List of special page names to the subclass of SpecialPage which handles them.
33 */
34 private static $mList = array(
35 // Maintenance Reports
36 'BrokenRedirects' => 'BrokenRedirectsPage',
37 'Deadendpages' => 'DeadendpagesPage',
38 'DoubleRedirects' => 'DoubleRedirectsPage',
39 'Longpages' => 'LongpagesPage',
40 'Ancientpages' => 'AncientpagesPage',
41 'Lonelypages' => 'LonelypagesPage',
42 'Fewestrevisions' => 'FewestrevisionsPage',
43 'Withoutinterwiki' => 'WithoutinterwikiPage',
44 'Protectedpages' => 'SpecialProtectedpages',
45 'Protectedtitles' => 'SpecialProtectedtitles',
46 'Shortpages' => 'ShortpagesPage',
47 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
48 'Uncategorizedimages' => 'UncategorizedimagesPage',
49 'Uncategorizedpages' => 'UncategorizedpagesPage',
50 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
51 'Unusedcategories' => 'UnusedcategoriesPage',
52 'Unusedimages' => 'UnusedimagesPage',
53 'Unusedtemplates' => 'UnusedtemplatesPage',
54 'Unwatchedpages' => 'UnwatchedpagesPage',
55 'Wantedcategories' => 'WantedcategoriesPage',
56 'Wantedfiles' => 'WantedfilesPage',
57 'Wantedpages' => 'WantedpagesPage',
58 'Wantedtemplates' => 'WantedtemplatesPage',
59
60 // List of pages
61 'Allpages' => 'SpecialAllpages',
62 'Prefixindex' => 'SpecialPrefixindex',
63 'Categories' => 'SpecialCategories',
64 'Disambiguations' => 'DisambiguationsPage',
65 'Listredirects' => 'ListredirectsPage',
66
67 // Login/create account
68 'Userlogin' => 'LoginForm',
69 'CreateAccount' => 'SpecialCreateAccount',
70
71 // Users and rights
72 'Block' => 'SpecialBlock',
73 'Unblock' => 'SpecialUnblock',
74 'BlockList' => 'SpecialBlockList',
75 'ChangePassword' => 'SpecialChangePassword',
76 'PasswordReset' => 'SpecialPasswordReset',
77 'DeletedContributions' => 'DeletedContributionsPage',
78 'Preferences' => 'SpecialPreferences',
79 'Contributions' => 'SpecialContributions',
80 'Listgrouprights' => 'SpecialListGroupRights',
81 'Listusers' => 'SpecialListUsers' ,
82 'Listadmins' => 'SpecialListAdmins',
83 'Listbots' => 'SpecialListBots',
84 'Activeusers' => 'SpecialActiveUsers',
85 'Userrights' => 'UserrightsPage',
86 'EditWatchlist' => 'SpecialEditWatchlist',
87
88 // Recent changes and logs
89 'Newimages' => 'SpecialNewFiles',
90 'Log' => 'SpecialLog',
91 'Watchlist' => 'SpecialWatchlist',
92 'Newpages' => 'SpecialNewpages',
93 'Recentchanges' => 'SpecialRecentchanges',
94 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
95 'Tags' => 'SpecialTags',
96
97 // Media reports and uploads
98 'Listfiles' => 'SpecialListFiles',
99 'Filepath' => 'SpecialFilepath',
100 'MIMEsearch' => 'MIMEsearchPage',
101 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
102 'Upload' => 'SpecialUpload',
103 'UploadStash' => 'SpecialUploadStash',
104
105 // Wiki data and tools
106 'Statistics' => 'SpecialStatistics',
107 'Allmessages' => 'SpecialAllmessages',
108 'Version' => 'SpecialVersion',
109 'Lockdb' => 'SpecialLockdb',
110 'Unlockdb' => 'SpecialUnlockdb',
111
112 // Redirecting special pages
113 'LinkSearch' => 'LinkSearchPage',
114 'Randompage' => 'Randompage',
115 'Randomredirect' => 'SpecialRandomredirect',
116
117 // High use pages
118 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
119 'Mostimages' => 'MostimagesPage',
120 'Mostlinked' => 'MostlinkedPage',
121 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
122 'Mostcategories' => 'MostcategoriesPage',
123 'Mostrevisions' => 'MostrevisionsPage',
124
125 // Page tools
126 'ComparePages' => 'SpecialComparePages',
127 'Export' => 'SpecialExport',
128 'Import' => 'SpecialImport',
129 'Undelete' => 'SpecialUndelete',
130 'Whatlinkshere' => 'SpecialWhatlinkshere',
131 'MergeHistory' => 'SpecialMergeHistory',
132
133 // Other
134 'Booksources' => 'SpecialBookSources',
135
136 // Unlisted / redirects
137 'Blankpage' => 'SpecialBlankpage',
138 'Blockme' => 'SpecialBlockme',
139 'Emailuser' => 'SpecialEmailUser',
140 'Movepage' => 'MovePageForm',
141 'Mycontributions' => 'SpecialMycontributions',
142 'Mypage' => 'SpecialMypage',
143 'Mytalk' => 'SpecialMytalk',
144 'Myuploads' => 'SpecialMyuploads',
145 'PermanentLink' => 'SpecialPermanentLink',
146 'Revisiondelete' => 'SpecialRevisionDelete',
147 'Specialpages' => 'SpecialSpecialpages',
148 'Userlogout' => 'SpecialUserlogout',
149 );
150
151 private static $mAliases;
152
153 /**
154 * Initialise the special page list
155 * This must be called before accessing SpecialPage::$mList
156 */
157 static function getList() {
158 global $wgSpecialPages;
159 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
160
161 if ( !is_object( self::$mList ) ) {
162 wfProfileIn( __METHOD__ );
163
164 if ( !$wgDisableCounters ) {
165 self::$mList['Popularpages'] = 'PopularpagesPage';
166 }
167
168 if ( !$wgDisableInternalSearch ) {
169 self::$mList['Search'] = 'SpecialSearch';
170 }
171
172 if ( $wgEmailAuthentication ) {
173 self::$mList['Confirmemail'] = 'EmailConfirmation';
174 self::$mList['Invalidateemail'] = 'EmailInvalidation';
175 }
176
177 // Add extension special pages
178 self::$mList = array_merge( self::$mList, $wgSpecialPages );
179
180 // Run hooks
181 // This hook can be used to remove undesired built-in special pages
182 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
183
184 // Cast to object: func()[$key] doesn't work, but func()->$key does
185 settype( self::$mList, 'object' );
186
187 wfProfileOut( __METHOD__ );
188 }
189 return self::$mList;
190 }
191
192 static function getAliasList() {
193 if ( !is_object( self::$mAliases ) ) {
194 global $wgContLang;
195 $aliases = $wgContLang->getSpecialPageAliases();
196
197 // Objects are passed by reference by default, need to create a copy
198 $missingPages = clone self::getList();
199
200 self::$mAliases = array();
201 foreach ( $aliases as $realName => $aliasList ) {
202 foreach ( $aliasList as $alias ) {
203 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
204 }
205 unset( $missingPages->$realName );
206 }
207 foreach ( $missingPages as $name => $stuff ) {
208 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
209 }
210
211 // Cast to object: func()[$key] doesn't work, but func()->$key does
212 self::$mAliases = (object)self::$mAliases;
213 }
214 return self::$mAliases;
215 }
216
217 /**
218 * Given a special page name with a possible subpage, return an array
219 * where the first element is the special page name and the second is the
220 * subpage.
221 *
222 * @param $alias String
223 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
224 */
225 public static function resolveAlias( $alias ) {
226 global $wgContLang;
227 $bits = explode( '/', $alias, 2 );
228
229 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
230 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
231 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
232 $name = self::getAliasList()->$caseFoldedAlias;
233 } else {
234 return array( null, null );
235 }
236
237 if ( !isset( $bits[1] ) ) { // bug 2087
238 $par = null;
239 } else {
240 $par = $bits[1];
241 }
242
243 return array( $name, $par );
244 }
245
246 /**
247 * Add a page to a certain display group for Special:SpecialPages
248 *
249 * @param $page Mixed: SpecialPage or string
250 * @param $group String
251 */
252 public static function setGroup( $page, $group ) {
253 global $wgSpecialPageGroups;
254 $name = is_object( $page ) ? $page->mName : $page;
255 $wgSpecialPageGroups[$name] = $group;
256 }
257
258 /**
259 * Add a page to a certain display group for Special:SpecialPages
260 *
261 * @param $page SpecialPage
262 */
263 public static function getGroup( &$page ) {
264 global $wgSpecialPageGroups;
265 static $specialPageGroupsCache = array();
266 if ( isset( $specialPageGroupsCache[$page->mName] ) ) {
267 return $specialPageGroupsCache[$page->mName];
268 }
269 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName ) );
270 if ( !$msg->isBlank() ) {
271 $group = $msg->text();
272 } else {
273 $group = isset( $wgSpecialPageGroups[$page->mName] )
274 ? $wgSpecialPageGroups[$page->mName]
275 : '-';
276 }
277 if ( $group == '-' ) {
278 $group = 'other';
279 }
280 $specialPageGroupsCache[$page->mName] = $group;
281 return $group;
282 }
283
284 /**
285 * Check if a given name exist as a special page or as a special page alias
286 *
287 * @param $name String: name of a special page
288 * @return Boolean: true if a special page exists with this name
289 */
290 public static function exists( $name ) {
291 list( $title, /*...*/ ) = self::resolveAlias( $name );
292 return property_exists( self::getList(), $title );
293 }
294
295 /**
296 * Find the object with a given name and return it (or NULL)
297 *
298 * @param $name String Special page name, may be localised and/or an alias
299 * @return SpecialPage object or null if the page doesn't exist
300 */
301 public static function getPage( $name ) {
302 list( $realName, /*...*/ ) = self::resolveAlias( $name );
303 if ( property_exists( self::getList(), $realName ) ) {
304 $rec = self::getList()->$realName;
305 if ( is_string( $rec ) ) {
306 $className = $rec;
307 self::getList()->$realName = new $className;
308 } elseif ( is_array( $rec ) ) {
309 // @deprecated, officially since 1.18, unofficially since forever
310 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
311 $className = array_shift( $rec );
312 self::getList()->$realName = MWFunction::newObj( $className, $rec );
313 }
314 return self::getList()->$realName;
315 } else {
316 return null;
317 }
318 }
319
320 /**
321 * Return categorised listable special pages which are available
322 * for the current user, and everyone.
323 *
324 * @return Array( String => Specialpage )
325 */
326 public static function getUsablePages() {
327 global $wgUser;
328 $pages = array();
329 foreach ( self::getList() as $name => $rec ) {
330 $page = self::getPage( $name );
331 if ( $page->isListed()
332 && (
333 !$page->isRestricted()
334 || $page->userCanExecute( $wgUser )
335 )
336 ) {
337 $pages[$name] = $page;
338 }
339 }
340 return $pages;
341 }
342
343 /**
344 * Return categorised listable special pages for all users
345 *
346 * @return Array( String => Specialpage )
347 */
348 public static function getRegularPages() {
349 $pages = array();
350 foreach ( self::getList() as $name => $rec ) {
351 $page = self::getPage( $name );
352 if ( $page->isListed() && !$page->isRestricted() ) {
353 $pages[$name] = $page;
354 }
355 }
356 return $pages;
357 }
358
359 /**
360 * Return categorised listable special pages which are available
361 * for the current user, but not for everyone
362 *
363 * @return Array( String => Specialpage )
364 */
365 public static function getRestrictedPages() {
366 global $wgUser;
367 $pages = array();
368 foreach ( self::getList() as $name => $rec ) {
369 $page = self::getPage( $name );
370 if (
371 $page->isListed()
372 && $page->isRestricted()
373 && $page->userCanExecute( $wgUser )
374 ) {
375 $pages[$name] = $page;
376 }
377 }
378 return $pages;
379 }
380
381 /**
382 * Execute a special page path.
383 * The path may contain parameters, e.g. Special:Name/Params
384 * Extracts the special page name and call the execute method, passing the parameters
385 *
386 * Returns a title object if the page is redirected, false if there was no such special
387 * page, and true if it was successful.
388 *
389 * @param $title Title object
390 * @param $context RequestContext
391 * @param $including Bool output is being captured for use in {{special:whatever}}
392 */
393 public static function executePath( Title &$title, RequestContext &$context, $including = false ) {
394 wfProfileIn( __METHOD__ );
395
396 // FIXME: redirects broken due to this call
397 $bits = explode( '/', $title->getDBkey(), 2 );
398 $name = $bits[0];
399 if ( !isset( $bits[1] ) ) { // bug 2087
400 $par = null;
401 } else {
402 $par = $bits[1];
403 }
404 $page = self::getPage( $name );
405 // Nonexistent?
406 if ( !$page ) {
407 $context->output->setArticleRelated( false );
408 $context->output->setRobotPolicy( 'noindex,nofollow' );
409 $context->output->setStatusCode( 404 );
410 $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
411 wfProfileOut( __METHOD__ );
412 return false;
413 }
414
415 // Page exists, set the context
416 $page->setContext( $context );
417
418 if ( !$including ) {
419 // Redirect to canonical alias for GET commands
420 // Not for POST, we'd lose the post data, so it's best to just distribute
421 // the request. Such POST requests are possible for old extensions that
422 // generate self-links without being aware that their default name has
423 // changed.
424 if ( $name != $page->getLocalName() && !$context->request->wasPosted() ) {
425 $query = $_GET;
426 unset( $query['title'] );
427 $query = wfArrayToCGI( $query );
428 $title = $page->getTitle( $par );
429 $url = $title->getFullUrl( $query );
430 $context->output->redirect( $url );
431 wfProfileOut( __METHOD__ );
432 return $title;
433 } else {
434 $context->title = $page->getTitle();
435 }
436
437 } elseif ( !$page->isIncludable() ) {
438 wfProfileOut( __METHOD__ );
439 return false;
440 }
441
442 $page->including( $including );
443
444 // Execute special page
445 $profName = 'Special:' . $page->getName();
446 wfProfileIn( $profName );
447 $page->execute( $par );
448 wfProfileOut( $profName );
449 wfProfileOut( __METHOD__ );
450 return true;
451 }
452
453 /**
454 * Just like executePath() except it returns the HTML instead of outputting it
455 * Returns false if there was no such special page, or a title object if it was
456 * a redirect.
457 *
458 * @return String: HTML fragment
459 */
460 static function capturePath( &$title ) {
461 global $wgOut, $wgTitle;
462
463 $oldTitle = $wgTitle;
464 $oldOut = $wgOut;
465
466 $context = new RequestContext;
467 $context->setTitle( $title );
468 $wgOut = $context->getOutput();
469
470 $ret = self::executePath( $title, $context, true );
471 if ( $ret === true ) {
472 $ret = $wgOut->getHTML();
473 }
474 $wgTitle = $oldTitle;
475 $wgOut = $oldOut;
476 return $ret;
477 }
478
479 /**
480 * Get the local name for a specified canonical name
481 *
482 * @param $name String
483 * @param $subpage String|Bool
484 *
485 * @return String
486 */
487 static function getLocalNameFor( $name, $subpage = false ) {
488 global $wgContLang;
489 $aliases = $wgContLang->getSpecialPageAliases();
490 if ( isset( $aliases[$name][0] ) ) {
491 $name = $aliases[$name][0];
492 } else {
493 // Try harder in case someone misspelled the correct casing
494 $found = false;
495 foreach ( $aliases as $n => $values ) {
496 if ( strcasecmp( $name, $n ) === 0 ) {
497 wfWarn( "Found alias defined for $n when searching for " .
498 "special page aliases for $name. Case mismatch?" );
499 $name = $values[0];
500 $found = true;
501 break;
502 }
503 }
504 if ( !$found ) {
505 wfWarn( "Did not find alias for special page '$name'. " .
506 "Perhaps no aliases are defined for it?" );
507 }
508 }
509 if ( $subpage !== false && !is_null( $subpage ) ) {
510 $name = "$name/$subpage";
511 }
512 return $wgContLang->ucfirst( $name );
513 }
514
515 /**
516 * Get a title for a given alias
517 *
518 * @return Title or null if there is no such alias
519 */
520 static function getTitleForAlias( $alias ) {
521 $name = self::resolveAlias( $alias );
522 if ( $name ) {
523 return self::getTitleFor( $name );
524 } else {
525 return null;
526 }
527 }
528 }