Add getRobotPolicy()
[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 /**
174 * Set whether this page is listed in Special:Specialpages, at run-time
175 * @since 1.3
176 * @param bool $listed
177 * @return bool
178 */
179 function setListed( $listed ) {
180 return wfSetVar( $this->mListed, $listed );
181 }
182
183 /**
184 * Get or set whether this special page is listed in Special:SpecialPages
185 * @since 1.6
186 * @param bool $x
187 * @return bool
188 */
189 function listed( $x = null ) {
190 return wfSetVar( $this->mListed, $x );
191 }
192
193 /**
194 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
195 * @return bool
196 */
197 public function isIncludable() {
198 return $this->mIncludable;
199 }
200
201 /**
202 * Whether the special page is being evaluated via transclusion
203 * @param bool $x
204 * @return bool
205 */
206 function including( $x = null ) {
207 return wfSetVar( $this->mIncluding, $x );
208 }
209
210 /**
211 * Get the localised name of the special page
212 * @return string
213 */
214 function getLocalName() {
215 if ( !isset( $this->mLocalName ) ) {
216 $this->mLocalName = SpecialPageFactory::getLocalNameFor( $this->mName );
217 }
218 return $this->mLocalName;
219 }
220
221 /**
222 * Is this page expensive (for some definition of expensive)?
223 * Expensive pages are disabled or cached in miser mode. Originally used
224 * (and still overridden) by QueryPage and subclasses, moved here so that
225 * Special:SpecialPages can safely call it for all special pages.
226 *
227 * @return bool
228 */
229 public function isExpensive() {
230 return false;
231 }
232
233 /**
234 * Is this page cached?
235 * Expensive pages are cached or disabled in miser mode.
236 * Used by QueryPage and subclasses, moved here so that
237 * Special:SpecialPages can safely call it for all special pages.
238 *
239 * @return bool
240 * @since 1.21
241 */
242 public function isCached() {
243 return false;
244 }
245
246 /**
247 * Can be overridden by subclasses with more complicated permissions
248 * schemes.
249 *
250 * @return bool Should the page be displayed with the restricted-access
251 * pages?
252 */
253 public function isRestricted() {
254 // DWIM: If anons can do something, then it is not restricted
255 return $this->mRestriction != '' && !User::groupHasPermission( '*', $this->mRestriction );
256 }
257
258 /**
259 * Checks if the given user (identified by an object) can execute this
260 * special page (as defined by $mRestriction). Can be overridden by sub-
261 * classes with more complicated permissions schemes.
262 *
263 * @param User $user The user to check
264 * @return bool Does the user have permission to view the page?
265 */
266 public function userCanExecute( User $user ) {
267 return $user->isAllowed( $this->mRestriction );
268 }
269
270 /**
271 * Output an error message telling the user what access level they have to have
272 * @throws PermissionsError
273 */
274 function displayRestrictionError() {
275 throw new PermissionsError( $this->mRestriction );
276 }
277
278 /**
279 * Checks if userCanExecute, and if not throws a PermissionsError
280 *
281 * @since 1.19
282 * @return void
283 * @throws PermissionsError
284 */
285 public function checkPermissions() {
286 if ( !$this->userCanExecute( $this->getUser() ) ) {
287 $this->displayRestrictionError();
288 }
289 }
290
291 /**
292 * If the wiki is currently in readonly mode, throws a ReadOnlyError
293 *
294 * @since 1.19
295 * @return void
296 * @throws ReadOnlyError
297 */
298 public function checkReadOnly() {
299 if ( wfReadOnly() ) {
300 throw new ReadOnlyError;
301 }
302 }
303
304 /**
305 * If the user is not logged in, throws UserNotLoggedIn error.
306 *
307 * Default error message includes a link to Special:Userlogin with properly set 'returnto' query
308 * parameter.
309 *
310 * @since 1.23
311 * @param string|Message $reasonMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
312 * will be used as message keys. If a string is given, the message will also receive a
313 * formatted login link (generated using the 'loginreqlink' message) as first parameter. If a
314 * Message is given, it will be passed on verbatim.
315 * @param string|Message $titleMsg [optional] Passed on to UserNotLoggedIn constructor. Strings
316 * will be used as message keys.
317 * @throws UserNotLoggedIn
318 */
319 public function requireLogin( $reasonMsg = null, $titleMsg = null ) {
320 if ( $this->getUser()->isAnon() ) {
321 // Use default messages if not given or explicit null passed
322 if ( !$reasonMsg ) {
323 $reasonMsg = 'exception-nologin-text-manual';
324 }
325 if ( !$titleMsg ) {
326 $titleMsg = 'exception-nologin';
327 }
328
329 // Convert to Messages with current context
330 if ( is_string( $reasonMsg ) ) {
331 $loginreqlink = Linker::linkKnown(
332 SpecialPage::getTitleFor( 'Userlogin' ),
333 $this->msg( 'loginreqlink' )->escaped(),
334 array(),
335 array( 'returnto' => $this->getPageTitle()->getPrefixedText() )
336 );
337 $reasonMsg = $this->msg( $reasonMsg )->rawParams( $loginreqlink );
338 }
339 if ( is_string( $titleMsg ) ) {
340 $titleMsg = $this->msg( $titleMsg );
341 }
342
343 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
344 }
345 }
346
347 /**
348 * Sets headers - this should be called from the execute() method of all derived classes!
349 */
350 function setHeaders() {
351 $out = $this->getOutput();
352 $out->setArticleRelated( false );
353 $out->setRobotPolicy( $this->getRobotPolicy() );
354 $out->setPageTitle( $this->getDescription() );
355 }
356
357 /**
358 * Entry point.
359 *
360 * @since 1.20
361 *
362 * @param string|null $subPage
363 */
364 final public function run( $subPage ) {
365 /**
366 * Gets called before @see SpecialPage::execute.
367 *
368 * @since 1.20
369 *
370 * @param SpecialPage $this
371 * @param string|null $subPage
372 */
373 wfRunHooks( 'SpecialPageBeforeExecute', array( $this, $subPage ) );
374
375 $this->beforeExecute( $subPage );
376 $this->execute( $subPage );
377 $this->afterExecute( $subPage );
378
379 /**
380 * Gets called after @see SpecialPage::execute.
381 *
382 * @since 1.20
383 *
384 * @param SpecialPage $this
385 * @param string|null $subPage
386 */
387 wfRunHooks( 'SpecialPageAfterExecute', array( $this, $subPage ) );
388 }
389
390 /**
391 * Gets called before @see SpecialPage::execute.
392 *
393 * @since 1.20
394 *
395 * @param string|null $subPage
396 */
397 protected function beforeExecute( $subPage ) {
398 // No-op
399 }
400
401 /**
402 * Gets called after @see SpecialPage::execute.
403 *
404 * @since 1.20
405 *
406 * @param string|null $subPage
407 */
408 protected function afterExecute( $subPage ) {
409 // No-op
410 }
411
412 /**
413 * Default execute method
414 * Checks user permissions, calls the function given in mFunction
415 *
416 * This must be overridden by subclasses; it will be made abstract in a future version
417 *
418 * @param string|null $subPage
419 */
420 public function execute( $subPage ) {
421 $this->setHeaders();
422 $this->checkPermissions();
423
424 $func = $this->mFunction;
425 // only load file if the function does not exist
426 if ( !is_callable( $func ) && $this->mFile ) {
427 require_once $this->mFile;
428 }
429 $this->outputHeader();
430 call_user_func( $func, $subPage, $this );
431 }
432
433 /**
434 * Outputs a summary message on top of special pages
435 * Per default the message key is the canonical name of the special page
436 * May be overridden, i.e. by extensions to stick with the naming conventions
437 * for message keys: 'extensionname-xxx'
438 *
439 * @param string $summaryMessageKey Message key of the summary
440 */
441 function outputHeader( $summaryMessageKey = '' ) {
442 global $wgContLang;
443
444 if ( $summaryMessageKey == '' ) {
445 $msg = $wgContLang->lc( $this->getName() ) . '-summary';
446 } else {
447 $msg = $summaryMessageKey;
448 }
449 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
450 $this->getOutput()->wrapWikiMsg(
451 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
452 }
453
454 }
455
456 /**
457 * Returns the name that goes in the \<h1\> in the special page itself, and
458 * also the name that will be listed in Special:Specialpages
459 *
460 * Derived classes can override this, but usually it is easier to keep the
461 * default behavior.
462 *
463 * @return string
464 */
465 function getDescription() {
466 return $this->msg( strtolower( $this->mName ) )->text();
467 }
468
469 /**
470 * Get a self-referential title object
471 *
472 * @param string|bool $subpage
473 * @return Title
474 * @deprecated in 1.23, use SpecialPage::getPageTitle
475 */
476 function getTitle( $subpage = false ) {
477 wfDeprecated( __METHOD__, '1.23' );
478 return $this->getPageTitle( $subpage );
479 }
480
481 /**
482 * Get a self-referential title object
483 *
484 * @param string|bool $subpage
485 * @return Title
486 * @since 1.23
487 */
488 function getPageTitle( $subpage = false ) {
489 return self::getTitleFor( $this->mName, $subpage );
490 }
491
492 /**
493 * Sets the context this SpecialPage is executed in
494 *
495 * @param IContextSource $context
496 * @since 1.18
497 */
498 public function setContext( $context ) {
499 $this->mContext = $context;
500 }
501
502 /**
503 * Gets the context this SpecialPage is executed in
504 *
505 * @return IContextSource|RequestContext
506 * @since 1.18
507 */
508 public function getContext() {
509 if ( $this->mContext instanceof IContextSource ) {
510 return $this->mContext;
511 } else {
512 wfDebug( __METHOD__ . " called and \$mContext is null. " .
513 "Return RequestContext::getMain(); for sanity\n" );
514 return RequestContext::getMain();
515 }
516 }
517
518 /**
519 * Get the WebRequest being used for this instance
520 *
521 * @return WebRequest
522 * @since 1.18
523 */
524 public function getRequest() {
525 return $this->getContext()->getRequest();
526 }
527
528 /**
529 * Get the OutputPage being used for this instance
530 *
531 * @return OutputPage
532 * @since 1.18
533 */
534 public function getOutput() {
535 return $this->getContext()->getOutput();
536 }
537
538 /**
539 * Shortcut to get the User executing this instance
540 *
541 * @return User
542 * @since 1.18
543 */
544 public function getUser() {
545 return $this->getContext()->getUser();
546 }
547
548 /**
549 * Shortcut to get the skin being used for this instance
550 *
551 * @return Skin
552 * @since 1.18
553 */
554 public function getSkin() {
555 return $this->getContext()->getSkin();
556 }
557
558 /**
559 * Shortcut to get user's language
560 *
561 * @deprecated since 1.19 Use getLanguage instead
562 * @return Language
563 * @since 1.18
564 */
565 public function getLang() {
566 wfDeprecated( __METHOD__, '1.19' );
567 return $this->getLanguage();
568 }
569
570 /**
571 * Shortcut to get user's language
572 *
573 * @return Language
574 * @since 1.19
575 */
576 public function getLanguage() {
577 return $this->getContext()->getLanguage();
578 }
579
580 /**
581 * Return the full title, including $par
582 *
583 * @return Title
584 * @since 1.18
585 */
586 public function getFullTitle() {
587 return $this->getContext()->getTitle();
588 }
589
590 /**
591 * Return the robot policy. Derived classes that override this can change
592 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
593 *
594 * @return string
595 * @since 1.23
596 */
597 protected function getRobotPolicy() {
598 return 'noindex,nofollow';
599 }
600
601 /**
602 * Wrapper around wfMessage that sets the current context.
603 *
604 * @return Message
605 * @see wfMessage
606 */
607 public function msg( /* $args */ ) {
608 $message = call_user_func_array(
609 array( $this->getContext(), 'msg' ),
610 func_get_args()
611 );
612 // RequestContext passes context to wfMessage, and the language is set from
613 // the context, but setting the language for Message class removes the
614 // interface message status, which breaks for example usernameless gender
615 // invocations. Restore the flag when not including special page in content.
616 if ( $this->including() ) {
617 $message->setInterfaceMessageFlag( false );
618 }
619 return $message;
620 }
621
622 /**
623 * Adds RSS/atom links
624 *
625 * @param array $params
626 */
627 protected function addFeedLinks( $params ) {
628 global $wgFeedClasses;
629
630 $feedTemplate = wfScript( 'api' );
631
632 foreach ( $wgFeedClasses as $format => $class ) {
633 $theseParams = $params + array( 'feedformat' => $format );
634 $url = wfAppendQuery( $feedTemplate, $theseParams );
635 $this->getOutput()->addFeedLink( $format, $url );
636 }
637 }
638
639 /**
640 * Get the group that the special page belongs in on Special:SpecialPage
641 * Use this method, instead of getGroupName to allow customization
642 * of the group name from the wiki side
643 *
644 * @return string Group of this special page
645 * @since 1.21
646 */
647 public function getFinalGroupName() {
648 global $wgSpecialPageGroups;
649 $name = $this->getName();
650
651 // Allow overbidding the group from the wiki side
652 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
653 if ( !$msg->isBlank() ) {
654 $group = $msg->text();
655 } else {
656 // Than use the group from this object
657 $group = $this->getGroupName();
658
659 // Group '-' is used as default to have the chance to determine,
660 // if the special pages overrides this method,
661 // if not overridden, $wgSpecialPageGroups is checked for b/c
662 if ( $group === '-' && isset( $wgSpecialPageGroups[$name] ) ) {
663 $group = $wgSpecialPageGroups[$name];
664 }
665 }
666
667 // never give '-' back, change to 'other'
668 if ( $group === '-' ) {
669 $group = 'other';
670 }
671
672 return $group;
673 }
674
675 /**
676 * Under which header this special page is listed in Special:SpecialPages
677 * See messages 'specialpages-group-*' for valid names
678 * This method defaults to group 'other'
679 *
680 * @return string
681 * @since 1.21
682 */
683 protected function getGroupName() {
684 // '-' used here to determine, if this group is overridden or has a hardcoded 'other'
685 // Needed for b/c in getFinalGroupName
686 return '-';
687 }
688 }