Merge "output: Narrow Title type hint to LinkTarget"
[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 use MediaWiki\Auth\AuthManager;
25 use MediaWiki\Linker\LinkRenderer;
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Navigation\PrevNextNavigationRenderer;
28
29 /**
30 * Parent class for all special pages.
31 *
32 * Includes some static functions for handling the special page list deprecated
33 * in favor of SpecialPageFactory.
34 *
35 * @ingroup SpecialPage
36 */
37 class SpecialPage implements MessageLocalizer {
38 // The canonical name of this special page
39 // Also used for the default <h1> heading, @see getDescription()
40 protected $mName;
41
42 // The local name of this special page
43 private $mLocalName;
44
45 // Minimum user level required to access this page, or "" for anyone.
46 // Also used to categorise the pages in Special:Specialpages
47 protected $mRestriction;
48
49 // Listed in Special:Specialpages?
50 private $mListed;
51
52 // Whether or not this special page is being included from an article
53 protected $mIncluding;
54
55 // Whether the special page can be included in an article
56 protected $mIncludable;
57
58 /**
59 * Current request context
60 * @var IContextSource
61 */
62 protected $mContext;
63
64 /**
65 * @var \MediaWiki\Linker\LinkRenderer|null
66 */
67 private $linkRenderer;
68
69 /**
70 * Get a localised Title object for a specified special page name
71 * If you don't need a full Title object, consider using TitleValue through
72 * getTitleValueFor() below.
73 *
74 * @since 1.9
75 * @since 1.21 $fragment parameter added
76 *
77 * @param string $name
78 * @param string|bool $subpage Subpage string, or false to not use a subpage
79 * @param string $fragment The link fragment (after the "#")
80 * @return Title
81 * @throws MWException
82 */
83 public static function getTitleFor( $name, $subpage = false, $fragment = '' ) {
84 return Title::newFromTitleValue(
85 self::getTitleValueFor( $name, $subpage, $fragment )
86 );
87 }
88
89 /**
90 * Get a localised TitleValue object for a specified special page name
91 *
92 * @since 1.28
93 * @param string $name
94 * @param string|bool $subpage Subpage string, or false to not use a subpage
95 * @param string $fragment The link fragment (after the "#")
96 * @return TitleValue
97 */
98 public static function getTitleValueFor( $name, $subpage = false, $fragment = '' ) {
99 $name = MediaWikiServices::getInstance()->getSpecialPageFactory()->
100 getLocalNameFor( $name, $subpage );
101
102 return new TitleValue( NS_SPECIAL, $name, $fragment );
103 }
104
105 /**
106 * Get a localised Title object for a page name with a possibly unvalidated subpage
107 *
108 * @param string $name
109 * @param string|bool $subpage Subpage string, or false to not use a subpage
110 * @return Title|null Title object or null if the page doesn't exist
111 */
112 public static function getSafeTitleFor( $name, $subpage = false ) {
113 $name = MediaWikiServices::getInstance()->getSpecialPageFactory()->
114 getLocalNameFor( $name, $subpage );
115 if ( $name ) {
116 return Title::makeTitleSafe( NS_SPECIAL, $name );
117 } else {
118 return null;
119 }
120 }
121
122 /**
123 * Default constructor for special pages
124 * Derivative classes should call this from their constructor
125 * Note that if the user does not have the required level, an error message will
126 * be displayed by the default execute() method, without the global function ever
127 * being called.
128 *
129 * If you override execute(), you can recover the default behavior with userCanExecute()
130 * and displayRestrictionError()
131 *
132 * @param string $name Name of the special page, as seen in links and URLs
133 * @param string $restriction User right required, e.g. "block" or "delete"
134 * @param bool $listed Whether the page is listed in Special:Specialpages
135 * @param callable|bool $function Unused
136 * @param string $file Unused
137 * @param bool $includable Whether the page can be included in normal pages
138 */
139 public function __construct(
140 $name = '', $restriction = '', $listed = true,
141 $function = false, $file = '', $includable = false
142 ) {
143 $this->mName = $name;
144 $this->mRestriction = $restriction;
145 $this->mListed = $listed;
146 $this->mIncludable = $includable;
147 }
148
149 /**
150 * Get the name of this Special Page.
151 * @return string
152 */
153 function getName() {
154 return $this->mName;
155 }
156
157 /**
158 * Get the permission that a user must have to execute this page
159 * @return string
160 */
161 function getRestriction() {
162 return $this->mRestriction;
163 }
164
165 // @todo FIXME: Decide which syntax to use for this, and stick to it
166
167 /**
168 * Whether this special page is listed in Special:SpecialPages
169 * @since 1.3 (r3583)
170 * @return bool
171 */
172 function isListed() {
173 return $this->mListed;
174 }
175
176 /**
177 * Set whether this page is listed in Special:Specialpages, at run-time
178 * @since 1.3
179 * @param bool $listed
180 * @return bool
181 */
182 function setListed( $listed ) {
183 return wfSetVar( $this->mListed, $listed );
184 }
185
186 /**
187 * Get or set whether this special page is listed in Special:SpecialPages
188 * @since 1.6
189 * @param bool|null $x
190 * @return bool
191 */
192 function listed( $x = null ) {
193 return wfSetVar( $this->mListed, $x );
194 }
195
196 /**
197 * Whether it's allowed to transclude the special page via {{Special:Foo/params}}
198 * @return bool
199 */
200 public function isIncludable() {
201 return $this->mIncludable;
202 }
203
204 /**
205 * How long to cache page when it is being included.
206 *
207 * @note If cache time is not 0, then the current user becomes an anon
208 * if you want to do any per-user customizations, than this method
209 * must be overriden to return 0.
210 * @since 1.26
211 * @return int Time in seconds, 0 to disable caching altogether,
212 * false to use the parent page's cache settings
213 */
214 public function maxIncludeCacheTime() {
215 return $this->getConfig()->get( 'MiserMode' ) ? $this->getCacheTTL() : 0;
216 }
217
218 /**
219 * @return int Seconds that this page can be cached
220 */
221 protected function getCacheTTL() {
222 return 60 * 60;
223 }
224
225 /**
226 * Whether the special page is being evaluated via transclusion
227 * @param bool|null $x
228 * @return bool
229 */
230 function including( $x = null ) {
231 return wfSetVar( $this->mIncluding, $x );
232 }
233
234 /**
235 * Get the localised name of the special page
236 * @return string
237 */
238 function getLocalName() {
239 if ( !isset( $this->mLocalName ) ) {
240 $this->mLocalName = MediaWikiServices::getInstance()->getSpecialPageFactory()->
241 getLocalNameFor( $this->mName );
242 }
243
244 return $this->mLocalName;
245 }
246
247 /**
248 * Is this page expensive (for some definition of expensive)?
249 * Expensive pages are disabled or cached in miser mode. Originally used
250 * (and still overridden) by QueryPage and subclasses, moved here so that
251 * Special:SpecialPages can safely call it for all special pages.
252 *
253 * @return bool
254 */
255 public function isExpensive() {
256 return false;
257 }
258
259 /**
260 * Is this page cached?
261 * Expensive pages are cached or disabled in miser mode.
262 * Used by QueryPage and subclasses, moved here so that
263 * Special:SpecialPages can safely call it for all special pages.
264 *
265 * @return bool
266 * @since 1.21
267 */
268 public function isCached() {
269 return false;
270 }
271
272 /**
273 * Can be overridden by subclasses with more complicated permissions
274 * schemes.
275 *
276 * @return bool Should the page be displayed with the restricted-access
277 * pages?
278 */
279 public function isRestricted() {
280 // DWIM: If anons can do something, then it is not restricted
281 return $this->mRestriction != '' && !MediaWikiServices::getInstance()
282 ->getPermissionManager()
283 ->groupHasPermission( '*', $this->mRestriction );
284 }
285
286 /**
287 * Checks if the given user (identified by an object) can execute this
288 * special page (as defined by $mRestriction). Can be overridden by sub-
289 * classes with more complicated permissions schemes.
290 *
291 * @param User $user The user to check
292 * @return bool Does the user have permission to view the page?
293 */
294 public function userCanExecute( User $user ) {
295 return MediaWikiServices::getInstance()
296 ->getPermissionManager()
297 ->userHasRight( $user, $this->mRestriction );
298 }
299
300 /**
301 * Output an error message telling the user what access level they have to have
302 * @throws PermissionsError
303 */
304 function displayRestrictionError() {
305 throw new PermissionsError( $this->mRestriction );
306 }
307
308 /**
309 * Checks if userCanExecute, and if not throws a PermissionsError
310 *
311 * @since 1.19
312 * @return void
313 * @throws PermissionsError
314 */
315 public function checkPermissions() {
316 if ( !$this->userCanExecute( $this->getUser() ) ) {
317 $this->displayRestrictionError();
318 }
319 }
320
321 /**
322 * If the wiki is currently in readonly mode, throws a ReadOnlyError
323 *
324 * @since 1.19
325 * @return void
326 * @throws ReadOnlyError
327 */
328 public function checkReadOnly() {
329 if ( wfReadOnly() ) {
330 throw new ReadOnlyError;
331 }
332 }
333
334 /**
335 * If the user is not logged in, throws UserNotLoggedIn error
336 *
337 * The user will be redirected to Special:Userlogin with the given message as an error on
338 * the form.
339 *
340 * @since 1.23
341 * @param string $reasonMsg [optional] Message key to be displayed on login page
342 * @param string $titleMsg [optional] Passed on to UserNotLoggedIn constructor
343 * @throws UserNotLoggedIn
344 */
345 public function requireLogin(
346 $reasonMsg = 'exception-nologin-text', $titleMsg = 'exception-nologin'
347 ) {
348 if ( $this->getUser()->isAnon() ) {
349 throw new UserNotLoggedIn( $reasonMsg, $titleMsg );
350 }
351 }
352
353 /**
354 * Tells if the special page does something security-sensitive and needs extra defense against
355 * a stolen account (e.g. a reauthentication). What exactly that will mean is decided by the
356 * authentication framework.
357 * @return bool|string False or the argument for AuthManager::securitySensitiveOperationStatus().
358 * Typically a special page needing elevated security would return its name here.
359 */
360 protected function getLoginSecurityLevel() {
361 return false;
362 }
363
364 /**
365 * Record preserved POST data after a reauthentication.
366 *
367 * This is called from checkLoginSecurityLevel() when returning from the
368 * redirect for reauthentication, if the redirect had been served in
369 * response to a POST request.
370 *
371 * The base SpecialPage implementation does nothing. If your subclass uses
372 * getLoginSecurityLevel() or checkLoginSecurityLevel(), it should probably
373 * implement this to do something with the data.
374 *
375 * @since 1.32
376 * @param array $data
377 */
378 protected function setReauthPostData( array $data ) {
379 }
380
381 /**
382 * Verifies that the user meets the security level, possibly reauthenticating them in the process.
383 *
384 * This should be used when the page does something security-sensitive and needs extra defense
385 * against a stolen account (e.g. a reauthentication). The authentication framework will make
386 * an extra effort to make sure the user account is not compromised. What that exactly means
387 * will depend on the system and user settings; e.g. the user might be required to log in again
388 * unless their last login happened recently, or they might be given a second-factor challenge.
389 *
390 * Calling this method will result in one if these actions:
391 * - return true: all good.
392 * - return false and set a redirect: caller should abort; the redirect will take the user
393 * to the login page for reauthentication, and back.
394 * - throw an exception if there is no way for the user to meet the requirements without using
395 * a different access method (e.g. this functionality is only available from a specific IP).
396 *
397 * Note that this does not in any way check that the user is authorized to use this special page
398 * (use checkPermissions() for that).
399 *
400 * @param string|null $level A security level. Can be an arbitrary string, defaults to the page
401 * name.
402 * @return bool False means a redirect to the reauthentication page has been set and processing
403 * of the special page should be aborted.
404 * @throws ErrorPageError If the security level cannot be met, even with reauthentication.
405 */
406 protected function checkLoginSecurityLevel( $level = null ) {
407 $level = $level ?: $this->getName();
408 $key = 'SpecialPage:reauth:' . $this->getName();
409 $request = $this->getRequest();
410
411 $securityStatus = AuthManager::singleton()->securitySensitiveOperationStatus( $level );
412 if ( $securityStatus === AuthManager::SEC_OK ) {
413 $uniqueId = $request->getVal( 'postUniqueId' );
414 if ( $uniqueId ) {
415 $key .= ':' . $uniqueId;
416 $session = $request->getSession();
417 $data = $session->getSecret( $key );
418 if ( $data ) {
419 $session->remove( $key );
420 $this->setReauthPostData( $data );
421 }
422 }
423 return true;
424 } elseif ( $securityStatus === AuthManager::SEC_REAUTH ) {
425 $title = self::getTitleFor( 'Userlogin' );
426 $queryParams = $request->getQueryValues();
427
428 if ( $request->wasPosted() ) {
429 $data = array_diff_assoc( $request->getValues(), $request->getQueryValues() );
430 if ( $data ) {
431 // unique ID in case the same special page is open in multiple browser tabs
432 $uniqueId = MWCryptRand::generateHex( 6 );
433 $key .= ':' . $uniqueId;
434 $queryParams['postUniqueId'] = $uniqueId;
435 $session = $request->getSession();
436 $session->persist(); // Just in case
437 $session->setSecret( $key, $data );
438 }
439 }
440
441 $query = [
442 'returnto' => $this->getFullTitle()->getPrefixedDBkey(),
443 'returntoquery' => wfArrayToCgi( array_diff_key( $queryParams, [ 'title' => true ] ) ),
444 'force' => $level,
445 ];
446 $url = $title->getFullURL( $query, false, PROTO_HTTPS );
447
448 $this->getOutput()->redirect( $url );
449 return false;
450 }
451
452 $titleMessage = wfMessage( 'specialpage-securitylevel-not-allowed-title' );
453 $errorMessage = wfMessage( 'specialpage-securitylevel-not-allowed' );
454 throw new ErrorPageError( $titleMessage, $errorMessage );
455 }
456
457 /**
458 * Return an array of subpages beginning with $search that this special page will accept.
459 *
460 * For example, if a page supports subpages "foo", "bar" and "baz" (as in Special:PageName/foo,
461 * etc.):
462 *
463 * - `prefixSearchSubpages( "ba" )` should return `[ "bar", "baz" ]`
464 * - `prefixSearchSubpages( "f" )` should return `[ "foo" ]`
465 * - `prefixSearchSubpages( "z" )` should return `[]`
466 * - `prefixSearchSubpages( "" )` should return `[ foo", "bar", "baz" ]`
467 *
468 * @param string $search Prefix to search for
469 * @param int $limit Maximum number of results to return (usually 10)
470 * @param int $offset Number of results to skip (usually 0)
471 * @return string[] Matching subpages
472 */
473 public function prefixSearchSubpages( $search, $limit, $offset ) {
474 $subpages = $this->getSubpagesForPrefixSearch();
475 if ( !$subpages ) {
476 return [];
477 }
478
479 return self::prefixSearchArray( $search, $limit, $subpages, $offset );
480 }
481
482 /**
483 * Return an array of subpages that this special page will accept for prefix
484 * searches. If this method requires a query you might instead want to implement
485 * prefixSearchSubpages() directly so you can support $limit and $offset. This
486 * method is better for static-ish lists of things.
487 *
488 * @return string[] subpages to search from
489 */
490 protected function getSubpagesForPrefixSearch() {
491 return [];
492 }
493
494 /**
495 * Perform a regular substring search for prefixSearchSubpages
496 * @param string $search Prefix to search for
497 * @param int $limit Maximum number of results to return (usually 10)
498 * @param int $offset Number of results to skip (usually 0)
499 * @return string[] Matching subpages
500 */
501 protected function prefixSearchString( $search, $limit, $offset ) {
502 $title = Title::newFromText( $search );
503 if ( !$title || !$title->canExist() ) {
504 // No prefix suggestion in special and media namespace
505 return [];
506 }
507
508 $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
509 $searchEngine->setLimitOffset( $limit, $offset );
510 $searchEngine->setNamespaces( [] );
511 $result = $searchEngine->defaultPrefixSearch( $search );
512 return array_map( function ( Title $t ) {
513 return $t->getPrefixedText();
514 }, $result );
515 }
516
517 /**
518 * Helper function for implementations of prefixSearchSubpages() that
519 * filter the values in memory (as opposed to making a query).
520 *
521 * @since 1.24
522 * @param string $search
523 * @param int $limit
524 * @param array $subpages
525 * @param int $offset
526 * @return string[]
527 */
528 protected static function prefixSearchArray( $search, $limit, array $subpages, $offset ) {
529 $escaped = preg_quote( $search, '/' );
530 return array_slice( preg_grep( "/^$escaped/i",
531 array_slice( $subpages, $offset ) ), 0, $limit );
532 }
533
534 /**
535 * Sets headers - this should be called from the execute() method of all derived classes!
536 */
537 function setHeaders() {
538 $out = $this->getOutput();
539 $out->setArticleRelated( false );
540 $out->setRobotPolicy( $this->getRobotPolicy() );
541 $out->setPageTitle( $this->getDescription() );
542 if ( $this->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
543 $out->addModuleStyles( [
544 'mediawiki.ui.input',
545 'mediawiki.ui.radio',
546 'mediawiki.ui.checkbox',
547 ] );
548 }
549 }
550
551 /**
552 * Entry point.
553 *
554 * @since 1.20
555 *
556 * @param string|null $subPage
557 */
558 final public function run( $subPage ) {
559 /**
560 * Gets called before @see SpecialPage::execute.
561 * Return false to prevent calling execute() (since 1.27+).
562 *
563 * @since 1.20
564 *
565 * @param SpecialPage $this
566 * @param string|null $subPage
567 */
568 if ( !Hooks::run( 'SpecialPageBeforeExecute', [ $this, $subPage ] ) ) {
569 return;
570 }
571
572 if ( $this->beforeExecute( $subPage ) === false ) {
573 return;
574 }
575 $this->execute( $subPage );
576 $this->afterExecute( $subPage );
577
578 /**
579 * Gets called after @see SpecialPage::execute.
580 *
581 * @since 1.20
582 *
583 * @param SpecialPage $this
584 * @param string|null $subPage
585 */
586 Hooks::run( 'SpecialPageAfterExecute', [ $this, $subPage ] );
587 }
588
589 /**
590 * Gets called before @see SpecialPage::execute.
591 * Return false to prevent calling execute() (since 1.27+).
592 *
593 * @since 1.20
594 *
595 * @param string|null $subPage
596 * @return bool|void
597 */
598 protected function beforeExecute( $subPage ) {
599 // No-op
600 }
601
602 /**
603 * Gets called after @see SpecialPage::execute.
604 *
605 * @since 1.20
606 *
607 * @param string|null $subPage
608 */
609 protected function afterExecute( $subPage ) {
610 // No-op
611 }
612
613 /**
614 * Default execute method
615 * Checks user permissions
616 *
617 * This must be overridden by subclasses; it will be made abstract in a future version
618 *
619 * @param string|null $subPage
620 */
621 public function execute( $subPage ) {
622 $this->setHeaders();
623 $this->checkPermissions();
624 $securityLevel = $this->getLoginSecurityLevel();
625 if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) {
626 return;
627 }
628 $this->outputHeader();
629 }
630
631 /**
632 * Outputs a summary message on top of special pages
633 * Per default the message key is the canonical name of the special page
634 * May be overridden, i.e. by extensions to stick with the naming conventions
635 * for message keys: 'extensionname-xxx'
636 *
637 * @param string $summaryMessageKey Message key of the summary
638 */
639 function outputHeader( $summaryMessageKey = '' ) {
640 if ( $summaryMessageKey == '' ) {
641 $msg = MediaWikiServices::getInstance()->getContentLanguage()->lc( $this->getName() ) .
642 '-summary';
643 } else {
644 $msg = $summaryMessageKey;
645 }
646 if ( !$this->msg( $msg )->isDisabled() && !$this->including() ) {
647 $this->getOutput()->wrapWikiMsg(
648 "<div class='mw-specialpage-summary'>\n$1\n</div>", $msg );
649 }
650 }
651
652 /**
653 * Returns the name that goes in the \<h1\> in the special page itself, and
654 * also the name that will be listed in Special:Specialpages
655 *
656 * Derived classes can override this, but usually it is easier to keep the
657 * default behavior.
658 *
659 * @return string
660 */
661 function getDescription() {
662 return $this->msg( strtolower( $this->mName ) )->text();
663 }
664
665 /**
666 * Get a self-referential title object
667 *
668 * @param string|bool $subpage
669 * @return Title
670 * @since 1.23
671 */
672 function getPageTitle( $subpage = false ) {
673 return self::getTitleFor( $this->mName, $subpage );
674 }
675
676 /**
677 * Sets the context this SpecialPage is executed in
678 *
679 * @param IContextSource $context
680 * @since 1.18
681 */
682 public function setContext( $context ) {
683 $this->mContext = $context;
684 }
685
686 /**
687 * Gets the context this SpecialPage is executed in
688 *
689 * @return IContextSource|RequestContext
690 * @since 1.18
691 */
692 public function getContext() {
693 if ( $this->mContext instanceof IContextSource ) {
694 return $this->mContext;
695 } else {
696 wfDebug( __METHOD__ . " called and \$mContext is null. " .
697 "Return RequestContext::getMain(); for sanity\n" );
698
699 return RequestContext::getMain();
700 }
701 }
702
703 /**
704 * Get the WebRequest being used for this instance
705 *
706 * @return WebRequest
707 * @since 1.18
708 */
709 public function getRequest() {
710 return $this->getContext()->getRequest();
711 }
712
713 /**
714 * Get the OutputPage being used for this instance
715 *
716 * @return OutputPage
717 * @since 1.18
718 */
719 public function getOutput() {
720 return $this->getContext()->getOutput();
721 }
722
723 /**
724 * Shortcut to get the User executing this instance
725 *
726 * @return User
727 * @since 1.18
728 */
729 public function getUser() {
730 return $this->getContext()->getUser();
731 }
732
733 /**
734 * Shortcut to get the skin being used for this instance
735 *
736 * @return Skin
737 * @since 1.18
738 */
739 public function getSkin() {
740 return $this->getContext()->getSkin();
741 }
742
743 /**
744 * Shortcut to get user's language
745 *
746 * @return Language
747 * @since 1.19
748 */
749 public function getLanguage() {
750 return $this->getContext()->getLanguage();
751 }
752
753 /**
754 * Shortcut to get main config object
755 * @return Config
756 * @since 1.24
757 */
758 public function getConfig() {
759 return $this->getContext()->getConfig();
760 }
761
762 /**
763 * Return the full title, including $par
764 *
765 * @return Title
766 * @since 1.18
767 */
768 public function getFullTitle() {
769 return $this->getContext()->getTitle();
770 }
771
772 /**
773 * Return the robot policy. Derived classes that override this can change
774 * the robot policy set by setHeaders() from the default 'noindex,nofollow'.
775 *
776 * @return string
777 * @since 1.23
778 */
779 protected function getRobotPolicy() {
780 return 'noindex,nofollow';
781 }
782
783 /**
784 * Wrapper around wfMessage that sets the current context.
785 *
786 * @since 1.16
787 * @return Message
788 * @see wfMessage
789 */
790 public function msg( $key /* $args */ ) {
791 $message = $this->getContext()->msg( ...func_get_args() );
792 // RequestContext passes context to wfMessage, and the language is set from
793 // the context, but setting the language for Message class removes the
794 // interface message status, which breaks for example usernameless gender
795 // invocations. Restore the flag when not including special page in content.
796 if ( $this->including() ) {
797 $message->setInterfaceMessageFlag( false );
798 }
799
800 return $message;
801 }
802
803 /**
804 * Adds RSS/atom links
805 *
806 * @param array $params
807 */
808 protected function addFeedLinks( $params ) {
809 $feedTemplate = wfScript( 'api' );
810
811 foreach ( $this->getConfig()->get( 'FeedClasses' ) as $format => $class ) {
812 $theseParams = $params + [ 'feedformat' => $format ];
813 $url = wfAppendQuery( $feedTemplate, $theseParams );
814 $this->getOutput()->addFeedLink( $format, $url );
815 }
816 }
817
818 /**
819 * Adds help link with an icon via page indicators.
820 * Link target can be overridden by a local message containing a wikilink:
821 * the message key is: lowercase special page name + '-helppage'.
822 * @param string $to Target MediaWiki.org page title or encoded URL.
823 * @param bool $overrideBaseUrl Whether $url is a full URL, to avoid MW.o.
824 * @since 1.25
825 */
826 public function addHelpLink( $to, $overrideBaseUrl = false ) {
827 if ( $this->including() ) {
828 return;
829 }
830
831 $msg = $this->msg(
832 MediaWikiServices::getInstance()->getContentLanguage()->lc( $this->getName() ) .
833 '-helppage' );
834
835 if ( !$msg->isDisabled() ) {
836 $helpUrl = Skin::makeUrl( $msg->plain() );
837 $this->getOutput()->addHelpLink( $helpUrl, true );
838 } else {
839 $this->getOutput()->addHelpLink( $to, $overrideBaseUrl );
840 }
841 }
842
843 /**
844 * Get the group that the special page belongs in on Special:SpecialPage
845 * Use this method, instead of getGroupName to allow customization
846 * of the group name from the wiki side
847 *
848 * @return string Group of this special page
849 * @since 1.21
850 */
851 public function getFinalGroupName() {
852 $name = $this->getName();
853
854 // Allow overriding the group from the wiki side
855 $msg = $this->msg( 'specialpages-specialpagegroup-' . strtolower( $name ) )->inContentLanguage();
856 if ( !$msg->isBlank() ) {
857 $group = $msg->text();
858 } else {
859 // Than use the group from this object
860 $group = $this->getGroupName();
861 }
862
863 return $group;
864 }
865
866 /**
867 * Indicates whether this special page may perform database writes
868 *
869 * @return bool
870 * @since 1.27
871 */
872 public function doesWrites() {
873 return false;
874 }
875
876 /**
877 * Under which header this special page is listed in Special:SpecialPages
878 * See messages 'specialpages-group-*' for valid names
879 * This method defaults to group 'other'
880 *
881 * @return string
882 * @since 1.21
883 */
884 protected function getGroupName() {
885 return 'other';
886 }
887
888 /**
889 * Call wfTransactionalTimeLimit() if this request was POSTed
890 * @since 1.26
891 */
892 protected function useTransactionalTimeLimit() {
893 if ( $this->getRequest()->wasPosted() ) {
894 wfTransactionalTimeLimit();
895 }
896 }
897
898 /**
899 * @since 1.28
900 * @return \MediaWiki\Linker\LinkRenderer
901 */
902 public function getLinkRenderer() {
903 if ( $this->linkRenderer ) {
904 return $this->linkRenderer;
905 } else {
906 return MediaWikiServices::getInstance()->getLinkRenderer();
907 }
908 }
909
910 /**
911 * @since 1.28
912 * @param \MediaWiki\Linker\LinkRenderer $linkRenderer
913 */
914 public function setLinkRenderer( LinkRenderer $linkRenderer ) {
915 $this->linkRenderer = $linkRenderer;
916 }
917
918 /**
919 * Generate (prev x| next x) (20|50|100...) type links for paging
920 *
921 * @param int $offset
922 * @param int $limit
923 * @param array $query Optional URL query parameter string
924 * @param bool $atend Optional param for specified if this is the last page
925 * @param string|bool $subpage Optional param for specifying subpage
926 * @return string
927 */
928 protected function buildPrevNextNavigation( $offset, $limit,
929 array $query = [], $atend = false, $subpage = false
930 ) {
931 $title = $this->getPageTitle( $subpage );
932 $prevNext = new PrevNextNavigationRenderer( $this );
933
934 return $prevNext->buildPrevNextNavigation( $title, $offset, $limit, $query, $atend );
935 }
936 }