Merge "Set $wgNoFollowLinks to false iff "Authorized editors only" selected"
[lhc/web/wiklou.git] / includes / SpecialPage.php
1 <?php
2 /**
3 * Parent class for all special pages.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Parent special page class, also static functions for handling the special
26 * page list.
27 * @ingroup SpecialPage
28 */
29 class SpecialPage {
30 // The canonical name of this special page
31 // Also used for the default <h1> heading, @see getDescription()
32 protected $mName;
33
34 // The local name of this special page
35 private $mLocalName;
36
37 // Minimum user level required to access this page, or "" for anyone.
38 // Also used to categorise the pages in Special:Specialpages
39 private $mRestriction;
40
41 // Listed in Special:Specialpages?
42 private $mListed;
43
44 // Function name called by the default execute()
45 private $mFunction;
46
47 // File which needs to be included before the function above can be called
48 private $mFile;
49
50 // Whether or not this special page is being included from an article
51 protected $mIncluding;
52
53 // Whether the special page can be included in an article
54 protected $mIncludable;
55
56 /**
57 * Current request context
58 * @var IContextSource
59 */
60 protected $mContext;
61
62 /**
63 * Initialise the special page list
64 * This must be called before accessing SpecialPage::$mList
65 * @deprecated since 1.18
66 */
67 static function initList() {
68 wfDeprecated( __METHOD__, '1.18' );
69 // Noop
70 }
71
72 /**
73 * @deprecated since 1.18
74 */
75 static function initAliasList() {
76 wfDeprecated( __METHOD__, '1.18' );
77 // Noop
78 }
79
80 /**
81 * Given a special page alias, return the special page name.
82 * Returns false if there is no such alias.
83 *
84 * @param $alias String
85 * @return String or false
86 * @deprecated since 1.18 call SpecialPageFactory method directly
87 */
88 static function resolveAlias( $alias ) {
89 wfDeprecated( __METHOD__, '1.18' );
90 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
91 return $name;
92 }
93
94 /**
95 * Given a special page name with a possible subpage, return an array
96 * where the first element is the special page name and the second is the
97 * subpage.
98 *
99 * @param $alias String
100 * @return Array
101 * @deprecated since 1.18 call SpecialPageFactory method directly
102 */
103 static function resolveAliasWithSubpage( $alias ) {
104 return SpecialPageFactory::resolveAlias( $alias );
105 }
106
107 /**
108 * Add a page to a certain display group for Special:SpecialPages
109 *
110 * @param $page Mixed: SpecialPage or string
111 * @param $group String
112 * @deprecated since 1.18 call SpecialPageFactory method directly
113 */
114 static function setGroup( $page, $group ) {
115 wfDeprecated( __METHOD__, '1.18' );
116 SpecialPageFactory::setGroup( $page, $group );
117 }
118
119 /**
120 * Get the group that the special page belongs in on Special:SpecialPage
121 *
122 * @param $page SpecialPage
123 * @return string
124 * @deprecated since 1.18 call SpecialPageFactory method directly
125 */
126 static function getGroup( &$page ) {
127 wfDeprecated( __METHOD__, '1.18' );
128 return SpecialPageFactory::getGroup( $page );
129 }
130
131 /**
132 * Remove a special page from the list
133 * Formerly used to disable expensive or dangerous special pages. The
134 * preferred method is now to add a SpecialPage_initList hook.
135 * @deprecated since 1.18
136 *
137 * @param string $name the page to remove
138 */
139 static function removePage( $name ) {
140 wfDeprecated( __METHOD__, '1.18' );
141 unset( SpecialPageFactory::getList()->$name );
142 }
143
144 /**
145 * Check if a given name exist as a special page or as a special page alias
146 *
147 * @param string $name name of a special page
148 * @return Boolean: true if a special page exists with this name
149 * @deprecated since 1.18 call SpecialPageFactory method directly
150 */
151 static function exists( $name ) {
152 wfDeprecated( __METHOD__, '1.18' );
153 return SpecialPageFactory::exists( $name );
154 }
155
156 /**
157 * Find the object with a given name and return it (or NULL)
158 *
159 * @param $name String
160 * @return SpecialPage object or null if the page doesn't exist
161 * @deprecated since 1.18 call SpecialPageFactory method directly
162 */
163 static function getPage( $name ) {
164 wfDeprecated( __METHOD__, '1.18' );
165 return SpecialPageFactory::getPage( $name );
166 }
167
168 /**
169 * Get a special page with a given localised name, or NULL if there
170 * is no such special page.
171 *
172 * @param $alias String
173 * @return SpecialPage object or null if the page doesn't exist
174 * @deprecated since 1.18 call SpecialPageFactory method directly
175 */
176 static function getPageByAlias( $alias ) {
177 wfDeprecated( __METHOD__, '1.18' );
178 return SpecialPageFactory::getPage( $alias );
179 }
180
181 /**
182 * Return categorised listable special pages which are available
183 * for the current user, and everyone.
184 *
185 * @param $user User object to check permissions, $wgUser will be used
186 * if not provided
187 * @return array Associative array mapping page's name to its SpecialPage object
188 * @deprecated since 1.18 call SpecialPageFactory method directly
189 */
190 static function getUsablePages( User $user = null ) {
191 wfDeprecated( __METHOD__, '1.18' );
192 return SpecialPageFactory::getUsablePages( $user );
193 }
194
195 /**
196 * Return categorised listable special pages for all users
197 *
198 * @return array Associative array mapping page's name to its SpecialPage object
199 * @deprecated since 1.18 call SpecialPageFactory method directly
200 */
201 static function getRegularPages() {
202 wfDeprecated( __METHOD__, '1.18' );
203 return SpecialPageFactory::getRegularPages();
204 }
205
206 /**
207 * Return categorised listable special pages which are available
208 * for the current user, but not for everyone
209 *
210 * @return array Associative array mapping page's name to its SpecialPage object
211 * @deprecated since 1.18 call SpecialPageFactory method directly
212 */
213 static function getRestrictedPages() {
214 wfDeprecated( __METHOD__, '1.18' );
215 return SpecialPageFactory::getRestrictedPages();
216 }
217
218 /**
219 * Execute a special page path.
220 * The path may contain parameters, e.g. Special:Name/Params
221 * Extracts the special page name and call the execute method, passing the parameters
222 *
223 * Returns a title object if the page is redirected, false if there was no such special
224 * page, and true if it was successful.
225 *
226 * @param $title Title object
227 * @param $context IContextSource
228 * @param $including Bool output is being captured for use in {{special:whatever}}
229 * @return Bool
230 * @deprecated since 1.18 call SpecialPageFactory method directly
231 */
232 public static function executePath( &$title, IContextSource &$context, $including = false ) {
233 wfDeprecated( __METHOD__, '1.18' );
234 return SpecialPageFactory::executePath( $title, $context, $including );
235 }
236
237 /**
238 * Get the local name for a specified canonical name
239 *
240 * @param $name String
241 * @param $subpage Mixed: boolean false, or string
242 *
243 * @return String
244 * @deprecated since 1.18 call SpecialPageFactory method directly
245 */
246 static function getLocalNameFor( $name, $subpage = false ) {
247 wfDeprecated( __METHOD__, '1.18' );
248 return SpecialPageFactory::getLocalNameFor( $name, $subpage );
249 }
250
251 /**
252 * Get a localised Title object for a specified special page name
253 *
254 * @param $name String
255 * @param string|Bool $subpage subpage string, or false to not use a subpage
256 * @param string $fragment the link fragment (after the "#")
257 * @throws MWException
258 * @return Title object
259 */
260 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
261 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
262 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
263 }
264
265 /**
266 * Get a localised Title object for a page name with a possibly unvalidated subpage
267 *
268 * @param $name String
269 * @param string|Bool $subpage subpage string, or false to not use a subpage
270 * @return Title object or null if the page doesn't exist
271 */
272 public static function getSafeTitleFor( $name, $subpage = false ) {
273 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
274 if ( $name ) {
275 return Title::makeTitleSafe( NS_SPECIAL, $name );
276 } else {
277 return null;
278 }
279 }
280
281 /**
282 * Get a title for a given alias
283 *
284 * @param $alias String
285 * @return Title or null if there is no such alias
286 * @deprecated since 1.18 call SpecialPageFactory method directly
287 */
288 static function getTitleForAlias( $alias ) {
289 wfDeprecated( __METHOD__, '1.18' );
290 return SpecialPageFactory::getTitleForAlias( $alias );
291 }
292
293 /**
294 * Default constructor for special pages
295 * Derivative classes should call this from their constructor
296 * Note that if the user does not have the required level, an error message will
297 * be displayed by the default execute() method, without the global function ever
298 * being called.
299 *
300 * If you override execute(), you can recover the default behavior with userCanExecute()
301 * and displayRestrictionError()
302 *
303 * @param string $name Name of the special page, as seen in links and URLs
304 * @param string $restriction User right required, e.g. "block" or "delete"
305 * @param bool $listed Whether the page is listed in Special:Specialpages
306 * @param Callback|Bool $function Function called by execute(). By default
307 * it is constructed from $name
308 * @param string $file File which is included by execute(). It is also
309 * constructed from $name by default
310 * @param bool $includable Whether the page can be included in normal pages
311 */
312 public function __construct(
313 $name = '', $restriction = '', $listed = true,
314 $function = false, $file = 'default', $includable = false
315 ) {
316 $this->init( $name, $restriction, $listed, $function, $file, $includable );
317 }
318
319 /**
320 * Do the real work for the constructor, mainly so __call() can intercept
321 * calls to SpecialPage()
322 * @param string $name Name of the special page, as seen in links and URLs
323 * @param string $restriction User right required, e.g. "block" or "delete"
324 * @param bool $listed Whether the page is listed in Special:Specialpages
325 * @param Callback|Bool $function Function called by execute(). By default
326 * it is constructed from $name
327 * @param string $file File which is included by execute(). It is also
328 * constructed from $name by default
329 * @param bool $includable Whether the page can be included in normal pages
330 */
331 private function init( $name, $restriction, $listed, $function, $file, $includable ) {
332 $this->mName = $name;
333 $this->mRestriction = $restriction;
334 $this->mListed = $listed;
335 $this->mIncludable = $includable;
336 if ( !$function ) {
337 $this->mFunction = 'wfSpecial' . $name;
338 } else {
339 $this->mFunction = $function;
340 }
341 if ( $file === 'default' ) {
342 $this->mFile = __DIR__ . "/specials/Special$name.php";
343 } else {
344 $this->mFile = $file;
345 }
346 }
347
348 /**
349 * Use PHP's magic __call handler to get calls to the old PHP4 constructor
350 * because PHP E_STRICT yells at you for having __construct() and SpecialPage()
351 *
352 * @param string $fName Name of called method
353 * @param array $a Arguments to the method
354 * @throws MWException
355 * @deprecated since 1.17, call parent::__construct()
356 */
357 public function __call( $fName, $a ) {
358 // Deprecated messages now, remove in 1.19 or 1.20?
359 wfDeprecated( __METHOD__, '1.17' );
360
361 // Sometimes $fName is SpecialPage, sometimes it's specialpage. <3 PHP
362 if ( strtolower( $fName ) == 'specialpage' ) {
363 $name = isset( $a[0] ) ? $a[0] : '';
364 $restriction = isset( $a[1] ) ? $a[1] : '';
365 $listed = isset( $a[2] ) ? $a[2] : true;
366 $function = isset( $a[3] ) ? $a[3] : false;
367 $file = isset( $a[4] ) ? $a[4] : 'default';
368 $includable = isset( $a[5] ) ? $a[5] : false;
369 $this->init( $name, $restriction, $listed, $function, $file, $includable );
370 } else {
371 $className = get_class( $this );
372 throw new MWException( "Call to undefined method $className::$fName" );
373 }
374 }
375
376 /**
377 * Get the name of this Special Page.
378 * @return String
379 */
380 function getName() {
381 return $this->mName;
382 }
383
384 /**
385 * Get the permission that a user must have to execute this page
386 * @return String
387 */
388 function getRestriction() {
389 return $this->mRestriction;
390 }
391
392 /**
393 * Get the file which will be included by SpecialPage::execute() if your extension is
394 * still stuck in the past and hasn't overridden the execute() method. No modern code
395 * should want or need to know this.
396 * @return String
397 * @deprecated since 1.18
398 */
399 function getFile() {
400 wfDeprecated( __METHOD__, '1.18' );
401 return $this->mFile;
402 }
403
404 // @todo FIXME: Decide which syntax to use for this, and stick to it
405 /**
406 * Whether this special page is listed in Special:SpecialPages
407 * @since r3583 (v1.3)
408 * @return Bool
409 */
410 function isListed() {
411 return $this->mListed;
412 }
413 /**
414 * Set whether this page is listed in Special:Specialpages, at run-time
415 * @since r3583 (v1.3)
416 * @param $listed Bool
417 * @return Bool
418 */
419 function setListed( $listed ) {
420 return wfSetVar( $this->mListed, $listed );
421 }
422 /**
423 * Get or set whether this special page is listed in Special:SpecialPages
424 * @since r11308 (v1.6)
425 * @param $x Bool
426 * @return Bool
427 */
428 function listed( $x = null ) {
429 return wfSetVar( $this->mListed, $x );
430 }
431
432 /**
433 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
434 * @return Bool
435 */
436 public function isIncludable() {
437 return $this->mIncludable;
438 }
439
440 /**
441 * These mutators are very evil, as the relevant variables should not mutate. So
442 * don't use them.
443 * @param $x Mixed
444 * @return Mixed
445 * @deprecated since 1.18
446 */
447 function name( $x = null ) {
448 wfDeprecated( __METHOD__, '1.18' );
449 return wfSetVar( $this->mName, $x );
450 }
451
452 /**
453 * These mutators are very evil, as the relevant variables should not mutate. So
454 * don't use them.
455 * @param $x Mixed
456 * @return Mixed
457 * @deprecated since 1.18
458 */
459 function restriction( $x = null ) {
460 wfDeprecated( __METHOD__, '1.18' );
461 return wfSetVar( $this->mRestriction, $x );
462 }
463
464 /**
465 * These mutators are very evil, as the relevant variables should not mutate. So
466 * don't use them.
467 * @param $x Mixed
468 * @return Mixed
469 * @deprecated since 1.18
470 */
471 function func( $x = null ) {
472 wfDeprecated( __METHOD__, '1.18' );
473 return wfSetVar( $this->mFunction, $x );
474 }
475
476 /**
477 * These mutators are very evil, as the relevant variables should not mutate. So
478 * don't use them.
479 * @param $x Mixed
480 * @return Mixed
481 * @deprecated since 1.18
482 */
483 function file( $x = null ) {
484 wfDeprecated( __METHOD__, '1.18' );
485 return wfSetVar( $this->mFile, $x );
486 }
487
488 /**
489 * These mutators are very evil, as the relevant variables should not mutate. So
490 * don't use them.
491 * @param $x Mixed
492 * @return Mixed
493 * @deprecated since 1.18
494 */
495 function includable( $x = null ) {
496 wfDeprecated( __METHOD__, '1.18' );
497 return wfSetVar( $this->mIncludable, $x );
498 }
499
500 /**
501 * Whether the special page is being evaluated via transclusion
502 * @param $x Bool
503 * @return Bool
504 */
505 function including( $x = null ) {
506 return wfSetVar( $this->mIncluding, $x );
507 }
508
509 /**
510 * Get the localised name of the special page
511 */
512 function getLocalName() {
513 if ( !isset( $this->mLocalName ) ) {
514 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
515 }
516 return $this->mLocalName;
517 }
518
519 /**
520 * Is this page expensive (for some definition of expensive)?
521 * Expensive pages are disabled or cached in miser mode. Originally used
522 * (and still overridden) by QueryPage and subclasses, moved here so that
523 * Special:SpecialPages can safely call it for all special pages.
524 *
525 * @return Boolean
526 */
527 public function isExpensive() {
528 return false;
529 }
530
531 /**
532 * Is this page cached?
533 * Expensive pages are cached or disabled in miser mode.
534 * Used by QueryPage and subclasses, moved here so that
535 * Special:SpecialPages can safely call it for all special pages.
536 *
537 * @return Boolean
538 * @since 1.21
539 */
540 public function isCached() {
541 return false;
542 }
543
544 /**
545 * Can be overridden by subclasses with more complicated permissions
546 * schemes.
547 *
548 * @return Boolean: should the page be displayed with the restricted-access
549 * pages?
550 */
551 public function isRestricted() {
552 // DWIM: If anons can do something, then it is not restricted
553 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
554 }
555
556 /**
557 * Checks if the given user (identified by an object) can execute this
558 * special page (as defined by $mRestriction). Can be overridden by sub-
559 * classes with more complicated permissions schemes.
560 *
561 * @param $user User: the user to check
562 * @return Boolean: does the user have permission to view the page?
563 */
564 public function userCanExecute( User $user ) {
565 return $user->isAllowed( $this->mRestriction );
566 }
567
568 /**
569 * Output an error message telling the user what access level they have to have
570 */
571 function displayRestrictionError() {
572 throw new PermissionsError( $this->mRestriction );
573 }
574
575 /**
576 * Checks if userCanExecute, and if not throws a PermissionsError
577 *
578 * @since 1.19
579 */
580 public function checkPermissions() {
581 if ( !$this->userCanExecute( $this->getUser() ) ) {
582 $this->displayRestrictionError();
583 }
584 }
585
586 /**
587 * If the wiki is currently in readonly mode, throws a ReadOnlyError
588 *
589 * @since 1.19
590 * @throws ReadOnlyError
591 */
592 public function checkReadOnly() {
593 if ( wfReadOnly() ) {
594 throw new ReadOnlyError;
595 }
596 }
597
598 /**
599 * If the user is not logged in, throws UserNotLoggedIn error.
600 *
601 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
602 * parameter.
603 *
604 * @since 1.23
605 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
606 * will be used as message keys. If a string is given, the message will also receive a
607 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
608 * Message is given, it will be passed on verbatim.
609 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
610 * will be used as message keys.
611 * @throws UserNotLoggedIn
612 */
613 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
614 if ( $this->getUser()->isAnon() ) {
615 // Use default messages if not given or explicit null passed
616 if ( !$reasonMsg ) {
617 $reasonMsg = 'exception-nologin-text-manual';
618 }
619 if ( !$titleMsg ) {
620 $titleMsg = 'exception-nologin';
621 }
622
623 // Convert to Messages with current context
624 if ( is_string( $reasonMsg ) ) {
625 $loginreqlink = Linker::linkKnown(
626 SpecialPage::getTitleFor( 'Userlogin' ),
627 $this->msg( 'loginreqlink' )->escaped(),
628 array(),
629 array( 'returnto' => $this->getTitle()->getPrefixedText() )
630 );
631 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
632 }
633 if ( is_string( $titleMsg ) ) {
634 $titleMsg = $this->msg( $titleMsg );
635 }
636
637 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
638 }
639 }
640
641 /**
642 * Sets headers - this should be called from the execute() method of all derived classes!
643 */
644 function setHeaders() {
645 $out = $this->getOutput();
646 $out->setArticleRelated( false );
647 $out->setRobotPolicy( "noindex,nofollow" );
648 $out->setPageTitle( $this->getDescription() );
649 }
650
651 /**
652 * Entry point.
653 *
654 * @since 1.20
655 *
656 * @param $subPage string|null
657 */
658 final public function run( $subPage ) {
659 /**
660 * Gets called before @see SpecialPage::execute.
661 *
662 * @since 1.20
663 *
664 * @param $special SpecialPage
665 * @param $subPage string|null
666 */
667 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
668
669 $this->beforeExecute( $subPage );
670 $this->execute( $subPage );
671 $this->afterExecute( $subPage );
672
673 /**
674 * Gets called after @see SpecialPage::execute.
675 *
676 * @since 1.20
677 *
678 * @param $special SpecialPage
679 * @param $subPage string|null
680 */
681 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
682 }
683
684 /**
685 * Gets called before @see SpecialPage::execute.
686 *
687 * @since 1.20
688 *
689 * @param $subPage string|null
690 */
691 protected function beforeExecute( $subPage ) {
692 // No-op
693 }
694
695 /**
696 * Gets called after @see SpecialPage::execute.
697 *
698 * @since 1.20
699 *
700 * @param $subPage string|null
701 */
702 protected function afterExecute( $subPage ) {
703 // No-op
704 }
705
706 /**
707 * Default execute method
708 * Checks user permissions, calls the function given in mFunction
709 *
710 * This must be overridden by subclasses; it will be made abstract in a future version
711 *
712 * @param $subPage string|null
713 */
714 public function execute( $subPage ) {
715 $this->setHeaders();
716 $this->checkPermissions();
717
718 $func = $this->mFunction;
719 // only load file if the function does not exist
720 if ( !is_callable( $func ) && $this->mFile ) {
721 require_once $this->mFile;
722 }
723 $this->outputHeader();
724 call_user_func( $func, $subPage, $this );
725 }
726
727 /**
728 * Outputs a summary message on top of special pages
729 * Per default the message key is the canonical name of the special page
730 * May be overridden, i.e. by extensions to stick with the naming conventions
731 * for message keys: 'extensionname-xxx'
732 *
733 * @param string $summaryMessageKey message key of the summary
734 */
735 function outputHeader( $summaryMessageKey = '' ) {
736 global $wgContLang;
737
738 if ( $summaryMessageKey == '' ) {
739 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
740 } else {
741 $msg = $summaryMessageKey;
742 }
743 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
744 $this->getOutput()->wrapWikiMsg(
745 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
746 }
747
748 }
749
750 /**
751 * Returns the name that goes in the \<h1\> in the special page itself, and
752 * also the name that will be listed in Special:Specialpages
753 *
754 * Derived classes can override this, but usually it is easier to keep the
755 * default behavior. Messages can be added at run-time, see
756 * MessageCache.php.
757 *
758 * @return String
759 */
760 function getDescription() {
761 return $this->msg( strtolower( $this->mName ) )->text();
762 }
763
764 /**
765 * Get a self-referential title object
766 *
767 * @param $subpage String|Bool
768 * @return Title object
769 */
770 function getTitle( $subpage = false ) {
771 return self::getTitleFor( $this->mName, $subpage );
772 }
773
774 /**
775 * Sets the context this SpecialPage is executed in
776 *
777 * @param $context IContextSource
778 * @since 1.18
779 */
780 public function setContext( $context ) {
781 $this->mContext = $context;
782 }
783
784 /**
785 * Gets the context this SpecialPage is executed in
786 *
787 * @return IContextSource|RequestContext
788 * @since 1.18
789 */
790 public function getContext() {
791 if ( $this->mContext instanceof IContextSource ) {
792 return $this->mContext;
793 } else {
794 wfDebug( __METHOD__ . " called and \$mContext is null. " .
795 "Return RequestContext::getMain(); for sanity\n" );
796 return RequestContext::getMain();
797 }
798 }
799
800 /**
801 * Get the WebRequest being used for this instance
802 *
803 * @return WebRequest
804 * @since 1.18
805 */
806 public function getRequest() {
807 return $this->getContext()->getRequest();
808 }
809
810 /**
811 * Get the OutputPage being used for this instance
812 *
813 * @return OutputPage
814 * @since 1.18
815 */
816 public function getOutput() {
817 return $this->getContext()->getOutput();
818 }
819
820 /**
821 * Shortcut to get the User executing this instance
822 *
823 * @return User
824 * @since 1.18
825 */
826 public function getUser() {
827 return $this->getContext()->getUser();
828 }
829
830 /**
831 * Shortcut to get the skin being used for this instance
832 *
833 * @return Skin
834 * @since 1.18
835 */
836 public function getSkin() {
837 return $this->getContext()->getSkin();
838 }
839
840 /**
841 * Shortcut to get user's language
842 *
843 * @deprecated since 1.19 Use getLanguage instead
844 * @return Language
845 * @since 1.18
846 */
847 public function getLang() {
848 wfDeprecated( __METHOD__, '1.19' );
849 return $this->getLanguage();
850 }
851
852 /**
853 * Shortcut to get user's language
854 *
855 * @return Language
856 * @since 1.19
857 */
858 public function getLanguage() {
859 return $this->getContext()->getLanguage();
860 }
861
862 /**
863 * Return the full title, including $par
864 *
865 * @return Title
866 * @since 1.18
867 */
868 public function getFullTitle() {
869 return $this->getContext()->getTitle();
870 }
871
872 /**
873 * Wrapper around wfMessage that sets the current context.
874 *
875 * @return Message
876 * @see wfMessage
877 */
878 public function msg( /* $args */ ) {
879 $message = call_user_func_array(
880 array( $this->getContext(), 'msg' ),
881 func_get_args()
882 );
883 // RequestContext passes context to wfMessage, and the language is set from
884 // the context, but setting the language for Message class removes the
885 // interface message status, which breaks for example usernameless gender
886 // invocations. Restore the flag when not including special page in content.
887 if ( $this->including() ) {
888 $message->setInterfaceMessageFlag( false );
889 }
890 return $message;
891 }
892
893 /**
894 * Adds RSS/atom links
895 *
896 * @param $params array
897 */
898 protected function addFeedLinks( $params ) {
899 global $wgFeedClasses;
900
901 $feedTemplate = wfScript( 'api' );
902
903 foreach ( $wgFeedClasses as $format => $class ) {
904 $theseParams = $params + array( 'feedformat' => $format );
905 $url = wfAppendQuery( $feedTemplate, $theseParams );
906 $this->getOutput()->addFeedLink( $format, $url );
907 }
908 }
909
910 /**
911 * Get the group that the special page belongs in on Special:SpecialPage
912 * Use this method, instead of getGroupName to allow customization
913 * of the group name from the wiki side
914 *
915 * @return string Group of this special page
916 * @since 1.21
917 */
918 public function getFinalGroupName() {
919 global $wgSpecialPageGroups;
920 $name = $this->getName();
921
922 // Allow overbidding the group from the wiki side
923 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
924 if ( !$msg->isBlank() ) {
925 $group = $msg->text();
926 } else {
927 // Than use the group from this object
928 $group = $this->getGroupName();
929
930 // Group '-' is used as default to have the chance to determine,
931 // if the special pages overrides this method,
932 // if not overridden, $wgSpecialPageGroups is checked for b/c
933 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
934 $group = $wgSpecialPageGroups[$name];
935 }
936 }
937
938 // never give '-' back, change to 'other'
939 if ( $group === '-' ) {
940 $group = 'other';
941 }
942
943 return $group;
944 }
945
946 /**
947 * Under which header this special page is listed in Special:SpecialPages
948 * See messages 'specialpages-group-*' for valid names
949 * This method defaults to group 'other'
950 *
951 * @return string
952 * @since 1.21
953 */
954 protected function getGroupName() {
955 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
956 // Needed for b/c in getFinalGroupName
957 return '-';
958 }
959 }
960
961 /**
962 * Special page which uses an HTMLForm to handle processing. This is mostly a
963 * clone of FormAction. More special pages should be built this way; maybe this could be
964 * a new structure for SpecialPages
965 */
966 abstract class FormSpecialPage extends SpecialPage {
967 /**
968 * The sub-page of the special page.
969 * @var string
970 */
971 protected $par = null;
972
973 /**
974 * Get an HTMLForm descriptor array
975 * @return Array
976 */
977 abstract protected function getFormFields();
978
979 /**
980 * Add pre-text to the form
981 * @return String HTML which will be sent to $form->addPreText()
982 */
983 protected function preText() {
984 return '';
985 }
986
987 /**
988 * Add post-text to the form
989 * @return String HTML which will be sent to $form->addPostText()
990 */
991 protected function postText() {
992 return '';
993 }
994
995 /**
996 * Play with the HTMLForm if you need to more substantially
997 * @param $form HTMLForm
998 */
999 protected function alterForm( HTMLForm $form ) {
1000 }
1001
1002 /**
1003 * Get message prefix for HTMLForm
1004 *
1005 * @since 1.21
1006 * @return string
1007 */
1008 protected function getMessagePrefix() {
1009 return strtolower( $this->getName() );
1010 }
1011
1012 /**
1013 * Get the HTMLForm to control behavior
1014 * @return HTMLForm|null
1015 */
1016 protected function getForm() {
1017 $this->fields = $this->getFormFields();
1018
1019 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
1020 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
1021 // If the form is a compact vertical form, then don't output this ugly
1022 // fieldset surrounding it.
1023 // XXX Special pages can setDisplayFormat to 'vform' in alterForm(), but that
1024 // is called after this.
1025 if ( !$form->isVForm() ) {
1026 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
1027 }
1028
1029 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
1030 if ( !$headerMsg->isDisabled() ) {
1031 $form->addHeaderText( $headerMsg->parseAsBlock() );
1032 }
1033
1034 // Retain query parameters (uselang etc)
1035 $params = array_diff_key(
1036 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
1037 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
1038
1039 $form->addPreText( $this->preText() );
1040 $form->addPostText( $this->postText() );
1041 $this->alterForm( $form );
1042
1043 // Give hooks a chance to alter the form, adding extra fields or text etc
1044 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
1045
1046 return $form;
1047 }
1048
1049 /**
1050 * Process the form on POST submission.
1051 * @param $data Array
1052 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
1053 */
1054 abstract public function onSubmit( array $data );
1055
1056 /**
1057 * Do something exciting on successful processing of the form, most likely to show a
1058 * confirmation message
1059 * @since 1.22 Default is to do nothing
1060 */
1061 public function onSuccess() {
1062 }
1063
1064 /**
1065 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
1066 *
1067 * @param string $par Subpage string if one was specified
1068 */
1069 public function execute( $par ) {
1070 $this->setParameter( $par );
1071 $this->setHeaders();
1072
1073 // This will throw exceptions if there's a problem
1074 $this->checkExecutePermissions( $this->getUser() );
1075
1076 $form = $this->getForm();
1077 if ( $form->show() ) {
1078 $this->onSuccess();
1079 }
1080 }
1081
1082 /**
1083 * Maybe do something interesting with the subpage parameter
1084 * @param string $par
1085 */
1086 protected function setParameter( $par ) {
1087 $this->par = $par;
1088 }
1089
1090 /**
1091 * Called from execute() to check if the given user can perform this action.
1092 * Failures here must throw subclasses of ErrorPageError.
1093 * @param $user User
1094 * @throws UserBlockedError
1095 * @return Bool true
1096 */
1097 protected function checkExecutePermissions( User $user ) {
1098 $this->checkPermissions();
1099
1100 if ( $this->requiresUnblock() && $user->isBlocked() ) {
1101 $block = $user->getBlock();
1102 throw new UserBlockedError( $block );
1103 }
1104
1105 if ( $this->requiresWrite() ) {
1106 $this->checkReadOnly();
1107 }
1108
1109 return true;
1110 }
1111
1112 /**
1113 * Whether this action requires the wiki not to be locked
1114 * @return Bool
1115 */
1116 public function requiresWrite() {
1117 return true;
1118 }
1119
1120 /**
1121 * Whether this action cannot be executed by a blocked user
1122 * @return Bool
1123 */
1124 public function requiresUnblock() {
1125 return true;
1126 }
1127 }
1128
1129 /**
1130 * Shortcut to construct a special page which is unlisted by default
1131 * @ingroup SpecialPage
1132 */
1133 class UnlistedSpecialPage extends SpecialPage {
1134 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1135 parent::__construct( $name, $restriction, false, $function, $file );
1136 }
1137
1138 public function isListed() {
1139 return false;
1140 }
1141 }
1142
1143 /**
1144 * Shortcut to construct an includable special page
1145 * @ingroup SpecialPage
1146 */
1147 class IncludableSpecialPage extends SpecialPage {
1148 function __construct(
1149 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1150 ) {
1151 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1152 }
1153
1154 public function isIncludable() {
1155 return true;
1156 }
1157 }
1158
1159 /**
1160 * Shortcut to construct a special page alias.
1161 * @ingroup SpecialPage
1162 */
1163 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1164
1165 // Query parameters that can be passed through redirects
1166 protected $mAllowedRedirectParams = array();
1167
1168 // Query parameters added by redirects
1169 protected $mAddedRedirectParams = array();
1170
1171 public function execute( $par ) {
1172 $redirect = $this->getRedirect( $par );
1173 $query = $this->getRedirectQuery();
1174 // Redirect to a page title with possible query parameters
1175 if ( $redirect instanceof Title ) {
1176 $url = $redirect->getFullURL( $query );
1177 $this->getOutput()->redirect( $url );
1178 return $redirect;
1179 } elseif ( $redirect === true ) {
1180 // Redirect to index.php with query parameters
1181 $url = wfAppendQuery( wfScript( 'index' ), $query );
1182 $this->getOutput()->redirect( $url );
1183 return $redirect;
1184 } else {
1185 $class = get_class( $this );
1186 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1187 }
1188 }
1189
1190 /**
1191 * If the special page is a redirect, then get the Title object it redirects to.
1192 * False otherwise.
1193 *
1194 * @param string $par Subpage string
1195 * @return Title|bool
1196 */
1197 abstract public function getRedirect( $par );
1198
1199 /**
1200 * Return part of the request string for a special redirect page
1201 * This allows passing, e.g. action=history to Special:Mypage, etc.
1202 *
1203 * @return String
1204 */
1205 public function getRedirectQuery() {
1206 $params = array();
1207
1208 foreach ( $this->mAllowedRedirectParams as $arg ) {
1209 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1210 $params[$arg] = $this->getRequest()->getVal( $arg );
1211 }
1212 }
1213
1214 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1215 $params[$arg] = $val;
1216 }
1217
1218 return count( $params )
1219 ? $params
1220 : false;
1221 }
1222 }
1223
1224 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1225 // @todo FIXME: Visibility must be declared
1226 var $redirName, $redirSubpage;
1227
1228 function __construct(
1229 $name, $redirName, $redirSubpage = false,
1230 $allowedRedirectParams = array(), $addedRedirectParams = array()
1231 ) {
1232 parent::__construct( $name );
1233 $this->redirName = $redirName;
1234 $this->redirSubpage = $redirSubpage;
1235 $this->mAllowedRedirectParams = $allowedRedirectParams;
1236 $this->mAddedRedirectParams = $addedRedirectParams;
1237 }
1238
1239 public function getRedirect( $subpage ) {
1240 if ( $this->redirSubpage === false ) {
1241 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1242 } else {
1243 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1244 }
1245 }
1246 }
1247
1248 /**
1249 * ListAdmins --> ListUsers/sysop
1250 */
1251 class SpecialListAdmins extends SpecialRedirectToSpecial {
1252 function __construct() {
1253 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1254 }
1255 }
1256
1257 /**
1258 * ListBots --> ListUsers/bot
1259 */
1260 class SpecialListBots extends SpecialRedirectToSpecial {
1261 function __construct() {
1262 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1263 }
1264 }
1265
1266 /**
1267 * CreateAccount --> UserLogin/signup
1268 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1269 */
1270 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1271 function __construct() {
1272 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'returnto', 'returntoquery', 'uselang' ) );
1273 }
1274
1275 // No reason to hide this link on Special:Specialpages
1276 public function isListed() {
1277 return true;
1278 }
1279
1280 protected function getGroupName() {
1281 return 'login';
1282 }
1283 }
1284 /**
1285 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1286 * are used to get user independent links pointing to the user page, talk
1287 * page and list of contributions.
1288 * This can let us cache a single copy of any generated content for all
1289 * users.
1290 */
1291
1292 /**
1293 * Superclass for any RedirectSpecialPage which redirects the user
1294 * to a particular article (as opposed to user contributions, logs, etc.).
1295 *
1296 * For security reasons these special pages are restricted to pass on
1297 * the following subset of GET parameters to the target page while
1298 * removing all others:
1299 *
1300 * - useskin, uselang, printable: to alter the appearance of the resulting page
1301 *
1302 * - redirect: allows viewing one's user page or talk page even if it is a
1303 * redirect.
1304 *
1305 * - rdfrom: allows redirecting to one's user page or talk page from an
1306 * external wiki with the "Redirect from..." notice.
1307 *
1308 * - limit, offset: Useful for linking to history of one's own user page or
1309 * user talk page. For example, this would be a link to "the last edit to your
1310 * user talk page in the year 2010":
1311 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
1312 *
1313 * - feed: would allow linking to the current user's RSS feed for their user
1314 * talk page:
1315 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1316 *
1317 * - preloadtitle: Can be used to provide a default section title for a
1318 * preloaded new comment on one's own talk page.
1319 *
1320 * - summary : Can be used to provide a default edit summary for a preloaded
1321 * edit to one's own user page or talk page.
1322 *
1323 * - preview: Allows showing/hiding preview on first edit regardless of user
1324 * preference, useful for preloaded edits where you know preview wouldn't be
1325 * useful.
1326 *
1327 * - internaledit, externaledit, mode: Allows forcing the use of the
1328 * internal/external editor, e.g. to force the internal editor for
1329 * short/simple preloaded edits.
1330 *
1331 * - redlink: Affects the message the user sees if their talk page/user talk
1332 * page does not currently exist. Avoids confusion for newbies with no user
1333 * pages over why they got a "permission error" following this link:
1334 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1335 *
1336 * - debug: determines whether the debug parameter is passed to load.php,
1337 * which disables reformatting and allows scripts to be debugged. Useful
1338 * when debugging scripts that manipulate one's own user page or talk page.
1339 *
1340 * @par Hook extension:
1341 * Extensions can add to the redirect parameters list by using the hook
1342 * RedirectSpecialArticleRedirectParams
1343 *
1344 * This hook allows extensions which add GET parameters like FlaggedRevs to
1345 * retain those parameters when redirecting using special pages.
1346 *
1347 * @par Hook extension example:
1348 * @code
1349 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1350 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1351 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1352 * $redirectParams[] = 'stable';
1353 * return true;
1354 * }
1355 * @endcode
1356 * @ingroup SpecialPage
1357 */
1358 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1359 function __construct( $name ) {
1360 parent::__construct( $name );
1361 $redirectParams = array(
1362 'action',
1363 'redirect', 'rdfrom',
1364 # Options for preloaded edits
1365 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
1366 # Options for overriding user settings
1367 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
1368 # Options for history/diffs
1369 'section', 'oldid', 'diff', 'dir',
1370 'limit', 'offset', 'feed',
1371 # Misc options
1372 'redlink', 'debug',
1373 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1374 'ctype', 'maxage', 'smaxage',
1375 );
1376
1377 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1378 $this->mAllowedRedirectParams = $redirectParams;
1379 }
1380 }
1381
1382 /**
1383 * Shortcut to construct a special page pointing to current user user's page.
1384 * @ingroup SpecialPage
1385 */
1386 class SpecialMypage extends RedirectSpecialArticle {
1387 function __construct() {
1388 parent::__construct( 'Mypage' );
1389 }
1390
1391 function getRedirect( $subpage ) {
1392 if ( strval( $subpage ) !== '' ) {
1393 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1394 } else {
1395 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1396 }
1397 }
1398 }
1399
1400 /**
1401 * Shortcut to construct a special page pointing to current user talk page.
1402 * @ingroup SpecialPage
1403 */
1404 class SpecialMytalk extends RedirectSpecialArticle {
1405 function __construct() {
1406 parent::__construct( 'Mytalk' );
1407 }
1408
1409 function getRedirect( $subpage ) {
1410 if ( strval( $subpage ) !== '' ) {
1411 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1412 } else {
1413 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1414 }
1415 }
1416 }
1417
1418 /**
1419 * Shortcut to construct a special page pointing to current user contributions.
1420 * @ingroup SpecialPage
1421 */
1422 class SpecialMycontributions extends RedirectSpecialPage {
1423 function __construct() {
1424 parent::__construct( 'Mycontributions' );
1425 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1426 'offset', 'dir', 'year', 'month', 'feed' );
1427 }
1428
1429 function getRedirect( $subpage ) {
1430 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1431 }
1432 }
1433
1434 /**
1435 * Redirect to Special:Listfiles?user=$wgUser
1436 */
1437 class SpecialMyuploads extends RedirectSpecialPage {
1438 function __construct() {
1439 parent::__construct( 'Myuploads' );
1440 $this->mAllowedRedirectParams = array( 'limit', 'ilshowall', 'ilsearch' );
1441 }
1442
1443 function getRedirect( $subpage ) {
1444 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1445 }
1446 }
1447
1448 /**
1449 * Redirect Special:Listfiles?user=$wgUser&ilshowall=true
1450 */
1451 class SpecialAllMyUploads extends RedirectSpecialPage {
1452 function __construct() {
1453 parent::__construct( 'AllMyUploads' );
1454 $this->mAllowedRedirectParams = array( 'limit', 'ilsearch' );
1455 }
1456
1457 function getRedirect( $subpage ) {
1458 $this->mAddedRedirectParams['ilshowall'] = 1;
1459 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1460 }
1461 }
1462
1463 /**
1464 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1465 */
1466 class SpecialPermanentLink extends RedirectSpecialPage {
1467 function __construct() {
1468 parent::__construct( 'PermanentLink' );
1469 $this->mAllowedRedirectParams = array();
1470 }
1471
1472 function getRedirect( $subpage ) {
1473 $subpage = intval( $subpage );
1474 if ( $subpage === 0 ) {
1475 # throw an error page when no subpage was given
1476 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1477 }
1478 $this->mAddedRedirectParams['oldid'] = $subpage;
1479 return true;
1480 }
1481 }