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