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