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