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 global $wgSpecialPages;
27 $wgSpecialPages = array(
28 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
29 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
30 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
32 'Userlogin' => new SpecialPage( 'Userlogin' ),
33 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
34 'Preferences' => new SpecialPage( 'Preferences' ),
35 'Watchlist' => new SpecialPage( 'Watchlist' ),
37 'Mytalk' => new UnlistedSpecialPage( 'Mytalk'),
38 'Mycontributions' => new UnlistedSpecialPage( 'Mycontributions'),
39 'Mypage' => new UnlistedSpecialPage( 'Mypage'),
41 'Recentchanges' => new SpecialPage( 'Recentchanges' ),
42 'Upload' => new SpecialPage( 'Upload' ),
43 'Imagelist' => new SpecialPage( 'Imagelist' ),
44 'Newimages' => new SpecialPage( 'Newimages' ),
45 'Listusers' => new SpecialPage( 'Listusers' ),
46 'Listadmins' => new UnlistedSpecialPage( 'Listadmins' ),
47 'Statistics' => new SpecialPage( 'Statistics' ),
48 'Randompage' => new SpecialPage( 'Randompage' ),
49 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
50 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
51 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
52 'Unusedimages' => new SpecialPage( 'Unusedimages' )
55 global $wgUseValidation ;
56 if ( $wgUseValidation )
57 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
59 global $wgDisableCounters;
60 if( !$wgDisableCounters ) {
61 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
64 global $wgDisableInternalSearch;
65 if( !$wgDisableInternalSearch ) {
66 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
69 $wgSpecialPages = array_merge($wgSpecialPages, array (
70 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
71 'Shortpages' => new SpecialPage( 'Shortpages' ),
72 'Longpages' => new SpecialPage( 'Longpages' ),
73 'Newpages' => new SpecialPage( 'Newpages' ),
74 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
75 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
76 'Allpages' => new SpecialPage( 'Allpages' ),
77 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
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 'Booksources' => new SpecialPage( 'Booksources' ),
86 'Categories' => new SpecialPage( 'Categories' ),
87 'Export' => new SpecialPage( 'Export' ),
88 'Version' => new SpecialPage( 'Version' ),
89 'Allmessages' => new SpecialPage( 'Allmessages' ),
90 'Log' => new SpecialPage( 'Log' ),
91 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
92 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
93 "Import" => new SpecialPage( "Import", 'import' ),
94 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
95 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
96 # 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
97 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
98 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
102 * Parent special page class, also static functions for handling the special
112 * The name of the class, used in the URL.
113 * Also used for the default <h1> heading, @see getDescription()
117 * Minimum user level required to access this page, or "" for anyone.
118 * Also used to categorise the pages in Special:Specialpages
122 * Listed in Special:Specialpages?
126 * Function name called by the default execute()
130 * File which needs to be included before the function above can be called
136 * Add a page to the list of valid special pages
137 * $obj->execute() must send HTML to $wgOut then return
138 * Use this for a special page extension
141 function addPage( &$obj ) {
142 global $wgSpecialPages;
143 $wgSpecialPages[$obj->mName
] = $obj;
147 * Remove a special page from the list
148 * Occasionally used to disable expensive or dangerous special pages
151 function removePage( $name ) {
152 global $wgSpecialPages;
153 unset( $wgSpecialPages[$name] );
157 * Find the object with a given name and return it (or NULL)
159 * @param string $name
161 function &getPage( $name ) {
162 global $wgSpecialPages;
163 if ( array_key_exists( $name, $wgSpecialPages ) ) {
164 return $wgSpecialPages[$name];
171 * Return categorised listable special pages
172 * Returns a 2d array where the first index is the restriction name
175 function getPages() {
176 global $wgSpecialPages;
180 'developer' => array()
183 foreach ( $wgSpecialPages as $name => $page ) {
184 if ( $page->isListed() ) {
185 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
192 * Execute a special page path.
193 * The path may contain parameters, e.g. Special:Name/Params
194 * Extracts the special page name and call the execute method, passing the parameters
196 * @param $title should be a title object
198 function executePath( &$title ) {
199 global $wgSpecialPages, $wgOut, $wgTitle;
201 $bits = split( "/", $title->getDBkey(), 2 );
203 if( empty( $bits[1] ) ) {
209 $page =& SpecialPage
::getPage( $name );
210 if ( is_null( $page ) ) {
211 $wgOut->setArticleRelated( false );
212 $wgOut->setRobotpolicy( "noindex,follow" );
213 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
216 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
221 $page->execute( $par );
226 * Default constructor for special pages
227 * Derivative classes should call this from their constructor
228 * Note that if the user does not have the required level, an error message will
229 * be displayed by the default execute() method, without the global function ever
232 * If you override execute(), you can recover the default behaviour with userCanExecute()
233 * and displayRestrictionError()
235 * @param string $name Name of the special page, as seen in links and URLs
236 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
237 * @param boolean $listed Whether the page is listed in Special:Specialpages
238 * @param string $function Function called by execute(). By default it is constructed from $name
239 * @param string $file File which is included by execute(). It is also constructed from $name by default
241 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
242 $this->mName
= $name;
243 $this->mRestriction
= $restriction;
244 $this->mListed
= $listed;
245 if ( $function == false ) {
246 $this->mFunction
= 'wfSpecial'.$name;
248 $this->mFunction
= $function;
250 if ( $file === 'default' ) {
251 $this->mFile
= "Special{$name}.php";
253 $this->mFile
= $file;
257 # Accessor functions, see the descriptions of the associated variables above
258 function getName() { return $this->mName
; }
259 function getRestriction() { return $this->mRestriction
; }
260 function isListed() { return $this->mListed
; }
261 function getFile() { return $this->mFile
; }
264 * Checks if the given user (identified by an object) can execute this
265 * special page (as defined by $mRestriction)
267 function userCanExecute( &$user ) {
268 if ( $this->mRestriction
== "" ) {
271 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
280 * Output an error message telling the user what access level they have to have
282 function displayRestrictionError() {
284 if ( $this->mRestriction
== "developer" ) {
285 $wgOut->developerRequired();
287 $wgOut->sysopRequired();
292 * Sets headers - this should be called from the execute() method of all derived classes!
294 function setHeaders() {
296 $wgOut->setArticleRelated( false );
297 $wgOut->setRobotPolicy( "noindex,follow" );
298 $wgOut->setPageTitle( $this->getDescription() );
302 * Default execute method
303 * Checks user permissions, calls the function given in mFunction
305 function execute( $par ) {
306 global $wgUser, $wgOut, $wgTitle;
310 if ( $this->userCanExecute( $wgUser ) ) {
311 if ( $this->mFile
) {
312 require_once( $this->mFile
);
314 $func = $this->mFunction
;
317 $this->displayRestrictionError();
321 # Returns the name that goes in the <h1> in the special page itself, and also the name that
322 # will be listed in Special:Specialpages
324 # Derived classes can override this, but usually it is easier to keep the default behaviour.
325 # Messages can be added at run-time, see MessageCache.php
326 function getDescription() {
327 return wfMsg( strtolower( $this->mName
) );
331 * Get a self-referential title object
333 function getTitle() {
334 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
338 * Set whether this page is listed in Special:Specialpages, at run-time
340 function setListed( $listed ) {
341 return wfSetVar( $this->mListed
, $listed );
346 * Shortcut to construct a special page which is unlisted by default
349 class UnlistedSpecialPage
extends SpecialPage
351 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
352 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );