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, $wgSpecialPageRedirects;
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 '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' ),
52 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
53 'Shortpages' => new SpecialPage( 'Shortpages' ),
54 'Longpages' => new SpecialPage( 'Longpages' ),
55 'Newpages' => new SpecialPage( 'Newpages' ),
56 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
57 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
58 'Allpages' => new SpecialPage( 'Allpages' ),
59 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
60 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
61 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
62 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
63 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
64 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
65 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
66 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
67 'Booksources' => new SpecialPage( 'Booksources' ),
68 'Categories' => new SpecialPage( 'Categories' ),
69 'Export' => new SpecialPage( 'Export' ),
70 'Version' => new SpecialPage( 'Version' ),
71 'Allmessages' => new SpecialPage( 'Allmessages' ),
72 'Log' => new SpecialPage( 'Log' ),
73 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
74 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
75 "Import" => new SpecialPage( "Import", 'import' ),
76 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
77 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
78 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
79 'Groups' => new SpecialPage( 'Groups' ),
83 * Sometimes the functionality of a specialpage is merged into the
84 * functionality of another or its simply renamed, this allows us to redirect
85 * the request to the proper place.
89 $wgSpecialPageRedirects = array(
90 'Listadmins' => 'Listusers'
93 global $wgUseValidation ;
94 if ( $wgUseValidation )
95 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
97 global $wgDisableCounters;
98 if( !$wgDisableCounters ) {
99 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
102 global $wgDisableInternalSearch;
103 if( !$wgDisableInternalSearch ) {
104 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
107 global $wgEmailAuthentication;
108 if( $wgEmailAuthentication ) {
109 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
113 * Parent special page class, also static functions for handling the special
123 * The name of the class, used in the URL.
124 * Also used for the default <h1> heading, @see getDescription()
128 * Minimum user level required to access this page, or "" for anyone.
129 * Also used to categorise the pages in Special:Specialpages
133 * Listed in Special:Specialpages?
137 * Function name called by the default execute()
141 * File which needs to be included before the function above can be called
147 * Add a page to the list of valid special pages
148 * $obj->execute() must send HTML to $wgOut then return
149 * Use this for a special page extension
152 function addPage( &$obj ) {
153 global $wgSpecialPages;
154 $wgSpecialPages[$obj->mName
] = $obj;
158 * Remove a special page from the list
159 * Occasionally used to disable expensive or dangerous special pages
162 function removePage( $name ) {
163 global $wgSpecialPages;
164 unset( $wgSpecialPages[$name] );
168 * Find the object with a given name and return it (or NULL)
170 * @param string $name
172 function &getPage( $name ) {
173 global $wgSpecialPages;
174 if ( array_key_exists( $name, $wgSpecialPages ) ) {
175 return $wgSpecialPages[$name];
185 * @return mixed string if the redirect exists, otherwise NULL
187 function &getRedirect( $name ) {
188 global $wgSpecialPageRedirects;
189 if ( array_key_exists( $name, $wgSpecialPageRedirects ) ) {
190 return $wgSpecialPageRedirects[$name];
197 * Return categorised listable special pages
198 * Returns a 2d array where the first index is the restriction name
201 function getPages() {
202 global $wgSpecialPages;
206 'developer' => array()
209 foreach ( $wgSpecialPages as $name => $page ) {
210 if ( $page->isListed() ) {
211 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
218 * Execute a special page path.
219 * The path may contain parameters, e.g. Special:Name/Params
220 * Extracts the special page name and call the execute method, passing the parameters
222 * @param $title should be a title object
224 function executePath( &$title ) {
225 global $wgSpecialPages, $wgOut, $wgTitle;
227 $bits = split( "/", $title->getDBkey(), 2 );
229 if( !isset( $bits[1] ) ) { // bug 2087
235 $page =& SpecialPage
::getPage( $name );
236 if ( is_null( $page ) ) {
237 $redir =& SpecialPage
::getRedirect( $name );
238 if ( isset( $redir ) ) {
239 $t = Title
::makeTitle( NS_SPECIAL
, "Listusers" );
240 $wgOut->redirect ($t->getFullURL());
242 $wgOut->setArticleRelated( false );
243 $wgOut->setRobotpolicy( "noindex,follow" );
244 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
248 $wgTitle = Title
::makeTitle( NS_SPECIAL
, $name );
253 $page->execute( $par );
258 * Default constructor for special pages
259 * Derivative classes should call this from their constructor
260 * Note that if the user does not have the required level, an error message will
261 * be displayed by the default execute() method, without the global function ever
264 * If you override execute(), you can recover the default behaviour with userCanExecute()
265 * and displayRestrictionError()
267 * @param string $name Name of the special page, as seen in links and URLs
268 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
269 * @param boolean $listed Whether the page is listed in Special:Specialpages
270 * @param string $function Function called by execute(). By default it is constructed from $name
271 * @param string $file File which is included by execute(). It is also constructed from $name by default
273 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
274 $this->mName
= $name;
275 $this->mRestriction
= $restriction;
276 $this->mListed
= $listed;
277 if ( $function == false ) {
278 $this->mFunction
= 'wfSpecial'.$name;
280 $this->mFunction
= $function;
282 if ( $file === 'default' ) {
283 $this->mFile
= "Special{$name}.php";
285 $this->mFile
= $file;
289 # Accessor functions, see the descriptions of the associated variables above
290 function getName() { return $this->mName
; }
291 function getRestriction() { return $this->mRestriction
; }
292 function isListed() { return $this->mListed
; }
293 function getFile() { return $this->mFile
; }
296 * Checks if the given user (identified by an object) can execute this
297 * special page (as defined by $mRestriction)
299 function userCanExecute( &$user ) {
300 if ( $this->mRestriction
== "" ) {
303 if ( in_array( $this->mRestriction
, $user->getRights() ) ) {
312 * Output an error message telling the user what access level they have to have
314 function displayRestrictionError() {
316 if ( $this->mRestriction
== "developer" ) {
317 $wgOut->developerRequired();
319 $wgOut->sysopRequired();
324 * Sets headers - this should be called from the execute() method of all derived classes!
326 function setHeaders() {
328 $wgOut->setArticleRelated( false );
329 $wgOut->setRobotPolicy( "noindex,follow" );
330 $wgOut->setPageTitle( $this->getDescription() );
334 * Default execute method
335 * Checks user permissions, calls the function given in mFunction
337 function execute( $par ) {
338 global $wgUser, $wgOut, $wgTitle;
342 if ( $this->userCanExecute( $wgUser ) ) {
343 if ( $this->mFile
) {
344 require_once( $this->mFile
);
346 $func = $this->mFunction
;
349 $this->displayRestrictionError();
353 # Returns the name that goes in the <h1> in the special page itself, and also the name that
354 # will be listed in Special:Specialpages
356 # Derived classes can override this, but usually it is easier to keep the default behaviour.
357 # Messages can be added at run-time, see MessageCache.php
358 function getDescription() {
359 return wfMsg( strtolower( $this->mName
) );
363 * Get a self-referential title object
365 function getTitle() {
366 return Title
::makeTitle( NS_SPECIAL
, $this->mName
);
370 * Set whether this page is listed in Special:Specialpages, at run-time
372 function setListed( $listed ) {
373 return wfSetVar( $this->mListed
, $listed );
378 * Shortcut to construct a special page which is unlisted by default
381 class UnlistedSpecialPage
extends SpecialPage
383 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
384 SpecialPage
::SpecialPage( $name, $restriction, false, $function, $file );