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