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