(bug 268) add Special:PermanentLink/### as an internally-linkable redirect to index...
[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' => 'BrokenRedirectsPage',
88 'Deadendpages' => 'DeadendpagesPage',
89 'DoubleRedirects' => 'DoubleRedirectsPage',
90 'Longpages' => 'LongpagesPage',
91 'Ancientpages' => 'AncientpagesPage',
92 'Lonelypages' => 'LonelypagesPage',
93 'Fewestrevisions' => 'FewestrevisionsPage',
94 'Withoutinterwiki' => 'WithoutinterwikiPage',
95 'Protectedpages' => 'SpecialProtectedpages',
96 'Protectedtitles' => 'SpecialProtectedtitles',
97 'Shortpages' => 'ShortpagesPage',
98 'Uncategorizedcategories' => 'UncategorizedcategoriesPage',
99 'Uncategorizedimages' => 'UncategorizedimagesPage',
100 'Uncategorizedpages' => 'UncategorizedpagesPage',
101 'Uncategorizedtemplates' => 'UncategorizedtemplatesPage',
102 'Unusedcategories' => 'UnusedcategoriesPage',
103 'Unusedimages' => 'UnusedimagesPage',
104 'Unusedtemplates' => 'UnusedtemplatesPage',
105 'Unwatchedpages' => 'UnwatchedpagesPage',
106 'Wantedcategories' => 'WantedcategoriesPage',
107 'Wantedfiles' => 'WantedfilesPage',
108 'Wantedpages' => 'WantedpagesPage',
109 'Wantedtemplates' => 'WantedtemplatesPage',
110
111 # List of pages
112 'Allpages' => 'SpecialAllpages',
113 'Prefixindex' => 'SpecialPrefixindex',
114 'Categories' => 'SpecialCategories',
115 'Disambiguations' => 'DisambiguationsPage',
116 'Listredirects' => 'ListredirectsPage',
117
118 # Login/create account
119 'Userlogin' => 'LoginForm',
120 'CreateAccount' => array( 'SpecialRedirectToSpecial', 'CreateAccount', 'Userlogin', 'signup', array( 'uselang' ) ),
121
122 # Users and rights
123 'Blockip' => 'IPBlockForm',
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 'Listadmins' => array( 'SpecialRedirectToSpecial', 'Listadmins', 'Listusers', 'sysop' ),
133 'Listbots' => array( 'SpecialRedirectToSpecial', 'Listbots', 'Listusers', 'bot' ),
134 'Activeusers' => 'SpecialActiveUsers',
135 'Userrights' => 'UserrightsPage',
136 'DisableAccount' => 'SpecialDisableAccount',
137
138 # Recent changes and logs
139 'Newimages' => array( 'IncludableSpecialPage', 'Newimages' ),
140 'Log' => 'SpecialLog',
141 'Watchlist' => array( 'SpecialPage', 'Watchlist' ),
142 'Newpages' => 'SpecialNewpages',
143 'Recentchanges' => 'SpecialRecentchanges',
144 'Recentchangeslinked' => 'SpecialRecentchangeslinked',
145 'Tags' => 'SpecialTags',
146
147 # Media reports and uploads
148 'Listfiles' => array( 'SpecialPage', 'Listfiles' ),
149 'Filepath' => 'SpecialFilepath',
150 'MIMEsearch' => 'MIMEsearchPage',
151 'FileDuplicateSearch' => 'FileDuplicateSearchPage',
152 'Upload' => 'SpecialUpload',
153 'UploadStash' => 'SpecialUploadStash',
154
155 # Wiki data and tools
156 'Statistics' => 'SpecialStatistics',
157 'Allmessages' => 'SpecialAllmessages',
158 'Version' => 'SpecialVersion',
159 'Lockdb' => 'SpecialLockdb',
160 'Unlockdb' => 'SpecialUnlockdb',
161
162 # Redirecting special pages
163 'LinkSearch' => 'LinkSearchPage',
164 'Randompage' => 'Randompage',
165 'Randomredirect' => 'SpecialRandomredirect',
166
167 # High use pages
168 'Mostlinkedcategories' => 'MostlinkedCategoriesPage',
169 'Mostimages' => 'MostimagesPage',
170 'Mostlinked' => 'MostlinkedPage',
171 'Mostlinkedtemplates' => 'MostlinkedTemplatesPage',
172 'Mostcategories' => 'MostcategoriesPage',
173 'Mostrevisions' => 'MostrevisionsPage',
174
175 # Page tools
176 'ComparePages' => 'SpecialComparePages',
177 'Export' => 'SpecialExport',
178 'Import' => 'SpecialImport',
179 'Undelete' => 'UndeleteForm',
180 'Whatlinkshere' => 'SpecialWhatlinkshere',
181 'MergeHistory' => 'SpecialMergeHistory',
182
183 # Other
184 'Booksources' => 'SpecialBookSources',
185
186 # Unlisted / redirects
187 'Blankpage' => 'SpecialBlankpage',
188 'Blockme' => 'SpecialBlockme',
189 'Emailuser' => 'SpecialEmailUser',
190 'Movepage' => 'MovePageForm',
191 'Mycontributions' => 'SpecialMycontributions',
192 'Mypage' => 'SpecialMypage',
193 'Mytalk' => 'SpecialMytalk',
194 'Myuploads' => 'SpecialMyuploads',
195 'PermanentLink' => 'SpecialPermanentLink',
196 'Revisiondelete' => 'SpecialRevisionDelete',
197 'RevisionMove' => 'SpecialRevisionMove',
198 'Specialpages' => 'SpecialSpecialpages',
199 'Userlogout' => 'SpecialUserlogout',
200 );
201
202 static public $mAliases;
203 static public $mListInitialised = false;
204
205 /**#@-*/
206
207 /**
208 * Initialise the special page list
209 * This must be called before accessing SpecialPage::$mList
210 */
211 static function initList() {
212 global $wgSpecialPages;
213 global $wgDisableCounters, $wgDisableInternalSearch, $wgEmailAuthentication;
214
215 if ( self::$mListInitialised ) {
216 return;
217 }
218 wfProfileIn( __METHOD__ );
219
220 # Better to set this now, to avoid infinite recursion in carelessly written hooks
221 self::$mListInitialised = true;
222
223 if( !$wgDisableCounters ) {
224 self::$mList['Popularpages'] = 'PopularpagesPage';
225 }
226
227 if( !$wgDisableInternalSearch ) {
228 self::$mList['Search'] = 'SpecialSearch';
229 }
230
231 if( $wgEmailAuthentication ) {
232 self::$mList['Confirmemail'] = 'EmailConfirmation';
233 self::$mList['Invalidateemail'] = 'EmailInvalidation';
234 }
235
236 # Add extension special pages
237 self::$mList = array_merge( self::$mList, $wgSpecialPages );
238
239 # Run hooks
240 # This hook can be used to remove undesired built-in special pages
241 wfRunHooks( 'SpecialPage_initList', array( &self::$mList ) );
242 wfProfileOut( __METHOD__ );
243 }
244
245 static function initAliasList() {
246 if ( !is_null( self::$mAliases ) ) {
247 return;
248 }
249
250 global $wgContLang;
251 $aliases = $wgContLang->getSpecialPageAliases();
252 $missingPages = self::$mList;
253 self::$mAliases = array();
254 foreach ( $aliases as $realName => $aliasList ) {
255 foreach ( $aliasList as $alias ) {
256 self::$mAliases[$wgContLang->caseFold( $alias )] = $realName;
257 }
258 unset( $missingPages[$realName] );
259 }
260 foreach ( $missingPages as $name => $stuff ) {
261 self::$mAliases[$wgContLang->caseFold( $name )] = $name;
262 }
263 }
264
265 /**
266 * Given a special page alias, return the special page name.
267 * Returns false if there is no such alias.
268 *
269 * @param $alias String
270 * @return String or false
271 */
272 static function resolveAlias( $alias ) {
273 global $wgContLang;
274
275 if ( !self::$mListInitialised ) self::initList();
276 if ( is_null( self::$mAliases ) ) self::initAliasList();
277 $caseFoldedAlias = $wgContLang->caseFold( $alias );
278 $caseFoldedAlias = str_replace( ' ', '_', $caseFoldedAlias );
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 $query = $page->getRedirectQuery();
546 if ( $redirect instanceof Title ) {
547 $url = $redirect->getFullUrl( $query );
548 $wgOut->redirect( $url );
549 wfProfileOut( __METHOD__ );
550 return $redirect;
551 } elseif( $redirect === true ) {
552 global $wgScript;
553 $url = $wgScript . '?' . wfArrayToCGI( $query );
554 $wgOut->redirect( $url );
555 wfProfileOut( __METHOD__ );
556 return $redirect;
557 }
558 }
559
560 # Redirect to canonical alias for GET commands
561 # Not for POST, we'd lose the post data, so it's best to just distribute
562 # the request. Such POST requests are possible for old extensions that
563 # generate self-links without being aware that their default name has
564 # changed.
565 if ( !$including && $name != $page->getLocalName() && !$wgRequest->wasPosted() ) {
566 $query = $_GET;
567 unset( $query['title'] );
568 $query = wfArrayToCGI( $query );
569 $title = $page->getTitle( $par );
570 $url = $title->getFullUrl( $query );
571 $wgOut->redirect( $url );
572 wfProfileOut( __METHOD__ );
573 return $redirect;
574 }
575
576 if ( $including && !$page->includable() ) {
577 wfProfileOut( __METHOD__ );
578 return false;
579 } elseif ( !$including ) {
580 $wgTitle = $page->getTitle();
581 }
582 $page->including( $including );
583
584 // Execute special page
585 $profName = 'Special:' . $page->name();
586 wfProfileIn( $profName );
587 $page->execute( $par );
588 wfProfileOut( $profName );
589 wfProfileOut( __METHOD__ );
590 return true;
591 }
592
593 /**
594 * Just like executePath() except it returns the HTML instead of outputting it
595 * Returns false if there was no such special page, or a title object if it was
596 * a redirect.
597 *
598 * @return String: HTML fragment
599 */
600 static function capturePath( &$title ) {
601 global $wgOut, $wgTitle;
602
603 $oldTitle = $wgTitle;
604 $oldOut = $wgOut;
605 $wgOut = new OutputPage;
606 $wgOut->setTitle( $title );
607
608 $ret = SpecialPage::executePath( $title, true );
609 if ( $ret === true ) {
610 $ret = $wgOut->getHTML();
611 }
612 $wgTitle = $oldTitle;
613 $wgOut = $oldOut;
614 return $ret;
615 }
616
617 /**
618 * Get the local name for a specified canonical name
619 *
620 * @param $name String
621 * @param $subpage Mixed: boolean false, or string
622 *
623 * @return String
624 */
625 static function getLocalNameFor( $name, $subpage = false ) {
626 global $wgContLang;
627 $aliases = $wgContLang->getSpecialPageAliases();
628 if ( isset( $aliases[$name][0] ) ) {
629 $name = $aliases[$name][0];
630 } else {
631 // Try harder in case someone misspelled the correct casing
632 $found = false;
633 foreach ( $aliases as $n => $values ) {
634 if ( strcasecmp( $name, $n ) === 0 ) {
635 wfWarn( "Found alias defined for $n when searching for" .
636 "special page aliases for $name. Case mismatch?" );
637 $name = $values[0];
638 $found = true;
639 break;
640 }
641 }
642 if ( !$found ) {
643 wfWarn( "Did not find alias for special page '$name'. " .
644 "Perhaps no aliases are defined for it?" );
645 }
646 }
647 if ( $subpage !== false && !is_null( $subpage ) ) {
648 $name = "$name/$subpage";
649 }
650 return $wgContLang->ucfirst( $name );
651 }
652
653 /**
654 * Get a localised Title object for a specified special page name
655 *
656 * @return Title object
657 */
658 static function getTitleFor( $name, $subpage = false ) {
659 $name = self::getLocalNameFor( $name, $subpage );
660 if ( $name ) {
661 return Title::makeTitle( NS_SPECIAL, $name );
662 } else {
663 throw new MWException( "Invalid special page name \"$name\"" );
664 }
665 }
666
667 /**
668 * Get a localised Title object for a page name with a possibly unvalidated subpage
669 *
670 * @return Title object or null if the page doesn't exist
671 */
672 static function getSafeTitleFor( $name, $subpage = false ) {
673 $name = self::getLocalNameFor( $name, $subpage );
674 if ( $name ) {
675 return Title::makeTitleSafe( NS_SPECIAL, $name );
676 } else {
677 return null;
678 }
679 }
680
681 /**
682 * Get a title for a given alias
683 *
684 * @return Title or null if there is no such alias
685 */
686 static function getTitleForAlias( $alias ) {
687 $name = self::resolveAlias( $alias );
688 if ( $name ) {
689 return self::getTitleFor( $name );
690 } else {
691 return null;
692 }
693 }
694
695 /**
696 * Default constructor for special pages
697 * Derivative classes should call this from their constructor
698 * Note that if the user does not have the required level, an error message will
699 * be displayed by the default execute() method, without the global function ever
700 * being called.
701 *
702 * If you override execute(), you can recover the default behaviour with userCanExecute()
703 * and displayRestrictionError()
704 *
705 * @param $name String: name of the special page, as seen in links and URLs
706 * @param $restriction String: user right required, e.g. "block" or "delete"
707 * @param $listed Boolean: whether the page is listed in Special:Specialpages
708 * @param $function Callback: function called by execute(). By default it is constructed from $name
709 * @param $file String: file which is included by execute(). It is also constructed from $name by default
710 * @param $includable Boolean: whether the page can be included in normal pages
711 */
712 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
713 $this->init( $name, $restriction, $listed, $function, $file, $includable );
714 }
715
716 /**
717 * Do the real work for the constructor, mainly so __call() can intercept
718 * calls to SpecialPage()
719 * @see __construct() for param docs
720 */
721 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
722 $this->mName = $name;
723 $this->mRestriction = $restriction;
724 $this->mListed = $listed;
725 $this->mIncludable = $includable;
726 if ( !$function ) {
727 $this->mFunction = 'wfSpecial'.$name;
728 } else {
729 $this->mFunction = $function;
730 }
731 if ( $file === 'default' ) {
732 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
733 } else {
734 $this->mFile = $file;
735 }
736 }
737
738 /**
739 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
740 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
741 *
742 * @param $fName String Name of called method
743 * @param $a Array Arguments to the method
744 * @deprecated Call isn't deprecated, but SpecialPage::SpecialPage() is
745 */
746 public function __call( $fName, $a ) {
747 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
748 if( strtolower( $fName ) == 'specialpage' ) {
749 // Debug messages now, warnings in 1.19 or 1.20?
750 wfDebug( "Deprecated SpecialPage::SpecialPage() called, use __construct();\n" );
751 $name = isset( $a[0] ) ? $a[0] : '';
752 $restriction = isset( $a[1] ) ? $a[1] : '';
753 $listed = isset( $a[2] ) ? $a[2] : true;
754 $function = isset( $a[3] ) ? $a[3] : false;
755 $file = isset( $a[4] ) ? $a[4] : 'default';
756 $includable = isset( $a[5] ) ? $a[5] : false;
757 $this->init( $name, $restriction, $listed, $function, $file, $includable );
758 } else {
759 $className = get_class( $this );
760 throw new MWException( "Call to undefined method $className::$fName" );
761 }
762 }
763
764 /**#@+
765 * Accessor
766 *
767 * @deprecated
768 */
769 function getName() { return $this->mName; }
770 function getRestriction() { return $this->mRestriction; }
771 function getFile() { return $this->mFile; }
772 function isListed() { return $this->mListed; }
773 /**#@-*/
774
775 /**#@+
776 * Accessor and mutator
777 */
778 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
779 function restrictions( $x = null) {
780 # Use the one below this
781 wfDeprecated( __METHOD__ );
782 return wfSetVar( $this->mRestriction, $x );
783 }
784 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
785 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
786 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
787 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
788 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
789 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
790 /**#@-*/
791
792 /**
793 * Get the localised name of the special page
794 */
795 function getLocalName() {
796 if ( !isset( $this->mLocalName ) ) {
797 $this->mLocalName = self::getLocalNameFor( $this->mName );
798 }
799 return $this->mLocalName;
800 }
801
802 /**
803 * Can be overridden by subclasses with more complicated permissions
804 * schemes.
805 *
806 * @return Boolean: should the page be displayed with the restricted-access
807 * pages?
808 */
809 public function isRestricted() {
810 global $wgGroupPermissions;
811 // DWIM: If all anons can do something, then it is not restricted
812 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
813 }
814
815 /**
816 * Checks if the given user (identified by an object) can execute this
817 * special page (as defined by $mRestriction). Can be overridden by sub-
818 * classes with more complicated permissions schemes.
819 *
820 * @param $user User: the user to check
821 * @return Boolean: does the user have permission to view the page?
822 */
823 public function userCanExecute( $user ) {
824 return $user->isAllowed( $this->mRestriction );
825 }
826
827 /**
828 * Output an error message telling the user what access level they have to have
829 */
830 function displayRestrictionError() {
831 global $wgOut;
832 $wgOut->permissionRequired( $this->mRestriction );
833 }
834
835 /**
836 * Sets headers - this should be called from the execute() method of all derived classes!
837 */
838 function setHeaders() {
839 global $wgOut;
840 $wgOut->setArticleRelated( false );
841 $wgOut->setRobotPolicy( "noindex,nofollow" );
842 $wgOut->setPageTitle( $this->getDescription() );
843 }
844
845 /**
846 * Default execute method
847 * Checks user permissions, calls the function given in mFunction
848 *
849 * This may be overridden by subclasses.
850 */
851 function execute( $par ) {
852 global $wgUser;
853
854 $this->setHeaders();
855
856 if ( $this->userCanExecute( $wgUser ) ) {
857 $func = $this->mFunction;
858 // only load file if the function does not exist
859 if(!is_callable($func) and $this->mFile) {
860 require_once( $this->mFile );
861 }
862 $this->outputHeader();
863 call_user_func( $func, $par, $this );
864 } else {
865 $this->displayRestrictionError();
866 }
867 }
868
869 /**
870 * Outputs a summary message on top of special pages
871 * Per default the message key is the canonical name of the special page
872 * May be overriden, i.e. by extensions to stick with the naming conventions
873 * for message keys: 'extensionname-xxx'
874 *
875 * @param $summaryMessageKey String: message key of the summary
876 */
877 function outputHeader( $summaryMessageKey = '' ) {
878 global $wgOut, $wgContLang;
879
880 if( $summaryMessageKey == '' ) {
881 $msg = $wgContLang->lc( $this->name() ) . '-summary';
882 } else {
883 $msg = $summaryMessageKey;
884 }
885 $out = wfMsgNoTrans( $msg );
886 if ( ! wfEmptyMsg( $msg, $out ) and $out !== '' and ! $this->including() ) {
887 $wgOut->wrapWikiMsg( "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
888 }
889
890 }
891
892 /**
893 * Returns the name that goes in the \<h1\> in the special page itself, and
894 * also the name that will be listed in Special:Specialpages
895 *
896 * Derived classes can override this, but usually it is easier to keep the
897 * default behaviour. Messages can be added at run-time, see
898 * MessageCache.php.
899 *
900 * @return String
901 */
902 function getDescription() {
903 return wfMsg( strtolower( $this->mName ) );
904 }
905
906 /**
907 * Get a self-referential title object
908 *
909 * @return Title object
910 */
911 function getTitle( $subpage = false ) {
912 return self::getTitleFor( $this->mName, $subpage );
913 }
914
915 /**
916 * Set whether this page is listed in Special:Specialpages, at run-time
917 */
918 function setListed( $listed ) {
919 return wfSetVar( $this->mListed, $listed );
920 }
921
922 /**
923 * If the special page is a redirect, then get the Title object it redirects to.
924 * False otherwise.
925 */
926 function getRedirect( $subpage ) {
927 return false;
928 }
929
930 /**
931 * Return part of the request string for a special redirect page
932 * This allows passing, e.g. action=history to Special:Mypage, etc.
933 *
934 * @return String
935 */
936 function getRedirectQuery() {
937 global $wgRequest;
938 $params = array();
939
940 foreach( $this->mAllowedRedirectParams as $arg ) {
941 if( $wgRequest->getVal( $arg, null ) !== null ){
942 $params[$arg] = $wgRequest->getVal( $arg );
943 }
944 }
945
946 foreach( $this->mAddedRedirectParams as $arg => $val ) {
947 $params[$arg] = $val;
948 }
949
950 return count( $params )
951 ? $params
952 : false;
953 }
954 }
955
956 /**
957 * Shortcut to construct a special page which is unlisted by default
958 * @ingroup SpecialPage
959 */
960 class UnlistedSpecialPage extends SpecialPage
961 {
962 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
963 parent::__construct( $name, $restriction, false, $function, $file );
964 }
965 }
966
967 /**
968 * Shortcut to construct an includable special page
969 * @ingroup SpecialPage
970 */
971 class IncludableSpecialPage extends SpecialPage
972 {
973 function __construct( $name, $restriction = '', $listed = true, $function = false, $file = 'default' ) {
974 parent::__construct( $name, $restriction, $listed, $function, $file, true );
975 }
976 }
977
978 /**
979 * Shortcut to construct a special page alias.
980 * @ingroup SpecialPage
981 */
982 class SpecialRedirectToSpecial extends UnlistedSpecialPage {
983 var $redirName, $redirSubpage;
984
985 function __construct( $name, $redirName, $redirSubpage = false, $allowedRedirectParams = array(), $addedRedirectParams = array() ) {
986 parent::__construct( $name );
987 $this->redirName = $redirName;
988 $this->redirSubpage = $redirSubpage;
989 $this->mAllowedRedirectParams = $allowedRedirectParams;
990 $this->mAddedRedirectParams = $addedRedirectParams;
991 }
992
993 function getRedirect( $subpage ) {
994 if ( $this->redirSubpage === false ) {
995 return SpecialPage::getTitleFor( $this->redirName, $subpage );
996 } else {
997 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
998 }
999 }
1000 }
1001
1002 /**
1003 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1004 * are used to get user independant links pointing to the user page, talk
1005 * page and list of contributions.
1006 * This can let us cache a single copy of any generated content for all
1007 * users.
1008 */
1009
1010 /**
1011 * Shortcut to construct a special page pointing to current user user's page.
1012 * @ingroup SpecialPage
1013 */
1014 class SpecialMypage extends UnlistedSpecialPage {
1015 function __construct() {
1016 parent::__construct( 'Mypage' );
1017 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1018 'section', 'oldid', 'diff', 'dir' );
1019 }
1020
1021 function getRedirect( $subpage ) {
1022 global $wgUser;
1023 if ( strval( $subpage ) !== '' ) {
1024 return Title::makeTitle( NS_USER, $wgUser->getName() . '/' . $subpage );
1025 } else {
1026 return Title::makeTitle( NS_USER, $wgUser->getName() );
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Shortcut to construct a special page pointing to current user talk page.
1033 * @ingroup SpecialPage
1034 */
1035 class SpecialMytalk extends UnlistedSpecialPage {
1036 function __construct() {
1037 parent::__construct( 'Mytalk' );
1038 $this->mAllowedRedirectParams = array( 'action' , 'preload' , 'editintro',
1039 'section', 'oldid', 'diff', 'dir' );
1040 }
1041
1042 function getRedirect( $subpage ) {
1043 global $wgUser;
1044 if ( strval( $subpage ) !== '' ) {
1045 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() . '/' . $subpage );
1046 } else {
1047 return Title::makeTitle( NS_USER_TALK, $wgUser->getName() );
1048 }
1049 }
1050 }
1051
1052 /**
1053 * Shortcut to construct a special page pointing to current user contributions.
1054 * @ingroup SpecialPage
1055 */
1056 class SpecialMycontributions extends UnlistedSpecialPage {
1057 function __construct() {
1058 parent::__construct( 'Mycontributions' );
1059 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1060 'offset', 'dir', 'year', 'month', 'feed' );
1061 }
1062
1063 function getRedirect( $subpage ) {
1064 global $wgUser;
1065 return SpecialPage::getTitleFor( 'Contributions', $wgUser->getName() );
1066 }
1067 }
1068
1069 /**
1070 * Redirect to Special:Listfiles?user=$wgUser
1071 */
1072 class SpecialMyuploads extends UnlistedSpecialPage {
1073 function __construct() {
1074 parent::__construct( 'Myuploads' );
1075 $this->mAllowedRedirectParams = array( 'limit' );
1076 }
1077
1078 function getRedirect( $subpage ) {
1079 global $wgUser;
1080 return SpecialPage::getTitleFor( 'Listfiles', $wgUser->getName() );
1081 }
1082 }
1083
1084 /**
1085 * Redirect to Special:Listfiles?user=$wgUser
1086 */
1087 class SpecialPermanentLink extends UnlistedSpecialPage {
1088 function __construct() {
1089 parent::__construct( 'PermanentLink' );
1090 $this->mAllowedRedirectParams = array();
1091 }
1092
1093 function getRedirect( $subpage ) {
1094 $subpage = intval( $subpage );
1095 $this->mAddedRedirectParams['oldid'] = $subpage;
1096 return true;
1097 }
1098 }