Fix doc
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * SpecialPage: handling special pages and lists thereof.
4 *
5 * To add a special page in an extension, add to $wgSpecialPages either
6 * an object instance or an array containing the name and constructor
7 * parameters. The latter is preferred for performance reasons.
8 *
9 * The object instantiated must be either an instance of SpecialPage or a
10 * sub-class thereof. It must have an execute() method, which sends the HTML
11 * for the special page to $wgOut. The parent class has an execute() method
12 * which distributes the call to the historical global functions. Additionally,
13 * execute() also checks if the user has the necessary access privileges
14 * and bails out if not.
15 *
16 * To add a core special page, use the similar static list in
17 * SpecialPage::$mList. To remove a core static special page at runtime, use
18 * a SpecialPage_initList hook.
19 *
20 * @addtogroup SpecialPage
21 */
22
23 /**
24 * Parent special page class, also static functions for handling the special
25 * page list.
26 * @addtogroup SpecialPage
27 */
28 class SpecialPage
29 {
30 /**#@+
31 * @access private
32 */
33 /**
34 * The canonical name of this special page
35 * Also used for the default <h1> heading, @see getDescription()
36 */
37 var $mName;
38 /**
39 * The local name of this special page
40 */
41 var $mLocalName;
42 /**
43 * Minimum user level required to access this page, or "" for anyone.
44 * Also used to categorise the pages in Special:Specialpages
45 */
46 var $mRestriction;
47 /**
48 * Listed in Special:Specialpages?
49 */
50 var $mListed;
51 /**
52 * Function name called by the default execute()
53 */
54 var $mFunction;
55 /**
56 * File which needs to be included before the function above can be called
57 */
58 var $mFile;
59 /**
60 * Whether or not this special page is being included from an article
61 */
62 var $mIncluding;
63 /**
64 * Whether the special page can be included in an article
65 */
66 var $mIncludable;
67 /**
68 * Query parameters that can be passed through redirects
69 */
70 var $mAllowedRedirectParams = array();
71 /**
72 * List of special pages, followed by parameters.
73 * If the only parameter is a string, that is the page name.
74 * Otherwise, it is an array. The format is one of:
75 ** array( 'SpecialPage', name, right )
76 ** array( 'IncludableSpecialPage', name, right, listed? )
77 ** array( 'UnlistedSpecialPage', name, right )
78 ** array( 'SpecialRedirectToSpecial', name, page to redirect to, special page param, ... )
79 */
80 static public $mList = array(
81 'DoubleRedirects' => array( 'SpecialPage', 'DoubleRedirects' ),
82 'BrokenRedirects' => array( 'SpecialPage', 'BrokenRedirects' ),
83 'Disambiguations' => array( 'SpecialPage', 'Disambiguations' ),
84
85 'Userlogin' => array( 'SpecialPage', 'Userlogin' ),
86 'Userlogout' => array( 'UnlistedSpecialPage', 'Userlogout' ),
87 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
88 'Preferences' => array( 'SpecialPage', 'Preferences' ),
89 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
90
91 'Recentchanges' => array( 'IncludableSpecialPage', 'Recentchanges' ),
92 'Upload' => array( 'SpecialPage', 'Upload' ),
93 'Imagelist' => array( 'SpecialPage', 'Imagelist' ),
94 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
95 'Listusers' => array( 'SpecialPage', 'Listusers' ),
96 'Listgrouprights' => 'SpecialListGroupRights',
97 'Statistics' => array( 'SpecialPage', 'Statistics' ),
98 'Randompage' => 'Randompage',
99 'Lonelypages' => array( 'SpecialPage', 'Lonelypages' ),
100 'Uncategorizedpages' => array( 'SpecialPage', 'Uncategorizedpages' ),
101 'Uncategorizedcategories' => array( 'SpecialPage', 'Uncategorizedcategories' ),
102 'Uncategorizedimages' => array( 'SpecialPage', 'Uncategorizedimages' ),
103 'Uncategorizedtemplates' => array( 'SpecialPage', 'Uncategorizedtemplates' ),
104 'Unusedcategories' => array( 'SpecialPage', 'Unusedcategories' ),
105 'Unusedimages' => array( 'SpecialPage', 'Unusedimages' ),
106 'Wantedpages' => array( 'IncludableSpecialPage', 'Wantedpages' ),
107 'Wantedcategories' => array( 'SpecialPage', 'Wantedcategories' ),
108 'Mostlinked' => array( 'SpecialPage', 'Mostlinked' ),
109 'Mostlinkedcategories' => array( 'SpecialPage', 'Mostlinkedcategories' ),
110 'Mostlinkedtemplates' => array( 'SpecialPage', 'Mostlinkedtemplates' ),
111 'Mostcategories' => array( 'SpecialPage', 'Mostcategories' ),
112 'Mostimages' => array( 'SpecialPage', 'Mostimages' ),
113 'Mostrevisions' => array( 'SpecialPage', 'Mostrevisions' ),
114 'Fewestrevisions' => array( 'SpecialPage', 'Fewestrevisions' ),
115 'Shortpages' => array( 'SpecialPage', 'Shortpages' ),
116 'Longpages' => array( 'SpecialPage', 'Longpages' ),
117 'Newpages' => array( 'IncludableSpecialPage', 'Newpages' ),
118 'Ancientpages' => array( 'SpecialPage', 'Ancientpages' ),
119 'Deadendpages' => array( 'SpecialPage', 'Deadendpages' ),
120 'Protectedpages' => array( 'SpecialPage', 'Protectedpages' ),
121 'Protectedtitles' => array( 'SpecialPage', 'Protectedtitles' ),
122 'Allpages' => array( 'IncludableSpecialPage', 'Allpages' ),
123 'Prefixindex' => array( 'IncludableSpecialPage', 'Prefixindex' ) ,
124 'Ipblocklist' => array( 'SpecialPage', 'Ipblocklist' ),
125 'Specialpages' => array( 'UnlistedSpecialPage', 'Specialpages' ),
126 'Contributions' => array( 'SpecialPage', 'Contributions' ),
127 'Emailuser' => array( 'UnlistedSpecialPage', 'Emailuser' ),
128 'Whatlinkshere' => array( 'SpecialPage', 'Whatlinkshere' ),
129 'Recentchangeslinked' => array( 'SpecialPage', 'Recentchangeslinked' ),
130 'Movepage' => array( 'UnlistedSpecialPage', 'Movepage' ),
131 'Blockme' => array( 'UnlistedSpecialPage', 'Blockme' ),
132 'Resetpass' => array( 'UnlistedSpecialPage', 'Resetpass' ),
133 'Booksources' => 'SpecialBookSources',
134 'Categories' => array( 'SpecialPage', 'Categories' ),
135 'Export' => array( 'SpecialPage', 'Export' ),
136 'Version' => array( 'SpecialPage', 'Version' ),
137 'Allmessages' => array( 'SpecialPage', 'Allmessages' ),
138 'Log' => array( 'SpecialPage', 'Log' ),
139 'Blockip' => array( 'SpecialPage', 'Blockip', 'block' ),
140 'Undelete' => array( 'SpecialPage', 'Undelete', 'deletedhistory' ),
141 'Import' => array( 'SpecialPage', 'Import', 'import' ),
142 'Lockdb' => array( 'SpecialPage', 'Lockdb', 'siteadmin' ),
143 'Unlockdb' => array( 'SpecialPage', 'Unlockdb', 'siteadmin' ),
144 'Userrights' => 'UserrightsPage',
145 'MIMEsearch' => array( 'SpecialPage', 'MIMEsearch' ),
146 'FileDuplicateSearch' => array( 'SpecialPage', 'FileDuplicateSearch' ),
147 'Unwatchedpages' => array( 'SpecialPage', 'Unwatchedpages', 'unwatchedpages' ),
148 'Listredirects' => array( 'SpecialPage', 'Listredirects' ),
149 'Revisiondelete' => array( 'UnlistedSpecialPage', 'Revisiondelete', 'deleterevision' ),
150 'Unusedtemplates' => array( 'SpecialPage', 'Unusedtemplates' ),
151 'Randomredirect' => 'SpecialRandomredirect',
152 'Withoutinterwiki' => array( 'SpecialPage', 'Withoutinterwiki' ),
153 'Filepath' => array( 'SpecialPage', 'Filepath' ),
154
155 'Mypage' => array( 'SpecialMypage' ),
156 'Mytalk' => array( 'SpecialMytalk' ),
157 'Mycontributions' => array( 'SpecialMycontributions' ),
158 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
159 'MergeHistory' => array( 'SpecialPage', 'MergeHistory', 'mergehistory' ),
160 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
161 );
162
163 static public $mAliases;
164 static public $mListInitialised = false;
165 /**
166 * List of special pages, followed by what subtitle they should go under
167 * at Special:SpecialPages
168 */
169 static public $mGroupsList = array(
170 'DoubleRedirects' => 'maintenance',
171 'BrokenRedirects' => 'maintenance',
172 'Lonelypages' => 'maintenance',
173 'Uncategorizedpages' => 'maintenance',
174 'Uncategorizedcategories' => 'maintenance',
175 'Uncategorizedimages' => 'maintenance',
176 'Uncategorizedtemplates' => 'maintenance',
177 'Unusedcategories' => 'maintenance',
178 'Unusedimages' => 'maintenance',
179 'Protectedpages' => 'maintenance',
180 'Protectedtitles' => 'maintenance',
181 'Unusedtemplates' => 'maintenance',
182 'Withoutinterwiki' => 'maintenance',
183 'Longpages' => 'maintenance',
184
185 'Userlogin' => 'login',
186 'Userlogout' => 'login',
187 'CreateAccount' => 'login',
188
189 'Recentchanges' => 'changes',
190 'Recentchangeslinked' => 'changes',
191 'Watchlist' => 'changes',
192 'Newimages' => 'changes',
193 'Newpages' => 'changes',
194 'Log' => 'changes',
195
196 'Upload' => 'media',
197 'Imagelist' => 'media',
198 'MIMEsearch' => 'media',
199 'FileDuplicateSearch' => 'media',
200 'Filepath' => 'media',
201
202 'Listusers' => 'users',
203 'Listgrouprights' => 'users',
204 'Ipblocklist' => 'users',
205 'Contributions' => 'users',
206 'Emailuser' => 'users',
207 'Listadmins' => 'users',
208 'Listbots' => 'users',
209
210 'Wantedpages' => 'needy',
211 'Wantedcategories' => 'needy',
212 'Shortpages' => 'needy',
213 'Ancientpages' => 'needy',
214 'Deadendpages' => 'needy',
215
216 'Mostlinked' => 'highuse',
217 'Mostlinkedcategories' => 'highuse',
218 'Mostlinkedtemplates' => 'highuse',
219 'Mostcategories' => 'highuse',
220 'Mostimages' => 'highuse',
221 'Mostrevisions' => 'highuse',
222
223 'Userrights' => 'permissions',
224 'Blockip' => 'permissions',
225
226 'Statistics' => 'other',
227 'Fewestrevisions' => 'other',
228 'Randompage' => 'other',
229 'Disambiguations' => 'other',
230 'Specialpages' => 'other',
231 'Blockme' => 'other',
232 'Movepage' => 'other',
233 'MergeHistory' => 'other',
234 'Lockdb' => 'other',
235 'Unlockdb' => 'other',
236 'Version' => 'other',
237 'Whatlinkshere' => 'other',
238 'Booksources' => 'other',
239 'Revisiondelete' => 'other',
240 'Export' => 'other',
241 'Categories' => 'other',
242 'Undelete' => 'other',
243 'Import' => 'other',
244 'Unwatchedpages' => 'other',
245 'Randomredirect' => 'other',
246 'Allpages' => 'other',
247 'Allmessages' => 'other',
248 'Prefixindex' => 'other',
249 'Listredirects' => 'other',
250 'Preferences' => 'other',
251 'Resetpass' => 'other',
252 'Mypage' => 'other',
253 'Mytalk' => 'other',
254 'Mycontributions' => 'other',
255 );
256
257 /**#@-*/
258
259 /**
260 * Initialise the special page list
261 * This must be called before accessing SpecialPage::$mList
262 */
263 static function initList() {
264 global $wgSpecialPages;
265 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
266
267 if ( self::$mListInitialised ) {
268 return;
269 }
270 wfProfileIn( __METHOD__ );
271
272 # Better to set this now, to avoid infinite recursion in carelessly written hooks
273 self::$mListInitialised = true;
274
275 if( !$wgDisableCounters ) {
276 self::$mList['Popularpages'] = array( 'SpecialPage', 'Popularpages' );
277 }
278
279 if( !$wgDisableInternalSearch ) {
280 self::$mList['Search'] = array( 'SpecialPage', 'Search' );
281 }
282
283 if( $wgEmailAuthentication ) {
284 self::$mList['Confirmemail'] = 'EmailConfirmation';
285 self::$mList['Invalidateemail'] = 'EmailInvalidation';
286 }
287
288 # Add extension special pages
289 self::$mList = array_merge( self::$mList, $wgSpecialPages );
290
291 # Run hooks
292 # This hook can be used to remove undesired built-in special pages
293 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
294 wfProfileOut( __METHOD__ );
295 }
296
297 static function initAliasList() {
298 if ( !is_null( self::$mAliases ) ) {
299 return;
300 }
301
302 global $wgContLang;
303 $aliases = $wgContLang->getSpecialPageAliases();
304 $missingPages = self::$mList;
305 self::$mAliases = array();
306 foreach ( $aliases as $realName => $aliasList ) {
307 foreach ( $aliasList as $alias ) {
308 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
309 }
310 unset( $missingPages[$realName] );
311 }
312 foreach ( $missingPages as $name => $stuff ) {
313 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
314 }
315 }
316
317 /**
318 * Given a special page alias, return the special page name.
319 * Returns false if there is no such alias.
320 */
321 static function resolveAlias( $alias ) {
322 global $wgContLang;
323
324 if ( !self::$mListInitialised ) self::initList();
325 if ( is_null( self::$mAliases ) ) self::initAliasList();
326 $caseFoldedAlias = $wgContLang->caseFold( $alias );
327 if ( isset( self::$mAliases[$caseFoldedAlias] ) ) {
328 return self::$mAliases[$caseFoldedAlias];
329 } else {
330 return false;
331 }
332 }
333
334 /**
335 * Given a special page name with a possible subpage, return an array
336 * where the first element is the special page name and the second is the
337 * subpage.
338 */
339 static function resolveAliasWithSubpage( $alias ) {
340 $bits = explode( '/', $alias, 2 );
341 $name = self::resolveAlias( $bits[0] );
342 if( !isset( $bits[1] ) ) { // bug 2087
343 $par = NULL;
344 } else {
345 $par = $bits[1];
346 }
347 return array( $name, $par );
348 }
349
350 /**
351 * Add a page to the list of valid special pages. This used to be the preferred
352 * method for adding special pages in extensions. It's now suggested that you add
353 * an associative record to $wgSpecialPages. This avoids autoloading SpecialPage.
354 *
355 * @param SpecialPage $page
356 * @static
357 */
358 static function addPage( &$page ) {
359 if ( !self::$mListInitialised ) {
360 self::initList();
361 }
362 self::$mList[$page->mName] = $page;
363 }
364
365 /**
366 * Add a page to a certain display group for Special:SpecialPages
367 *
368 * @param SpecialPage $page
369 * @param string $group
370 * @static
371 */
372 static function setGroup( &$page, $group ) {
373 if ( !self::$mListInitialised ) {
374 self::initList();
375 }
376 self::$mGroupsList[$page->mName] = $group;
377 }
378
379 /**
380 * Add a page to a certain display group for Special:SpecialPages
381 *
382 * @param SpecialPage $page
383 * @static
384 */
385 static function getGroup( &$page ) {
386 if ( !self::$mListInitialised ) {
387 self::initList();
388 }
389 $group = isset(self::$mGroupsList[$page->mName]) ?
390 self::$mGroupsList[$page->mName] : 'other';
391 return $group;
392 }
393
394 /**
395 * Remove a special page from the list
396 * Formerly used to disable expensive or dangerous special pages. The
397 * preferred method is now to add a SpecialPage_initList hook.
398 *
399 * @static
400 */
401 static function removePage( $name ) {
402 if ( !self::$mListInitialised ) {
403 self::initList();
404 }
405 unset( self::$mList[$name] );
406 }
407
408 /**
409 * Check if a given name exist as a special page or as a special page alias
410 * @param $name string: name of a special page
411 * @return boolean: true if a special page exists with this name
412 */
413 static function exists( $name ) {
414 global $wgContLang;
415 if ( !self::$mListInitialised ) {
416 self::initList();
417 }
418 if( !self::$mAliases ) {
419 self::initAliasList();
420 }
421
422 # Remove special pages inline parameters:
423 $bits = explode( '/', $name );
424 $name = $wgContLang->caseFold($bits[0]);
425
426 return
427 array_key_exists( $name, self::$mList )
428 or array_key_exists( $name, self::$mAliases )
429 ;
430 }
431
432 /**
433 * Find the object with a given name and return it (or NULL)
434 * @static
435 * @param string $name
436 */
437 static function getPage( $name ) {
438 if ( !self::$mListInitialised ) {
439 self::initList();
440 }
441 if ( array_key_exists( $name, self::$mList ) ) {
442 $rec = self::$mList[$name];
443 if ( is_string( $rec ) ) {
444 $className = $rec;
445 self::$mList[$name] = new $className;
446 } elseif ( is_array( $rec ) ) {
447 $className = array_shift( $rec );
448 self::$mList[$name] = wfCreateObject( $className, $rec );
449 }
450 return self::$mList[$name];
451 } else {
452 return NULL;
453 }
454 }
455
456 /**
457 * Get a special page with a given localised name, or NULL if there
458 * is no such special page.
459 */
460 static function getPageByAlias( $alias ) {
461 $realName = self::resolveAlias( $alias );
462 if ( $realName ) {
463 return self::getPage( $realName );
464 } else {
465 return NULL;
466 }
467 }
468
469 /**
470 * Return categorised listable special pages for all users
471 * @static
472 */
473 static function getRegularPages() {
474 if ( !self::$mListInitialised ) {
475 self::initList();
476 }
477 $pages = array();
478
479 foreach ( self::$mList as $name => $rec ) {
480 $page = self::getPage( $name );
481 if ( $page->isListed() && !$page->isRestricted() ) {
482 $pages[$name] = $page;
483 }
484 }
485 return $pages;
486 }
487
488 /**
489 * Return categorised listable special pages which are available
490 * for the current user, but not for everyone
491 * @static
492 */
493 static function getRestrictedPages() {
494 global $wgUser;
495 if ( !self::$mListInitialised ) {
496 self::initList();
497 }
498 $pages = array();
499
500 foreach ( self::$mList as $name => $rec ) {
501 $page = self::getPage( $name );
502 if (
503 $page->isListed()
504 and $page->isRestricted()
505 and $page->userCanExecute( $wgUser )
506 ) {
507 $pages[$name] = $page;
508 }
509 }
510 return $pages;
511 }
512
513 /**
514 * Execute a special page path.
515 * The path may contain parameters, e.g. Special:Name/Params
516 * Extracts the special page name and call the execute method, passing the parameters
517 *
518 * Returns a title object if the page is redirected, false if there was no such special
519 * page, and true if it was successful.
520 *
521 * @param $title a title object
522 * @param $including output is being captured for use in {{special:whatever}}
523 */
524 static function executePath( &$title, $including = false ) {
525 global $wgOut, $wgTitle, $wgRequest;
526 wfProfileIn( __METHOD__ );
527
528 # FIXME: redirects broken due to this call
529 $bits = explode( '/', $title->getDBkey(), 2 );
530 $name = $bits[0];
531 if( !isset( $bits[1] ) ) { // bug 2087
532 $par = NULL;
533 } else {
534 $par = $bits[1];
535 }
536 $page = SpecialPage::getPageByAlias( $name );
537 # Nonexistent?
538 if ( !$page ) {
539 if ( !$including ) {
540 $wgOut->setArticleRelated( false );
541 $wgOut->setRobotpolicy( 'noindex,nofollow' );
542 $wgOut->setStatusCode( 404 );
543 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
544 }
545 wfProfileOut( __METHOD__ );
546 return false;
547 }
548
549 # Check for redirect
550 if ( !$including ) {
551 $redirect = $page->getRedirect( $par );
552 if ( $redirect ) {
553 $query = $page->getRedirectQuery();
554 $url = $redirect->getFullUrl( $query );
555 $wgOut->redirect( $url );
556 wfProfileOut( __METHOD__ );
557 return $redirect;
558 }
559 }
560
561 # Redirect to canonical alias for GET commands
562 # Not for POST, we'd lose the post data, so it's best to just distribute
563 # the request. Such POST requests are possible for old extensions that
564 # generate self-links without being aware that their default name has
565 # changed.
566 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
567 $query = $_GET;
568 unset( $query['title'] );
569 $query = wfArrayToCGI( $query );
570 $title = $page->getTitle( $par );
571 $url = $title->getFullUrl( $query );
572 $wgOut->redirect( $url );
573 wfProfileOut( __METHOD__ );
574 return $redirect;
575 }
576
577 if ( $including && !$page->includable() ) {
578 wfProfileOut( __METHOD__ );
579 return false;
580 } elseif ( !$including ) {
581 $wgTitle = $page->getTitle();
582 }
583 $page->including( $including );
584
585 // Execute special page
586 $profName = 'Special:' . $page->getName();
587 wfProfileIn( $profName );
588 $page->execute( $par );
589 wfProfileOut( $profName );
590 wfProfileOut( __METHOD__ );
591 return true;
592 }
593
594 /**
595 * Just like executePath() except it returns the HTML instead of outputting it
596 * Returns false if there was no such special page, or a title object if it was
597 * a redirect.
598 * @static
599 */
600 static function capturePath( &$title ) {
601 global $wgOut, $wgTitle;
602
603 $oldTitle = $wgTitle;
604 $oldOut = $wgOut;
605 $wgOut = new OutputPage;
606
607 $ret = SpecialPage::executePath( $title, true );
608 if ( $ret === true ) {
609 $ret = $wgOut->getHTML();
610 }
611 $wgTitle = $oldTitle;
612 $wgOut = $oldOut;
613 return $ret;
614 }
615
616 /**
617 * Get the local name for a specified canonical name
618 *
619 * @param $name
620 * @param mixed $subpage Boolean false, or string
621 *
622 * @return string
623 */
624 static function getLocalNameFor( $name, $subpage = false ) {
625 global $wgContLang;
626 $aliases = $wgContLang->getSpecialPageAliases();
627 if ( isset( $aliases[$name][0] ) ) {
628 $name = $aliases[$name][0];
629 }
630 if ( $subpage !== false && !is_null( $subpage ) ) {
631 $name = "$name/$subpage";
632 }
633 return $name;
634 }
635
636 /**
637 * Get a localised Title object for a specified special page name
638 */
639 static function getTitleFor( $name, $subpage = false ) {
640 $name = self::getLocalNameFor( $name, $subpage );
641 if ( $name ) {
642 return Title::makeTitle( NS_SPECIAL, $name );
643 } else {
644 throw new MWException( "Invalid special page name \"$name\"" );
645 }
646 }
647
648 /**
649 * Get a localised Title object for a page name with a possibly unvalidated subpage
650 */
651 static function getSafeTitleFor( $name, $subpage = false ) {
652 $name = self::getLocalNameFor( $name, $subpage );
653 if ( $name ) {
654 return Title::makeTitleSafe( NS_SPECIAL, $name );
655 } else {
656 return null;
657 }
658 }
659
660 /**
661 * Get a title for a given alias
662 * @return Title or null if there is no such alias
663 */
664 static function getTitleForAlias( $alias ) {
665 $name = self::resolveAlias( $alias );
666 if ( $name ) {
667 return self::getTitleFor( $name );
668 } else {
669 return null;
670 }
671 }
672
673 /**
674 * Default constructor for special pages
675 * Derivative classes should call this from their constructor
676 * Note that if the user does not have the required level, an error message will
677 * be displayed by the default execute() method, without the global function ever
678 * being called.
679 *
680 * If you override execute(), you can recover the default behaviour with userCanExecute()
681 * and displayRestrictionError()
682 *
683 * @param string $name Name of the special page, as seen in links and URLs
684 * @param string $restriction User right required, e.g. "block" or "delete"
685 * @param boolean $listed Whether the page is listed in Special:Specialpages
686 * @param string $function Function called by execute(). By default it is constructed from $name
687 * @param string $file File which is included by execute(). It is also constructed from $name by default
688 */
689 function SpecialPage( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
690 $this->mName = $name;
691 $this->mRestriction = $restriction;
692 $this->mListed = $listed;
693 $this->mIncludable = $includable;
694 if ( $function == false ) {
695 $this->mFunction = 'wfSpecial'.$name;
696 } else {
697 $this->mFunction = $function;
698 }
699 if ( $file === 'default' ) {
700 $this->mFile = dirname(__FILE__) . "/Special{$name}.php";
701 } else {
702 $this->mFile = $file;
703 }
704 }
705
706 /**#@+
707 * Accessor
708 *
709 * @deprecated
710 */
711 function getName() { return $this->mName; }
712 function getRestriction() { return $this->mRestriction; }
713 function getFile() { return $this->mFile; }
714 function isListed() { return $this->mListed; }
715 /**#@-*/
716
717 /**#@+
718 * Accessor and mutator
719 */
720 function name( $x = NULL ) { return wfSetVar( $this->mName, $x ); }
721 function restrictions( $x = NULL) { return wfSetVar( $this->mRestrictions, $x ); }
722 function listed( $x = NULL) { return wfSetVar( $this->mListed, $x ); }
723 function func( $x = NULL) { return wfSetVar( $this->mFunction, $x ); }
724 function file( $x = NULL) { return wfSetVar( $this->mFile, $x ); }
725 function includable( $x = NULL ) { return wfSetVar( $this->mIncludable, $x ); }
726 function including( $x = NULL ) { return wfSetVar( $this->mIncluding, $x ); }
727 /**#@-*/
728
729 /**
730 * Get the localised name of the special page
731 */
732 function getLocalName() {
733 if ( !isset( $this->mLocalName ) ) {
734 $this->mLocalName = self::getLocalNameFor( $this->mName );
735 }
736 return $this->mLocalName;
737 }
738
739 /**
740 * Can be overridden by subclasses with more complicated permissions
741 * schemes.
742 *
743 * @return bool Should the page be displayed with the restricted-access
744 * pages?
745 */
746 public function isRestricted() {
747 return $this->mRestriction != '';
748 }
749
750 /**
751 * Checks if the given user (identified by an object) can execute this
752 * special page (as defined by $mRestriction). Can be overridden by sub-
753 * classes with more complicated permissions schemes.
754 *
755 * @param User $user The user to check
756 * @return bool Does the user have permission to view the page?
757 */
758 public function userCanExecute( $user ) {
759 return $user->isAllowed( $this->mRestriction );
760 }
761
762 /**
763 * Output an error message telling the user what access level they have to have
764 */
765 function displayRestrictionError() {
766 global $wgOut;
767 $wgOut->permissionRequired( $this->mRestriction );
768 }
769
770 /**
771 * Sets headers - this should be called from the execute() method of all derived classes!
772 */
773 function setHeaders() {
774 global $wgOut;
775 $wgOut->setArticleRelated( false );
776 $wgOut->setRobotPolicy( "noindex,nofollow" );
777 $wgOut->setPageTitle( $this->getDescription() );
778 }
779
780 /**
781 * Default execute method
782 * Checks user permissions, calls the function given in mFunction
783 *
784 * This may be overridden by subclasses.
785 */
786 function execute( $par ) {
787 global $wgUser;
788
789 $this->setHeaders();
790
791 if ( $this->userCanExecute( $wgUser ) ) {
792 $func = $this->mFunction;
793 // only load file if the function does not exist
794 if(!is_callable($func) and $this->mFile) {
795 require_once( $this->mFile );
796 }
797 # FIXME: these hooks are broken for extensions and anything else that subclasses SpecialPage.
798 if ( wfRunHooks( 'SpecialPageExecuteBeforeHeader', array( &$this, &$par, &$func ) ) )
799 $this->outputHeader();
800 if ( ! wfRunHooks( 'SpecialPageExecuteBeforePage', array( &$this, &$par, &$func ) ) )
801 return;
802 call_user_func( $func, $par, $this );
803 if ( ! wfRunHooks( 'SpecialPageExecuteAfterPage', array( &$this, &$par, &$func ) ) )
804 return;
805 } else {
806 $this->displayRestrictionError();
807 }
808 }
809
810 function outputHeader() {
811 global $wgOut, $wgContLang;
812
813 $msg = $wgContLang->lc( $this->name() ) . '-summary';
814 $out = wfMsgNoTrans( $msg );
815 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
816 $wgOut->addWikiText( $out );
817 }
818
819 }
820
821 # Returns the name that goes in the <h1> in the special page itself, and also the name that
822 # will be listed in Special:Specialpages
823 #
824 # Derived classes can override this, but usually it is easier to keep the default behaviour.
825 # Messages can be added at run-time, see MessageCache.php
826 function getDescription() {
827 return wfMsg( strtolower( $this->mName ) );
828 }
829
830 /**
831 * Get a self-referential title object
832 */
833 function getTitle( $subpage = false) {
834 return self::getTitleFor( $this->mName, $subpage );
835 }
836
837 /**
838 * Set whether this page is listed in Special:Specialpages, at run-time
839 */
840 function setListed( $listed ) {
841 return wfSetVar( $this->mListed, $listed );
842 }
843
844 /**
845 * If the special page is a redirect, then get the Title object it redirects to.
846 * False otherwise.
847 */
848 function getRedirect( $subpage ) {
849 return false;
850 }
851
852 /**
853 * Return part of the request string for a special redirect page
854 * This allows passing, e.g. action=history to Special:Mypage, etc.
855 *
856 * @return string
857 */
858 function getRedirectQuery() {
859 global $wgRequest;
860 $params = array();
861 foreach( $this->mAllowedRedirectParams as $arg ) {
862 if( $val = $wgRequest->getVal( $arg, false ) )
863 $params[] = $arg . '=' . $val;
864 }
865
866 return count( $params ) ? implode( '&', $params ) : false;
867 }
868 }
869
870 /**
871 * Shortcut to construct a special page which is unlisted by default
872 * @addtogroup SpecialPage
873 */
874 class UnlistedSpecialPage extends SpecialPage
875 {
876 function UnlistedSpecialPage( $name, $restriction = '', $function = false, $file = 'default' ) {
877 SpecialPage::SpecialPage( $name, $restriction, false, $function, $file );
878 }
879 }
880
881 /**
882 * Shortcut to construct an includable special page
883 * @addtogroup SpecialPage
884 */
885 class IncludableSpecialPage extends SpecialPage
886 {
887 function IncludableSpecialPage( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
888 SpecialPage::SpecialPage( $name, $restriction, $listed, $function, $file, true );
889 }
890 }
891
892 /**
893 * Shortcut to construct a special page alias.
894 * @addtogroup SpecialPage
895 */
896 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
897 var $redirName, $redirSubpage;
898
899 function __construct( $name, $redirName, $redirSubpage = false, $redirectParams = array() ) {
900 parent::__construct( $name );
901 $this->redirName = $redirName;
902 $this->redirSubpage = $redirSubpage;
903 $this->mAllowedRedirectParams = $redirectParams;
904 }
905
906 function getRedirect( $subpage ) {
907 if ( $this->redirSubpage === false ) {
908 return SpecialPage::getTitleFor( $this->redirName, $subpage );
909 } else {
910 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
911 }
912 }
913 }
914
915 /** SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
916 * are used to get user independant links pointing to the user page, talk
917 * page and list of contributions.
918 * This can let us cache a single copy of any generated content for all
919 * users.
920 */
921
922 /**
923 * Shortcut to construct a special page pointing to current user user's page.
924 * @addtogroup SpecialPage
925 */
926 class SpecialMypage extends UnlistedSpecialPage {
927 function __construct() {
928 parent::__construct( 'Mypage' );
929 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
930 }
931
932 function getRedirect( $subpage ) {
933 global $wgUser;
934 if ( strval( $subpage ) !== '' ) {
935 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
936 } else {
937 return Title::makeTitle( NS_USER, $wgUser->getName() );
938 }
939 }
940 }
941
942 /**
943 * Shortcut to construct a special page pointing to current user talk page.
944 * @addtogroup SpecialPage
945 */
946 class SpecialMytalk extends UnlistedSpecialPage {
947 function __construct() {
948 parent::__construct( 'Mytalk' );
949 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro', 'section' );
950 }
951
952 function getRedirect( $subpage ) {
953 global $wgUser;
954 if ( strval( $subpage ) !== '' ) {
955 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
956 } else {
957 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
958 }
959 }
960 }
961
962 /**
963 * Shortcut to construct a special page pointing to current user contributions.
964 * @addtogroup SpecialPage
965 */
966 class SpecialMycontributions extends UnlistedSpecialPage {
967 function __construct() {
968 parent::__construct( 'Mycontributions' );
969 }
970
971 function getRedirect( $subpage ) {
972 global $wgUser;
973 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
974 }
975 }