479ea14ec71c9500e5bd730056889de5856d2e3c
3 * SpecialPage: handling special pages and lists thereof
4 * $wgSpecialPages is a list of all SpecialPage objects. These objects are
5 * either instances of SpecialPage or a sub-class thereof. They have an
6 * execute() method, which sends the HTML for the special page to $wgOut.
7 * The parent class has an execute() method which distributes the call to
8 * the historical global functions. Additionally, execute() also checks if the
9 * user has the necessary access privileges and bails out if not.
11 * To add a special page at run-time, use SpecialPage::addPage().
12 * DO NOT manipulate this array at run-time.
15 * @subpackage SpecialPage
22 $wgSpecialPages = array(
23 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
24 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
25 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
27 'Userlogin' => new SpecialPage( 'Userlogin' ),
28 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
29 'Preferences' => new SpecialPage( 'Preferences' ),
30 'Watchlist' => new SpecialPage( 'Watchlist' ),
32 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
33 'Upload' => new SpecialPage( 'Upload' ),
34 'Imagelist' => new SpecialPage( 'Imagelist' ),
35 'Newimages' => new IncludableSpecialPage( 'Newimages' ),
36 'Listusers' => new SpecialPage( 'Listusers' ),
37 'Statistics' => new SpecialPage( 'Statistics' ),
38 'Random' => new SpecialPage( 'Randompage' ),
39 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
40 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
41 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
42 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
43 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
44 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
45 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
46 'Mostcategories' => new SpecialPage( 'Mostcategories' ),
47 'Mostimages' => new SpecialPage( 'Mostimages' ),
48 'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
49 'Shortpages' => new SpecialPage( 'Shortpages' ),
50 'Longpages' => new SpecialPage( 'Longpages' ),
51 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
52 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
53 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
54 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
55 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
56 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
57 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
58 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
59 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
60 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
61 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
62 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
63 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
64 'Booksources' => new SpecialPage( 'Booksources' ),
65 'Categories' => new SpecialPage( 'Categories' ),
66 'Export' => new SpecialPage( 'Export' ),
67 'Version' => new SpecialPage( 'Version' ),
68 'Allmessages' => new SpecialPage( 'Allmessages' ),
69 'Log' => new SpecialPage( 'Log' ),
70 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
71 'Undelete' => new SpecialPage( 'Undelete' ),
72 "Import" => new SpecialPage( "Import", 'import' ),
73 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
74 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
75 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
76 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
79 if ( $wgUseValidation )
80 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
82 if( !$wgDisableCounters ) {
83 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
86 if( !$wgDisableInternalSearch ) {
87 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
90 if( $wgEmailAuthentication ) {
91 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
94 if ( $wgEnableUnwatchedpages )
95 $wgSpecialPages['Unwatchedpages'] = new SpecialPage( 'Unwatchedpages' );
98 * Parent special page class, also static functions for handling the special
108 * The name of the class, used in the URL.
109 * Also used for the default <h1> heading, @see getDescription()
113 * Minimum user level required to access this page, or "" for anyone.
114 * Also used to categorise the pages in Special:Specialpages
118 * Listed in Special:Specialpages?
122 * Function name called by the default execute()
126 * File which needs to be included before the function above can be called
130 * Whether or not this special page is being included from an article
134 * Whether the special page can be included in an article
143 * Add a page to the list of valid special pages
144 * $obj->execute() must send HTML to $wgOut then return
145 * Use this for a special page extension
148 function addPage( &$obj ) {
149 global $wgSpecialPages;
150 $wgSpecialPages[$obj->mName
] = $obj;
154 * Remove a special page from the list
155 * Occasionally used to disable expensive or dangerous special pages
158 function removePage( $name ) {
159 global $wgSpecialPages;
160 unset( $wgSpecialPages[$name] );
164 * Find the object with a given name and return it (or NULL)
166 * @param string $name
168 function getPage( $name ) {
169 global $wgSpecialPages;
170 if ( array_key_exists( $name, $wgSpecialPages ) ) {
171 return $wgSpecialPages[$name];
179 * @param string $name
180 * @return mixed Title object if the redirect exists, otherwise NULL
182 function getRedirect( $name ) {
186 return Title
::makeTitle( NS_USER
, $wgUser->getName() );
188 return Title
::makeTitle( NS_USER_TALK
, $wgUser->getName() );
189 case 'Mycontributions':
190 return Title
::makeTitle( NS_SPECIAL
, 'Contributions/' . $wgUser->getName() );
192 return Title
::makeTitle( NS_SPECIAL
, 'Listusers/sysop' ); # @bug 2832
194 return Title
::makeTitle( NS_SPECIAL
, 'Random' );
201 * Return categorised listable special pages
202 * Returns a 2d array where the first index is the restriction name
205 function getPages() {
206 global $wgSpecialPages;
210 'developer' => array()
213 foreach ( $wgSpecialPages as $name => $page ) {
214 if ( $page->isListed() ) {
215 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
222 * Execute a special page path.
223 * The path may contain parameters, e.g. Special:Name/Params
224 * Extracts the special page name and call the execute method, passing the parameters
226 * Returns a title object if the page is redirected, false if there was no such special
227 * page, and true if it was successful.
229 * @param $title a title object
230 * @param $including output is being captured for use in {{special:whatever}}
232 function executePath( &$title, $including = false ) {
233 global $wgSpecialPages, $wgOut, $wgTitle;
234 $fname = 'SpecialPage::executePath';
235 wfProfileIn( $fname );
237 $bits = split( "/", $title->getDBkey(), 2 );
239 if( !isset( $bits[1] ) ) { // bug 2087
245 $page = SpecialPage
::getPage( $name );
246 if ( is_null( $page ) ) {
248 wfProfileOut( $fname );
251 $redir = SpecialPage
::getRedirect( $name );
252 if ( isset( $redir ) ) {
254 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
256 $wgOut->redirect( $redir->getFullURL() );
259 $wgOut->setArticleRelated( false );
260 $wgOut->setRobotpolicy( "noindex,follow" );
261 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
266 if ( $including && !$page->includable() ) {
267 wfProfileOut( $fname );
271 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
275 $page->including( $including );
277 $profName = 'Special:' . $page->getName();
278 wfProfileIn( $profName );
279 $page->execute( $par );
280 wfProfileOut( $profName );
283 wfProfileOut( $fname );
288 * Just like executePath() except it returns the HTML instead of outputting it
289 * Returns false if there was no such special page, or a title object if it was
293 function capturePath( &$title ) {
294 global $wgOut, $wgTitle;
296 $oldTitle = $wgTitle;
298 $wgOut = new OutputPage
;
300 $ret = SpecialPage
::executePath( $title, true );
301 if ( $ret === true ) {
302 $ret = $wgOut->getHTML();
304 $wgTitle = $oldTitle;
310 * Default constructor for special pages
311 * Derivative classes should call this from their constructor
312 * Note that if the user does not have the required level, an error message will
313 * be displayed by the default execute() method, without the global function ever
316 * If you override execute(), you can recover the default behaviour with userCanExecute()
317 * and displayRestrictionError()
319 * @param string $name Name of the special page, as seen in links and URLs
320 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
321 * @param boolean $listed Whether the page is listed in Special:Specialpages
322 * @param string $function Function called by execute(). By default it is constructed from $name
323 * @param string $file File which is included by execute(). It is also constructed from $name by default
325 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
326 $this->mName
= $name;
327 $this->mRestriction
= $restriction;
328 $this->mListed
= $listed;
329 $this->mIncludable
= $includable;
330 if ( $function == false ) {
331 $this->mFunction
= 'wfSpecial'.$name;
333 $this->mFunction
= $function;
335 if ( $file === 'default' ) {
336 $this->mFile
= "Special{$name}.php";
338 $this->mFile
= $file;
342 # Accessor functions, see the descriptions of the associated variables above
343 function getName() { return $this->mName
; }
344 function getRestriction() { return $this->mRestriction
; }
345 function isListed() { return $this->mListed
; }
346 function getFile() { return $this->mFile
; }
347 function including( $x = NULL ) { return wfSetVar( $this->mIncluding
, $x ); }
348 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable
, $x ); }
351 * Checks if the given user (identified by an object) can execute this
352 * special page (as defined by $mRestriction)
354 function userCanExecute( &$user ) {
355 if ( $this->mRestriction
== "" ) {
358 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
367 * Output an error message telling the user what access level they have to have
369 function displayRestrictionError() {
371 $wgOut->permissionRequired( $this->mRestriction
);
375 * Sets headers - this should be called from the execute() method of all derived classes!
377 function setHeaders() {
379 $wgOut->setArticleRelated( false );
380 $wgOut->setRobotPolicy( "noindex,follow" );
381 $wgOut->setPageTitle( $this->getDescription() );
385 * Default execute method
386 * Checks user permissions, calls the function given in mFunction
388 function execute( $par ) {
389 global $wgUser, $wgOut, $wgTitle;
393 if ( $this->userCanExecute( $wgUser ) ) {
394 $func = $this->mFunction
;
395 // only load file if the function does not exist
396 if(!function_exists($func) and $this->mFile
) {
397 require_once( $this->mFile
);
399 $func( $par, $this );
401 $this->displayRestrictionError();
405 # Returns the name that goes in the <h1> in the special page itself, and also the name that
406 # will be listed in Special:Specialpages
408 # Derived classes can override this, but usually it is easier to keep the default behaviour.
409 # Messages can be added at run-time, see MessageCache.php
410 function getDescription() {
411 return wfMsg( strtolower( $this->mName
) );
415 * Get a self-referential title object
417 function getTitle() {
418 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
422 * Set whether this page is listed in Special:Specialpages, at run-time
424 function setListed( $listed ) {
425 return wfSetVar( $this->mListed
, $listed );
431 * Shortcut to construct a special page which is unlisted by default
434 class UnlistedSpecialPage
extends SpecialPage
436 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
437 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );
442 * Shortcut to construct an includable special page
445 class IncludableSpecialPage
extends SpecialPage
447 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
448 SpecialPage
::SpecialPage( $name, $restriction, $listed, $function, $file, true );