Merge "assertValidHtml for checking html in test cases."
[lhc/web/wiklou.git] / includes / specialpage / 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 class for all special pages.
26 *
27 * Includes some static functions for handling the special page list deprecated
28 * in favor of SpecialPageFactory.
29 *
30 * @todo Turn this into a real ContextSource
31 * @ingroup SpecialPage
32 */
33 class SpecialPage {
34 // The canonical name of this special page
35 // Also used for the default <h1> heading, @see getDescription()
36 protected $mName;
37
38 // The local name of this special page
39 private $mLocalName;
40
41 // Minimum user level required to access this page, or "" for anyone.
42 // Also used to categorise the pages in Special:Specialpages
43 private $mRestriction;
44
45 // Listed in Special:Specialpages?
46 private $mListed;
47
48 // Function name called by the default execute()
49 private $mFunction;
50
51 // File which needs to be included before the function above can be called
52 private $mFile;
53
54 // Whether or not this special page is being included from an article
55 protected $mIncluding;
56
57 // Whether the special page can be included in an article
58 protected $mIncludable;
59
60 /**
61 * Current request context
62 * @var IContextSource
63 */
64 protected $mContext;
65
66 /**
67 * Get a localised Title object for a specified special page name
68 *
69 * @param string $name
70 * @param string|bool $subpage Subpage string, or false to not use a subpage
71 * @param string $fragment The link fragment (after the "#")
72 * @return Title
73 * @throws MWException
74 */
75 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
76 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
77 return Title::makeTitle( NS_SPECIAL, $name, $fragment );
78 }
79
80 /**
81 * Get a localised Title object for a page name with a possibly unvalidated subpage
82 *
83 * @param string $name
84 * @param string|bool $subpage Subpage string, or false to not use a subpage
85 * @return Title|null Title object or null if the page doesn't exist
86 */
87 public static function getSafeTitleFor( $name, $subpage = false ) {
88 $name = SpecialPageFactory::getLocalNameFor( $name, $subpage );
89 if ( $name ) {
90 return Title::makeTitleSafe( NS_SPECIAL, $name );
91 } else {
92 return null;
93 }
94 }
95
96 /**
97 * Default constructor for special pages
98 * Derivative classes should call this from their constructor
99 * Note that if the user does not have the required level, an error message will
100 * be displayed by the default execute() method, without the global function ever
101 * being called.
102 *
103 * If you override execute(), you can recover the default behavior with userCanExecute()
104 * and displayRestrictionError()
105 *
106 * @param string $name Name of the special page, as seen in links and URLs
107 * @param string $restriction User right required, e.g. "block" or "delete"
108 * @param bool $listed Whether the page is listed in Special:Specialpages
109 * @param callable|bool $function Function called by execute(). By default
110 * it is constructed from $name
111 * @param string $file File which is included by execute(). It is also
112 * constructed from $name by default
113 * @param bool $includable Whether the page can be included in normal pages
114 */
115 public function __construct(
116 $name = '', $restriction = '', $listed = true,
117 $function = false, $file = 'default', $includable = false
118 ) {
119 $this->mName = $name;
120 $this->mRestriction = $restriction;
121 $this->mListed = $listed;
122 $this->mIncludable = $includable;
123 if ( !$function ) {
124 $this->mFunction = 'wfSpecial' . $name;
125 } else {
126 $this->mFunction = $function;
127 }
128 if ( $file === 'default' ) {
129 $this->mFile = __DIR__ . "/specials/Special$name.php";
130 } else {
131 $this->mFile = $file;
132 }
133 }
134
135 /**
136 * Get the name of this Special Page.
137 * @return string
138 */
139 function getName() {
140 return $this->mName;
141 }
142
143 /**
144 * Get the permission that a user must have to execute this page
145 * @return string
146 */
147 function getRestriction() {
148 return $this->mRestriction;
149 }
150
151 /**
152 * Get the file which will be included by SpecialPage::execute() if your extension is
153 * still stuck in the past and hasn't overridden the execute() method. No modern code
154 * should want or need to know this.
155 * @return string
156 * @deprecated since 1.18
157 */
158 function getFile() {
159 wfDeprecated( __METHOD__, '1.18' );
160 return $this->mFile;
161 }
162
163 // @todo FIXME: Decide which syntax to use for this, and stick to it
164 /**
165 * Whether this special page is listed in Special:SpecialPages
166 * @since r3583 (v1.3)
167 * @return bool
168 */
169 function isListed() {
170 return $this->mListed;
171 }
172 /**
173 * Set whether this page is listed in Special:Specialpages, at run-time
174 * @since 1.3
175 * @param bool $listed
176 * @return bool
177 */
178 function setListed( $listed ) {
179 return wfSetVar( $this->mListed, $listed );
180 }
181 /**
182 * Get or set whether this special page is listed in Special:SpecialPages
183 * @since 1.6
184 * @param bool $x
185 * @return bool
186 */
187 function listed( $x = null ) {
188 return wfSetVar( $this->mListed, $x );
189 }
190
191 /**
192 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
193 * @return bool
194 */
195 public function isIncludable() {
196 return $this->mIncludable;
197 }
198
199 /**
200 * Whether the special page is being evaluated via transclusion
201 * @param bool $x
202 * @return bool
203 */
204 function including( $x = null ) {
205 return wfSetVar( $this->mIncluding, $x );
206 }
207
208 /**
209 * Get the localised name of the special page
210 * @return string
211 */
212 function getLocalName() {
213 if ( !isset( $this->mLocalName ) ) {
214 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
215 }
216 return $this->mLocalName;
217 }
218
219 /**
220 * Is this page expensive (for some definition of expensive)?
221 * Expensive pages are disabled or cached in miser mode. Originally used
222 * (and still overridden) by QueryPage and subclasses, moved here so that
223 * Special:SpecialPages can safely call it for all special pages.
224 *
225 * @return bool
226 */
227 public function isExpensive() {
228 return false;
229 }
230
231 /**
232 * Is this page cached?
233 * Expensive pages are cached or disabled in miser mode.
234 * Used by QueryPage and subclasses, moved here so that
235 * Special:SpecialPages can safely call it for all special pages.
236 *
237 * @return bool
238 * @since 1.21
239 */
240 public function isCached() {
241 return false;
242 }
243
244 /**
245 * Can be overridden by subclasses with more complicated permissions
246 * schemes.
247 *
248 * @return bool Should the page be displayed with the restricted-access
249 * pages?
250 */
251 public function isRestricted() {
252 // DWIM: If anons can do something, then it is not restricted
253 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
254 }
255
256 /**
257 * Checks if the given user (identified by an object) can execute this
258 * special page (as defined by $mRestriction). Can be overridden by sub-
259 * classes with more complicated permissions schemes.
260 *
261 * @param User $user The user to check
262 * @return bool Does the user have permission to view the page?
263 */
264 public function userCanExecute( User $user ) {
265 return $user->isAllowed( $this->mRestriction );
266 }
267
268 /**
269 * Output an error message telling the user what access level they have to have
270 * @throws PermissionsError
271 */
272 function displayRestrictionError() {
273 throw new PermissionsError( $this->mRestriction );
274 }
275
276 /**
277 * Checks if userCanExecute, and if not throws a PermissionsError
278 *
279 * @since 1.19
280 * @return void
281 * @throws PermissionsError
282 */
283 public function checkPermissions() {
284 if ( !$this->userCanExecute( $this->getUser() ) ) {
285 $this->displayRestrictionError();
286 }
287 }
288
289 /**
290 * If the wiki is currently in readonly mode, throws a ReadOnlyError
291 *
292 * @since 1.19
293 * @return void
294 * @throws ReadOnlyError
295 */
296 public function checkReadOnly() {
297 if ( wfReadOnly() ) {
298 throw new ReadOnlyError;
299 }
300 }
301
302 /**
303 * If the user is not logged in, throws UserNotLoggedIn error.
304 *
305 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
306 * parameter.
307 *
308 * @since 1.23
309 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
310 * will be used as message keys. If a string is given, the message will also receive a
311 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
312 * Message is given, it will be passed on verbatim.
313 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
314 * will be used as message keys.
315 * @throws UserNotLoggedIn
316 */
317 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
318 if ( $this->getUser()->isAnon() ) {
319 // Use default messages if not given or explicit null passed
320 if ( !$reasonMsg ) {
321 $reasonMsg = 'exception-nologin-text-manual';
322 }
323 if ( !$titleMsg ) {
324 $titleMsg = 'exception-nologin';
325 }
326
327 // Convert to Messages with current context
328 if ( is_string( $reasonMsg ) ) {
329 $loginreqlink = Linker::linkKnown(
330 SpecialPage::getTitleFor( 'Userlogin' ),
331 $this->msg( 'loginreqlink' )->escaped(),
332 array(),
333 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
334 );
335 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
336 }
337 if ( is_string( $titleMsg ) ) {
338 $titleMsg = $this->msg( $titleMsg );
339 }
340
341 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
342 }
343 }
344
345 /**
346 * Sets headers - this should be called from the execute() method of all derived classes!
347 */
348 function setHeaders() {
349 $out = $this->getOutput();
350 $out->setArticleRelated( false );
351 $out->setRobotPolicy( "noindex,nofollow" );
352 $out->setPageTitle( $this->getDescription() );
353 }
354
355 /**
356 * Entry point.
357 *
358 * @since 1.20
359 *
360 * @param string|null $subPage
361 */
362 final public function run( $subPage ) {
363 /**
364 * Gets called before @see SpecialPage::execute.
365 *
366 * @since 1.20
367 *
368 * @param SpecialPage $this
369 * @param string|null $subPage
370 */
371 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
372
373 $this->beforeExecute( $subPage );
374 $this->execute( $subPage );
375 $this->afterExecute( $subPage );
376
377 /**
378 * Gets called after @see SpecialPage::execute.
379 *
380 * @since 1.20
381 *
382 * @param SpecialPage $this
383 * @param string|null $subPage
384 */
385 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
386 }
387
388 /**
389 * Gets called before @see SpecialPage::execute.
390 *
391 * @since 1.20
392 *
393 * @param string|null $subPage
394 */
395 protected function beforeExecute( $subPage ) {
396 // No-op
397 }
398
399 /**
400 * Gets called after @see SpecialPage::execute.
401 *
402 * @since 1.20
403 *
404 * @param string|null $subPage
405 */
406 protected function afterExecute( $subPage ) {
407 // No-op
408 }
409
410 /**
411 * Default execute method
412 * Checks user permissions, calls the function given in mFunction
413 *
414 * This must be overridden by subclasses; it will be made abstract in a future version
415 *
416 * @param string|null $subPage
417 */
418 public function execute( $subPage ) {
419 $this->setHeaders();
420 $this->checkPermissions();
421
422 $func = $this->mFunction;
423 // only load file if the function does not exist
424 if ( !is_callable( $func ) && $this->mFile ) {
425 require_once $this->mFile;
426 }
427 $this->outputHeader();
428 call_user_func( $func, $subPage, $this );
429 }
430
431 /**
432 * Outputs a summary message on top of special pages
433 * Per default the message key is the canonical name of the special page
434 * May be overridden, i.e. by extensions to stick with the naming conventions
435 * for message keys: 'extensionname-xxx'
436 *
437 * @param string $summaryMessageKey Message key of the summary
438 */
439 function outputHeader( $summaryMessageKey = '' ) {
440 global $wgContLang;
441
442 if ( $summaryMessageKey == '' ) {
443 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
444 } else {
445 $msg = $summaryMessageKey;
446 }
447 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
448 $this->getOutput()->wrapWikiMsg(
449 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
450 }
451
452 }
453
454 /**
455 * Returns the name that goes in the \<h1\> in the special page itself, and
456 * also the name that will be listed in Special:Specialpages
457 *
458 * Derived classes can override this, but usually it is easier to keep the
459 * default behavior.
460 *
461 * @return string
462 */
463 function getDescription() {
464 return $this->msg( strtolower( $this->mName ) )->text();
465 }
466
467 /**
468 * Get a self-referential title object
469 *
470 * @param string|bool $subpage
471 * @return Title
472 * @deprecated in 1.23, use SpecialPage::getPageTitle
473 */
474 function getTitle( $subpage = false ) {
475 wfDeprecated( __METHOD__, '1.23' );
476 return $this->getPageTitle( $subpage );
477 }
478
479 /**
480 * Get a self-referential title object
481 *
482 * @param string|bool $subpage
483 * @return Title
484 * @since 1.23
485 */
486 function getPageTitle( $subpage = false ) {
487 return self::getTitleFor( $this->mName, $subpage );
488 }
489
490 /**
491 * Sets the context this SpecialPage is executed in
492 *
493 * @param IContextSource $context
494 * @since 1.18
495 */
496 public function setContext( $context ) {
497 $this->mContext = $context;
498 }
499
500 /**
501 * Gets the context this SpecialPage is executed in
502 *
503 * @return IContextSource|RequestContext
504 * @since 1.18
505 */
506 public function getContext() {
507 if ( $this->mContext instanceof IContextSource ) {
508 return $this->mContext;
509 } else {
510 wfDebug( __METHOD__ . " called and \$mContext is null. " .
511 "Return RequestContext::getMain(); for sanity\n" );
512 return RequestContext::getMain();
513 }
514 }
515
516 /**
517 * Get the WebRequest being used for this instance
518 *
519 * @return WebRequest
520 * @since 1.18
521 */
522 public function getRequest() {
523 return $this->getContext()->getRequest();
524 }
525
526 /**
527 * Get the OutputPage being used for this instance
528 *
529 * @return OutputPage
530 * @since 1.18
531 */
532 public function getOutput() {
533 return $this->getContext()->getOutput();
534 }
535
536 /**
537 * Shortcut to get the User executing this instance
538 *
539 * @return User
540 * @since 1.18
541 */
542 public function getUser() {
543 return $this->getContext()->getUser();
544 }
545
546 /**
547 * Shortcut to get the skin being used for this instance
548 *
549 * @return Skin
550 * @since 1.18
551 */
552 public function getSkin() {
553 return $this->getContext()->getSkin();
554 }
555
556 /**
557 * Shortcut to get user's language
558 *
559 * @deprecated since 1.19 Use getLanguage instead
560 * @return Language
561 * @since 1.18
562 */
563 public function getLang() {
564 wfDeprecated( __METHOD__, '1.19' );
565 return $this->getLanguage();
566 }
567
568 /**
569 * Shortcut to get user's language
570 *
571 * @return Language
572 * @since 1.19
573 */
574 public function getLanguage() {
575 return $this->getContext()->getLanguage();
576 }
577
578 /**
579 * Return the full title, including $par
580 *
581 * @return Title
582 * @since 1.18
583 */
584 public function getFullTitle() {
585 return $this->getContext()->getTitle();
586 }
587
588 /**
589 * Wrapper around wfMessage that sets the current context.
590 *
591 * @return Message
592 * @see wfMessage
593 */
594 public function msg( /* $args */ ) {
595 $message = call_user_func_array(
596 array( $this->getContext(), 'msg' ),
597 func_get_args()
598 );
599 // RequestContext passes context to wfMessage, and the language is set from
600 // the context, but setting the language for Message class removes the
601 // interface message status, which breaks for example usernameless gender
602 // invocations. Restore the flag when not including special page in content.
603 if ( $this->including() ) {
604 $message->setInterfaceMessageFlag( false );
605 }
606 return $message;
607 }
608
609 /**
610 * Adds RSS/atom links
611 *
612 * @param array $params
613 */
614 protected function addFeedLinks( $params ) {
615 global $wgFeedClasses;
616
617 $feedTemplate = wfScript( 'api' );
618
619 foreach ( $wgFeedClasses as $format => $class ) {
620 $theseParams = $params + array( 'feedformat' => $format );
621 $url = wfAppendQuery( $feedTemplate, $theseParams );
622 $this->getOutput()->addFeedLink( $format, $url );
623 }
624 }
625
626 /**
627 * Get the group that the special page belongs in on Special:SpecialPage
628 * Use this method, instead of getGroupName to allow customization
629 * of the group name from the wiki side
630 *
631 * @return string Group of this special page
632 * @since 1.21
633 */
634 public function getFinalGroupName() {
635 global $wgSpecialPageGroups;
636 $name = $this->getName();
637
638 // Allow overbidding the group from the wiki side
639 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
640 if ( !$msg->isBlank() ) {
641 $group = $msg->text();
642 } else {
643 // Than use the group from this object
644 $group = $this->getGroupName();
645
646 // Group '-' is used as default to have the chance to determine,
647 // if the special pages overrides this method,
648 // if not overridden, $wgSpecialPageGroups is checked for b/c
649 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
650 $group = $wgSpecialPageGroups[$name];
651 }
652 }
653
654 // never give '-' back, change to 'other'
655 if ( $group === '-' ) {
656 $group = 'other';
657 }
658
659 return $group;
660 }
661
662 /**
663 * Under which header this special page is listed in Special:SpecialPages
664 * See messages 'specialpages-group-*' for valid names
665 * This method defaults to group 'other'
666 *
667 * @return string
668 * @since 1.21
669 */
670 protected function getGroupName() {
671 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
672 // Needed for b/c in getFinalGroupName
673 return '-';
674 }
675 }