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.
20 global $wgSpecialPages;
25 $wgSpecialPages = array(
26 'DoubleRedirects' => new UnlistedSpecialPage ( 'DoubleRedirects' ),
27 'BrokenRedirects' => new UnlistedSpecialPage ( 'BrokenRedirects' ),
28 'Disambiguations' => new UnlistedSpecialPage ( 'Disambiguations' ),
30 'Userlogin' => new SpecialPage( 'Userlogin' ),
31 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
32 'Preferences' => new SpecialPage( 'Preferences' ),
33 'Watchlist' => new SpecialPage( 'Watchlist' ),
35 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
36 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
37 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
39 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
40 'Upload' => new SpecialPage( 'Upload' ),
41 'Imagelist' => new SpecialPage( 'Imagelist' ),
42 'Newimages' => new SpecialPage( 'Newimages' ),
43 'Listusers' => new SpecialPage( 'Listusers' ),
44 'Listadmins' => new UnlistedSpecialPage( 'Listadmins' ),
45 'Statistics' => new SpecialPage( 'Statistics' ),
46 'Randompage' => new SpecialPage( 'Randompage' ),
47 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
48 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
49 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
50 'Unusedimages' => new SpecialPage( 'Unusedimages' )
53 global $wgDisableCounters;
54 if( !$wgDisableCounters ) {
55 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
60 $wgSpecialPages['Data'] = new SpecialPage( 'Data' );
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 'Maintenance' => new SpecialPage( 'Maintenance' ),
78 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
79 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
80 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
81 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
82 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
83 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
84 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
85 'Geo' => new UnlistedSpecialPage( 'Geo' ),
86 'Validate' => new UnlistedSpecialPage( 'Validate' ),
87 'Booksources' => new SpecialPage( 'Booksources' ),
88 'Categories' => new SpecialPage( 'Categories' ),
89 'Export' => new SpecialPage( 'Export' ),
90 'Version' => new SpecialPage( 'Version' ),
91 'Allmessages' => new SpecialPage( 'Allmessages' ),
92 'Log' => new SpecialPage( 'Log' ),
93 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
94 'Asksql' => new SpecialPage( 'Asksql', 'asksql' ),
95 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
96 // Makesysop is obsolete, replaced by Special:Userlevels [av]
97 # 'Makesysop' => new SpecialPage( 'Makesysop', 'userrights' ),
99 # Special:Import is half-written
100 # "Import" => new SpecialPage( "Import", "sysop" ),
101 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
102 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
103 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
104 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
105 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
109 * Parent special page class, also static functions for handling the special
119 * The name of the class, used in the URL.
120 * Also used for the default <h1> heading, @see getDescription()
124 * Minimum user level required to access this page, or "" for anyone.
125 * Also used to categorise the pages in Special:Specialpages
129 * Listed in Special:Specialpages?
133 * Function name called by the default execute()
137 * File which needs to be included before the function above can be called
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];
178 * Return categorised listable special pages
179 * Returns a 2d array where the first index is the restriction name
182 function getPages() {
183 global $wgSpecialPages;
187 'developer' => array()
190 foreach ( $wgSpecialPages as $name => $page ) {
191 if ( $page->isListed() ) {
192 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
199 * Execute a special page path.
200 * The path may contain parameters, e.g. Special:Name/Params
201 * Extracts the special page name and call the execute method, passing the parameters
203 * @param $title should be a title object
205 function executePath( &$title ) {
206 global $wgSpecialPages, $wgOut, $wgTitle;
208 $bits = split( "/", $title->getDBkey(), 2 );
210 if( empty( $bits[1] ) ) {
216 $page =& SpecialPage
::getPage( $name );
217 if ( is_null( $page ) ) {
218 $wgOut->setArticleRelated( false );
219 $wgOut->setRobotpolicy( "noindex,follow" );
220 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
223 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
228 $page->execute( $par );
233 * Default constructor for special pages
234 * Derivative classes should call this from their constructor
235 * Note that if the user does not have the required level, an error message will
236 * be displayed by the default execute() method, without the global function ever
239 * If you override execute(), you can recover the default behaviour with userCanExecute()
240 * and displayRestrictionError()
242 * @param string $name Name of the special page, as seen in links and URLs
243 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
244 * @param boolean $listed Whether the page is listed in Special:Specialpages
245 * @param string $function Function called by execute(). By default it is constructed from $name
246 * @param string $file File which is included by execute(). It is also constructed from $name by default
248 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
249 $this->mName
= $name;
250 $this->mRestriction
= $restriction;
251 $this->mListed
= $listed;
252 if ( $function == false ) {
253 $this->mFunction
= 'wfSpecial'.$name;
255 $this->mFunction
= $function;
257 if ( $file === 'default' ) {
258 $this->mFile
= "Special{$name}.php";
260 $this->mFile
= $file;
264 # Accessor functions, see the descriptions of the associated variables above
265 function getName() { return $this->mName
; }
266 function getRestriction() { return $this->mRestriction
; }
267 function isListed() { return $this->mListed
; }
270 * Checks if the given user (identified by an object) can execute this
271 * special page (as defined by $mRestriction)
273 function userCanExecute( &$user ) {
274 if ( $this->mRestriction
== "" ) {
277 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
286 * Output an error message telling the user what access level they have to have
288 function displayRestrictionError() {
290 if ( $this->mRestriction
== "developer" ) {
291 $wgOut->developerRequired();
293 $wgOut->sysopRequired();
298 * Sets headers - this should be called from the execute() method of all derived classes!
300 function setHeaders() {
302 $wgOut->setArticleRelated( false );
303 $wgOut->setRobotPolicy( "noindex,follow" );
304 $wgOut->setPageTitle( $this->getDescription() );
308 * Default execute method
309 * Checks user permissions, calls the function given in mFunction
311 function execute( $par ) {
312 global $wgUser, $wgOut, $wgTitle;
316 if ( $this->userCanExecute( $wgUser ) ) {
317 if ( $this->mFile
) {
318 require_once( $this->mFile
);
320 $func = $this->mFunction
;
323 $this->displayRestrictionError();
327 # Returns the name that goes in the <h1> in the special page itself, and also the name that
328 # will be listed in Special:Specialpages
330 # Derived classes can override this, but usually it is easier to keep the default behaviour.
331 # Messages can be added at run-time, see MessageCache.php
332 function getDescription() {
333 return wfMsg( strtolower( $this->mName
) );
337 * Get a self-referential title object
339 function getTitle() {
340 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
344 * Set whether this page is listed in Special:Specialpages, at run-time
346 function setListed( $listed ) {
347 return wfSetVar( $this->mListed
, $listed );
352 * Shortcut to construct a special page which is unlisted by default
355 class UnlistedSpecialPage
extends SpecialPage
357 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
358 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );