d1eececad59920d594f8c8cb65bc0f080a345113
[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 'Block' => new SpecialPage( 'Blockip', 'block' ),
73 'Undelete' => new SpecialPage( 'Undelete' ),
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 );
80
81 if ( $wgUseValidation )
82 $wgSpecialPages['Validate'] = new SpecialPage( 'Validate' );
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 if ( $wgEnableUnwatchedpages )
97 $wgSpecialPages['Unwatchedpages'] = new SpecialPage( 'Unwatchedpages' );
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 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 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 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 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 'Randompage' => Title::makeTitle( NS_SPECIAL, 'Random' ),
193 'Blockip' => Title::makeTitle( NS_SPECIAL, 'Block' )
194 );
195 wfRunHooks( 'SpecialPageGetRedirect', array( &$redirects ) );
196
197 return isset( $redirects[$name] ) ? $redirects[$name] : null;
198 }
199
200 /**
201 * Return categorised listable special pages
202 * Returns a 2d array where the first index is the restriction name
203 * @static
204 */
205 function getPages() {
206 global $wgSpecialPages;
207 $pages = array(
208 '' => array(),
209 'sysop' => array(),
210 'developer' => array()
211 );
212
213 foreach ( $wgSpecialPages as $name => $page ) {
214 if ( $page->isListed() ) {
215 $pages[$page->getRestriction()][$page->getName()] =& $wgSpecialPages[$name];
216 }
217 }
218 return $pages;
219 }
220
221 /**
222 * Execute a special page path.
223 * The path may contain parameters, e.g. Special:Name/Params
224 * Extracts the special page name and call the execute method, passing the parameters
225 *
226 * Returns a title object if the page is redirected, false if there was no such special
227 * page, and true if it was successful.
228 *
229 * @param $title a title object
230 * @param $including output is being captured for use in {{special:whatever}}
231 */
232 function executePath( &$title, $including = false ) {
233 global $wgSpecialPages, $wgOut, $wgTitle;
234 $fname = 'SpecialPage::executePath';
235 wfProfileIn( $fname );
236
237 $bits = split( "/", $title->getDBkey(), 2 );
238 $name = $bits[0];
239 if( !isset( $bits[1] ) ) { // bug 2087
240 $par = NULL;
241 } else {
242 $par = $bits[1];
243 }
244
245 $page = SpecialPage::getPage( $name );
246 if ( is_null( $page ) ) {
247 if ( $including ) {
248 wfProfileOut( $fname );
249 return false;
250 } else {
251 $redir = SpecialPage::getRedirect( $name );
252 if ( isset( $redir ) ) {
253 if ( isset( $par ) )
254 $wgOut->redirect( $redir->getFullURL() . '/' . $par );
255 else
256 $wgOut->redirect( $redir->getFullURL() );
257 $retVal = $redir;
258 } else {
259 $wgOut->setArticleRelated( false );
260 $wgOut->setRobotpolicy( 'noindex,follow' );
261 $wgOut->setStatusCode( 404 );
262 $wgOut->errorpage( 'nosuchspecialpage', 'nospecialpagetext' );
263 $retVal = false;
264 }
265 }
266 } else {
267 if ( $including && !$page->includable() ) {
268 wfProfileOut( $fname );
269 return false;
270 }
271 if($par !== NULL) {
272 $wgTitle = Title::makeTitle( NS_SPECIAL, $name );
273 } else {
274 $wgTitle = $title;
275 }
276 $page->including( $including );
277
278 $profName = 'Special:' . $page->getName();
279 wfProfileIn( $profName );
280 $page->execute( $par );
281 wfProfileOut( $profName );
282 $retVal = true;
283 }
284 wfProfileOut( $fname );
285 return $retVal;
286 }
287
288 /**
289 * Just like executePath() except it returns the HTML instead of outputting it
290 * Returns false if there was no such special page, or a title object if it was
291 * a redirect.
292 * @static
293 */
294 function capturePath( &$title ) {
295 global $wgOut, $wgTitle;
296
297 $oldTitle = $wgTitle;
298 $oldOut = $wgOut;
299 $wgOut = new OutputPage;
300
301 $ret = SpecialPage::executePath( $title, true );
302 if ( $ret === true ) {
303 $ret = $wgOut->getHTML();
304 }
305 $wgTitle = $oldTitle;
306 $wgOut = $oldOut;
307 return $ret;
308 }
309
310 /**
311 * Default constructor for special pages
312 * Derivative classes should call this from their constructor
313 * Note that if the user does not have the required level, an error message will
314 * be displayed by the default execute() method, without the global function ever
315 * being called.
316 *
317 * If you override execute(), you can recover the default behaviour with userCanExecute()
318 * and displayRestrictionError()
319 *
320 * @param string $name Name of the special page, as seen in links and URLs
321 * @param string $restriction Minimum user level required, e.g. "sysop" or "developer".
322 * @param boolean $listed Whether the page is listed in Special:Specialpages
323 * @param string $function Function called by execute(). By default it is constructed from $name
324 * @param string $file File which is included by execute(). It is also constructed from $name by default
325 */
326 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
327 $this->mName = $name;
328 $this->mRestriction = $restriction;
329 $this->mListed = $listed;
330 $this->mIncludable = $includable;
331 if ( $function == false ) {
332 $this->mFunction = 'wfSpecial'.$name;
333 } else {
334 $this->mFunction = $function;
335 }
336 if ( $file === 'default' ) {
337 $this->mFile = "Special{$name}.php";
338 } else {
339 $this->mFile = $file;
340 }
341 }
342
343 /**#@+
344 * Accessor
345 *
346 * @deprecated
347 */
348 function getName() { return $this->mName; }
349 function getRestriction() { return $this->mRestriction; }
350 function getFile() { return $this->mFile; }
351 function isListed() { return $this->mListed; }
352 /**#@-*/
353
354 /**#@+
355 * Accessor and mutator
356 */
357 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
358 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
359 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
360 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
361 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
362 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
363 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
364 /**#@-*/
365
366 /**
367 * Checks if the given user (identified by an object) can execute this
368 * special page (as defined by $mRestriction)
369 */
370 function userCanExecute( &$user ) {
371 if ( $this->mRestriction == "" ) {
372 return true;
373 } else {
374 if ( in_array( $this->mRestriction, $user->getRights() ) ) {
375 return true;
376 } else {
377 return false;
378 }
379 }
380 }
381
382 /**
383 * Output an error message telling the user what access level they have to have
384 */
385 function displayRestrictionError() {
386 global $wgOut;
387 $wgOut->permissionRequired( $this->mRestriction );
388 }
389
390 /**
391 * Sets headers - this should be called from the execute() method of all derived classes!
392 */
393 function setHeaders() {
394 global $wgOut;
395 $wgOut->setArticleRelated( false );
396 $wgOut->setRobotPolicy( "noindex,follow" );
397 $wgOut->setPageTitle( $this->getDescription() );
398 }
399
400 /**
401 * Default execute method
402 * Checks user permissions, calls the function given in mFunction
403 */
404 function execute( $par ) {
405 global $wgUser, $wgOut, $wgTitle;
406
407 $this->setHeaders();
408
409 if ( $this->userCanExecute( $wgUser ) ) {
410 $func = $this->mFunction;
411 // only load file if the function does not exist
412 if(!function_exists($func) and $this->mFile) {
413 require_once( $this->mFile );
414 }
415 $this->outputHeader();
416 $func( $par, $this );
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 ?>