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