(bug 12698) Create PAGESIZE parser function, to return the size of a page. Quite...
[mediawiki.git] / includes / SpecialPage.php
blob2e331c587f476f053a7f4f9479ca4cb385695b9e
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
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.
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.
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.
20 * @addtogroup SpecialPage
23 /**
24 * Parent special page class, also static functions for handling the special
25 * page list.
26 * @addtogroup SpecialPage
28 class SpecialPage
30 /**#@+
31 * @access private
33 /**
34 * The canonical name of this special page
35 * Also used for the default <h1> heading, @see getDescription()
37 var $mName;
38 /**
39 * The local name of this special page
41 var $mLocalName;
42 /**
43 * Minimum user level required to access this page, or "" for anyone.
44 * Also used to categorise the pages in Special:Specialpages
46 var $mRestriction;
47 /**
48 * Listed in Special:Specialpages?
50 var $mListed;
51 /**
52 * Function name called by the default execute()
54 var $mFunction;
55 /**
56 * File which needs to be included before the function above can be called
58 var $mFile;
59 /**
60 * Whether or not this special page is being included from an article
62 var $mIncluding;
63 /**
64 * Whether the special page can be included in an article
66 var $mIncludable;
67 /**
68 * Query parameters that can be passed through redirects
70 var $mAllowedRedirectParams = array();
71 /**
72 * List of special pages, followed by parameters.
73 * If the only parameter is a string, that is the page name.
74 * Otherwise, it is an array. The format is one of:
75 ** array( 'SpecialPage', name, right )
76 ** array( 'IncludableSpecialPage', name, right, listed? )
77 ** array( 'UnlistedSpecialPage', name, right )
78 ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
80 static public $mList = array(
81 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
82 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
83 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
85 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
86 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
87 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
88 'Preferences' => array( 'SpecialPage', 'Preferences' ),
89 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
91 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
92 'Upload' => array( 'SpecialPage', 'Upload' ),
93 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
94 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
95 'Listusers' => array( 'SpecialPage', 'Listusers' ),
96 'Listgrouprights' => 'SpecialListGroupRights',
97 'Statistics' => array( 'SpecialPage', 'Statistics' ),
98 'Randompage' => 'Randompage',
99 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
100 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
101 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
102 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
103 'Uncategorizedtemplates' => array( 'SpecialPage', 'Uncategorizedtemplates' ),
104 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
105 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
106 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
107 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
108 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
109 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
110 'Mostlinkedtemplates' => array( 'SpecialPage', 'Mostlinkedtemplates' ),
111 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
112 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
113 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
114 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
115 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
116 'Longpages' => array( 'SpecialPage', 'Longpages' ),
117 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
118 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
119 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
120 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
121 'Protectedtitles' => array( 'SpecialPage', 'Protectedtitles' ),
122 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
123 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
124 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
125 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
126 'Contributions' => array( 'SpecialPage', 'Contributions' ),
127 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
128 'Whatlinkshere' => array( 'SpecialPage', 'Whatlinkshere' ),
129 'Recentchangeslinked' => array( 'SpecialPage', 'Recentchangeslinked' ),
130 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
131 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
132 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
133 'Booksources' => 'SpecialBookSources',
134 'Categories' => array( 'SpecialPage', 'Categories' ),
135 'Export' => array( 'SpecialPage', 'Export' ),
136 'Version' => array( 'SpecialPage', 'Version' ),
137 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
138 'Log' => array( 'SpecialPage', 'Log' ),
139 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
140 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
141 'Import' => array( 'SpecialPage', 'Import', 'import' ),
142 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
143 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
144 'Userrights' => 'UserrightsPage',
145 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
146 'FileDuplicateSearch' => array( 'SpecialPage', 'FileDuplicateSearch' ),
147 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
148 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
149 'Revisiondelete' => array( 'UnlistedSpecialPage', 'Revisiondelete', 'deleterevision' ),
150 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
151 'Randomredirect' => 'SpecialRandomredirect',
152 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
153 'Filepath' => array( 'SpecialPage', 'Filepath' ),
155 'Mypage' => array( 'SpecialMypage' ),
156 'Mytalk' => array( 'SpecialMytalk' ),
157 'Mycontributions' => array( 'SpecialMycontributions' ),
158 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
159 'MergeHistory' => array( 'SpecialPage', 'MergeHistory', 'mergehistory' ),
160 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
163 static public $mAliases;
164 static public $mListInitialised = false;
166 * List of special pages, followed by what subtitle they should go under
167 * at Special:SpecialPages
169 static public $mGroupsList = array(
170 'DoubleRedirects' => 'maintenance',
171 'BrokenRedirects' => 'maintenance',
172 'Lonelypages' => 'maintenance',
173 'Uncategorizedpages' => 'maintenance',
174 'Uncategorizedcategories' => 'maintenance',
175 'Uncategorizedimages' => 'maintenance',
176 'Uncategorizedtemplates' => 'maintenance',
177 'Unusedcategories' => 'maintenance',
178 'Unusedimages' => 'maintenance',
179 'Protectedpages' => 'maintenance',
180 'Protectedtitles' => 'maintenance',
181 'Unusedtemplates' => 'maintenance',
182 'Withoutinterwiki' => 'maintenance',
183 'Longpages' => 'maintenance',
185 'Userlogin' => 'login',
186 'Userlogout' => 'login',
187 'CreateAccount' => 'login',
189 'Recentchanges' => 'changes',
190 'Recentchangeslinked' => 'changes',
191 'Watchlist' => 'changes',
192 'Newimages' => 'changes',
193 'Newpages' => 'changes',
194 'Log' => 'changes',
196 'Upload' => 'media',
197 'Imagelist' => 'media',
198 'MIMEsearch' => 'media',
199 'FileDuplicateSearch' => 'media',
200 'Filepath' => 'media',
202 'Listusers' => 'users',
203 'Listgrouprights' => 'users',
204 'Ipblocklist' => 'users',
205 'Contributions' => 'users',
206 'Emailuser' => 'users',
207 'Listadmins' => 'users',
208 'Listbots' => 'users',
210 'Wantedpages' => 'needy',
211 'Wantedcategories' => 'needy',
212 'Shortpages' => 'needy',
213 'Ancientpages' => 'needy',
214 'Deadendpages' => 'needy',
216 'Mostlinked' => 'highuse',
217 'Mostlinkedcategories' => 'highuse',
218 'Mostlinkedtemplates' => 'highuse',
219 'Mostcategories' => 'highuse',
220 'Mostimages' => 'highuse',
221 'Mostrevisions' => 'highuse',
223 'Userrights' => 'permissions',
224 'Blockip' => 'permissions',
226 'Statistics' => 'other',
227 'Fewestrevisions' => 'other',
228 'Randompage' => 'other',
229 'Disambiguations' => 'other',
230 'Specialpages' => 'other',
231 'Blockme' => 'other',
232 'Movepage' => 'other',
233 'MergeHistory' => 'other',
234 'Lockdb' => 'other',
235 'Unlockdb' => 'other',
236 'Version' => 'other',
237 'Whatlinkshere' => 'other',
238 'Booksources' => 'other',
239 'Revisiondelete' => 'other',
240 'Export' => 'other',
241 'Categories' => 'other',
242 'Undelete' => 'other',
243 'Import' => 'other',
244 'Unwatchedpages' => 'other',
245 'Randomredirect' => 'other',
246 'Allpages' => 'other',
247 'Allmessages' => 'other',
248 'Prefixindex' => 'other',
249 'Listredirects' => 'other',
250 'Preferences' => 'other',
251 'Resetpass' => 'other',
252 'Mypage' => 'other',
253 'Mytalk' => 'other',
254 'Mycontributions' => 'other',
257 /**#@-*/
260 * Initialise the special page list
261 * This must be called before accessing SpecialPage::$mList
263 static function initList() {
264 global $wgSpecialPages;
265 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
267 if ( self::$mListInitialised ) {
268 return;
270 wfProfileIn( __METHOD__ );
272 # Better to set this now, to avoid infinite recursion in carelessly written hooks
273 self::$mListInitialised = true;
275 if( !$wgDisableCounters ) {
276 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
279 if( !$wgDisableInternalSearch ) {
280 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
283 if( $wgEmailAuthentication ) {
284 self::$mList['Confirmemail'] = 'EmailConfirmation';
285 self::$mList['Invalidateemail'] = 'EmailInvalidation';
288 # Add extension special pages
289 self::$mList = array_merge( self::$mList, $wgSpecialPages );
291 # Run hooks
292 # This hook can be used to remove undesired built-in special pages
293 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
294 wfProfileOut( __METHOD__ );
297 static function initAliasList() {
298 if ( !is_null( self::$mAliases ) ) {
299 return;
302 global $wgContLang;
303 $aliases = $wgContLang->getSpecialPageAliases();
304 $missingPages = self::$mList;
305 self::$mAliases = array();
306 foreach ( $aliases as $realName => $aliasList ) {
307 foreach ( $aliasList as $alias ) {
308 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
310 unset( $missingPages[$realName] );
312 foreach ( $missingPages as $name => $stuff ) {
313 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
318 * Given a special page alias, return the special page name.
319 * Returns false if there is no such alias.
321 static function resolveAlias( $alias ) {
322 global $wgContLang;
324 if ( !self::$mListInitialised ) self::initList();
325 if ( is_null( self::$mAliases ) ) self::initAliasList();
326 $caseFoldedAlias = $wgContLang->caseFold( $alias );
327 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
328 return self::$mAliases[$caseFoldedAlias];
329 } else {
330 return false;
335 * Given a special page name with a possible subpage, return an array
336 * where the first element is the special page name and the second is the
337 * subpage.
339 static function resolveAliasWithSubpage( $alias ) {
340 $bits = explode( '/', $alias, 2 );
341 $name = self::resolveAlias( $bits[0] );
342 if( !isset( $bits[1] ) ) { // bug 2087
343 $par = NULL;
344 } else {
345 $par = $bits[1];
347 return array( $name, $par );
351 * Add a page to the list of valid special pages. This used to be the preferred
352 * method for adding special pages in extensions. It's now suggested that you add
353 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
355 * @param SpecialPage $page
356 * @static
358 static function addPage( &$page ) {
359 if ( !self::$mListInitialised ) {
360 self::initList();
362 self::$mList[$page->mName] = $page;
366 * Add a page to a certain display group for Special:SpecialPages
368 * @param mixed $page (SpecialPage or string)
369 * @param string $group
370 * @static
372 static function setGroup( $page, $group ) {
373 if ( !self::$mListInitialised ) {
374 self::initList();
376 $name = is_object($page) ? $page->mName : $page;
377 self::$mGroupsList[$name] = $group;
381 * Add a page to a certain display group for Special:SpecialPages
383 * @param SpecialPage $page
384 * @static
386 static function getGroup( &$page ) {
387 if ( !self::$mListInitialised ) {
388 self::initList();
390 $group = isset(self::$mGroupsList[$page->mName]) ?
391 self::$mGroupsList[$page->mName] : 'other';
392 return $group;
396 * Remove a special page from the list
397 * Formerly used to disable expensive or dangerous special pages. The
398 * preferred method is now to add a SpecialPage_initList hook.
400 * @static
402 static function removePage( $name ) {
403 if ( !self::$mListInitialised ) {
404 self::initList();
406 unset( self::$mList[$name] );
410 * Check if a given name exist as a special page or as a special page alias
411 * @param $name string: name of a special page
412 * @return boolean: true if a special page exists with this name
414 static function exists( $name ) {
415 global $wgContLang;
416 if ( !self::$mListInitialised ) {
417 self::initList();
419 if( !self::$mAliases ) {
420 self::initAliasList();
423 # Remove special pages inline parameters:
424 $bits = explode( '/', $name );
425 $name = $wgContLang->caseFold($bits[0]);
427 return
428 array_key_exists( $name, self::$mList )
429 or array_key_exists( $name, self::$mAliases )
434 * Find the object with a given name and return it (or NULL)
435 * @static
436 * @param string $name
438 static function getPage( $name ) {
439 if ( !self::$mListInitialised ) {
440 self::initList();
442 if ( array_key_exists( $name, self::$mList ) ) {
443 $rec = self::$mList[$name];
444 if ( is_string( $rec ) ) {
445 $className = $rec;
446 self::$mList[$name] = new $className;
447 } elseif ( is_array( $rec ) ) {
448 $className = array_shift( $rec );
449 self::$mList[$name] = wfCreateObject( $className, $rec );
451 return self::$mList[$name];
452 } else {
453 return NULL;
458 * Get a special page with a given localised name, or NULL if there
459 * is no such special page.
461 static function getPageByAlias( $alias ) {
462 $realName = self::resolveAlias( $alias );
463 if ( $realName ) {
464 return self::getPage( $realName );
465 } else {
466 return NULL;
471 * Return categorised listable special pages for all users
472 * @static
474 static function getRegularPages() {
475 if ( !self::$mListInitialised ) {
476 self::initList();
478 $pages = array();
480 foreach ( self::$mList as $name => $rec ) {
481 $page = self::getPage( $name );
482 if ( $page->isListed() && !$page->isRestricted() ) {
483 $pages[$name] = $page;
486 return $pages;
490 * Return categorised listable special pages which are available
491 * for the current user, but not for everyone
492 * @static
494 static function getRestrictedPages() {
495 global $wgUser;
496 if ( !self::$mListInitialised ) {
497 self::initList();
499 $pages = array();
501 foreach ( self::$mList as $name => $rec ) {
502 $page = self::getPage( $name );
503 if (
504 $page->isListed()
505 and $page->isRestricted()
506 and $page->userCanExecute( $wgUser )
508 $pages[$name] = $page;
511 return $pages;
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
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.
522 * @param $title a title object
523 * @param $including output is being captured for use in {{special:whatever}}
525 static function executePath( &$title, $including = false ) {
526 global $wgOut, $wgTitle, $wgRequest;
527 wfProfileIn( __METHOD__ );
529 # FIXME: redirects broken due to this call
530 $bits = explode( '/', $title->getDBkey(), 2 );
531 $name = $bits[0];
532 if( !isset( $bits[1] ) ) { // bug 2087
533 $par = NULL;
534 } else {
535 $par = $bits[1];
537 $page = SpecialPage::getPageByAlias( $name );
538 # Nonexistent?
539 if ( !$page ) {
540 if ( !$including ) {
541 $wgOut->setArticleRelated( false );
542 $wgOut->setRobotpolicy( 'noindex,nofollow' );
543 $wgOut->setStatusCode( 404 );
544 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
546 wfProfileOut( __METHOD__ );
547 return false;
550 # Check for redirect
551 if ( !$including ) {
552 $redirect = $page->getRedirect( $par );
553 if ( $redirect ) {
554 $query = $page->getRedirectQuery();
555 $url = $redirect->getFullUrl( $query );
556 $wgOut->redirect( $url );
557 wfProfileOut( __METHOD__ );
558 return $redirect;
562 # Redirect to canonical alias for GET commands
563 # Not for POST, we'd lose the post data, so it's best to just distribute
564 # the request. Such POST requests are possible for old extensions that
565 # generate self-links without being aware that their default name has
566 # changed.
567 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
568 $query = $_GET;
569 unset( $query['title'] );
570 $query = wfArrayToCGI( $query );
571 $title = $page->getTitle( $par );
572 $url = $title->getFullUrl( $query );
573 $wgOut->redirect( $url );
574 wfProfileOut( __METHOD__ );
575 return $redirect;
578 if ( $including && !$page->includable() ) {
579 wfProfileOut( __METHOD__ );
580 return false;
581 } elseif ( !$including ) {
582 $wgTitle = $page->getTitle();
584 $page->including( $including );
586 // Execute special page
587 $profName = 'Special:' . $page->getName();
588 wfProfileIn( $profName );
589 $page->execute( $par );
590 wfProfileOut( $profName );
591 wfProfileOut( __METHOD__ );
592 return true;
596 * Just like executePath() except it returns the HTML instead of outputting it
597 * Returns false if there was no such special page, or a title object if it was
598 * a redirect.
599 * @static
601 static function capturePath( &$title ) {
602 global $wgOut, $wgTitle;
604 $oldTitle = $wgTitle;
605 $oldOut = $wgOut;
606 $wgOut = new OutputPage;
608 $ret = SpecialPage::executePath( $title, true );
609 if ( $ret === true ) {
610 $ret = $wgOut->getHTML();
612 $wgTitle = $oldTitle;
613 $wgOut = $oldOut;
614 return $ret;
618 * Get the local name for a specified canonical name
620 * @param $name
621 * @param mixed $subpage Boolean false, or string
623 * @return string
625 static function getLocalNameFor( $name, $subpage = false ) {
626 global $wgContLang;
627 $aliases = $wgContLang->getSpecialPageAliases();
628 if ( isset( $aliases[$name][0] ) ) {
629 $name = $aliases[$name][0];
631 if ( $subpage !== false && !is_null( $subpage ) ) {
632 $name = "$name/$subpage";
634 return $name;
638 * Get a localised Title object for a specified special page name
640 static function getTitleFor( $name, $subpage = false ) {
641 $name = self::getLocalNameFor( $name, $subpage );
642 if ( $name ) {
643 return Title::makeTitle( NS_SPECIAL, $name );
644 } else {
645 throw new MWException( "Invalid special page name \"$name\"" );
650 * Get a localised Title object for a page name with a possibly unvalidated subpage
652 static function getSafeTitleFor( $name, $subpage = false ) {
653 $name = self::getLocalNameFor( $name, $subpage );
654 if ( $name ) {
655 return Title::makeTitleSafe( NS_SPECIAL, $name );
656 } else {
657 return null;
662 * Get a title for a given alias
663 * @return Title or null if there is no such alias
665 static function getTitleForAlias( $alias ) {
666 $name = self::resolveAlias( $alias );
667 if ( $name ) {
668 return self::getTitleFor( $name );
669 } else {
670 return null;
675 * Default constructor for special pages
676 * Derivative classes should call this from their constructor
677 * Note that if the user does not have the required level, an error message will
678 * be displayed by the default execute() method, without the global function ever
679 * being called.
681 * If you override execute(), you can recover the default behaviour with userCanExecute()
682 * and displayRestrictionError()
684 * @param string $name Name of the special page, as seen in links and URLs
685 * @param string $restriction User right required, e.g. "block" or "delete"
686 * @param boolean $listed Whether the page is listed in Special:Specialpages
687 * @param string $function Function called by execute(). By default it is constructed from $name
688 * @param string $file File which is included by execute(). It is also constructed from $name by default
690 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
691 $this->mName = $name;
692 $this->mRestriction = $restriction;
693 $this->mListed = $listed;
694 $this->mIncludable = $includable;
695 if ( $function == false ) {
696 $this->mFunction = 'wfSpecial'.$name;
697 } else {
698 $this->mFunction = $function;
700 if ( $file === 'default' ) {
701 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
702 } else {
703 $this->mFile = $file;
707 /**#@+
708 * Accessor
710 * @deprecated
712 function getName() { return $this->mName; }
713 function getRestriction() { return $this->mRestriction; }
714 function getFile() { return $this->mFile; }
715 function isListed() { return $this->mListed; }
716 /**#@-*/
718 /**#@+
719 * Accessor and mutator
721 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
722 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
723 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
724 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
725 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
726 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
727 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
728 /**#@-*/
731 * Get the localised name of the special page
733 function getLocalName() {
734 if ( !isset( $this->mLocalName ) ) {
735 $this->mLocalName = self::getLocalNameFor( $this->mName );
737 return $this->mLocalName;
741 * Can be overridden by subclasses with more complicated permissions
742 * schemes.
744 * @return bool Should the page be displayed with the restricted-access
745 * pages?
747 public function isRestricted() {
748 return $this->mRestriction != '';
752 * Checks if the given user (identified by an object) can execute this
753 * special page (as defined by $mRestriction). Can be overridden by sub-
754 * classes with more complicated permissions schemes.
756 * @param User $user The user to check
757 * @return bool Does the user have permission to view the page?
759 public function userCanExecute( $user ) {
760 return $user->isAllowed( $this->mRestriction );
764 * Output an error message telling the user what access level they have to have
766 function displayRestrictionError() {
767 global $wgOut;
768 $wgOut->permissionRequired( $this->mRestriction );
772 * Sets headers - this should be called from the execute() method of all derived classes!
774 function setHeaders() {
775 global $wgOut;
776 $wgOut->setArticleRelated( false );
777 $wgOut->setRobotPolicy( "noindex,nofollow" );
778 $wgOut->setPageTitle( $this->getDescription() );
782 * Default execute method
783 * Checks user permissions, calls the function given in mFunction
785 * This may be overridden by subclasses.
787 function execute( $par ) {
788 global $wgUser;
790 $this->setHeaders();
792 if ( $this->userCanExecute( $wgUser ) ) {
793 $func = $this->mFunction;
794 // only load file if the function does not exist
795 if(!is_callable($func) and $this->mFile) {
796 require_once( $this->mFile );
798 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
799 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
800 $this->outputHeader();
801 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
802 return;
803 call_user_func( $func, $par, $this );
804 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
805 return;
806 } else {
807 $this->displayRestrictionError();
811 function outputHeader() {
812 global $wgOut, $wgContLang;
814 $msg = $wgContLang->lc( $this->name() ) . '-summary';
815 $out = wfMsgNoTrans( $msg );
816 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
817 $wgOut->addWikiText( $out );
822 # Returns the name that goes in the <h1> in the special page itself, and also the name that
823 # will be listed in Special:Specialpages
825 # Derived classes can override this, but usually it is easier to keep the default behaviour.
826 # Messages can be added at run-time, see MessageCache.php
827 function getDescription() {
828 return wfMsg( strtolower( $this->mName ) );
832 * Get a self-referential title object
834 function getTitle( $subpage = false) {
835 return self::getTitleFor( $this->mName, $subpage );
839 * Set whether this page is listed in Special:Specialpages, at run-time
841 function setListed( $listed ) {
842 return wfSetVar( $this->mListed, $listed );
846 * If the special page is a redirect, then get the Title object it redirects to.
847 * False otherwise.
849 function getRedirect( $subpage ) {
850 return false;
854 * Return part of the request string for a special redirect page
855 * This allows passing, e.g. action=history to Special:Mypage, etc.
857 * @return string
859 function getRedirectQuery() {
860 global $wgRequest;
861 $params = array();
862 foreach( $this->mAllowedRedirectParams as $arg ) {
863 if( $val = $wgRequest->getVal( $arg, false ) )
864 $params[] = $arg . '=' . $val;
867 return count( $params ) ? implode( '&', $params ) : false;
872 * Shortcut to construct a special page which is unlisted by default
873 * @addtogroup SpecialPage
875 class UnlistedSpecialPage extends SpecialPage
877 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
878 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
883 * Shortcut to construct an includable special page
884 * @addtogroup SpecialPage
886 class IncludableSpecialPage extends SpecialPage
888 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
889 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
894 * Shortcut to construct a special page alias.
895 * @addtogroup SpecialPage
897 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
898 var $redirName, $redirSubpage;
900 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
901 parent::__construct( $name );
902 $this->redirName = $redirName;
903 $this->redirSubpage = $redirSubpage;
904 $this->mAllowedRedirectParams = $redirectParams;
907 function getRedirect( $subpage ) {
908 if ( $this->redirSubpage === false ) {
909 return SpecialPage::getTitleFor( $this->redirName, $subpage );
910 } else {
911 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
916 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
917 * are used to get user independant links pointing to the user page, talk
918 * page and list of contributions.
919 * This can let us cache a single copy of any generated content for all
920 * users.
924 * Shortcut to construct a special page pointing to current user user's page.
925 * @addtogroup SpecialPage
927 class SpecialMypage extends UnlistedSpecialPage {
928 function __construct() {
929 parent::__construct( 'Mypage' );
930 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
933 function getRedirect( $subpage ) {
934 global $wgUser;
935 if ( strval( $subpage ) !== '' ) {
936 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
937 } else {
938 return Title::makeTitle( NS_USER, $wgUser->getName() );
944 * Shortcut to construct a special page pointing to current user talk page.
945 * @addtogroup SpecialPage
947 class SpecialMytalk extends UnlistedSpecialPage {
948 function __construct() {
949 parent::__construct( 'Mytalk' );
950 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
953 function getRedirect( $subpage ) {
954 global $wgUser;
955 if ( strval( $subpage ) !== '' ) {
956 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
957 } else {
958 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
964 * Shortcut to construct a special page pointing to current user contributions.
965 * @addtogroup SpecialPage
967 class SpecialMycontributions extends UnlistedSpecialPage {
968 function __construct() {
969 parent::__construct( 'Mycontributions' );
972 function getRedirect( $subpage ) {
973 global $wgUser;
974 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );