* Follow-up r84610: don't assume a Parser object is attached
[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
78 * @var WebRequest
79 */
80 protected $mRequest;
81 /**
82 * Current output page
83 * @var OutputPage
84 */
85 protected $mOutput;
86 /**
87 * Full title including $par
88 * @var Title
89 */
90 protected $mFullTitle;
91
92 /**
93 * List of special pages, followed by parameters.
94 * If the only parameter is a string, that is the page name.
95 * Otherwise, it is an array. The format is one of:
96 ** array( 'SpecialPage', name, right )
97 ** array( 'IncludableSpecialPage', name, right, listed? )
98 ** array( 'UnlistedSpecialPage', name, right )
99 ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
100 */
101 static public $mList = array(
102 # Maintenance Reports
103 'BrokenRedirects' => 'BrokenRedirectsPage',
104 'Deadendpages' => 'DeadendpagesPage',
105 'DoubleRedirects' => 'DoubleRedirectsPage',
106 'Longpages' => 'LongpagesPage',
107 'Ancientpages' => 'AncientpagesPage',
108 'Lonelypages' => 'LonelypagesPage',
109 'Fewestrevisions' => 'FewestrevisionsPage',
110 'Withoutinterwiki' => 'WithoutinterwikiPage',
111 'Protectedpages' => 'SpecialProtectedpages',
112 'Protectedtitles' => 'SpecialProtectedtitles',
113 'Shortpages' => 'ShortpagesPage',
114 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
115 'Uncategorizedimages' => 'UncategorizedimagesPage',
116 'Uncategorizedpages' => 'UncategorizedpagesPage',
117 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
118 'Unusedcategories' => 'UnusedcategoriesPage',
119 'Unusedimages' => 'UnusedimagesPage',
120 'Unusedtemplates' => 'UnusedtemplatesPage',
121 'Unwatchedpages' => 'UnwatchedpagesPage',
122 'Wantedcategories' => 'WantedcategoriesPage',
123 'Wantedfiles' => 'WantedfilesPage',
124 'Wantedpages' => 'WantedpagesPage',
125 'Wantedtemplates' => 'WantedtemplatesPage',
126
127 # List of pages
128 'Allpages' => 'SpecialAllpages',
129 'Prefixindex' => 'SpecialPrefixindex',
130 'Categories' => 'SpecialCategories',
131 'Disambiguations' => 'DisambiguationsPage',
132 'Listredirects' => 'ListredirectsPage',
133
134 # Login/create account
135 'Userlogin' => 'LoginForm',
136 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
137
138 # Users and rights
139 'Block' => 'SpecialBlock',
140 'Unblock' => 'SpecialUnblock',
141 'BlockList' => 'SpecialBlockList',
142 'Resetpass' => 'SpecialResetpass',
143 'DeletedContributions' => 'DeletedContributionsPage',
144 'Preferences' => 'SpecialPreferences',
145 'Contributions' => 'SpecialContributions',
146 'Listgrouprights' => 'SpecialListGroupRights',
147 'Listusers' => array( 'SpecialPage', 'Listusers' ),
148 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
149 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
150 'Activeusers' => 'SpecialActiveUsers',
151 'Userrights' => 'UserrightsPage',
152 'DisableAccount' => 'SpecialDisableAccount',
153
154 # Recent changes and logs
155 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
156 'Log' => 'SpecialLog',
157 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
158 'Newpages' => 'SpecialNewpages',
159 'Recentchanges' => 'SpecialRecentchanges',
160 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
161 'Tags' => 'SpecialTags',
162
163 # Media reports and uploads
164 'Listfiles' => array( 'SpecialPage', 'Listfiles' ),
165 'Filepath' => 'SpecialFilepath',
166 'MIMEsearch' => 'MIMEsearchPage',
167 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
168 'Upload' => 'SpecialUpload',
169 'UploadStash' => 'SpecialUploadStash',
170
171 # Wiki data and tools
172 'Statistics' => 'SpecialStatistics',
173 'Allmessages' => 'SpecialAllmessages',
174 'Version' => 'SpecialVersion',
175 'Lockdb' => 'SpecialLockdb',
176 'Unlockdb' => 'SpecialUnlockdb',
177
178 # Redirecting special pages
179 'LinkSearch' => 'LinkSearchPage',
180 'Randompage' => 'Randompage',
181 'Randomredirect' => 'SpecialRandomredirect',
182
183 # High use pages
184 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
185 'Mostimages' => 'MostimagesPage',
186 'Mostlinked' => 'MostlinkedPage',
187 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
188 'Mostcategories' => 'MostcategoriesPage',
189 'Mostrevisions' => 'MostrevisionsPage',
190
191 # Page tools
192 'ComparePages' => 'SpecialComparePages',
193 'Export' => 'SpecialExport',
194 'Import' => 'SpecialImport',
195 'Undelete' => 'SpecialUndelete',
196 'Whatlinkshere' => 'SpecialWhatlinkshere',
197 'MergeHistory' => 'SpecialMergeHistory',
198
199 # Other
200 'Booksources' => 'SpecialBookSources',
201
202 # Unlisted / redirects
203 'Blankpage' => 'SpecialBlankpage',
204 'Blockme' => 'SpecialBlockme',
205 'Emailuser' => 'SpecialEmailUser',
206 'Movepage' => 'MovePageForm',
207 'Mycontributions' => 'SpecialMycontributions',
208 'Mypage' => 'SpecialMypage',
209 'Mytalk' => 'SpecialMytalk',
210 'Myuploads' => 'SpecialMyuploads',
211 'PermanentLink' => 'SpecialPermanentLink',
212 'Revisiondelete' => 'SpecialRevisionDelete',
213 'RevisionMove' => 'SpecialRevisionMove',
214 'Specialpages' => 'SpecialSpecialpages',
215 'Userlogout' => 'SpecialUserlogout',
216 );
217
218 static public $mAliases;
219 static public $mListInitialised = false;
220
221 /**#@-*/
222
223 /**
224 * Initialise the special page list
225 * This must be called before accessing SpecialPage::$mList
226 */
227 static function initList() {
228 global $wgSpecialPages;
229 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
230
231 if ( self::$mListInitialised ) {
232 return;
233 }
234 wfProfileIn( __METHOD__ );
235
236 # Better to set this now, to avoid infinite recursion in carelessly written hooks
237 self::$mListInitialised = true;
238
239 if( !$wgDisableCounters ) {
240 self::$mList['Popularpages'] = 'PopularpagesPage';
241 }
242
243 if( !$wgDisableInternalSearch ) {
244 self::$mList['Search'] = 'SpecialSearch';
245 }
246
247 if( $wgEmailAuthentication ) {
248 self::$mList['Confirmemail'] = 'EmailConfirmation';
249 self::$mList['Invalidateemail'] = 'EmailInvalidation';
250 }
251
252 # Add extension special pages
253 self::$mList = array_merge( self::$mList, $wgSpecialPages );
254
255 # Run hooks
256 # This hook can be used to remove undesired built-in special pages
257 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
258 wfProfileOut( __METHOD__ );
259 }
260
261 static function initAliasList() {
262 if ( !is_null( self::$mAliases ) ) {
263 return;
264 }
265
266 global $wgContLang;
267 $aliases = $wgContLang->getSpecialPageAliases();
268 $missingPages = self::$mList;
269 self::$mAliases = array();
270 foreach ( $aliases as $realName => $aliasList ) {
271 foreach ( $aliasList as $alias ) {
272 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
273 }
274 unset( $missingPages[$realName] );
275 }
276 foreach ( $missingPages as $name => $stuff ) {
277 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
278 }
279 }
280
281 /**
282 * Given a special page alias, return the special page name.
283 * Returns false if there is no such alias.
284 *
285 * @param $alias String
286 * @return String or false
287 */
288 static function resolveAlias( $alias ) {
289 global $wgContLang;
290
291 if ( !self::$mListInitialised ) self::initList();
292 if ( is_null( self::$mAliases ) ) self::initAliasList();
293 $caseFoldedAlias = $wgContLang->caseFold( $alias );
294 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
295 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
296 return self::$mAliases[$caseFoldedAlias];
297 } else {
298 return false;
299 }
300 }
301
302 /**
303 * Given a special page name with a possible subpage, return an array
304 * where the first element is the special page name and the second is the
305 * subpage.
306 *
307 * @param $alias String
308 * @return Array
309 */
310 static function resolveAliasWithSubpage( $alias ) {
311 $bits = explode( '/', $alias, 2 );
312 $name = self::resolveAlias( $bits[0] );
313 if( !isset( $bits[1] ) ) { // bug 2087
314 $par = null;
315 } else {
316 $par = $bits[1];
317 }
318 return array( $name, $par );
319 }
320
321 /**
322 * Add a page to the list of valid special pages. This used to be the preferred
323 * method for adding special pages in extensions. It's now suggested that you add
324 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
325 *
326 * @param $page SpecialPage
327 * Deprecated in 1.7, warnings in 1.17, might be removed in 1.20
328 */
329 static function addPage( &$page ) {
330 wfDeprecated( __METHOD__ );
331 if ( !self::$mListInitialised ) {
332 self::initList();
333 }
334 self::$mList[$page->mName] = $page;
335 }
336
337 /**
338 * Add a page to a certain display group for Special:SpecialPages
339 *
340 * @param $page Mixed: SpecialPage or string
341 * @param $group String
342 */
343 static function setGroup( $page, $group ) {
344 global $wgSpecialPageGroups;
345 $name = is_object($page) ? $page->mName : $page;
346 $wgSpecialPageGroups[$name] = $group;
347 }
348
349 /**
350 * Add a page to a certain display group for Special:SpecialPages
351 *
352 * @param $page SpecialPage
353 */
354 static function getGroup( &$page ) {
355 global $wgSpecialPageGroups;
356 static $specialPageGroupsCache = array();
357 if( isset($specialPageGroupsCache[$page->mName]) ) {
358 return $specialPageGroupsCache[$page->mName];
359 }
360 $msg = wfMessage('specialpages-specialpagegroup-'.strtolower($page->mName));
361 if ( !$msg->isBlank() ) {
362 $group = $msg->text();
363 } else {
364 $group = isset($wgSpecialPageGroups[$page->mName])
365 ? $wgSpecialPageGroups[$page->mName]
366 : '-';
367 }
368 if( $group == '-' ) $group = 'other';
369 $specialPageGroupsCache[$page->mName] = $group;
370 return $group;
371 }
372
373 /**
374 * Remove a special page from the list
375 * Formerly used to disable expensive or dangerous special pages. The
376 * preferred method is now to add a SpecialPage_initList hook.
377 */
378 static function removePage( $name ) {
379 if ( !self::$mListInitialised ) {
380 self::initList();
381 }
382 unset( self::$mList[$name] );
383 }
384
385 /**
386 * Check if a given name exist as a special page or as a special page alias
387 *
388 * @param $name String: name of a special page
389 * @return Boolean: true if a special page exists with this name
390 */
391 static function exists( $name ) {
392 global $wgContLang;
393 if ( !self::$mListInitialised ) {
394 self::initList();
395 }
396 if( !self::$mAliases ) {
397 self::initAliasList();
398 }
399
400 # Remove special pages inline parameters:
401 $bits = explode( '/', $name );
402 $name = $wgContLang->caseFold($bits[0]);
403
404 return
405 array_key_exists( $name, self::$mList )
406 or array_key_exists( $name, self::$mAliases )
407 ;
408 }
409
410 /**
411 * Find the object with a given name and return it (or NULL)
412 *
413 * @param $name String
414 * @return SpecialPage object or null if the page doesn't exist
415 */
416 static function getPage( $name ) {
417 if ( !self::$mListInitialised ) {
418 self::initList();
419 }
420 if ( array_key_exists( $name, self::$mList ) ) {
421 $rec = self::$mList[$name];
422 if ( is_string( $rec ) ) {
423 $className = $rec;
424 self::$mList[$name] = new $className;
425 } elseif ( is_array( $rec ) ) {
426 $className = array_shift( $rec );
427 self::$mList[$name] = MWFunction::newObj( $className, $rec );
428 }
429 return self::$mList[$name];
430 } else {
431 return null;
432 }
433 }
434
435 /**
436 * Get a special page with a given localised name, or NULL if there
437 * is no such special page.
438 *
439 * @return SpecialPage object or null if the page doesn't exist
440 */
441 static function getPageByAlias( $alias ) {
442 $realName = self::resolveAlias( $alias );
443 if ( $realName ) {
444 return self::getPage( $realName );
445 } else {
446 return null;
447 }
448 }
449
450 /**
451 * Return categorised listable special pages which are available
452 * for the current user, and everyone.
453 *
454 * @return Associative array mapping page's name to its SpecialPage object
455 */
456 static function getUsablePages() {
457 global $wgUser;
458 if ( !self::$mListInitialised ) {
459 self::initList();
460 }
461 $pages = array();
462
463 foreach ( self::$mList as $name => $rec ) {
464 $page = self::getPage( $name );
465 if ( $page->isListed()
466 && (
467 !$page->isRestricted()
468 || $page->userCanExecute( $wgUser )
469 )
470 ) {
471 $pages[$name] = $page;
472 }
473 }
474 return $pages;
475 }
476
477 /**
478 * Return categorised listable special pages for all users
479 *
480 * @return Associative array mapping page's name to its SpecialPage object
481 */
482 static function getRegularPages() {
483 if ( !self::$mListInitialised ) {
484 self::initList();
485 }
486 $pages = array();
487
488 foreach ( self::$mList as $name => $rec ) {
489 $page = self::getPage( $name );
490 if ( $page->isListed() && !$page->isRestricted() ) {
491 $pages[$name] = $page;
492 }
493 }
494 return $pages;
495 }
496
497 /**
498 * Return categorised listable special pages which are available
499 * for the current user, but not for everyone
500 *
501 * @return Associative array mapping page's name to its SpecialPage object
502 */
503 static function getRestrictedPages() {
504 global $wgUser;
505 if( !self::$mListInitialised ) {
506 self::initList();
507 }
508 $pages = array();
509
510 foreach( self::$mList as $name => $rec ) {
511 $page = self::getPage( $name );
512 if(
513 $page->isListed()
514 && $page->isRestricted()
515 && $page->userCanExecute( $wgUser )
516 ) {
517 $pages[$name] = $page;
518 }
519 }
520 return $pages;
521 }
522
523 /**
524 * Execute a special page path.
525 * The path may contain parameters, e.g. Special:Name/Params
526 * Extracts the special page name and call the execute method, passing the parameters
527 *
528 * Returns a title object if the page is redirected, false if there was no such special
529 * page, and true if it was successful.
530 *
531 * @param $title a title object
532 * @param $including output is being captured for use in {{special:whatever}}
533 */
534 static function executePath( &$title, $including = false ) {
535 global $wgOut, $wgTitle, $wgRequest;
536 wfProfileIn( __METHOD__ );
537
538 # FIXME: redirects broken due to this call
539 $bits = explode( '/', $title->getDBkey(), 2 );
540 $name = $bits[0];
541 if( !isset( $bits[1] ) ) { // bug 2087
542 $par = null;
543 } else {
544 $par = $bits[1];
545 }
546 $page = SpecialPage::getPageByAlias( $name );
547 # Nonexistent?
548 if ( !$page ) {
549 if ( !$including ) {
550 $wgOut->setArticleRelated( false );
551 $wgOut->setRobotPolicy( 'noindex,nofollow' );
552 $wgOut->setStatusCode( 404 );
553 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
554 }
555 wfProfileOut( __METHOD__ );
556 return false;
557 }
558
559 # Page exists, set the context
560 $page->setContext( $wgRequest, $wgOut );
561
562 # Check for redirect
563 if ( !$including ) {
564 $redirect = $page->getRedirect( $par );
565 $query = $page->getRedirectQuery();
566 if ( $redirect instanceof Title ) {
567 $url = $redirect->getFullUrl( $query );
568 $wgOut->redirect( $url );
569 wfProfileOut( __METHOD__ );
570 return $redirect;
571 } elseif( $redirect === true ) {
572 global $wgScript;
573 $url = $wgScript . '?' . wfArrayToCGI( $query );
574 $wgOut->redirect( $url );
575 wfProfileOut( __METHOD__ );
576 return $redirect;
577 }
578 }
579
580 # Redirect to canonical alias for GET commands
581 # Not for POST, we'd lose the post data, so it's best to just distribute
582 # the request. Such POST requests are possible for old extensions that
583 # generate self-links without being aware that their default name has
584 # changed.
585 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
586 $query = $_GET;
587 unset( $query['title'] );
588 $query = wfArrayToCGI( $query );
589 $title = $page->getTitle( $par );
590 $url = $title->getFullUrl( $query );
591 $wgOut->redirect( $url );
592 wfProfileOut( __METHOD__ );
593 return $redirect;
594 }
595
596 if ( $including && !$page->includable() ) {
597 wfProfileOut( __METHOD__ );
598 return false;
599 } elseif ( !$including ) {
600 $wgTitle = $page->getTitle();
601 }
602 $page->including( $including );
603
604 // Execute special page
605 $profName = 'Special:' . $page->name();
606 wfProfileIn( $profName );
607 $page->execute( $par );
608 wfProfileOut( $profName );
609 wfProfileOut( __METHOD__ );
610 return true;
611 }
612
613 /**
614 * Just like executePath() except it returns the HTML instead of outputting it
615 * Returns false if there was no such special page, or a title object if it was
616 * a redirect.
617 *
618 * @return String: HTML fragment
619 */
620 static function capturePath( &$title ) {
621 global $wgOut, $wgTitle;
622
623 $oldTitle = $wgTitle;
624 $oldOut = $wgOut;
625 $wgOut = new OutputPage;
626 $wgOut->setTitle( $title );
627
628 $ret = SpecialPage::executePath( $title, 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 global $wgOut;
864 $wgOut->permissionRequired( $this->mRestriction );
865 }
866
867 /**
868 * Sets headers - this should be called from the execute() method of all derived classes!
869 */
870 function setHeaders() {
871 global $wgOut;
872 $wgOut->setArticleRelated( false );
873 $wgOut->setRobotPolicy( "noindex,nofollow" );
874 $wgOut->setPageTitle( $this->getDescription() );
875 }
876
877 /**
878 * Default execute method
879 * Checks user permissions, calls the function given in mFunction
880 *
881 * This may be overridden by subclasses.
882 */
883 function execute( $par ) {
884 global $wgUser;
885
886 $this->setHeaders();
887
888 if ( $this->userCanExecute( $wgUser ) ) {
889 $func = $this->mFunction;
890 // only load file if the function does not exist
891 if(!is_callable($func) and $this->mFile) {
892 require_once( $this->mFile );
893 }
894 $this->outputHeader();
895 call_user_func( $func, $par, $this );
896 } else {
897 $this->displayRestrictionError();
898 }
899 }
900
901 /**
902 * Outputs a summary message on top of special pages
903 * Per default the message key is the canonical name of the special page
904 * May be overriden, i.e. by extensions to stick with the naming conventions
905 * for message keys: 'extensionname-xxx'
906 *
907 * @param $summaryMessageKey String: message key of the summary
908 */
909 function outputHeader( $summaryMessageKey = '' ) {
910 global $wgOut, $wgContLang;
911
912 if( $summaryMessageKey == '' ) {
913 $msg = $wgContLang->lc( $this->name() ) . '-summary';
914 } else {
915 $msg = $summaryMessageKey;
916 }
917 if ( !wfMessage( $msg )->isBlank() and ! $this->including() ) {
918 $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
919 }
920
921 }
922
923 /**
924 * Returns the name that goes in the \<h1\> in the special page itself, and
925 * also the name that will be listed in Special:Specialpages
926 *
927 * Derived classes can override this, but usually it is easier to keep the
928 * default behaviour. Messages can be added at run-time, see
929 * MessageCache.php.
930 *
931 * @return String
932 */
933 function getDescription() {
934 return wfMsg( strtolower( $this->mName ) );
935 }
936
937 /**
938 * Get a self-referential title object
939 *
940 * @return Title object
941 */
942 function getTitle( $subpage = false ) {
943 return self::getTitleFor( $this->mName, $subpage );
944 }
945
946 /**
947 * Set whether this page is listed in Special:Specialpages, at run-time
948 */
949 function setListed( $listed ) {
950 return wfSetVar( $this->mListed, $listed );
951 }
952
953 /**
954 * If the special page is a redirect, then get the Title object it redirects to.
955 * False otherwise.
956 */
957 function getRedirect( $subpage ) {
958 return false;
959 }
960
961 /**
962 * Return part of the request string for a special redirect page
963 * This allows passing, e.g. action=history to Special:Mypage, etc.
964 *
965 * @return String
966 */
967 function getRedirectQuery() {
968 global $wgRequest;
969 $params = array();
970
971 foreach( $this->mAllowedRedirectParams as $arg ) {
972 if( $wgRequest->getVal( $arg, null ) !== null ){
973 $params[$arg] = $wgRequest->getVal( $arg );
974 }
975 }
976
977 foreach( $this->mAddedRedirectParams as $arg => $val ) {
978 $params[$arg] = $val;
979 }
980
981 return count( $params )
982 ? $params
983 : false;
984 }
985
986 /**
987 * Sets the context this SpecialPage is executed in
988 *
989 * @param $request WebRequest
990 * @param $output OutputPage
991 */
992 protected function setContext( $request, $output ) {
993 $this->mRequest = $request;
994 $this->mOutput = $output;
995 $this->mFullTitle = $output->getTitle();
996 }
997 /**
998 * Wrapper around wfMessage that sets the current context. Currently this
999 * is only the title.
1000 *
1001 * @see wfMessage
1002 */
1003 public function msg( /* $args */ ) {
1004 return call_user_func_array( 'wfMessage', func_get_args() )->title( $this->mFullTitle );
1005 }
1006 }
1007
1008 /**
1009 * Shortcut to construct a special page which is unlisted by default
1010 * @ingroup SpecialPage
1011 */
1012 class UnlistedSpecialPage extends SpecialPage
1013 {
1014 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1015 parent::__construct( $name, $restriction, false, $function, $file );
1016 }
1017 }
1018
1019 /**
1020 * Shortcut to construct an includable special page
1021 * @ingroup SpecialPage
1022 */
1023 class IncludableSpecialPage extends SpecialPage
1024 {
1025 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
1026 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1027 }
1028 }
1029
1030 /**
1031 * Shortcut to construct a special page alias.
1032 * @ingroup SpecialPage
1033 */
1034 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
1035 var $redirName, $redirSubpage;
1036
1037 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
1038 parent::__construct( $name );
1039 $this->redirName = $redirName;
1040 $this->redirSubpage = $redirSubpage;
1041 $this->mAllowedRedirectParams = $allowedRedirectParams;
1042 $this->mAddedRedirectParams = $addedRedirectParams;
1043 }
1044
1045 function getRedirect( $subpage ) {
1046 if ( $this->redirSubpage === false ) {
1047 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1048 } else {
1049 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1050 }
1051 }
1052 }
1053
1054 /**
1055 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1056 * are used to get user independant links pointing to the user page, talk
1057 * page and list of contributions.
1058 * This can let us cache a single copy of any generated content for all
1059 * users.
1060 */
1061
1062 /**
1063 * Shortcut to construct a special page pointing to current user user's page.
1064 * @ingroup SpecialPage
1065 */
1066 class SpecialMypage extends UnlistedSpecialPage {
1067 function __construct() {
1068 parent::__construct( 'Mypage' );
1069 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1070 'section', 'oldid', 'diff', 'dir' );
1071 }
1072
1073 function getRedirect( $subpage ) {
1074 global $wgUser;
1075 if ( strval( $subpage ) !== '' ) {
1076 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
1077 } else {
1078 return Title::makeTitle( NS_USER, $wgUser->getName() );
1079 }
1080 }
1081 }
1082
1083 /**
1084 * Shortcut to construct a special page pointing to current user talk page.
1085 * @ingroup SpecialPage
1086 */
1087 class SpecialMytalk extends UnlistedSpecialPage {
1088 function __construct() {
1089 parent::__construct( 'Mytalk' );
1090 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1091 'section', 'oldid', 'diff', 'dir' );
1092 }
1093
1094 function getRedirect( $subpage ) {
1095 global $wgUser;
1096 if ( strval( $subpage ) !== '' ) {
1097 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1098 } else {
1099 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1100 }
1101 }
1102 }
1103
1104 /**
1105 * Shortcut to construct a special page pointing to current user contributions.
1106 * @ingroup SpecialPage
1107 */
1108 class SpecialMycontributions extends UnlistedSpecialPage {
1109 function __construct() {
1110 parent::__construct( 'Mycontributions' );
1111 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1112 'offset', 'dir', 'year', 'month', 'feed' );
1113 }
1114
1115 function getRedirect( $subpage ) {
1116 global $wgUser;
1117 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1118 }
1119 }
1120
1121 /**
1122 * Redirect to Special:Listfiles?user=$wgUser
1123 */
1124 class SpecialMyuploads extends UnlistedSpecialPage {
1125 function __construct() {
1126 parent::__construct( 'Myuploads' );
1127 $this->mAllowedRedirectParams = array( 'limit' );
1128 }
1129
1130 function getRedirect( $subpage ) {
1131 global $wgUser;
1132 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1133 }
1134 }
1135
1136 /**
1137 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1138 */
1139 class SpecialPermanentLink extends UnlistedSpecialPage {
1140 function __construct() {
1141 parent::__construct( 'PermanentLink' );
1142 $this->mAllowedRedirectParams = array();
1143 }
1144
1145 function getRedirect( $subpage ) {
1146 $subpage = intval( $subpage );
1147 $this->mAddedRedirectParams['oldid'] = $subpage;
1148 return true;
1149 }
1150 }