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