Don't look for pipes in the root node.
[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' => 'SpecialUndelete',
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 $msg = wfMessage('specialpages-specialpagegroup-'.strtolower($page->mName));
345 if ( !$msg->isBlank() ) {
346 $group = $msg->text();
347 } else {
348 $group = isset($wgSpecialPageGroups[$page->mName])
349 ? $wgSpecialPageGroups[$page->mName]
350 : '-';
351 }
352 if( $group == '-' ) $group = 'other';
353 $specialPageGroupsCache[$page->mName] = $group;
354 return $group;
355 }
356
357 /**
358 * Remove a special page from the list
359 * Formerly used to disable expensive or dangerous special pages. The
360 * preferred method is now to add a SpecialPage_initList hook.
361 */
362 static function removePage( $name ) {
363 if ( !self::$mListInitialised ) {
364 self::initList();
365 }
366 unset( self::$mList[$name] );
367 }
368
369 /**
370 * Check if a given name exist as a special page or as a special page alias
371 *
372 * @param $name String: name of a special page
373 * @return Boolean: true if a special page exists with this name
374 */
375 static function exists( $name ) {
376 global $wgContLang;
377 if ( !self::$mListInitialised ) {
378 self::initList();
379 }
380 if( !self::$mAliases ) {
381 self::initAliasList();
382 }
383
384 # Remove special pages inline parameters:
385 $bits = explode( '/', $name );
386 $name = $wgContLang->caseFold($bits[0]);
387
388 return
389 array_key_exists( $name, self::$mList )
390 or array_key_exists( $name, self::$mAliases )
391 ;
392 }
393
394 /**
395 * Find the object with a given name and return it (or NULL)
396 *
397 * @param $name String
398 * @return SpecialPage object or null if the page doesn't exist
399 */
400 static function getPage( $name ) {
401 if ( !self::$mListInitialised ) {
402 self::initList();
403 }
404 if ( array_key_exists( $name, self::$mList ) ) {
405 $rec = self::$mList[$name];
406 if ( is_string( $rec ) ) {
407 $className = $rec;
408 self::$mList[$name] = new $className;
409 } elseif ( is_array( $rec ) ) {
410 $className = array_shift( $rec );
411 self::$mList[$name] = MWFunction::newObj( $className, $rec );
412 }
413 return self::$mList[$name];
414 } else {
415 return null;
416 }
417 }
418
419 /**
420 * Get a special page with a given localised name, or NULL if there
421 * is no such special page.
422 *
423 * @return SpecialPage object or null if the page doesn't exist
424 */
425 static function getPageByAlias( $alias ) {
426 $realName = self::resolveAlias( $alias );
427 if ( $realName ) {
428 return self::getPage( $realName );
429 } else {
430 return null;
431 }
432 }
433
434 /**
435 * Return categorised listable special pages which are available
436 * for the current user, and everyone.
437 *
438 * @return Associative array mapping page's name to its SpecialPage object
439 */
440 static function getUsablePages() {
441 global $wgUser;
442 if ( !self::$mListInitialised ) {
443 self::initList();
444 }
445 $pages = array();
446
447 foreach ( self::$mList as $name => $rec ) {
448 $page = self::getPage( $name );
449 if ( $page->isListed()
450 && (
451 !$page->isRestricted()
452 || $page->userCanExecute( $wgUser )
453 )
454 ) {
455 $pages[$name] = $page;
456 }
457 }
458 return $pages;
459 }
460
461 /**
462 * Return categorised listable special pages for all users
463 *
464 * @return Associative array mapping page's name to its SpecialPage object
465 */
466 static function getRegularPages() {
467 if ( !self::$mListInitialised ) {
468 self::initList();
469 }
470 $pages = array();
471
472 foreach ( self::$mList as $name => $rec ) {
473 $page = self::getPage( $name );
474 if ( $page->isListed() && !$page->isRestricted() ) {
475 $pages[$name] = $page;
476 }
477 }
478 return $pages;
479 }
480
481 /**
482 * Return categorised listable special pages which are available
483 * for the current user, but not for everyone
484 *
485 * @return Associative array mapping page's name to its SpecialPage object
486 */
487 static function getRestrictedPages() {
488 global $wgUser;
489 if( !self::$mListInitialised ) {
490 self::initList();
491 }
492 $pages = array();
493
494 foreach( self::$mList as $name => $rec ) {
495 $page = self::getPage( $name );
496 if(
497 $page->isListed()
498 && $page->isRestricted()
499 && $page->userCanExecute( $wgUser )
500 ) {
501 $pages[$name] = $page;
502 }
503 }
504 return $pages;
505 }
506
507 /**
508 * Execute a special page path.
509 * The path may contain parameters, e.g. Special:Name/Params
510 * Extracts the special page name and call the execute method, passing the parameters
511 *
512 * Returns a title object if the page is redirected, false if there was no such special
513 * page, and true if it was successful.
514 *
515 * @param $title a title object
516 * @param $including output is being captured for use in {{special:whatever}}
517 */
518 static function executePath( &$title, $including = false ) {
519 global $wgOut, $wgTitle, $wgRequest;
520 wfProfileIn( __METHOD__ );
521
522 # FIXME: redirects broken due to this call
523 $bits = explode( '/', $title->getDBkey(), 2 );
524 $name = $bits[0];
525 if( !isset( $bits[1] ) ) { // bug 2087
526 $par = null;
527 } else {
528 $par = $bits[1];
529 }
530 $page = SpecialPage::getPageByAlias( $name );
531 # Nonexistent?
532 if ( !$page ) {
533 if ( !$including ) {
534 $wgOut->setArticleRelated( false );
535 $wgOut->setRobotPolicy( 'noindex,nofollow' );
536 $wgOut->setStatusCode( 404 );
537 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
538 }
539 wfProfileOut( __METHOD__ );
540 return false;
541 }
542
543 # Check for redirect
544 if ( !$including ) {
545 $redirect = $page->getRedirect( $par );
546 $query = $page->getRedirectQuery();
547 if ( $redirect instanceof Title ) {
548 $url = $redirect->getFullUrl( $query );
549 $wgOut->redirect( $url );
550 wfProfileOut( __METHOD__ );
551 return $redirect;
552 } elseif( $redirect === true ) {
553 global $wgScript;
554 $url = $wgScript . '?' . wfArrayToCGI( $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->name();
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 *
599 * @return String: HTML fragment
600 */
601 static function capturePath( &$title ) {
602 global $wgOut, $wgTitle;
603
604 $oldTitle = $wgTitle;
605 $oldOut = $wgOut;
606 $wgOut = new OutputPage;
607 $wgOut->setTitle( $title );
608
609 $ret = SpecialPage::executePath( $title, true );
610 if ( $ret === true ) {
611 $ret = $wgOut->getHTML();
612 }
613 $wgTitle = $oldTitle;
614 $wgOut = $oldOut;
615 return $ret;
616 }
617
618 /**
619 * Get the local name for a specified canonical name
620 *
621 * @param $name String
622 * @param $subpage Mixed: boolean false, or string
623 *
624 * @return String
625 */
626 static function getLocalNameFor( $name, $subpage = false ) {
627 global $wgContLang;
628 $aliases = $wgContLang->getSpecialPageAliases();
629 if ( isset( $aliases[$name][0] ) ) {
630 $name = $aliases[$name][0];
631 } else {
632 // Try harder in case someone misspelled the correct casing
633 $found = false;
634 foreach ( $aliases as $n => $values ) {
635 if ( strcasecmp( $name, $n ) === 0 ) {
636 wfWarn( "Found alias defined for $n when searching for" .
637 "special page aliases for $name. Case mismatch?" );
638 $name = $values[0];
639 $found = true;
640 break;
641 }
642 }
643 if ( !$found ) {
644 wfWarn( "Did not find alias for special page '$name'. " .
645 "Perhaps no aliases are defined for it?" );
646 }
647 }
648 if ( $subpage !== false && !is_null( $subpage ) ) {
649 $name = "$name/$subpage";
650 }
651 return $wgContLang->ucfirst( $name );
652 }
653
654 /**
655 * Get a localised Title object for a specified special page name
656 *
657 * @return Title object
658 */
659 static function getTitleFor( $name, $subpage = false ) {
660 $name = self::getLocalNameFor( $name, $subpage );
661 if ( $name ) {
662 return Title::makeTitle( NS_SPECIAL, $name );
663 } else {
664 throw new MWException( "Invalid special page name \"$name\"" );
665 }
666 }
667
668 /**
669 * Get a localised Title object for a page name with a possibly unvalidated subpage
670 *
671 * @return Title object or null if the page doesn't exist
672 */
673 static function getSafeTitleFor( $name, $subpage = false ) {
674 $name = self::getLocalNameFor( $name, $subpage );
675 if ( $name ) {
676 return Title::makeTitleSafe( NS_SPECIAL, $name );
677 } else {
678 return null;
679 }
680 }
681
682 /**
683 * Get a title for a given alias
684 *
685 * @return Title or null if there is no such alias
686 */
687 static function getTitleForAlias( $alias ) {
688 $name = self::resolveAlias( $alias );
689 if ( $name ) {
690 return self::getTitleFor( $name );
691 } else {
692 return null;
693 }
694 }
695
696 /**
697 * Default constructor for special pages
698 * Derivative classes should call this from their constructor
699 * Note that if the user does not have the required level, an error message will
700 * be displayed by the default execute() method, without the global function ever
701 * being called.
702 *
703 * If you override execute(), you can recover the default behaviour with userCanExecute()
704 * and displayRestrictionError()
705 *
706 * @param $name String: name of the special page, as seen in links and URLs
707 * @param $restriction String: user right required, e.g. "block" or "delete"
708 * @param $listed Boolean: whether the page is listed in Special:Specialpages
709 * @param $function Callback: function called by execute(). By default it is constructed from $name
710 * @param $file String: file which is included by execute(). It is also constructed from $name by default
711 * @param $includable Boolean: whether the page can be included in normal pages
712 */
713 public function __construct( $name = '', $restriction = '', $listed = true, $function = false, $file = 'default', $includable = false ) {
714 $this->init( $name, $restriction, $listed, $function, $file, $includable );
715 }
716
717 /**
718 * Do the real work for the constructor, mainly so __call() can intercept
719 * calls to SpecialPage()
720 * @see __construct() for param docs
721 */
722 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
723 $this->mName = $name;
724 $this->mRestriction = $restriction;
725 $this->mListed = $listed;
726 $this->mIncludable = $includable;
727 if ( !$function ) {
728 $this->mFunction = 'wfSpecial'.$name;
729 } else {
730 $this->mFunction = $function;
731 }
732 if ( $file === 'default' ) {
733 $this->mFile = dirname(__FILE__) . "/specials/Special$name.php";
734 } else {
735 $this->mFile = $file;
736 }
737 }
738
739 /**
740 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
741 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
742 *
743 * @param $fName String Name of called method
744 * @param $a Array Arguments to the method
745 * @deprecated Call isn't deprecated, but SpecialPage::SpecialPage() is
746 */
747 public function __call( $fName, $a ) {
748 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
749 if( strtolower( $fName ) == 'specialpage' ) {
750 // Debug messages now, warnings in 1.19 or 1.20?
751 wfDebug( "Deprecated SpecialPage::SpecialPage() called, use __construct();\n" );
752 $name = isset( $a[0] ) ? $a[0] : '';
753 $restriction = isset( $a[1] ) ? $a[1] : '';
754 $listed = isset( $a[2] ) ? $a[2] : true;
755 $function = isset( $a[3] ) ? $a[3] : false;
756 $file = isset( $a[4] ) ? $a[4] : 'default';
757 $includable = isset( $a[5] ) ? $a[5] : false;
758 $this->init( $name, $restriction, $listed, $function, $file, $includable );
759 } else {
760 $className = get_class( $this );
761 throw new MWException( "Call to undefined method $className::$fName" );
762 }
763 }
764
765 /**#@+
766 * Accessor
767 *
768 * @deprecated
769 */
770 function getName() { return $this->mName; }
771 function getRestriction() { return $this->mRestriction; }
772 function getFile() { return $this->mFile; }
773 function isListed() { return $this->mListed; }
774 /**#@-*/
775
776 /**#@+
777 * Accessor and mutator
778 */
779 function name( $x = null ) { return wfSetVar( $this->mName, $x ); }
780 function restrictions( $x = null) {
781 # Use the one below this
782 wfDeprecated( __METHOD__ );
783 return wfSetVar( $this->mRestriction, $x );
784 }
785 function restriction( $x = null) { return wfSetVar( $this->mRestriction, $x ); }
786 function listed( $x = null) { return wfSetVar( $this->mListed, $x ); }
787 function func( $x = null) { return wfSetVar( $this->mFunction, $x ); }
788 function file( $x = null) { return wfSetVar( $this->mFile, $x ); }
789 function includable( $x = null ) { return wfSetVar( $this->mIncludable, $x ); }
790 function including( $x = null ) { return wfSetVar( $this->mIncluding, $x ); }
791 /**#@-*/
792
793 /**
794 * Get the localised name of the special page
795 */
796 function getLocalName() {
797 if ( !isset( $this->mLocalName ) ) {
798 $this->mLocalName = self::getLocalNameFor( $this->mName );
799 }
800 return $this->mLocalName;
801 }
802
803 /**
804 * Can be overridden by subclasses with more complicated permissions
805 * schemes.
806 *
807 * @return Boolean: should the page be displayed with the restricted-access
808 * pages?
809 */
810 public function isRestricted() {
811 global $wgGroupPermissions;
812 // DWIM: If all anons can do something, then it is not restricted
813 return $this->mRestriction != '' && empty($wgGroupPermissions['*'][$this->mRestriction]);
814 }
815
816 /**
817 * Checks if the given user (identified by an object) can execute this
818 * special page (as defined by $mRestriction). Can be overridden by sub-
819 * classes with more complicated permissions schemes.
820 *
821 * @param $user User: the user to check
822 * @return Boolean: does the user have permission to view the page?
823 */
824 public function userCanExecute( $user ) {
825 return $user->isAllowed( $this->mRestriction );
826 }
827
828 /**
829 * Output an error message telling the user what access level they have to have
830 */
831 function displayRestrictionError() {
832 global $wgOut;
833 $wgOut->permissionRequired( $this->mRestriction );
834 }
835
836 /**
837 * Sets headers - this should be called from the execute() method of all derived classes!
838 */
839 function setHeaders() {
840 global $wgOut;
841 $wgOut->setArticleRelated( false );
842 $wgOut->setRobotPolicy( "noindex,nofollow" );
843 $wgOut->setPageTitle( $this->getDescription() );
844 }
845
846 /**
847 * Default execute method
848 * Checks user permissions, calls the function given in mFunction
849 *
850 * This may be overridden by subclasses.
851 */
852 function execute( $par ) {
853 global $wgUser;
854
855 $this->setHeaders();
856
857 if ( $this->userCanExecute( $wgUser ) ) {
858 $func = $this->mFunction;
859 // only load file if the function does not exist
860 if(!is_callable($func) and $this->mFile) {
861 require_once( $this->mFile );
862 }
863 $this->outputHeader();
864 call_user_func( $func, $par, $this );
865 } else {
866 $this->displayRestrictionError();
867 }
868 }
869
870 /**
871 * Outputs a summary message on top of special pages
872 * Per default the message key is the canonical name of the special page
873 * May be overriden, i.e. by extensions to stick with the naming conventions
874 * for message keys: 'extensionname-xxx'
875 *
876 * @param $summaryMessageKey String: message key of the summary
877 */
878 function outputHeader( $summaryMessageKey = '' ) {
879 global $wgOut, $wgContLang;
880
881 if( $summaryMessageKey == '' ) {
882 $msg = $wgContLang->lc( $this->name() ) . '-summary';
883 } else {
884 $msg = $summaryMessageKey;
885 }
886 if ( !wfMessage( $msg )->isBlank() 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 from Special:PermanentLink/### to index.php?oldid=###
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 }