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