Various live patches ported from REL1_4
[mediawiki.git] / includes / SpecialPage.php
blob5cc1e84231641a06ce8d418eb4ae24d18fffc222
1 <?php
2 /**
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.
14 * @package MediaWiki
15 * @subpackage SpecialPage
18 /**
21 global $wgSpecialPages;
23 /**
24 * @access private
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 "Import" => new SpecialPage( "Import", 'import' ),
93 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
94 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
95 'Sitesettings' => new SpecialPage( 'Sitesettings', 'siteadmin' ),
96 'Userlevels' => new SpecialPage( 'Userlevels', 'userrights' ),
97 'Grouplevels' => new SpecialPage( 'Grouplevels', 'grouprights' ),
98 ));
101 * Parent special page class, also static functions for handling the special
102 * page list
103 * @package MediaWiki
105 class SpecialPage
107 /**#@+
108 * @access private
111 * The name of the class, used in the URL.
112 * Also used for the default <h1> heading, @see getDescription()
114 var $mName;
116 * Minimum user level required to access this page, or "" for anyone.
117 * Also used to categorise the pages in Special:Specialpages
119 var $mRestriction;
121 * Listed in Special:Specialpages?
123 var $mListed;
125 * Function name called by the default execute()
127 var $mFunction;
129 * File which needs to be included before the function above can be called
131 var $mFile;
132 /**#@-*/
135 * Add a page to the list of valid special pages
136 * $obj->execute() must send HTML to $wgOut then return
137 * Use this for a special page extension
138 * @static
140 function addPage( &$obj ) {
141 global $wgSpecialPages;
142 $wgSpecialPages[$obj->mName] = $obj;
146 * Remove a special page from the list
147 * Occasionally used to disable expensive or dangerous special pages
148 * @static
150 function removePage( $name ) {
151 global $wgSpecialPages;
152 unset( $wgSpecialPages[$name] );
156 * Find the object with a given name and return it (or NULL)
157 * @static
158 * @param string $name
160 function &getPage( $name ) {
161 global $wgSpecialPages;
162 if ( array_key_exists( $name, $wgSpecialPages ) ) {
163 return $wgSpecialPages[$name];
164 } else {
165 return NULL;
170 * Return categorised listable special pages
171 * Returns a 2d array where the first index is the restriction name
172 * @static
174 function getPages() {
175 global $wgSpecialPages;
176 $pages = array(
177 '' => array(),
178 'sysop' => array(),
179 'developer' => array()
182 foreach ( $wgSpecialPages as $name => $page ) {
183 if ( $page->isListed() ) {
184 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
187 return $pages;
191 * Execute a special page path.
192 * The path may contain parameters, e.g. Special:Name/Params
193 * Extracts the special page name and call the execute method, passing the parameters
195 * @param $title should be a title object
197 function executePath( &$title ) {
198 global $wgSpecialPages, $wgOut, $wgTitle;
200 $bits = split( "/", $title->getDBkey(), 2 );
201 $name = $bits[0];
202 if( empty( $bits[1] ) ) {
203 $par = NULL;
204 } else {
205 $par = $bits[1];
208 $page =& SpecialPage::getPage( $name );
209 if ( is_null( $page ) ) {
210 $wgOut->setArticleRelated( false );
211 $wgOut->setRobotpolicy( "noindex,follow" );
212 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
213 } else {
214 if($par !== NULL) {
215 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
216 } else {
217 $wgTitle = $title;
220 $page->execute( $par );
225 * Default constructor for special pages
226 * Derivative classes should call this from their constructor
227 * Note that if the user does not have the required level, an error message will
228 * be displayed by the default execute() method, without the global function ever
229 * being called.
231 * If you override execute(), you can recover the default behaviour with userCanExecute()
232 * and displayRestrictionError()
234 * @param string $name Name of the special page, as seen in links and URLs
235 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
236 * @param boolean $listed Whether the page is listed in Special:Specialpages
237 * @param string $function Function called by execute(). By default it is constructed from $name
238 * @param string $file File which is included by execute(). It is also constructed from $name by default
240 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default' ) {
241 $this->mName = $name;
242 $this->mRestriction = $restriction;
243 $this->mListed = $listed;
244 if ( $function == false ) {
245 $this->mFunction = 'wfSpecial'.$name;
246 } else {
247 $this->mFunction = $function;
249 if ( $file === 'default' ) {
250 $this->mFile = "Special{$name}.php";
251 } else {
252 $this->mFile = $file;
256 # Accessor functions, see the descriptions of the associated variables above
257 function getName() { return $this->mName; }
258 function getRestriction() { return $this->mRestriction; }
259 function isListed() { return $this->mListed; }
262 * Checks if the given user (identified by an object) can execute this
263 * special page (as defined by $mRestriction)
265 function userCanExecute( &$user ) {
266 if ( $this->mRestriction == "" ) {
267 return true;
268 } else {
269 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
270 return true;
271 } else {
272 return false;
278 * Output an error message telling the user what access level they have to have
280 function displayRestrictionError() {
281 global $wgOut;
282 if ( $this->mRestriction == "developer" ) {
283 $wgOut->developerRequired();
284 } else {
285 $wgOut->sysopRequired();
290 * Sets headers - this should be called from the execute() method of all derived classes!
292 function setHeaders() {
293 global $wgOut;
294 $wgOut->setArticleRelated( false );
295 $wgOut->setRobotPolicy( "noindex,follow" );
296 $wgOut->setPageTitle( $this->getDescription() );
300 * Default execute method
301 * Checks user permissions, calls the function given in mFunction
303 function execute( $par ) {
304 global $wgUser, $wgOut, $wgTitle;
306 $this->setHeaders();
308 if ( $this->userCanExecute( $wgUser ) ) {
309 if ( $this->mFile ) {
310 require_once( $this->mFile );
312 $func = $this->mFunction;
313 $func( $par );
314 } else {
315 $this->displayRestrictionError();
319 # Returns the name that goes in the <h1> in the special page itself, and also the name that
320 # will be listed in Special:Specialpages
322 # Derived classes can override this, but usually it is easier to keep the default behaviour.
323 # Messages can be added at run-time, see MessageCache.php
324 function getDescription() {
325 return wfMsg( strtolower( $this->mName ) );
329 * Get a self-referential title object
331 function getTitle() {
332 return Title::makeTitle( NS_SPECIAL, $this->mName );
336 * Set whether this page is listed in Special:Specialpages, at run-time
338 function setListed( $listed ) {
339 return wfSetVar( $this->mListed, $listed );
344 * Shortcut to construct a special page which is unlisted by default
345 * @package MediaWiki
347 class UnlistedSpecialPage extends SpecialPage
349 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
350 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );