* Made a new permission for Special:Unwatchedpages, enabled by default for sysops
[lhc/web/wiklou.git] / includes / SpecialPage.php
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.
10 *
11 * To add a special page at run-time, use SpecialPage::addPage().
12 * DO NOT manipulate this array at run-time.
13 *
14 * @package MediaWiki
15 * @subpackage SpecialPage
16 */
17
18
19 /**
20 * @access private
21 */
22 $wgSpecialPages = array(
23 'DoubleRedirects' => new SpecialPage ( 'DoubleRedirects' ),
24 'BrokenRedirects' => new SpecialPage ( 'BrokenRedirects' ),
25 'Disambiguations' => new SpecialPage ( 'Disambiguations' ),
26
27 'Userlogin' => new SpecialPage( 'Userlogin' ),
28 'Userlogout' => new UnlistedSpecialPage( 'Userlogout' ),
29 'Preferences' => new SpecialPage( 'Preferences' ),
30 'Watchlist' => new SpecialPage( 'Watchlist' ),
31
32 'Recentchanges' => new IncludableSpecialPage( 'Recentchanges' ),
33 'Upload' => new SpecialPage( 'Upload' ),
34 'Imagelist' => new SpecialPage( 'Imagelist' ),
35 'Newimages' => new IncludableSpecialPage( 'Newimages' ),
36 'Listusers' => new SpecialPage( 'Listusers' ),
37 'Statistics' => new SpecialPage( 'Statistics' ),
38 'Random' => new SpecialPage( 'Randompage' ),
39 'Lonelypages' => new SpecialPage( 'Lonelypages' ),
40 'Uncategorizedpages'=> new SpecialPage( 'Uncategorizedpages' ),
41 'Uncategorizedcategories'=> new SpecialPage( 'Uncategorizedcategories' ),
42 'Unusedcategories' => new SpecialPage( 'Unusedcategories' ),
43 'Unusedimages' => new SpecialPage( 'Unusedimages' ),
44 'Wantedpages' => new IncludableSpecialPage( 'Wantedpages' ),
45 'Wantedcategories' => new SpecialPage( 'Wantedcategories' ),
46 'Mostlinked' => new SpecialPage( 'Mostlinked' ),
47 'Mostlinkedcategories' => new SpecialPage( 'Mostlinkedcategories' ),
48 'Mostcategories' => new SpecialPage( 'Mostcategories' ),
49 'Mostimages' => new SpecialPage( 'Mostimages' ),
50 'Mostrevisions' => new SpecialPage( 'Mostrevisions' ),
51 'Shortpages' => new SpecialPage( 'Shortpages' ),
52 'Longpages' => new SpecialPage( 'Longpages' ),
53 'Newpages' => new IncludableSpecialPage( 'Newpages' ),
54 'Ancientpages' => new SpecialPage( 'Ancientpages' ),
55 'Deadendpages' => new SpecialPage( 'Deadendpages' ),
56 'Allpages' => new IncludableSpecialPage( 'Allpages' ),
57 'Prefixindex' => new IncludableSpecialPage( 'Prefixindex' ) ,
58 'Ipblocklist' => new SpecialPage( 'Ipblocklist' ),
59 'Specialpages' => new UnlistedSpecialPage( 'Specialpages' ),
60 'Contributions' => new UnlistedSpecialPage( 'Contributions' ),
61 'Emailuser' => new UnlistedSpecialPage( 'Emailuser' ),
62 'Whatlinkshere' => new UnlistedSpecialPage( 'Whatlinkshere' ),
63 'Recentchangeslinked' => new UnlistedSpecialPage( 'Recentchangeslinked' ),
64 'Movepage' => new UnlistedSpecialPage( 'Movepage' ),
65 'Blockme' => new UnlistedSpecialPage( 'Blockme' ),
66 'Booksources' => new SpecialPage( 'Booksources' ),
67 'Categories' => new SpecialPage( 'Categories' ),
68 'Export' => new SpecialPage( 'Export' ),
69 'Version' => new SpecialPage( 'Version' ),
70 'Allmessages' => new SpecialPage( 'Allmessages' ),
71 'Log' => new SpecialPage( 'Log' ),
72 'Blockip' => new SpecialPage( 'Blockip', 'block' ),
73 'Undelete' => new SpecialPage( 'Undelete' ),
74 "Import" => new SpecialPage( "Import", 'import' ),
75 'Lockdb' => new SpecialPage( 'Lockdb', 'siteadmin' ),
76 'Unlockdb' => new SpecialPage( 'Unlockdb', 'siteadmin' ),
77 'Userrights' => new SpecialPage( 'Userrights', 'userrights' ),
78 'MIMEsearch' => new SpecialPage( 'MIMEsearch' ),
79 'Unwatchedpages' => new SpecialPage( 'Unwatchedpages', 'unwatchedpages' )
80 );
81
82 if ( $wgUseValidation )
83 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
84
85 if( !$wgDisableCounters ) {
86 $wgSpecialPages['Popularpages'] = new SpecialPage( 'Popularpages' );
87 }
88
89 if( !$wgDisableInternalSearch ) {
90 $wgSpecialPages['Search'] = new SpecialPage( 'Search' );
91 }
92
93 if( $wgEmailAuthentication ) {
94 $wgSpecialPages['Confirmemail'] = new UnlistedSpecialPage( 'Confirmemail' );
95 }
96
97 /**
98 * Parent special page class, also static functions for handling the special
99 * page list
100 * @package MediaWiki
101 */
102 class SpecialPage
103 {
104 /**#@+
105 * @access private
106 */
107 /**
108 * The name of the class, used in the URL.
109 * Also used for the default <h1> heading, @see getDescription()
110 */
111 var $mName;
112 /**
113 * Minimum user level required to access this page, or "" for anyone.
114 * Also used to categorise the pages in Special:Specialpages
115 */
116 var $mRestriction;
117 /**
118 * Listed in Special:Specialpages?
119 */
120 var $mListed;
121 /**
122 * Function name called by the default execute()
123 */
124 var $mFunction;
125 /**
126 * File which needs to be included before the function above can be called
127 */
128 var $mFile;
129 /**
130 * Whether or not this special page is being included from an article
131 */
132 var $mIncluding;
133 /**
134 * Whether the special page can be included in an article
135 */
136 var $mIncludable;
137
138
139 /**#@-*/
140
141
142 /**
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
147 */
148 function addPage( &$obj ) {
149 global $wgSpecialPages;
150 $wgSpecialPages[$obj->mName] = $obj;
151 }
152
153 /**
154 * Remove a special page from the list
155 * Occasionally used to disable expensive or dangerous special pages
156 * @static
157 */
158 function removePage( $name ) {
159 global $wgSpecialPages;
160 unset( $wgSpecialPages[$name] );
161 }
162
163 /**
164 * Find the object with a given name and return it (or NULL)
165 * @static
166 * @param string $name
167 */
168 function getPage( $name ) {
169 global $wgSpecialPages;
170 if ( array_key_exists( $name, $wgSpecialPages ) ) {
171 return $wgSpecialPages[$name];
172 } else {
173 return NULL;
174 }
175 }
176
177 /**
178 * @static
179 * @param string $name
180 * @return mixed Title object if the redirect exists, otherwise NULL
181 */
182 function getRedirect( $name ) {
183 global $wgUser;
184
185 $redirects = array(
186 'Mypage' => Title::makeTitle( NS_USER, $wgUser->getName() ),
187 'Mytalk' => Title::makeTitle( NS_USER_TALK, $wgUser->getName() ),
188 'Mycontributions' => Title::makeTitle( NS_SPECIAL, 'Contributions/' . $wgUser->getName() ),
189 'Listadmins' => Title::makeTitle( NS_SPECIAL, 'Listusers/sysop' ), # @bug 2832
190 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' )
191 );
192 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
193
194 return isset( $redirects[$name] ) ? $redirects[$name] : null;
195 }
196
197 /**
198 * Return categorised listable special pages
199 * Returns a 2d array where the first index is the restriction name
200 * @static
201 */
202 function getPages() {
203 global $wgSpecialPages;
204 $pages = array(
205 '' => array(),
206 'sysop' => array(),
207 'developer' => array()
208 );
209
210 foreach ( $wgSpecialPages as $name => $page ) {
211 if ( $page->isListed() ) {
212 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
213 }
214 }
215 return $pages;
216 }
217
218 /**
219 * Execute a special page path.
220 * The path may contain parameters, e.g. Special:Name/Params
221 * Extracts the special page name and call the execute method, passing the parameters
222 *
223 * Returns a title object if the page is redirected, false if there was no such special
224 * page, and true if it was successful.
225 *
226 * @param $title a title object
227 * @param $including output is being captured for use in {{special:whatever}}
228 */
229 function executePath( &$title, $including = false ) {
230 global $wgSpecialPages, $wgOut, $wgTitle;
231 $fname = 'SpecialPage::executePath';
232 wfProfileIn( $fname );
233
234 $bits = split( "/", $title->getDBkey(), 2 );
235 $name = $bits[0];
236 if( !isset( $bits[1] ) ) { // bug 2087
237 $par = NULL;
238 } else {
239 $par = $bits[1];
240 }
241
242 $page = SpecialPage::getPage( $name );
243 if ( is_null( $page ) ) {
244 if ( $including ) {
245 wfProfileOut( $fname );
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->setStatusCode( 404 );
259 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
260 $retVal = false;
261 }
262 }
263 } else {
264 if ( $including && !$page->includable() ) {
265 wfProfileOut( $fname );
266 return false;
267 }
268 if($par !== NULL) {
269 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
270 } else {
271 $wgTitle = $title;
272 }
273 $page->including( $including );
274
275 $profName = 'Special:' . $page->getName();
276 wfProfileIn( $profName );
277 $page->execute( $par );
278 wfProfileOut( $profName );
279 $retVal = true;
280 }
281 wfProfileOut( $fname );
282 return $retVal;
283 }
284
285 /**
286 * Just like executePath() except it returns the HTML instead of outputting it
287 * Returns false if there was no such special page, or a title object if it was
288 * a redirect.
289 * @static
290 */
291 function capturePath( &$title ) {
292 global $wgOut, $wgTitle;
293
294 $oldTitle = $wgTitle;
295 $oldOut = $wgOut;
296 $wgOut = new OutputPage;
297
298 $ret = SpecialPage::executePath( $title, true );
299 if ( $ret === true ) {
300 $ret = $wgOut->getHTML();
301 }
302 $wgTitle = $oldTitle;
303 $wgOut = $oldOut;
304 return $ret;
305 }
306
307 /**
308 * Default constructor for special pages
309 * Derivative classes should call this from their constructor
310 * Note that if the user does not have the required level, an error message will
311 * be displayed by the default execute() method, without the global function ever
312 * being called.
313 *
314 * If you override execute(), you can recover the default behaviour with userCanExecute()
315 * and displayRestrictionError()
316 *
317 * @param string $name Name of the special page, as seen in links and URLs
318 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
319 * @param boolean $listed Whether the page is listed in Special:Specialpages
320 * @param string $function Function called by execute(). By default it is constructed from $name
321 * @param string $file File which is included by execute(). It is also constructed from $name by default
322 */
323 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
324 $this->mName = $name;
325 $this->mRestriction = $restriction;
326 $this->mListed = $listed;
327 $this->mIncludable = $includable;
328 if ( $function == false ) {
329 $this->mFunction = 'wfSpecial'.$name;
330 } else {
331 $this->mFunction = $function;
332 }
333 if ( $file === 'default' ) {
334 $this->mFile = "Special{$name}.php";
335 } else {
336 $this->mFile = $file;
337 }
338 }
339
340 /**#@+
341 * Accessor
342 *
343 * @deprecated
344 */
345 function getName() { return $this->mName; }
346 function getRestriction() { return $this->mRestriction; }
347 function getFile() { return $this->mFile; }
348 function isListed() { return $this->mListed; }
349 /**#@-*/
350
351 /**#@+
352 * Accessor and mutator
353 */
354 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
355 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
356 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
357 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
358 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
359 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
360 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
361 /**#@-*/
362
363 /**
364 * Checks if the given user (identified by an object) can execute this
365 * special page (as defined by $mRestriction)
366 */
367 function userCanExecute( &$user ) {
368 if ( $this->mRestriction == "" ) {
369 return true;
370 } else {
371 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
372 return true;
373 } else {
374 return false;
375 }
376 }
377 }
378
379 /**
380 * Output an error message telling the user what access level they have to have
381 */
382 function displayRestrictionError() {
383 global $wgOut;
384 $wgOut->permissionRequired( $this->mRestriction );
385 }
386
387 /**
388 * Sets headers - this should be called from the execute() method of all derived classes!
389 */
390 function setHeaders() {
391 global $wgOut;
392 $wgOut->setArticleRelated( false );
393 $wgOut->setRobotPolicy( "noindex,follow" );
394 $wgOut->setPageTitle( $this->getDescription() );
395 }
396
397 /**
398 * Default execute method
399 * Checks user permissions, calls the function given in mFunction
400 */
401 function execute( $par ) {
402 global $wgUser, $wgOut, $wgTitle;
403
404 $this->setHeaders();
405
406 if ( $this->userCanExecute( $wgUser ) ) {
407 $func = $this->mFunction;
408 // only load file if the function does not exist
409 if(!function_exists($func) and $this->mFile) {
410 require_once( $this->mFile );
411 }
412 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
413 $this->outputHeader();
414 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
415 return;
416 $func( $par, $this );
417 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
418 return;
419 } else {
420 $this->displayRestrictionError();
421 }
422 }
423
424 function outputHeader() {
425 global $wgOut, $wgContLang;
426
427 $msg = $wgContLang->lc( $this->name() ) . '-summary';
428 $out = wfMsg( $msg );
429 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() )
430 $wgOut->addWikiText( $out );
431
432 }
433
434 # Returns the name that goes in the <h1> in the special page itself, and also the name that
435 # will be listed in Special:Specialpages
436 #
437 # Derived classes can override this, but usually it is easier to keep the default behaviour.
438 # Messages can be added at run-time, see MessageCache.php
439 function getDescription() {
440 return wfMsg( strtolower( $this->mName ) );
441 }
442
443 /**
444 * Get a self-referential title object
445 */
446 function getTitle() {
447 return Title::makeTitle( NS_SPECIAL, $this->mName );
448 }
449
450 /**
451 * Set whether this page is listed in Special:Specialpages, at run-time
452 */
453 function setListed( $listed ) {
454 return wfSetVar( $this->mListed, $listed );
455 }
456
457 }
458
459 /**
460 * Shortcut to construct a special page which is unlisted by default
461 * @package MediaWiki
462 */
463 class UnlistedSpecialPage extends SpecialPage
464 {
465 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
466 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
467 }
468 }
469
470 /**
471 * Shortcut to construct an includable special page
472 * @package MediaWiki
473 */
474 class IncludableSpecialPage extends SpecialPage
475 {
476 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
477 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
478 }
479 }
480 ?>