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