Sample files for noobs like me.
[mediawiki.git] / includes / SpecialPage.php
blob64bf9180493d13b1f3755921c754989d769ed561
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
19 /**
22 global $wgSpecialPages, $wgUser;
24 /**
25 * @access private
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 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
38 'Upload' => new SpecialPage( 'Upload' ),
39 'Imagelist' => new SpecialPage( 'Imagelist' ),
40 'Newimages' => new SpecialPage( 'Newimages' ),
41 'Listusers' => new SpecialPage( 'Listusers' ),
42 'Statistics' => new SpecialPage( 'Statistics' ),
43 'Random' => new SpecialPage( 'Randompage' ),
44 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
45 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
46 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
47 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
48 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
49 'Wantedpages' => new SpecialPage( 'Wantedpages' ),
50 'Shortpages' => new SpecialPage( 'Shortpages' ),
51 'Longpages' => new SpecialPage( 'Longpages' ),
52 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
53 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
54 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
55 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
56 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
57 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
58 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
59 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
60 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
61 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
62 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
63 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
64 'Booksources' => new SpecialPage( 'Booksources' ),
65 'Categories' => new SpecialPage( 'Categories' ),
66 'Export' => new SpecialPage( 'Export' ),
67 'Version' => new SpecialPage( 'Version' ),
68 'Allmessages' => new SpecialPage( 'Allmessages' ),
69 'Log' => new SpecialPage( 'Log' ),
70 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
71 'Undelete' => new SpecialPage( 'Undelete', 'delete' ),
72 "Import" => new SpecialPage( "Import", 'import' ),
73 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
74 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
75 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
78 global $wgUseValidation ;
79 if ( $wgUseValidation )
80 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
82 global $wgDisableCounters;
83 if( !$wgDisableCounters ) {
84 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
87 global $wgDisableInternalSearch;
88 if( !$wgDisableInternalSearch ) {
89 $wgSpecialPages['Search'] = new UnlistedSpecialPage( 'Search' );
92 global $wgEmailAuthentication;
93 if( $wgEmailAuthentication ) {
94 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
97 /**
98 * Parent special page class, also static functions for handling the special
99 * page list
100 * @package MediaWiki
102 class SpecialPage
104 /**#@+
105 * @access private
108 * The name of the class, used in the URL.
109 * Also used for the default <h1> heading, @see getDescription()
111 var $mName;
113 * Minimum user level required to access this page, or "" for anyone.
114 * Also used to categorise the pages in Special:Specialpages
116 var $mRestriction;
118 * Listed in Special:Specialpages?
120 var $mListed;
122 * Function name called by the default execute()
124 var $mFunction;
126 * File which needs to be included before the function above can be called
128 var $mFile;
130 * Whether or not this special page is being included from an article
132 var $mIncluding;
134 * Whether the special page can be included in an article
136 var $mIncludable;
139 /**#@-*/
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
146 * @static
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
156 * @static
158 function removePage( $name ) {
159 global $wgSpecialPages;
160 unset( $wgSpecialPages[$name] );
164 * Find the object with a given name and return it (or NULL)
165 * @static
166 * @param string $name
168 function &getPage( $name ) {
169 global $wgSpecialPages;
170 if ( array_key_exists( $name, $wgSpecialPages ) ) {
171 return $wgSpecialPages[$name];
172 } else {
173 return NULL;
178 * @static
179 * @param string $name
180 * @return mixed Title object if the redirect exists, otherwise NULL
182 function &getRedirect( $name ) {
183 global $wgUser;
184 switch ( $name ) {
185 case 'Mypage':
186 return Title::makeTitle( NS_USER, $wgUser->getName() );
187 case 'Mytalk':
188 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
189 case 'Mycontributions':
190 return Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() );
191 case 'Listadmins':
192 return Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ); # @bug 2832
193 case 'Randompage':
194 return Title::makeTitle( NS_SPECIAL, 'Random' );
195 default:
196 return NULL;
201 * Return categorised listable special pages
202 * Returns a 2d array where the first index is the restriction name
203 * @static
205 function getPages() {
206 global $wgSpecialPages;
207 $pages = array(
208 '' => array(),
209 'sysop' => array(),
210 'developer' => array()
213 foreach ( $wgSpecialPages as $name => $page ) {
214 if ( $page->isListed() ) {
215 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
218 return $pages;
222 * Execute a special page path.
223 * The path may contain parameters, e.g. Special:Name/Params
224 * Extracts the special page name and call the execute method, passing the parameters
226 * Returns a title object if the page is redirected, false if there was no such special
227 * page, and true if it was successful.
229 * @param $title a title object
230 * @param $including output is being captured for use in {{special:whatever}}
232 function executePath( &$title, $including = false ) {
233 global $wgSpecialPages, $wgOut, $wgTitle;
235 $bits = split( "/", $title->getDBkey(), 2 );
236 $name = $bits[0];
237 if( !isset( $bits[1] ) ) { // bug 2087
238 $par = NULL;
239 } else {
240 $par = $bits[1];
243 $page =& SpecialPage::getPage( $name );
244 if ( is_null( $page ) ) {
245 if ( $including ) {
246 return false;
247 } else {
248 $redir =& SpecialPage::getRedirect( $name );
249 if ( isset( $redir ) ) {
250 if ( isset( $par ) )
251 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
252 else
253 $wgOut->redirect( $redir->getFullURL() );
254 $retVal = $redir;
255 } else {
256 $wgOut->setArticleRelated( false );
257 $wgOut->setRobotpolicy( "noindex,follow" );
258 $wgOut->errorpage( "nosuchspecialpage", "nospecialpagetext" );
259 $retVal = false;
262 } else {
263 if ( $including && !$page->includable() ) {
264 return false;
266 if($par !== NULL) {
267 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
268 } else {
269 $wgTitle = $title;
271 $page->including( $including );
273 $page->execute( $par );
274 $retVal = true;
276 return $retVal;
280 * Just like executePath() except it returns the HTML instead of outputting it
281 * Returns false if there was no such special page, or a title object if it was
282 * a redirect.
283 * @static
285 function capturePath( &$title ) {
286 global $wgOut, $wgTitle;
288 $oldTitle = $wgTitle;
289 $oldOut = $wgOut;
290 $wgOut = new OutputPage;
292 $ret = SpecialPage::executePath( $title, true );
293 if ( $ret === true ) {
294 $ret = $wgOut->getHTML();
296 $wgTitle = $oldTitle;
297 $wgOut = $oldOut;
298 return $ret;
302 * Default constructor for special pages
303 * Derivative classes should call this from their constructor
304 * Note that if the user does not have the required level, an error message will
305 * be displayed by the default execute() method, without the global function ever
306 * being called.
308 * If you override execute(), you can recover the default behaviour with userCanExecute()
309 * and displayRestrictionError()
311 * @param string $name Name of the special page, as seen in links and URLs
312 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
313 * @param boolean $listed Whether the page is listed in Special:Specialpages
314 * @param string $function Function called by execute(). By default it is constructed from $name
315 * @param string $file File which is included by execute(). It is also constructed from $name by default
317 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
318 $this->mName = $name;
319 $this->mRestriction = $restriction;
320 $this->mListed = $listed;
321 $this->mIncludable = $includable;
322 if ( $function == false ) {
323 $this->mFunction = 'wfSpecial'.$name;
324 } else {
325 $this->mFunction = $function;
327 if ( $file === 'default' ) {
328 $this->mFile = "Special{$name}.php";
329 } else {
330 $this->mFile = $file;
334 # Accessor functions, see the descriptions of the associated variables above
335 function getName() { return $this->mName; }
336 function getRestriction() { return $this->mRestriction; }
337 function isListed() { return $this->mListed; }
338 function getFile() { return $this->mFile; }
339 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
340 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
343 * Checks if the given user (identified by an object) can execute this
344 * special page (as defined by $mRestriction)
346 function userCanExecute( &$user ) {
347 if ( $this->mRestriction == "" ) {
348 return true;
349 } else {
350 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
351 return true;
352 } else {
353 return false;
359 * Output an error message telling the user what access level they have to have
361 function displayRestrictionError() {
362 global $wgOut;
363 $wgOut->permissionRequired( $this->mRestriction );
367 * Sets headers - this should be called from the execute() method of all derived classes!
369 function setHeaders() {
370 global $wgOut;
371 $wgOut->setArticleRelated( false );
372 $wgOut->setRobotPolicy( "noindex,follow" );
373 $wgOut->setPageTitle( $this->getDescription() );
377 * Default execute method
378 * Checks user permissions, calls the function given in mFunction
380 function execute( $par ) {
381 global $wgUser, $wgOut, $wgTitle;
383 $this->setHeaders();
385 if ( $this->userCanExecute( $wgUser ) ) {
386 if ( $this->mFile ) {
387 require_once( $this->mFile );
389 $func = $this->mFunction;
390 $func( $par, $this );
391 } else {
392 $this->displayRestrictionError();
396 # Returns the name that goes in the <h1> in the special page itself, and also the name that
397 # will be listed in Special:Specialpages
399 # Derived classes can override this, but usually it is easier to keep the default behaviour.
400 # Messages can be added at run-time, see MessageCache.php
401 function getDescription() {
402 return wfMsg( strtolower( $this->mName ) );
406 * Get a self-referential title object
408 function getTitle() {
409 return Title::makeTitle( NS_SPECIAL, $this->mName );
413 * Set whether this page is listed in Special:Specialpages, at run-time
415 function setListed( $listed ) {
416 return wfSetVar( $this->mListed, $listed );
422 * Shortcut to construct a special page which is unlisted by default
423 * @package MediaWiki
425 class UnlistedSpecialPage extends SpecialPage
427 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
428 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
433 * Shortcut to construct an includable special page
434 * @package MediaWiki
436 class IncludableSpecialPage extends SpecialPage
438 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
439 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );