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
21 global $wgSpecialPages;
26 $wgSpecialPages = array(
27 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
28 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
29 'Disambiguations' => new UnlistedSpecialPage ( 'Disambiguations' ),
31 'Userlogin' => new SpecialPage( 'Userlogin' ),
32 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
33 'Preferences' => new SpecialPage( 'Preferences' ),
34 'Watchlist' => new SpecialPage( 'Watchlist' ),
36 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
37 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
38 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
40 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
41 'Upload' => new SpecialPage( 'Upload' ),
42 'Imagelist' => new SpecialPage( 'Imagelist' ),
43 'Newimages' => new SpecialPage( 'Newimages' ),
44 'Listusers' => new SpecialPage( 'Listusers' ),
45 'Listadmins' => new UnlistedSpecialPage( 'Listadmins' ),
46 'Statistics' => new SpecialPage( 'Statistics' ),
47 'Randompage' => new SpecialPage( 'Randompage' ),
48 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
49 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
50 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
51 'Unusedimages' => new SpecialPage( 'Unusedimages' )
54 global $wgUseValidation ;
55 if ( $wgUseValidation )
56 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
58 global $wgDisableCounters;
59 if( !$wgDisableCounters ) {
60 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
63 global $wgDisableInternalSearch;
64 if( !$wgDisableInternalSearch ) {
65 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
68 $wgSpecialPages = array_merge($wgSpecialPages, array (
69 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
70 'Shortpages' => new SpecialPage( 'Shortpages' ),
71 'Longpages' => new SpecialPage( 'Longpages' ),
72 'Newpages' => new SpecialPage( 'Newpages' ),
73 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
74 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
75 'Allpages' => new SpecialPage( 'Allpages' ),
76 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
77 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
78 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
79 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
80 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
81 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
82 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
83 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
84 'Booksources' => new SpecialPage( 'Booksources' ),
85 'Categories' => new SpecialPage( 'Categories' ),
86 'Export' => new SpecialPage( 'Export' ),
87 'Version' => new SpecialPage( 'Version' ),
88 'Allmessages' => new SpecialPage( 'Allmessages' ),
89 'Log' => new SpecialPage( 'Log' ),
90 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
91 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
92 // Makesysop is obsolete, replaced by Special:Userlevels [av]
93 # 'Makesysop' => new SpecialPage( 'Makesysop', 'userrights' ),
95 # Special:Import is half-written
96 "Import" => new SpecialPage( "Import", "sysop" ),
97 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
98 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
99 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
100 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
101 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
105 * Parent special page class, also static functions for handling the special
115 * The name of the class, used in the URL.
116 * Also used for the default <h1> heading, @see getDescription()
120 * Minimum user level required to access this page, or "" for anyone.
121 * Also used to categorise the pages in Special:Specialpages
125 * Listed in Special:Specialpages?
129 * Function name called by the default execute()
133 * File which needs to be included before the function above can be called
139 * Add a page to the list of valid special pages
140 * $obj->execute() must send HTML to $wgOut then return
141 * Use this for a special page extension
144 function addPage( &$obj ) {
145 global $wgSpecialPages;
146 $wgSpecialPages[$obj->mName
] = $obj;
150 * Remove a special page from the list
151 * Occasionally used to disable expensive or dangerous special pages
154 function removePage( $name ) {
155 global $wgSpecialPages;
156 unset( $wgSpecialPages[$name] );
160 * Find the object with a given name and return it (or NULL)
162 * @param string $name
164 function &getPage( $name ) {
165 global $wgSpecialPages;
166 if ( array_key_exists( $name, $wgSpecialPages ) ) {
167 return $wgSpecialPages[$name];
174 * Return categorised listable special pages
175 * Returns a 2d array where the first index is the restriction name
178 function getPages() {
179 global $wgSpecialPages;
183 'developer' => array()
186 foreach ( $wgSpecialPages as $name => $page ) {
187 if ( $page->isListed() ) {
188 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
195 * Execute a special page path.
196 * The path may contain parameters, e.g. Special:Name/Params
197 * Extracts the special page name and call the execute method, passing the parameters
199 * @param $title should be a title object
201 function executePath( &$title ) {
202 global $wgSpecialPages, $wgOut, $wgTitle;
204 $bits = split( "/", $title->getDBkey(), 2 );
206 if( empty( $bits[1] ) ) {
212 $page =& SpecialPage
::getPage( $name );
213 if ( is_null( $page ) ) {
214 $wgOut->setArticleRelated( false );
215 $wgOut->setRobotpolicy( "noindex,follow" );
216 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
219 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
224 $page->execute( $par );
229 * Default constructor for special pages
230 * Derivative classes should call this from their constructor
231 * Note that if the user does not have the required level, an error message will
232 * be displayed by the default execute() method, without the global function ever
235 * If you override execute(), you can recover the default behaviour with userCanExecute()
236 * and displayRestrictionError()
238 * @param string $name Name of the special page, as seen in links and URLs
239 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
240 * @param boolean $listed Whether the page is listed in Special:Specialpages
241 * @param string $function Function called by execute(). By default it is constructed from $name
242 * @param string $file File which is included by execute(). It is also constructed from $name by default
244 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
245 $this->mName
= $name;
246 $this->mRestriction
= $restriction;
247 $this->mListed
= $listed;
248 if ( $function == false ) {
249 $this->mFunction
= 'wfSpecial'.$name;
251 $this->mFunction
= $function;
253 if ( $file === 'default' ) {
254 $this->mFile
= "Special{$name}.php";
256 $this->mFile
= $file;
260 # Accessor functions, see the descriptions of the associated variables above
261 function getName() { return $this->mName
; }
262 function getRestriction() { return $this->mRestriction
; }
263 function isListed() { return $this->mListed
; }
266 * Checks if the given user (identified by an object) can execute this
267 * special page (as defined by $mRestriction)
269 function userCanExecute( &$user ) {
270 if ( $this->mRestriction
== "" ) {
273 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
282 * Output an error message telling the user what access level they have to have
284 function displayRestrictionError() {
286 if ( $this->mRestriction
== "developer" ) {
287 $wgOut->developerRequired();
289 $wgOut->sysopRequired();
294 * Sets headers - this should be called from the execute() method of all derived classes!
296 function setHeaders() {
298 $wgOut->setArticleRelated( false );
299 $wgOut->setRobotPolicy( "noindex,follow" );
300 $wgOut->setPageTitle( $this->getDescription() );
304 * Default execute method
305 * Checks user permissions, calls the function given in mFunction
307 function execute( $par ) {
308 global $wgUser, $wgOut, $wgTitle;
312 if ( $this->userCanExecute( $wgUser ) ) {
313 if ( $this->mFile
) {
314 require_once( $this->mFile
);
316 $func = $this->mFunction
;
319 $this->displayRestrictionError();
323 # Returns the name that goes in the <h1> in the special page itself, and also the name that
324 # will be listed in Special:Specialpages
326 # Derived classes can override this, but usually it is easier to keep the default behaviour.
327 # Messages can be added at run-time, see MessageCache.php
328 function getDescription() {
329 return wfMsg( strtolower( $this->mName
) );
333 * Get a self-referential title object
335 function getTitle() {
336 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
340 * Set whether this page is listed in Special:Specialpages, at run-time
342 function setListed( $listed ) {
343 return wfSetVar( $this->mListed
, $listed );
348 * Shortcut to construct a special page which is unlisted by default
351 class UnlistedSpecialPage
extends SpecialPage
353 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
354 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );