Revert r77555 and followups r77563, r77572, r78116 (merge DisableAccount to core).
[lhc/web/wiklou.git] / includes / SpecialPage.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 * Parent special page class, also static functions for handling the special
27 * page list.
28 * @ingroup SpecialPage
29 */
30 class SpecialPage {
31 /**#@+
32 * @access private
33 */
34 /**
35 * The canonical name of this special page
36 * Also used for the default <h1> heading, @see getDescription()
37 */
38 var $mName;
39 /**
40 * The local name of this special page
41 */
42 var $mLocalName;
43 /**
44 * Minimum user level required to access this page, or "" for anyone.
45 * Also used to categorise the pages in Special:Specialpages
46 */
47 var $mRestriction;
48 /**
49 * Listed in Special:Specialpages?
50 */
51 var $mListed;
52 /**
53 * Function name called by the default execute()
54 */
55 var $mFunction;
56 /**
57 * File which needs to be included before the function above can be called
58 */
59 var $mFile;
60 /**
61 * Whether or not this special page is being included from an article
62 */
63 var $mIncluding;
64 /**
65 * Whether the special page can be included in an article
66 */
67 var $mIncludable;
68 /**
69 * Query parameters that can be passed through redirects
70 */
71 var $mAllowedRedirectParams = array();
72 /**
73 * Query parameteres added by redirects
74 */
75 var $mAddedRedirectParams = array();
76 /**
77 * Current request context
78 * @var RequestContext
79 */
80 protected $mContext;
81
82 /**
83 * List of special page names to the subclass of SpecialPage which handles them.
84 */
85 static public $mList = array(
86 # Maintenance Reports
87 'BrokenRedirects' => 'BrokenRedirectsPage',
88 'Deadendpages' => 'DeadendpagesPage',
89 'DoubleRedirects' => 'DoubleRedirectsPage',
90 'Longpages' => 'LongpagesPage',
91 'Ancientpages' => 'AncientpagesPage',
92 'Lonelypages' => 'LonelypagesPage',
93 'Fewestrevisions' => 'FewestrevisionsPage',
94 'Withoutinterwiki' => 'WithoutinterwikiPage',
95 'Protectedpages' => 'SpecialProtectedpages',
96 'Protectedtitles' => 'SpecialProtectedtitles',
97 'Shortpages' => 'ShortpagesPage',
98 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
99 'Uncategorizedimages' => 'UncategorizedimagesPage',
100 'Uncategorizedpages' => 'UncategorizedpagesPage',
101 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
102 'Unusedcategories' => 'UnusedcategoriesPage',
103 'Unusedimages' => 'UnusedimagesPage',
104 'Unusedtemplates' => 'UnusedtemplatesPage',
105 'Unwatchedpages' => 'UnwatchedpagesPage',
106 'Wantedcategories' => 'WantedcategoriesPage',
107 'Wantedfiles' => 'WantedfilesPage',
108 'Wantedpages' => 'WantedpagesPage',
109 'Wantedtemplates' => 'WantedtemplatesPage',
110
111 # List of pages
112 'Allpages' => 'SpecialAllpages',
113 'Prefixindex' => 'SpecialPrefixindex',
114 'Categories' => 'SpecialCategories',
115 'Disambiguations' => 'DisambiguationsPage',
116 'Listredirects' => 'ListredirectsPage',
117
118 # Login/create account
119 'Userlogin' => 'LoginForm',
120 'CreateAccount' => 'SpecialCreateAccount',
121
122 # Users and rights
123 'Block' => 'SpecialBlock',
124 'Unblock' => 'SpecialUnblock',
125 'BlockList' => 'SpecialBlockList',
126 'Resetpass' => 'SpecialResetpass',
127 'DeletedContributions' => 'DeletedContributionsPage',
128 'Preferences' => 'SpecialPreferences',
129 'Contributions' => 'SpecialContributions',
130 'Listgrouprights' => 'SpecialListGroupRights',
131 'Listusers' => 'SpecialListUsers' ,
132 'Listadmins' => 'SpecialListAdmins',
133 'Listbots' => 'SpecialListBots',
134 'Activeusers' => 'SpecialActiveUsers',
135 'Userrights' => 'UserrightsPage',
136 'EditWatchlist' => 'SpecialEditWatchlist',
137
138 # Recent changes and logs
139 'Newimages' => 'SpecialNewFiles',
140 'Log' => 'SpecialLog',
141 'Watchlist' => 'SpecialWatchlist',
142 'Newpages' => 'SpecialNewpages',
143 'Recentchanges' => 'SpecialRecentchanges',
144 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
145 'Tags' => 'SpecialTags',
146
147 # Media reports and uploads
148 'Listfiles' => 'SpecialListFiles',
149 'Filepath' => 'SpecialFilepath',
150 'MIMEsearch' => 'MIMEsearchPage',
151 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
152 'Upload' => 'SpecialUpload',
153 'UploadStash' => 'SpecialUploadStash',
154
155 # Wiki data and tools
156 'Statistics' => 'SpecialStatistics',
157 'Allmessages' => 'SpecialAllmessages',
158 'Version' => 'SpecialVersion',
159 'Lockdb' => 'SpecialLockdb',
160 'Unlockdb' => 'SpecialUnlockdb',
161
162 # Redirecting special pages
163 'LinkSearch' => 'LinkSearchPage',
164 'Randompage' => 'Randompage',
165 'Randomredirect' => 'SpecialRandomredirect',
166
167 # High use pages
168 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
169 'Mostimages' => 'MostimagesPage',
170 'Mostlinked' => 'MostlinkedPage',
171 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
172 'Mostcategories' => 'MostcategoriesPage',
173 'Mostrevisions' => 'MostrevisionsPage',
174
175 # Page tools
176 'ComparePages' => 'SpecialComparePages',
177 'Export' => 'SpecialExport',
178 'Import' => 'SpecialImport',
179 'Undelete' => 'SpecialUndelete',
180 'Whatlinkshere' => 'SpecialWhatlinkshere',
181 'MergeHistory' => 'SpecialMergeHistory',
182
183 # Other
184 'Booksources' => 'SpecialBookSources',
185
186 # Unlisted / redirects
187 'Blankpage' => 'SpecialBlankpage',
188 'Blockme' => 'SpecialBlockme',
189 'Emailuser' => 'SpecialEmailUser',
190 'Movepage' => 'MovePageForm',
191 'Mycontributions' => 'SpecialMycontributions',
192 'Mypage' => 'SpecialMypage',
193 'Mytalk' => 'SpecialMytalk',
194 'Myuploads' => 'SpecialMyuploads',
195 'PermanentLink' => 'SpecialPermanentLink',
196 'Revisiondelete' => 'SpecialRevisionDelete',
197 'RevisionMove' => 'SpecialRevisionMove',
198 'Specialpages' => 'SpecialSpecialpages',
199 'Userlogout' => 'SpecialUserlogout',
200 );
201
202 static public $mAliases;
203 static public $mListInitialised = false;
204
205 /**#@-*/
206
207 /**
208 * Initialise the special page list
209 * This must be called before accessing SpecialPage::$mList
210 */
211 static function initList() {
212 global $wgSpecialPages;
213 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
214
215 if ( self::$mListInitialised ) {
216 return;
217 }
218 wfProfileIn( __METHOD__ );
219
220 # Better to set this now, to avoid infinite recursion in carelessly written hooks
221 self::$mListInitialised = true;
222
223 if( !$wgDisableCounters ) {
224 self::$mList['Popularpages'] = 'PopularpagesPage';
225 }
226
227 if( !$wgDisableInternalSearch ) {
228 self::$mList['Search'] = 'SpecialSearch';
229 }
230
231 if( $wgEmailAuthentication ) {
232 self::$mList['Confirmemail'] = 'EmailConfirmation';
233 self::$mList['Invalidateemail'] = 'EmailInvalidation';
234 }
235
236 # Add extension special pages
237 self::$mList = array_merge( self::$mList, $wgSpecialPages );
238
239 # Run hooks
240 # This hook can be used to remove undesired built-in special pages
241 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
242 wfProfileOut( __METHOD__ );
243 }
244
245 static function initAliasList() {
246 if ( !is_null( self::$mAliases ) ) {
247 return;
248 }
249
250 global $wgContLang;
251 $aliases = $wgContLang->getSpecialPageAliases();
252 $missingPages = self::$mList;
253 self::$mAliases = array();
254 foreach ( $aliases as $realName => $aliasList ) {
255 foreach ( $aliasList as $alias ) {
256 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
257 }
258 unset( $missingPages[$realName] );
259 }
260 foreach ( $missingPages as $name => $stuff ) {
261 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
262 }
263 }
264
265 /**
266 * Given a special page alias, return the special page name.
267 * Returns false if there is no such alias.
268 *
269 * @param $alias String
270 * @return String or false
271 */
272 static function resolveAlias( $alias ) {
273 global $wgContLang;
274
275 if ( !self::$mListInitialised ) self::initList();
276 if ( is_null( self::$mAliases ) ) self::initAliasList();
277 $caseFoldedAlias = $wgContLang->caseFold( $alias );
278 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
279 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
280 return self::$mAliases[$caseFoldedAlias];
281 } else {
282 return false;
283 }
284 }
285
286 /**
287 * Given a special page name with a possible subpage, return an array
288 * where the first element is the special page name and the second is the
289 * subpage.
290 *
291 * @param $alias String
292 * @return Array
293 */
294 static function resolveAliasWithSubpage( $alias ) {
295 $bits = explode( '/', $alias, 2 );
296 $name = self::resolveAlias( $bits[0] );
297 if( !isset( $bits[1] ) ) { // bug 2087
298 $par = null;
299 } else {
300 $par = $bits[1];
301 }
302 return array( $name, $par );
303 }
304
305 /**
306 * Add a page to the list of valid special pages. This used to be the preferred
307 * method for adding special pages in extensions. It's now suggested that you add
308 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
309 *
310 * @param $page SpecialPage
311 * Deprecated in 1.7, warnings in 1.17, might be removed in 1.20
312 */
313 static function addPage( &$page ) {
314 wfDeprecated( __METHOD__ );
315 if ( !self::$mListInitialised ) {
316 self::initList();
317 }
318 self::$mList[$page->mName] = $page;
319 }
320
321 /**
322 * Add a page to a certain display group for Special:SpecialPages
323 *
324 * @param $page Mixed: SpecialPage or string
325 * @param $group String
326 */
327 static function setGroup( $page, $group ) {
328 global $wgSpecialPageGroups;
329 $name = is_object($page) ? $page->mName : $page;
330 $wgSpecialPageGroups[$name] = $group;
331 }
332
333 /**
334 * Add a page to a certain display group for Special:SpecialPages
335 *
336 * @param $page SpecialPage
337 */
338 static function getGroup( &$page ) {
339 global $wgSpecialPageGroups;
340 static $specialPageGroupsCache = array();
341 if( isset($specialPageGroupsCache[$page->mName]) ) {
342 return $specialPageGroupsCache[$page->mName];
343 }
344 $msg = wfMessage('specialpages-specialpagegroup-'.strtolower($page->mName));
345 if ( !$msg->isBlank() ) {
346 $group = $msg->text();
347 } else {
348 $group = isset($wgSpecialPageGroups[$page->mName])
349 ? $wgSpecialPageGroups[$page->mName]
350 : '-';
351 }
352 if( $group == '-' ) $group = 'other';
353 $specialPageGroupsCache[$page->mName] = $group;
354 return $group;
355 }
356
357 /**
358 * Remove a special page from the list
359 * Formerly used to disable expensive or dangerous special pages. The
360 * preferred method is now to add a SpecialPage_initList hook.
361 */
362 static function removePage( $name ) {
363 if ( !self::$mListInitialised ) {
364 self::initList();
365 }
366 unset( self::$mList[$name] );
367 }
368
369 /**
370 * Check if a given name exist as a special page or as a special page alias
371 *
372 * @param $name String: name of a special page
373 * @return Boolean: true if a special page exists with this name
374 */
375 static function exists( $name ) {
376 global $wgContLang;
377 if ( !self::$mListInitialised ) {
378 self::initList();
379 }
380 if( !self::$mAliases ) {
381 self::initAliasList();
382 }
383
384 # Remove special pages inline parameters:
385 $bits = explode( '/', $name );
386 $name = $wgContLang->caseFold($bits[0]);
387
388 return
389 array_key_exists( $name, self::$mList )
390 or array_key_exists( $name, self::$mAliases )
391 ;
392 }
393
394 /**
395 * Find the object with a given name and return it (or NULL)
396 *
397 * @param $name String
398 * @return SpecialPage object or null if the page doesn't exist
399 */
400 static function getPage( $name ) {
401 if ( !self::$mListInitialised ) {
402 self::initList();
403 }
404 if ( array_key_exists( $name, self::$mList ) ) {
405 $rec = self::$mList[$name];
406 if ( is_string( $rec ) ) {
407 $className = $rec;
408 self::$mList[$name] = new $className;
409 } elseif ( is_array( $rec ) ) {
410 // @deprecated officially since 1.18, unofficially since forever
411 wfDebug( "Array syntax for \$wgSpecialPages is deprecated, define a subclass of SpecialPage instead." );
412 $className = array_shift( $rec );
413 self::$mList[$name] = MWFunction::newObj( $className, $rec );
414 }
415 return self::$mList[$name];
416 } else {
417 return null;
418 }
419 }
420
421 /**
422 * Get a special page with a given localised name, or NULL if there
423 * is no such special page.
424 *
425 * @return SpecialPage object or null if the page doesn't exist
426 */
427 static function getPageByAlias( $alias ) {
428 $realName = self::resolveAlias( $alias );
429 if ( $realName ) {
430 return self::getPage( $realName );
431 } else {
432 return null;
433 }
434 }
435
436 /**
437 * Return categorised listable special pages which are available
438 * for the current user, and everyone.
439 *
440 * @return Associative array mapping page's name to its SpecialPage object
441 */
442 static function getUsablePages() {
443 global $wgUser;
444 if ( !self::$mListInitialised ) {
445 self::initList();
446 }
447 $pages = array();
448
449 foreach ( self::$mList as $name => $rec ) {
450 $page = self::getPage( $name );
451 if ( $page->isListed()
452 && (
453 !$page->isRestricted()
454 || $page->userCanExecute( $wgUser )
455 )
456 ) {
457 $pages[$name] = $page;
458 }
459 }
460 return $pages;
461 }
462
463 /**
464 * Return categorised listable special pages for all users
465 *
466 * @return Associative array mapping page's name to its SpecialPage object
467 */
468 static function getRegularPages() {
469 if ( !self::$mListInitialised ) {
470 self::initList();
471 }
472 $pages = array();
473
474 foreach ( self::$mList as $name => $rec ) {
475 $page = self::getPage( $name );
476 if ( $page->isListed() && !$page->isRestricted() ) {
477 $pages[$name] = $page;
478 }
479 }
480 return $pages;
481 }
482
483 /**
484 * Return categorised listable special pages which are available
485 * for the current user, but not for everyone
486 *
487 * @return Associative array mapping page's name to its SpecialPage object
488 */
489 static function getRestrictedPages() {
490 global $wgUser;
491 if( !self::$mListInitialised ) {
492 self::initList();
493 }
494 $pages = array();
495
496 foreach( self::$mList as $name => $rec ) {
497 $page = self::getPage( $name );
498 if(
499 $page->isListed()
500 && $page->isRestricted()
501 && $page->userCanExecute( $wgUser )
502 ) {
503 $pages[$name] = $page;
504 }
505 }
506 return $pages;
507 }
508
509 /**
510 * Execute a special page path.
511 * The path may contain parameters, e.g. Special:Name/Params
512 * Extracts the special page name and call the execute method, passing the parameters
513 *
514 * Returns a title object if the page is redirected, false if there was no such special
515 * page, and true if it was successful.
516 *
517 * @param $title Title object
518 * @param $context RequestContext
519 * @param $including Bool output is being captured for use in {{special:whatever}}
520 */
521 public static function executePath( &$title, RequestContext &$context, $including = false ) {
522 wfProfileIn( __METHOD__ );
523
524 # FIXME: redirects broken due to this call
525 $bits = explode( '/', $title->getDBkey(), 2 );
526 $name = $bits[0];
527 if( !isset( $bits[1] ) ) { // bug 2087
528 $par = null;
529 } else {
530 $par = $bits[1];
531 }
532 $page = SpecialPage::getPageByAlias( $name );
533 # Nonexistent?
534 if ( !$page ) {
535 $context->output->setArticleRelated( false );
536 $context->output->setRobotPolicy( 'noindex,nofollow' );
537 $context->output->setStatusCode( 404 );
538 $context->output->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
539 wfProfileOut( __METHOD__ );
540 return false;
541 }
542
543 # Page exists, set the context
544 $page->setContext( $context );
545
546 # Check for redirect
547 if ( !$including ) {
548 $redirect = $page->getRedirect( $par );
549 $query = $page->getRedirectQuery();
550 if ( $redirect instanceof Title ) {
551 $url = $redirect->getFullUrl( $query );
552 $context->output->redirect( $url );
553 wfProfileOut( __METHOD__ );
554 return $redirect;
555 } elseif( $redirect === true ) {
556 global $wgScript;
557 $url = $wgScript . '?' . wfArrayToCGI( $query );
558 $context->output->redirect( $url );
559 wfProfileOut( __METHOD__ );
560 return $redirect;
561 }
562 }
563
564 # Redirect to canonical alias for GET commands
565 # Not for POST, we'd lose the post data, so it's best to just distribute
566 # the request. Such POST requests are possible for old extensions that
567 # generate self-links without being aware that their default name has
568 # changed.
569 if ( !$including && $name != $page->getLocalName() && !$context->request->wasPosted() ) {
570 $query = $_GET;
571 unset( $query['title'] );
572 $query = wfArrayToCGI( $query );
573 $title = $page->getTitle( $par );
574 $url = $title->getFullUrl( $query );
575 $context->output->redirect( $url );
576 wfProfileOut( __METHOD__ );
577 return $redirect;
578 }
579
580 if ( $including && !$page->includable() ) {
581 wfProfileOut( __METHOD__ );
582 return false;
583 } elseif ( !$including ) {
584 $context->title = $page->getTitle();
585 }
586 $page->including( $including );
587
588 // Execute special page
589 $profName = 'Special:' . $page->name();
590 wfProfileIn( $profName );
591 $page->execute( $par );
592 wfProfileOut( $profName );
593 wfProfileOut( __METHOD__ );
594 return true;
595 }
596
597 /**
598 * Just like executePath() except it returns the HTML instead of outputting it
599 * Returns false if there was no such special page, or a title object if it was
600 * a redirect.
601 *
602 * @return String: HTML fragment
603 */
604 static function capturePath( &$title ) {
605 global $wgOut, $wgTitle;
606
607 $oldTitle = $wgTitle;
608 $oldOut = $wgOut;
609
610 $context = new RequestContext;
611 $context->setTitle( $title );
612 $wgOut = $context->getOutput();
613
614 $ret = SpecialPage::executePath( $title, $context, true );
615 if ( $ret === true ) {
616 $ret = $wgOut->getHTML();
617 }
618 $wgTitle = $oldTitle;
619 $wgOut = $oldOut;
620 return $ret;
621 }
622
623 /**
624 * Get the local name for a specified canonical name
625 *
626 * @param $name String
627 * @param $subpage Mixed: boolean false, or string
628 *
629 * @return String
630 */
631 static function getLocalNameFor( $name, $subpage = false ) {
632 global $wgContLang;
633 $aliases = $wgContLang->getSpecialPageAliases();
634 if ( isset( $aliases[$name][0] ) ) {
635 $name = $aliases[$name][0];
636 } else {
637 // Try harder in case someone misspelled the correct casing
638 $found = false;
639 foreach ( $aliases as $n => $values ) {
640 if ( strcasecmp( $name, $n ) === 0 ) {
641 wfWarn( "Found alias defined for $n when searching for " .
642 "special page aliases for $name. Case mismatch?" );
643 $name = $values[0];
644 $found = true;
645 break;
646 }
647 }
648 if ( !$found ) {
649 wfWarn( "Did not find alias for special page '$name'. " .
650 "Perhaps no aliases are defined for it?" );
651 }
652 }
653 if ( $subpage !== false && !is_null( $subpage ) ) {
654 $name = "$name/$subpage";
655 }
656 return $wgContLang->ucfirst( $name );
657 }
658
659 /**
660 * Get a localised Title object for a specified special page name
661 *
662 * @return Title object
663 */
664 static function getTitleFor( $name, $subpage = false ) {
665 $name = self::getLocalNameFor( $name, $subpage );
666 if ( $name ) {
667 return Title::makeTitle( NS_SPECIAL, $name );
668 } else {
669 throw new MWException( "Invalid special page name \"$name\"" );
670 }
671 }
672
673 /**
674 * Get a localised Title object for a page name with a possibly unvalidated subpage
675 *
676 * @return Title object or null if the page doesn't exist
677 */
678 static function getSafeTitleFor( $name, $subpage = false ) {
679 $name = self::getLocalNameFor( $name, $subpage );
680 if ( $name ) {
681 return Title::makeTitleSafe( NS_SPECIAL, $name );
682 } else {
683 return null;
684 }
685 }
686
687 /**
688 * Get a title for a given alias
689 *
690 * @return Title or null if there is no such alias
691 */
692 static function getTitleForAlias( $alias ) {
693 $name = self::resolveAlias( $alias );
694 if ( $name ) {
695 return self::getTitleFor( $name );
696 } else {
697 return null;
698 }
699 }
700
701 /**
702 * Default constructor for special pages
703 * Derivative classes should call this from their constructor
704 * Note that if the user does not have the required level, an error message will
705 * be displayed by the default execute() method, without the global function ever
706 * being called.
707 *
708 * If you override execute(), you can recover the default behaviour with userCanExecute()
709 * and displayRestrictionError()
710 *
711 * @param $name String: name of the special page, as seen in links and URLs
712 * @param $restriction String: user right required, e.g. "block" or "delete"
713 * @param $listed Boolean: whether the page is listed in Special:Specialpages
714 * @param $function Callback: function called by execute(). By default it is constructed from $name
715 * @param $file String: file which is included by execute(). It is also constructed from $name by default
716 * @param $includable Boolean: whether the page can be included in normal pages
717 */
718 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
719 $this->init( $name, $restriction, $listed, $function, $file, $includable );
720 }
721
722 /**
723 * Do the real work for the constructor, mainly so __call() can intercept
724 * calls to SpecialPage()
725 * @see __construct() for param docs
726 */
727 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
728 $this->mName = $name;
729 $this->mRestriction = $restriction;
730 $this->mListed = $listed;
731 $this->mIncludable = $includable;
732 if ( !$function ) {
733 $this->mFunction = 'wfSpecial'.$name;
734 } else {
735 $this->mFunction = $function;
736 }
737 if ( $file === 'default' ) {
738 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
739 } else {
740 $this->mFile = $file;
741 }
742 }
743
744 /**
745 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
746 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
747 *
748 * @param $fName String Name of called method
749 * @param $a Array Arguments to the method
750 * @deprecated since 1.17, call parent::__construct()
751 */
752 public function __call( $fName, $a ) {
753 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
754 if( strtolower( $fName ) == 'specialpage' ) {
755 // Deprecated messages now, remove in 1.19 or 1.20?
756 wfDeprecated( __METHOD__ );
757
758 $name = isset( $a[0] ) ? $a[0] : '';
759 $restriction = isset( $a[1] ) ? $a[1] : '';
760 $listed = isset( $a[2] ) ? $a[2] : true;
761 $function = isset( $a[3] ) ? $a[3] : false;
762 $file = isset( $a[4] ) ? $a[4] : 'default';
763 $includable = isset( $a[5] ) ? $a[5] : false;
764 $this->init( $name, $restriction, $listed, $function, $file, $includable );
765 } else {
766 $className = get_class( $this );
767 throw new MWException( "Call to undefined method $className::$fName" );
768 }
769 }
770
771 /**#@+
772 * Accessor
773 *
774 * @deprecated
775 */
776 function getName() { return $this->mName; }
777 function getRestriction() { return $this->mRestriction; }
778 function getFile() { return $this->mFile; }
779 function isListed() { return $this->mListed; }
780 /**#@-*/
781
782 /**#@+
783 * Accessor and mutator
784 */
785 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
786 function restrictions( $x = null) {
787 # Use the one below this
788 wfDeprecated( __METHOD__ );
789 return wfSetVar( $this->mRestriction, $x );
790 }
791 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
792 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
793 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
794 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
795 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
796 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
797 /**#@-*/
798
799 /**
800 * Get the localised name of the special page
801 */
802 function getLocalName() {
803 if ( !isset( $this->mLocalName ) ) {
804 $this->mLocalName = self::getLocalNameFor( $this->mName );
805 }
806 return $this->mLocalName;
807 }
808
809 /**
810 * Is this page expensive (for some definition of expensive)?
811 * Expensive pages are disabled or cached in miser mode. Originally used
812 * (and still overridden) by QueryPage and subclasses, moved here so that
813 * Special:SpecialPages can safely call it for all special pages.
814 *
815 * @return Boolean
816 */
817 public function isExpensive() {
818 return false;
819 }
820
821 /**
822 * Can be overridden by subclasses with more complicated permissions
823 * schemes.
824 *
825 * @return Boolean: should the page be displayed with the restricted-access
826 * pages?
827 */
828 public function isRestricted() {
829 global $wgGroupPermissions;
830 // DWIM: If all anons can do something, then it is not restricted
831 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
832 }
833
834 /**
835 * Checks if the given user (identified by an object) can execute this
836 * special page (as defined by $mRestriction). Can be overridden by sub-
837 * classes with more complicated permissions schemes.
838 *
839 * @param $user User: the user to check
840 * @return Boolean: does the user have permission to view the page?
841 */
842 public function userCanExecute( $user ) {
843 return $user->isAllowed( $this->mRestriction );
844 }
845
846 /**
847 * Output an error message telling the user what access level they have to have
848 */
849 function displayRestrictionError() {
850 $this->getOutput()->permissionRequired( $this->mRestriction );
851 }
852
853 /**
854 * Sets headers - this should be called from the execute() method of all derived classes!
855 */
856 function setHeaders() {
857 $out = $this->getOutput();
858 $out->setArticleRelated( false );
859 $out->setRobotPolicy( "noindex,nofollow" );
860 $out->setPageTitle( $this->getDescription() );
861 }
862
863 /**
864 * Default execute method
865 * Checks user permissions, calls the function given in mFunction
866 *
867 * This may be overridden by subclasses.
868 */
869 function execute( $par ) {
870 $this->setHeaders();
871
872 if ( $this->userCanExecute( $this->getUser() ) ) {
873 $func = $this->mFunction;
874 // only load file if the function does not exist
875 if(!is_callable($func) and $this->mFile) {
876 require_once( $this->mFile );
877 }
878 $this->outputHeader();
879 call_user_func( $func, $par, $this );
880 } else {
881 $this->displayRestrictionError();
882 }
883 }
884
885 /**
886 * Outputs a summary message on top of special pages
887 * Per default the message key is the canonical name of the special page
888 * May be overriden, i.e. by extensions to stick with the naming conventions
889 * for message keys: 'extensionname-xxx'
890 *
891 * @param $summaryMessageKey String: message key of the summary
892 */
893 function outputHeader( $summaryMessageKey = '' ) {
894 global $wgContLang;
895
896 if( $summaryMessageKey == '' ) {
897 $msg = $wgContLang->lc( $this->name() ) . '-summary';
898 } else {
899 $msg = $summaryMessageKey;
900 }
901 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
902 $this->getOutput()->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
903 }
904
905 }
906
907 /**
908 * Returns the name that goes in the \<h1\> in the special page itself, and
909 * also the name that will be listed in Special:Specialpages
910 *
911 * Derived classes can override this, but usually it is easier to keep the
912 * default behaviour. Messages can be added at run-time, see
913 * MessageCache.php.
914 *
915 * @return String
916 */
917 function getDescription() {
918 return wfMsg( strtolower( $this->mName ) );
919 }
920
921 /**
922 * Get a self-referential title object
923 *
924 * @return Title object
925 */
926 function getTitle( $subpage = false ) {
927 return self::getTitleFor( $this->mName, $subpage );
928 }
929
930 /**
931 * Set whether this page is listed in Special:Specialpages, at run-time
932 */
933 function setListed( $listed ) {
934 return wfSetVar( $this->mListed, $listed );
935 }
936
937 /**
938 * If the special page is a redirect, then get the Title object it redirects to.
939 * False otherwise.
940 */
941 function getRedirect( $subpage ) {
942 return false;
943 }
944
945 /**
946 * Return part of the request string for a special redirect page
947 * This allows passing, e.g. action=history to Special:Mypage, etc.
948 *
949 * @return String
950 */
951 function getRedirectQuery() {
952 $params = array();
953
954 foreach( $this->mAllowedRedirectParams as $arg ) {
955 if( $this->getContext()->request->getVal( $arg, null ) !== null ){
956 $params[$arg] = $this->getContext()->request->getVal( $arg );
957 }
958 }
959
960 foreach( $this->mAddedRedirectParams as $arg => $val ) {
961 $params[$arg] = $val;
962 }
963
964 return count( $params )
965 ? $params
966 : false;
967 }
968
969 /**
970 * Sets the context this SpecialPage is executed in
971 *
972 * @param $context RequestContext
973 * @since 1.18
974 */
975 protected function setContext( $context ) {
976 $this->mContext = $context;
977 }
978
979 /**
980 * Gets the context this SpecialPage is executed in
981 *
982 * @return RequestContext
983 * @since 1.18
984 */
985 public function getContext() {
986 if ( $this->mContext instanceof RequestContext ) {
987 return $this->mContext;
988 } else {
989 wfDebug( __METHOD__ . " called and \$mContext is null. Return RequestContext::getMain(); for sanity\n" );
990 return RequestContext::getMain();
991 }
992 }
993
994 /**
995 * Get the WebRequest being used for this instance
996 *
997 * @return WebRequest
998 * @since 1.18
999 */
1000 public function getRequest() {
1001 return $this->getContext()->getRequest();
1002 }
1003
1004 /**
1005 * Get the OutputPage being used for this instance
1006 *
1007 * @return OutputPage
1008 * @since 1.18
1009 */
1010 public function getOutput() {
1011 return $this->getContext()->getOutput();
1012 }
1013
1014 /**
1015 * Shortcut to get the skin being used for this instance
1016 *
1017 * @return User
1018 * @since 1.18
1019 */
1020 public function getUser() {
1021 return $this->getContext()->getUser();
1022 }
1023
1024 /**
1025 * Shortcut to get the skin being used for this instance
1026 *
1027 * @return Skin
1028 * @since 1.18
1029 */
1030 public function getSkin() {
1031 return $this->getContext()->getSkin();
1032 }
1033
1034 /**
1035 * Return the full title, including $par
1036 *
1037 * @return Title
1038 * @since 1.18
1039 */
1040 public function getFullTitle() {
1041 return $this->getContext()->getTitle();
1042 }
1043
1044 /**
1045 * Wrapper around wfMessage that sets the current context. Currently this
1046 * is only the title.
1047 *
1048 * @see wfMessage
1049 */
1050 public function msg( /* $args */ ) {
1051 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->getFullTitle() );
1052 }
1053 }
1054
1055 /**
1056 * Shortcut to construct a special page which is unlisted by default
1057 * @ingroup SpecialPage
1058 */
1059 class UnlistedSpecialPage extends SpecialPage
1060 {
1061 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1062 parent::__construct( $name, $restriction, false, $function, $file );
1063 }
1064 }
1065
1066 /**
1067 * Shortcut to construct an includable special page
1068 * @ingroup SpecialPage
1069 */
1070 class IncludableSpecialPage extends SpecialPage
1071 {
1072 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
1073 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1074 }
1075 }
1076
1077 /**
1078 * Shortcut to construct a special page alias.
1079 * @ingroup SpecialPage
1080 */
1081 abstract class SpecialRedirectToSpecial extends UnlistedSpecialPage {
1082 var $redirName, $redirSubpage;
1083
1084 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
1085 parent::__construct( $name );
1086 $this->redirName = $redirName;
1087 $this->redirSubpage = $redirSubpage;
1088 $this->mAllowedRedirectParams = $allowedRedirectParams;
1089 $this->mAddedRedirectParams = $addedRedirectParams;
1090 }
1091
1092 function getRedirect( $subpage ) {
1093 if ( $this->redirSubpage === false ) {
1094 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1095 } else {
1096 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1097 }
1098 }
1099 }
1100
1101 /**
1102 * ListAdmins --> ListUsers/admin
1103 */
1104 class SpecialListAdmins extends SpecialRedirectToSpecial {
1105 function __construct(){
1106 parent::__construct( 'ListAdmins', 'ListUsers', 'sysop' );
1107 }
1108 }
1109
1110 /**
1111 * ListBots --> ListUsers/admin
1112 */
1113 class SpecialListBots extends SpecialRedirectToSpecial {
1114 function __construct(){
1115 parent::__construct( 'ListAdmins', 'ListUsers', 'bot' );
1116 }
1117 }
1118
1119 /**
1120 * CreateAccount --> UserLogin/signup
1121 * FIXME: this (and the rest of the login frontend) needs to die a horrible painful death
1122 */
1123 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1124 function __construct(){
1125 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) );
1126 }
1127 }
1128 /**
1129 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1130 * are used to get user independant links pointing to the user page, talk
1131 * page and list of contributions.
1132 * This can let us cache a single copy of any generated content for all
1133 * users.
1134 */
1135
1136 /**
1137 * Shortcut to construct a special page pointing to current user user's page.
1138 * @ingroup SpecialPage
1139 */
1140 class SpecialMypage extends UnlistedSpecialPage {
1141 function __construct() {
1142 parent::__construct( 'Mypage' );
1143 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1144 'section', 'oldid', 'diff', 'dir' );
1145 }
1146
1147 function getRedirect( $subpage ) {
1148 global $wgUser;
1149 if ( strval( $subpage ) !== '' ) {
1150 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
1151 } else {
1152 return Title::makeTitle( NS_USER, $wgUser->getName() );
1153 }
1154 }
1155 }
1156
1157 /**
1158 * Shortcut to construct a special page pointing to current user talk page.
1159 * @ingroup SpecialPage
1160 */
1161 class SpecialMytalk extends UnlistedSpecialPage {
1162 function __construct() {
1163 parent::__construct( 'Mytalk' );
1164 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1165 'section', 'oldid', 'diff', 'dir' );
1166 }
1167
1168 function getRedirect( $subpage ) {
1169 global $wgUser;
1170 if ( strval( $subpage ) !== '' ) {
1171 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1172 } else {
1173 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1174 }
1175 }
1176 }
1177
1178 /**
1179 * Shortcut to construct a special page pointing to current user contributions.
1180 * @ingroup SpecialPage
1181 */
1182 class SpecialMycontributions extends UnlistedSpecialPage {
1183 function __construct() {
1184 parent::__construct( 'Mycontributions' );
1185 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1186 'offset', 'dir', 'year', 'month', 'feed' );
1187 }
1188
1189 function getRedirect( $subpage ) {
1190 global $wgUser;
1191 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1192 }
1193 }
1194
1195 /**
1196 * Redirect to Special:Listfiles?user=$wgUser
1197 */
1198 class SpecialMyuploads extends UnlistedSpecialPage {
1199 function __construct() {
1200 parent::__construct( 'Myuploads' );
1201 $this->mAllowedRedirectParams = array( 'limit' );
1202 }
1203
1204 function getRedirect( $subpage ) {
1205 global $wgUser;
1206 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1207 }
1208 }
1209
1210 /**
1211 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1212 */
1213 class SpecialPermanentLink extends UnlistedSpecialPage {
1214 function __construct() {
1215 parent::__construct( 'PermanentLink' );
1216 $this->mAllowedRedirectParams = array();
1217 }
1218
1219 function getRedirect( $subpage ) {
1220 $subpage = intval( $subpage );
1221 $this->mAddedRedirectParams['oldid'] = $subpage;
1222 return true;
1223 }
1224 }