Merge "Update documentation for OldChangesList"
[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 * Sets headers - this should be called from the execute() method of all derived classes!
600 */
601 function setHeaders() {
602 $out = $this->getOutput();
603 $out->setArticleRelated( false );
604 $out->setRobotPolicy( "noindex,nofollow" );
605 $out->setPageTitle( $this->getDescription() );
606 }
607
608 /**
609 * Entry point.
610 *
611 * @since 1.20
612 *
613 * @param $subPage string|null
614 */
615 final public function run( $subPage ) {
616 /**
617 * Gets called before @see SpecialPage::execute.
618 *
619 * @since 1.20
620 *
621 * @param $special SpecialPage
622 * @param $subPage string|null
623 */
624 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
625
626 $this->beforeExecute( $subPage );
627 $this->execute( $subPage );
628 $this->afterExecute( $subPage );
629
630 /**
631 * Gets called after @see SpecialPage::execute.
632 *
633 * @since 1.20
634 *
635 * @param $special SpecialPage
636 * @param $subPage string|null
637 */
638 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
639 }
640
641 /**
642 * Gets called before @see SpecialPage::execute.
643 *
644 * @since 1.20
645 *
646 * @param $subPage string|null
647 */
648 protected function beforeExecute( $subPage ) {
649 // No-op
650 }
651
652 /**
653 * Gets called after @see SpecialPage::execute.
654 *
655 * @since 1.20
656 *
657 * @param $subPage string|null
658 */
659 protected function afterExecute( $subPage ) {
660 // No-op
661 }
662
663 /**
664 * Default execute method
665 * Checks user permissions, calls the function given in mFunction
666 *
667 * This must be overridden by subclasses; it will be made abstract in a future version
668 *
669 * @param $subPage string|null
670 */
671 public function execute( $subPage ) {
672 $this->setHeaders();
673 $this->checkPermissions();
674
675 $func = $this->mFunction;
676 // only load file if the function does not exist
677 if ( !is_callable( $func ) && $this->mFile ) {
678 require_once $this->mFile;
679 }
680 $this->outputHeader();
681 call_user_func( $func, $subPage, $this );
682 }
683
684 /**
685 * Outputs a summary message on top of special pages
686 * Per default the message key is the canonical name of the special page
687 * May be overridden, i.e. by extensions to stick with the naming conventions
688 * for message keys: 'extensionname-xxx'
689 *
690 * @param string $summaryMessageKey message key of the summary
691 */
692 function outputHeader( $summaryMessageKey = '' ) {
693 global $wgContLang;
694
695 if ( $summaryMessageKey == '' ) {
696 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
697 } else {
698 $msg = $summaryMessageKey;
699 }
700 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
701 $this->getOutput()->wrapWikiMsg(
702 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
703 }
704
705 }
706
707 /**
708 * Returns the name that goes in the \<h1\> in the special page itself, and
709 * also the name that will be listed in Special:Specialpages
710 *
711 * Derived classes can override this, but usually it is easier to keep the
712 * default behavior. Messages can be added at run-time, see
713 * MessageCache.php.
714 *
715 * @return String
716 */
717 function getDescription() {
718 return $this->msg( strtolower( $this->mName ) )->text();
719 }
720
721 /**
722 * Get a self-referential title object
723 *
724 * @param $subpage String|Bool
725 * @return Title object
726 */
727 function getTitle( $subpage = false ) {
728 return self::getTitleFor( $this->mName, $subpage );
729 }
730
731 /**
732 * Sets the context this SpecialPage is executed in
733 *
734 * @param $context IContextSource
735 * @since 1.18
736 */
737 public function setContext( $context ) {
738 $this->mContext = $context;
739 }
740
741 /**
742 * Gets the context this SpecialPage is executed in
743 *
744 * @return IContextSource|RequestContext
745 * @since 1.18
746 */
747 public function getContext() {
748 if ( $this->mContext instanceof IContextSource ) {
749 return $this->mContext;
750 } else {
751 wfDebug( __METHOD__ . " called and \$mContext is null. " .
752 "Return RequestContext::getMain(); for sanity\n" );
753 return RequestContext::getMain();
754 }
755 }
756
757 /**
758 * Get the WebRequest being used for this instance
759 *
760 * @return WebRequest
761 * @since 1.18
762 */
763 public function getRequest() {
764 return $this->getContext()->getRequest();
765 }
766
767 /**
768 * Get the OutputPage being used for this instance
769 *
770 * @return OutputPage
771 * @since 1.18
772 */
773 public function getOutput() {
774 return $this->getContext()->getOutput();
775 }
776
777 /**
778 * Shortcut to get the User executing this instance
779 *
780 * @return User
781 * @since 1.18
782 */
783 public function getUser() {
784 return $this->getContext()->getUser();
785 }
786
787 /**
788 * Shortcut to get the skin being used for this instance
789 *
790 * @return Skin
791 * @since 1.18
792 */
793 public function getSkin() {
794 return $this->getContext()->getSkin();
795 }
796
797 /**
798 * Shortcut to get user's language
799 *
800 * @deprecated since 1.19 Use getLanguage instead
801 * @return Language
802 * @since 1.18
803 */
804 public function getLang() {
805 wfDeprecated( __METHOD__, '1.19' );
806 return $this->getLanguage();
807 }
808
809 /**
810 * Shortcut to get user's language
811 *
812 * @return Language
813 * @since 1.19
814 */
815 public function getLanguage() {
816 return $this->getContext()->getLanguage();
817 }
818
819 /**
820 * Return the full title, including $par
821 *
822 * @return Title
823 * @since 1.18
824 */
825 public function getFullTitle() {
826 return $this->getContext()->getTitle();
827 }
828
829 /**
830 * Wrapper around wfMessage that sets the current context.
831 *
832 * @return Message
833 * @see wfMessage
834 */
835 public function msg( /* $args */ ) {
836 $message = call_user_func_array(
837 array( $this->getContext(), 'msg' ),
838 func_get_args()
839 );
840 // RequestContext passes context to wfMessage, and the language is set from
841 // the context, but setting the language for Message class removes the
842 // interface message status, which breaks for example usernameless gender
843 // invocations. Restore the flag when not including special page in content.
844 if ( $this->including() ) {
845 $message->setInterfaceMessageFlag( false );
846 }
847 return $message;
848 }
849
850 /**
851 * Adds RSS/atom links
852 *
853 * @param $params array
854 */
855 protected function addFeedLinks( $params ) {
856 global $wgFeedClasses;
857
858 $feedTemplate = wfScript( 'api' );
859
860 foreach ( $wgFeedClasses as $format => $class ) {
861 $theseParams = $params + array( 'feedformat' => $format );
862 $url = wfAppendQuery( $feedTemplate, $theseParams );
863 $this->getOutput()->addFeedLink( $format, $url );
864 }
865 }
866
867 /**
868 * Get the group that the special page belongs in on Special:SpecialPage
869 * Use this method, instead of getGroupName to allow customization
870 * of the group name from the wiki side
871 *
872 * @return string Group of this special page
873 * @since 1.21
874 */
875 public function getFinalGroupName() {
876 global $wgSpecialPageGroups;
877 $name = $this->getName();
878
879 // Allow overbidding the group from the wiki side
880 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
881 if ( !$msg->isBlank() ) {
882 $group = $msg->text();
883 } else {
884 // Than use the group from this object
885 $group = $this->getGroupName();
886
887 // Group '-' is used as default to have the chance to determine,
888 // if the special pages overrides this method,
889 // if not overridden, $wgSpecialPageGroups is checked for b/c
890 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
891 $group = $wgSpecialPageGroups[$name];
892 }
893 }
894
895 // never give '-' back, change to 'other'
896 if ( $group === '-' ) {
897 $group = 'other';
898 }
899
900 return $group;
901 }
902
903 /**
904 * Under which header this special page is listed in Special:SpecialPages
905 * See messages 'specialpages-group-*' for valid names
906 * This method defaults to group 'other'
907 *
908 * @return string
909 * @since 1.21
910 */
911 protected function getGroupName() {
912 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
913 // Needed for b/c in getFinalGroupName
914 return '-';
915 }
916 }
917
918 /**
919 * Special page which uses an HTMLForm to handle processing. This is mostly a
920 * clone of FormAction. More special pages should be built this way; maybe this could be
921 * a new structure for SpecialPages
922 */
923 abstract class FormSpecialPage extends SpecialPage {
924 /**
925 * The sub-page of the special page.
926 * @var string
927 */
928 protected $par = null;
929
930 /**
931 * Get an HTMLForm descriptor array
932 * @return Array
933 */
934 abstract protected function getFormFields();
935
936 /**
937 * Add pre-text to the form
938 * @return String HTML which will be sent to $form->addPreText()
939 */
940 protected function preText() {
941 return '';
942 }
943
944 /**
945 * Add post-text to the form
946 * @return String HTML which will be sent to $form->addPostText()
947 */
948 protected function postText() {
949 return '';
950 }
951
952 /**
953 * Play with the HTMLForm if you need to more substantially
954 * @param $form HTMLForm
955 */
956 protected function alterForm( HTMLForm $form ) {
957 }
958
959 /**
960 * Get message prefix for HTMLForm
961 *
962 * @since 1.21
963 * @return string
964 */
965 protected function getMessagePrefix() {
966 return strtolower( $this->getName() );
967 }
968
969 /**
970 * Get the HTMLForm to control behavior
971 * @return HTMLForm|null
972 */
973 protected function getForm() {
974 $this->fields = $this->getFormFields();
975
976 $form = new HTMLForm( $this->fields, $this->getContext(), $this->getMessagePrefix() );
977 $form->setSubmitCallback( array( $this, 'onSubmit' ) );
978 // If the form is a compact vertical form, then don't output this ugly
979 // fieldset surrounding it.
980 // XXX Special pages can setDisplayFormat to 'vform' in alterForm(), but that
981 // is called after this.
982 if ( !$form->isVForm() ) {
983 $form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' );
984 }
985
986 $headerMsg = $this->msg( $this->getMessagePrefix() . '-text' );
987 if ( !$headerMsg->isDisabled() ) {
988 $form->addHeaderText( $headerMsg->parseAsBlock() );
989 }
990
991 // Retain query parameters (uselang etc)
992 $params = array_diff_key(
993 $this->getRequest()->getQueryValues(), array( 'title' => null ) );
994 $form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) );
995
996 $form->addPreText( $this->preText() );
997 $form->addPostText( $this->postText() );
998 $this->alterForm( $form );
999
1000 // Give hooks a chance to alter the form, adding extra fields or text etc
1001 wfRunHooks( "Special{$this->getName()}BeforeFormDisplay", array( &$form ) );
1002
1003 return $form;
1004 }
1005
1006 /**
1007 * Process the form on POST submission.
1008 * @param $data Array
1009 * @return Bool|Array true for success, false for didn't-try, array of errors on failure
1010 */
1011 abstract public function onSubmit( array $data );
1012
1013 /**
1014 * Do something exciting on successful processing of the form, most likely to show a
1015 * confirmation message
1016 * @since 1.22 Default is to do nothing
1017 */
1018 public function onSuccess() {
1019 }
1020
1021 /**
1022 * Basic SpecialPage workflow: get a form, send it to the user; get some data back,
1023 *
1024 * @param string $par Subpage string if one was specified
1025 */
1026 public function execute( $par ) {
1027 $this->setParameter( $par );
1028 $this->setHeaders();
1029
1030 // This will throw exceptions if there's a problem
1031 $this->checkExecutePermissions( $this->getUser() );
1032
1033 $form = $this->getForm();
1034 if ( $form->show() ) {
1035 $this->onSuccess();
1036 }
1037 }
1038
1039 /**
1040 * Maybe do something interesting with the subpage parameter
1041 * @param string $par
1042 */
1043 protected function setParameter( $par ) {
1044 $this->par = $par;
1045 }
1046
1047 /**
1048 * Called from execute() to check if the given user can perform this action.
1049 * Failures here must throw subclasses of ErrorPageError.
1050 * @param $user User
1051 * @throws UserBlockedError
1052 * @return Bool true
1053 */
1054 protected function checkExecutePermissions( User $user ) {
1055 $this->checkPermissions();
1056
1057 if ( $this->requiresUnblock() && $user->isBlocked() ) {
1058 $block = $user->getBlock();
1059 throw new UserBlockedError( $block );
1060 }
1061
1062 if ( $this->requiresWrite() ) {
1063 $this->checkReadOnly();
1064 }
1065
1066 return true;
1067 }
1068
1069 /**
1070 * Whether this action requires the wiki not to be locked
1071 * @return Bool
1072 */
1073 public function requiresWrite() {
1074 return true;
1075 }
1076
1077 /**
1078 * Whether this action cannot be executed by a blocked user
1079 * @return Bool
1080 */
1081 public function requiresUnblock() {
1082 return true;
1083 }
1084 }
1085
1086 /**
1087 * Shortcut to construct a special page which is unlisted by default
1088 * @ingroup SpecialPage
1089 */
1090 class UnlistedSpecialPage extends SpecialPage {
1091 function __construct( $name, $restriction = '', $function = false, $file = 'default' ) {
1092 parent::__construct( $name, $restriction, false, $function, $file );
1093 }
1094
1095 public function isListed() {
1096 return false;
1097 }
1098 }
1099
1100 /**
1101 * Shortcut to construct an includable special page
1102 * @ingroup SpecialPage
1103 */
1104 class IncludableSpecialPage extends SpecialPage {
1105 function __construct(
1106 $name, $restriction = '', $listed = true, $function = false, $file = 'default'
1107 ) {
1108 parent::__construct( $name, $restriction, $listed, $function, $file, true );
1109 }
1110
1111 public function isIncludable() {
1112 return true;
1113 }
1114 }
1115
1116 /**
1117 * Shortcut to construct a special page alias.
1118 * @ingroup SpecialPage
1119 */
1120 abstract class RedirectSpecialPage extends UnlistedSpecialPage {
1121
1122 // Query parameters that can be passed through redirects
1123 protected $mAllowedRedirectParams = array();
1124
1125 // Query parameters added by redirects
1126 protected $mAddedRedirectParams = array();
1127
1128 public function execute( $par ) {
1129 $redirect = $this->getRedirect( $par );
1130 $query = $this->getRedirectQuery();
1131 // Redirect to a page title with possible query parameters
1132 if ( $redirect instanceof Title ) {
1133 $url = $redirect->getFullURL( $query );
1134 $this->getOutput()->redirect( $url );
1135 return $redirect;
1136 } elseif ( $redirect === true ) {
1137 // Redirect to index.php with query parameters
1138 $url = wfAppendQuery( wfScript( 'index' ), $query );
1139 $this->getOutput()->redirect( $url );
1140 return $redirect;
1141 } else {
1142 $class = get_class( $this );
1143 throw new MWException( "RedirectSpecialPage $class doesn't redirect!" );
1144 }
1145 }
1146
1147 /**
1148 * If the special page is a redirect, then get the Title object it redirects to.
1149 * False otherwise.
1150 *
1151 * @param string $par Subpage string
1152 * @return Title|bool
1153 */
1154 abstract public function getRedirect( $par );
1155
1156 /**
1157 * Return part of the request string for a special redirect page
1158 * This allows passing, e.g. action=history to Special:Mypage, etc.
1159 *
1160 * @return String
1161 */
1162 public function getRedirectQuery() {
1163 $params = array();
1164
1165 foreach ( $this->mAllowedRedirectParams as $arg ) {
1166 if ( $this->getRequest()->getVal( $arg, null ) !== null ) {
1167 $params[$arg] = $this->getRequest()->getVal( $arg );
1168 }
1169 }
1170
1171 foreach ( $this->mAddedRedirectParams as $arg => $val ) {
1172 $params[$arg] = $val;
1173 }
1174
1175 return count( $params )
1176 ? $params
1177 : false;
1178 }
1179 }
1180
1181 abstract class SpecialRedirectToSpecial extends RedirectSpecialPage {
1182 // @todo FIXME: Visibility must be declared
1183 var $redirName, $redirSubpage;
1184
1185 function __construct(
1186 $name, $redirName, $redirSubpage = false,
1187 $allowedRedirectParams = array(), $addedRedirectParams = array()
1188 ) {
1189 parent::__construct( $name );
1190 $this->redirName = $redirName;
1191 $this->redirSubpage = $redirSubpage;
1192 $this->mAllowedRedirectParams = $allowedRedirectParams;
1193 $this->mAddedRedirectParams = $addedRedirectParams;
1194 }
1195
1196 public function getRedirect( $subpage ) {
1197 if ( $this->redirSubpage === false ) {
1198 return SpecialPage::getTitleFor( $this->redirName, $subpage );
1199 } else {
1200 return SpecialPage::getTitleFor( $this->redirName, $this->redirSubpage );
1201 }
1202 }
1203 }
1204
1205 /**
1206 * ListAdmins --> ListUsers/sysop
1207 */
1208 class SpecialListAdmins extends SpecialRedirectToSpecial {
1209 function __construct() {
1210 parent::__construct( 'Listadmins', 'Listusers', 'sysop' );
1211 }
1212 }
1213
1214 /**
1215 * ListBots --> ListUsers/bot
1216 */
1217 class SpecialListBots extends SpecialRedirectToSpecial {
1218 function __construct() {
1219 parent::__construct( 'Listbots', 'Listusers', 'bot' );
1220 }
1221 }
1222
1223 /**
1224 * CreateAccount --> UserLogin/signup
1225 * @todo FIXME: This (and the rest of the login frontend) needs to die a horrible painful death
1226 */
1227 class SpecialCreateAccount extends SpecialRedirectToSpecial {
1228 function __construct() {
1229 parent::__construct( 'CreateAccount', 'Userlogin', 'signup', array( 'returnto', 'returntoquery', 'uselang' ) );
1230 }
1231
1232 // No reason to hide this link on Special:Specialpages
1233 public function isListed() {
1234 return true;
1235 }
1236
1237 protected function getGroupName() {
1238 return 'login';
1239 }
1240 }
1241 /**
1242 * SpecialMypage, SpecialMytalk and SpecialMycontributions special pages
1243 * are used to get user independent links pointing to the user page, talk
1244 * page and list of contributions.
1245 * This can let us cache a single copy of any generated content for all
1246 * users.
1247 */
1248
1249 /**
1250 * Superclass for any RedirectSpecialPage which redirects the user
1251 * to a particular article (as opposed to user contributions, logs, etc.).
1252 *
1253 * For security reasons these special pages are restricted to pass on
1254 * the following subset of GET parameters to the target page while
1255 * removing all others:
1256 *
1257 * - useskin, uselang, printable: to alter the appearance of the resulting page
1258 *
1259 * - redirect: allows viewing one's user page or talk page even if it is a
1260 * redirect.
1261 *
1262 * - rdfrom: allows redirecting to one's user page or talk page from an
1263 * external wiki with the "Redirect from..." notice.
1264 *
1265 * - limit, offset: Useful for linking to history of one's own user page or
1266 * user talk page. For example, this would be a link to "the last edit to your
1267 * user talk page in the year 2010":
1268 * http://en.wikipedia.org/wiki/Special:MyPage?offset=20110000000000&limit=1&action=history
1269 *
1270 * - feed: would allow linking to the current user's RSS feed for their user
1271 * talk page:
1272 * http://en.wikipedia.org/w/index.php?title=Special:MyTalk&action=history&feed=rss
1273 *
1274 * - preloadtitle: Can be used to provide a default section title for a
1275 * preloaded new comment on one's own talk page.
1276 *
1277 * - summary : Can be used to provide a default edit summary for a preloaded
1278 * edit to one's own user page or talk page.
1279 *
1280 * - preview: Allows showing/hiding preview on first edit regardless of user
1281 * preference, useful for preloaded edits where you know preview wouldn't be
1282 * useful.
1283 *
1284 * - internaledit, externaledit, mode: Allows forcing the use of the
1285 * internal/external editor, e.g. to force the internal editor for
1286 * short/simple preloaded edits.
1287 *
1288 * - redlink: Affects the message the user sees if their talk page/user talk
1289 * page does not currently exist. Avoids confusion for newbies with no user
1290 * pages over why they got a "permission error" following this link:
1291 * http://en.wikipedia.org/w/index.php?title=Special:MyPage&redlink=1
1292 *
1293 * - debug: determines whether the debug parameter is passed to load.php,
1294 * which disables reformatting and allows scripts to be debugged. Useful
1295 * when debugging scripts that manipulate one's own user page or talk page.
1296 *
1297 * @par Hook extension:
1298 * Extensions can add to the redirect parameters list by using the hook
1299 * RedirectSpecialArticleRedirectParams
1300 *
1301 * This hook allows extensions which add GET parameters like FlaggedRevs to
1302 * retain those parameters when redirecting using special pages.
1303 *
1304 * @par Hook extension example:
1305 * @code
1306 * $wgHooks['RedirectSpecialArticleRedirectParams'][] =
1307 * 'MyExtensionHooks::onRedirectSpecialArticleRedirectParams';
1308 * public static function onRedirectSpecialArticleRedirectParams( &$redirectParams ) {
1309 * $redirectParams[] = 'stable';
1310 * return true;
1311 * }
1312 * @endcode
1313 * @ingroup SpecialPage
1314 */
1315 abstract class RedirectSpecialArticle extends RedirectSpecialPage {
1316 function __construct( $name ) {
1317 parent::__construct( $name );
1318 $redirectParams = array(
1319 'action',
1320 'redirect', 'rdfrom',
1321 # Options for preloaded edits
1322 'preload', 'editintro', 'preloadtitle', 'summary', 'nosummary',
1323 # Options for overriding user settings
1324 'preview', 'internaledit', 'externaledit', 'mode', 'minor', 'watchthis',
1325 # Options for history/diffs
1326 'section', 'oldid', 'diff', 'dir',
1327 'limit', 'offset', 'feed',
1328 # Misc options
1329 'redlink', 'debug',
1330 # Options for action=raw; missing ctype can break JS or CSS in some browsers
1331 'ctype', 'maxage', 'smaxage',
1332 );
1333
1334 wfRunHooks( "RedirectSpecialArticleRedirectParams", array( &$redirectParams ) );
1335 $this->mAllowedRedirectParams = $redirectParams;
1336 }
1337 }
1338
1339 /**
1340 * Shortcut to construct a special page pointing to current user user's page.
1341 * @ingroup SpecialPage
1342 */
1343 class SpecialMypage extends RedirectSpecialArticle {
1344 function __construct() {
1345 parent::__construct( 'Mypage' );
1346 }
1347
1348 function getRedirect( $subpage ) {
1349 if ( strval( $subpage ) !== '' ) {
1350 return Title::makeTitle( NS_USER, $this->getUser()->getName() . '/' . $subpage );
1351 } else {
1352 return Title::makeTitle( NS_USER, $this->getUser()->getName() );
1353 }
1354 }
1355 }
1356
1357 /**
1358 * Shortcut to construct a special page pointing to current user talk page.
1359 * @ingroup SpecialPage
1360 */
1361 class SpecialMytalk extends RedirectSpecialArticle {
1362 function __construct() {
1363 parent::__construct( 'Mytalk' );
1364 }
1365
1366 function getRedirect( $subpage ) {
1367 if ( strval( $subpage ) !== '' ) {
1368 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() . '/' . $subpage );
1369 } else {
1370 return Title::makeTitle( NS_USER_TALK, $this->getUser()->getName() );
1371 }
1372 }
1373 }
1374
1375 /**
1376 * Shortcut to construct a special page pointing to current user contributions.
1377 * @ingroup SpecialPage
1378 */
1379 class SpecialMycontributions extends RedirectSpecialPage {
1380 function __construct() {
1381 parent::__construct( 'Mycontributions' );
1382 $this->mAllowedRedirectParams = array( 'limit', 'namespace', 'tagfilter',
1383 'offset', 'dir', 'year', 'month', 'feed' );
1384 }
1385
1386 function getRedirect( $subpage ) {
1387 return SpecialPage::getTitleFor( 'Contributions', $this->getUser()->getName() );
1388 }
1389 }
1390
1391 /**
1392 * Redirect to Special:Listfiles?user=$wgUser
1393 */
1394 class SpecialMyuploads extends RedirectSpecialPage {
1395 function __construct() {
1396 parent::__construct( 'Myuploads' );
1397 $this->mAllowedRedirectParams = array( 'limit', 'ilshowall', 'ilsearch' );
1398 }
1399
1400 function getRedirect( $subpage ) {
1401 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1402 }
1403 }
1404
1405 /**
1406 * Redirect Special:Listfiles?user=$wgUser&ilshowall=true
1407 */
1408 class SpecialAllMyUploads extends RedirectSpecialPage {
1409 function __construct() {
1410 parent::__construct( 'AllMyUploads' );
1411 $this->mAllowedRedirectParams = array( 'limit', 'ilsearch' );
1412 }
1413
1414 function getRedirect( $subpage ) {
1415 $this->mAddedRedirectParams['ilshowall'] = 1;
1416 return SpecialPage::getTitleFor( 'Listfiles', $this->getUser()->getName() );
1417 }
1418 }
1419
1420
1421 /**
1422 * Redirect from Special:PermanentLink/### to index.php?oldid=###
1423 */
1424 class SpecialPermanentLink extends RedirectSpecialPage {
1425 function __construct() {
1426 parent::__construct( 'PermanentLink' );
1427 $this->mAllowedRedirectParams = array();
1428 }
1429
1430 function getRedirect( $subpage ) {
1431 $subpage = intval( $subpage );
1432 if ( $subpage === 0 ) {
1433 # throw an error page when no subpage was given
1434 throw new ErrorPageError( 'nopagetitle', 'nopagetext' );
1435 }
1436 $this->mAddedRedirectParams['oldid'] = $subpage;
1437 return true;
1438 }
1439 }