Added 'fromdbmaster' param option to WikiPage::loadPageData()
[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 'Movepage' => 'MovePageForm',
142 'Mycontributions' => 'SpecialMycontributions',
143 'Mypage' => 'SpecialMypage',
144 'Mytalk' => 'SpecialMytalk',
145 'Myuploads' => 'SpecialMyuploads',
146 'PermanentLink' => 'SpecialPermanentLink',
147 'Revisiondelete' => 'SpecialRevisionDelete',
148 'Specialpages' => 'SpecialSpecialpages',
149 'Userlogout' => 'SpecialUserlogout',
150 );
151
152 private static $mAliases;
153
154 /**
155 * Initialise the special page list
156 * This must be called before accessing SpecialPage::$mList
157 *
158 * @return array
159 */
160 static function getList() {
161 global $wgSpecialPages;
162 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
163
164 if ( !is_object( self::$mList ) ) {
165 wfProfileIn( __METHOD__ );
166
167 if ( !$wgDisableCounters ) {
168 self::$mList['Popularpages'] = 'PopularpagesPage';
169 }
170
171 if ( !$wgDisableInternalSearch ) {
172 self::$mList['Search'] = 'SpecialSearch';
173 }
174
175 if ( $wgEmailAuthentication ) {
176 self::$mList['Confirmemail'] = 'EmailConfirmation';
177 self::$mList['Invalidateemail'] = 'EmailInvalidation';
178 }
179
180 // Add extension special pages
181 self::$mList = array_merge( self::$mList, $wgSpecialPages );
182
183 // Run hooks
184 // This hook can be used to remove undesired built-in special pages
185 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
186
187 // Cast to object: func()[$key] doesn't work, but func()->$key does
188 settype( self::$mList, 'object' );
189
190 wfProfileOut( __METHOD__ );
191 }
192 return self::$mList;
193 }
194
195 /**
196 * Initialise and return the list of special page aliases. Returns an object with
197 * properties which can be accessed $obj->pagename - each property is an array of
198 * aliases; the first in the array is the cannonical alias. All registered special
199 * pages are guaranteed to have a property entry, and for that property array to
200 * contain at least one entry (English fallbacks will be added if necessary).
201 * @return Object
202 */
203 static function getAliasList() {
204 if ( !is_object( self::$mAliases ) ) {
205 global $wgContLang;
206 $aliases = $wgContLang->getSpecialPageAliases();
207
208 // Objects are passed by reference by default, need to create a copy
209 $missingPages = clone self::getList();
210
211 self::$mAliases = array();
212 foreach ( $aliases as $realName => $aliasList ) {
213 foreach ( $aliasList as $alias ) {
214 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
215 }
216 unset( $missingPages->$realName );
217 }
218 foreach ( $missingPages as $name => $stuff ) {
219 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
220 }
221
222 // Cast to object: func()[$key] doesn't work, but func()->$key does
223 self::$mAliases = (object)self::$mAliases;
224 }
225 return self::$mAliases;
226 }
227
228 /**
229 * Given a special page name with a possible subpage, return an array
230 * where the first element is the special page name and the second is the
231 * subpage.
232 *
233 * @param $alias String
234 * @return Array( String, String|null ), or array( null, null ) if the page is invalid
235 */
236 public static function resolveAlias( $alias ) {
237 global $wgContLang;
238 $bits = explode( '/', $alias, 2 );
239
240 $caseFoldedAlias = $wgContLang->caseFold( $bits[0] );
241 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
242 if ( isset( self::getAliasList()->$caseFoldedAlias ) ) {
243 $name = self::getAliasList()->$caseFoldedAlias;
244 } else {
245 return array( null, null );
246 }
247
248 if ( !isset( $bits[1] ) ) { // bug 2087
249 $par = null;
250 } else {
251 $par = $bits[1];
252 }
253
254 return array( $name, $par );
255 }
256
257 /**
258 * Add a page to a certain display group for Special:SpecialPages
259 *
260 * @param $page Mixed: SpecialPage or string
261 * @param $group String
262 */
263 public static function setGroup( $page, $group ) {
264 global $wgSpecialPageGroups;
265 $name = is_object( $page ) ? $page->mName : $page;
266 $wgSpecialPageGroups[$name] = $group;
267 }
268
269 /**
270 * Get the group that the special page belongs in on Special:SpecialPage
271 *
272 * @param $page SpecialPage
273 */
274 public static function getGroup( &$page ) {
275 global $wgSpecialPageGroups;
276 static $specialPageGroupsCache = array();
277 if ( isset( $specialPageGroupsCache[$page->mName] ) ) {
278 return $specialPageGroupsCache[$page->mName];
279 }
280 $msg = wfMessage( 'specialpages-specialpagegroup-' . strtolower( $page->mName ) );
281 if ( !$msg->isBlank() ) {
282 $group = $msg->text();
283 } else {
284 $group = isset( $wgSpecialPageGroups[$page->mName] )
285 ? $wgSpecialPageGroups[$page->mName]
286 : '-';
287 }
288 if ( $group == '-' ) {
289 $group = 'other';
290 }
291 $specialPageGroupsCache[$page->mName] = $group;
292 return $group;
293 }
294
295 /**
296 * Check if a given name exist as a special page or as a special page alias
297 *
298 * @param $name String: name of a special page
299 * @return Boolean: true if a special page exists with this name
300 */
301 public static function exists( $name ) {
302 list( $title, /*...*/ ) = self::resolveAlias( $name );
303 return property_exists( self::getList(), $title );
304 }
305
306 /**
307 * Find the object with a given name and return it (or NULL)
308 *
309 * @param $name String Special page name, may be localised and/or an alias
310 * @return SpecialPage object or null if the page doesn't exist
311 */
312 public static function getPage( $name ) {
313 list( $realName, /*...*/ ) = self::resolveAlias( $name );
314 if ( property_exists( self::getList(), $realName ) ) {
315 $rec = self::getList()->$realName;
316 if ( is_string( $rec ) ) {
317 $className = $rec;
318 return new $className;
319 } elseif ( is_array( $rec ) ) {
320 // @deprecated, officially since 1.18, unofficially since forever
321 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
322 $className = array_shift( $rec );
323 self::getList()->$realName = MWFunction::newObj( $className, $rec );
324 }
325 return self::getList()->$realName;
326 } else {
327 return null;
328 }
329 }
330
331 /**
332 * Return categorised listable special pages which are available
333 * for the current user, and everyone.
334 *
335 * @return Array( String => Specialpage )
336 */
337 public static function getUsablePages() {
338 global $wgUser;
339 $pages = array();
340 foreach ( self::getList() as $name => $rec ) {
341 $page = self::getPage( $name );
342 if ( $page->isListed()
343 && (
344 !$page->isRestricted()
345 || $page->userCanExecute( $wgUser )
346 )
347 ) {
348 $pages[$name] = $page;
349 }
350 }
351 return $pages;
352 }
353
354 /**
355 * Return categorised listable special pages for all users
356 *
357 * @return Array( String => Specialpage )
358 */
359 public static function getRegularPages() {
360 $pages = array();
361 foreach ( self::getList() as $name => $rec ) {
362 $page = self::getPage( $name );
363 if ( $page->isListed() && !$page->isRestricted() ) {
364 $pages[$name] = $page;
365 }
366 }
367 return $pages;
368 }
369
370 /**
371 * Return categorised listable special pages which are available
372 * for the current user, but not for everyone
373 *
374 * @return Array( String => Specialpage )
375 */
376 public static function getRestrictedPages() {
377 global $wgUser;
378 $pages = array();
379 foreach ( self::getList() as $name => $rec ) {
380 $page = self::getPage( $name );
381 if (
382 $page->isListed()
383 && $page->isRestricted()
384 && $page->userCanExecute( $wgUser )
385 ) {
386 $pages[$name] = $page;
387 }
388 }
389 return $pages;
390 }
391
392 /**
393 * Execute a special page path.
394 * The path may contain parameters, e.g. Special:Name/Params
395 * Extracts the special page name and call the execute method, passing the parameters
396 *
397 * Returns a title object if the page is redirected, false if there was no such special
398 * page, and true if it was successful.
399 *
400 * @param $title Title object
401 * @param $context RequestContext
402 * @param $including Bool output is being captured for use in {{special:whatever}}
403 *
404 * @return bool
405 */
406 public static function executePath( Title &$title, RequestContext &$context, $including = false ) {
407 wfProfileIn( __METHOD__ );
408
409 // @todo FIXME: Redirects broken due to this call
410 $bits = explode( '/', $title->getDBkey(), 2 );
411 $name = $bits[0];
412 if ( !isset( $bits[1] ) ) { // bug 2087
413 $par = null;
414 } else {
415 $par = $bits[1];
416 }
417 $page = self::getPage( $name );
418 // Nonexistent?
419 if ( !$page ) {
420 $context->getOutput()->setArticleRelated( false );
421 $context->getOutput()->setRobotPolicy( 'noindex,nofollow' );
422 $context->getOutput()->setStatusCode( 404 );
423 $context->getOutput()->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
424 wfProfileOut( __METHOD__ );
425 return false;
426 }
427
428 // Page exists, set the context
429 $page->setContext( $context );
430
431 if ( !$including ) {
432 // Redirect to canonical alias for GET commands
433 // Not for POST, we'd lose the post data, so it's best to just distribute
434 // the request. Such POST requests are possible for old extensions that
435 // generate self-links without being aware that their default name has
436 // changed.
437 if ( $name != $page->getLocalName() && !$context->getRequest()->wasPosted() ) {
438 $query = $context->getRequest()->getQueryValues();
439 unset( $query['title'] );
440 $query = wfArrayToCGI( $query );
441 $title = $page->getTitle( $par );
442 $url = $title->getFullUrl( $query );
443 $context->getOutput()->redirect( $url );
444 wfProfileOut( __METHOD__ );
445 return $title;
446 } else {
447 $context->setTitle( $page->getTitle() );
448 }
449
450 } elseif ( !$page->isIncludable() ) {
451 wfProfileOut( __METHOD__ );
452 return false;
453 }
454
455 $page->including( $including );
456
457 // Execute special page
458 $profName = 'Special:' . $page->getName();
459 wfProfileIn( $profName );
460 $page->execute( $par );
461 wfProfileOut( $profName );
462 wfProfileOut( __METHOD__ );
463 return true;
464 }
465
466 /**
467 * Just like executePath() except it returns the HTML instead of outputting it
468 * Returns false if there was no such special page, or a title object if it was
469 * a redirect.
470 *
471 * Also saves the current $wgTitle, $wgOut, and $wgRequest variables so that
472 * the special page will get the context it'd expect on a normal request,
473 * and then restores them to their previous values after.
474 *
475 * @param $title Title
476 *
477 * @return String: HTML fragment
478 */
479 static function capturePath( &$title ) {
480 global $wgOut, $wgTitle, $wgRequest;
481
482 $oldTitle = $wgTitle;
483 $oldOut = $wgOut;
484 $oldRequest = $wgRequest;
485
486 // Don't want special pages interpreting ?feed=atom parameters.
487 $wgRequest = new FauxRequest( array() );
488
489 $context = new RequestContext;
490 $context->setTitle( $title );
491 $context->setRequest( $wgRequest );
492 $wgOut = $context->getOutput();
493
494 $ret = self::executePath( $title, $context, true );
495 if ( $ret === true ) {
496 $ret = $wgOut->getHTML();
497 }
498 $wgTitle = $oldTitle;
499 $wgOut = $oldOut;
500 $wgRequest = $oldRequest;
501 return $ret;
502 }
503
504 /**
505 * Get the local name for a specified canonical name
506 *
507 * @param $name String
508 * @param $subpage String|Bool
509 *
510 * @return String
511 */
512 static function getLocalNameFor( $name, $subpage = false ) {
513 global $wgContLang;
514 $aliases = $wgContLang->getSpecialPageAliases();
515
516 if ( isset( $aliases[$name][0] ) ) {
517 $name = $aliases[$name][0];
518 } else {
519 // Try harder in case someone misspelled the correct casing
520 $found = false;
521 foreach ( $aliases as $n => $values ) {
522 if ( strcasecmp( $name, $n ) === 0 ) {
523 wfWarn( "Found alias defined for $n when searching for " .
524 "special page aliases for $name. Case mismatch?" );
525 $name = $values[0];
526 $found = true;
527 break;
528 }
529 }
530 if ( !$found ) {
531 wfWarn( "Did not find alias for special page '$name'. " .
532 "Perhaps no aliases are defined for it?" );
533 }
534 }
535 if ( $subpage !== false && !is_null( $subpage ) ) {
536 $name = "$name/$subpage";
537 }
538 return $wgContLang->ucfirst( $name );
539 }
540
541 /**
542 * Get a title for a given alias
543 *
544 * @param $alias String
545 *
546 * @return Title or null if there is no such alias
547 */
548 static function getTitleForAlias( $alias ) {
549 $name = self::resolveAlias( $alias );
550 if ( $name ) {
551 return SpecialPage::getTitleFor( $name );
552 } else {
553 return null;
554 }
555 }
556 }