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