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