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