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