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