Make transcluded special pages not disable cache in miser mode.
[lhc/web/wiklou.git] / includes / specialpage / SpecialPage.php
index fb153fc..6624414 100644 (file)
@@ -1,4 +1,6 @@
 <?php
+use MediaWiki\MediaWikiServices;
+
 /**
  * Parent class for all special pages.
  *
@@ -21,6 +23,8 @@
  * @ingroup SpecialPage
  */
 
+use MediaWiki\Auth\AuthManager;
+
 /**
  * Parent class for all special pages.
  *
@@ -59,6 +63,9 @@ class SpecialPage {
        /**
         * Get a localised Title object for a specified special page name
         *
+        * @since 1.9
+        * @since 1.21 $fragment parameter added
+        *
         * @param string $name
         * @param string|bool $subpage Subpage string, or false to not use a subpage
         * @param string $fragment The link fragment (after the "#")
@@ -168,6 +175,25 @@ class SpecialPage {
                return $this->mIncludable;
        }
 
+       /**
+        * How long to cache page when it is being included.
+        *
+        * @note If cache time is not 0, then the current user becomes an anon
+        *   if you want to do any per-user customizations, than this method
+        *   must be overriden to return 0.
+        * @since 1.26
+        * @return int Time in seconds, 0 to disable caching altogether,
+        *  false to use the parent page's cache settings
+        */
+       public function maxIncludeCacheTime() {
+               global $wgMiserMode;
+               if ( !$wgMiserMode ) {
+                       return 0;
+               } else {
+                       return 60*60;
+               }
+       }
+
        /**
         * Whether the special page is being evaluated via transclusion
         * @param bool $x
@@ -291,6 +317,66 @@ class SpecialPage {
                }
        }
 
+       /**
+        * Tells if the special page does something security-sensitive and needs extra defense against
+        * a stolen account (e.g. a reauthentication). What exactly that will mean is decided by the
+        * authentication framework.
+        * @return bool|string False or the argument for AuthManager::securitySensitiveOperationStatus().
+        *   Typically a special page needing elevated security would return its name here.
+        */
+       protected function getLoginSecurityLevel() {
+               return false;
+       }
+
+       /**
+        * Verifies that the user meets the security level, possibly reauthenticating them in the process.
+        *
+        * This should be used when the page does something security-sensitive and needs extra defense
+        * against a stolen account (e.g. a reauthentication). The authentication framework will make
+        * an extra effort to make sure the user account is not compromised. What that exactly means
+        * will depend on the system and user settings; e.g. the user might be required to log in again
+        * unless their last login happened recently, or they might be given a second-factor challenge.
+        *
+        * Calling this method will result in one if these actions:
+        * - return true: all good.
+        * - return false and set a redirect: caller should abort; the redirect will take the user
+        *   to the login page for reauthentication, and back.
+        * - throw an exception if there is no way for the user to meet the requirements without using
+        *   a different access method (e.g. this functionality is only available from a specific IP).
+        *
+        * Note that this does not in any way check that the user is authorized to use this special page
+        * (use checkPermissions() for that).
+        *
+        * @param string $level A security level. Can be an arbitrary string, defaults to the page name.
+        * @return bool False means a redirect to the reauthentication page has been set and processing
+        *   of the special page should be aborted.
+        * @throws ErrorPageError If the security level cannot be met, even with reauthentication.
+        */
+       protected function checkLoginSecurityLevel( $level = null ) {
+               $level = $level ?: $this->getName();
+               $securityStatus = AuthManager::singleton()->securitySensitiveOperationStatus( $level );
+               if ( $securityStatus === AuthManager::SEC_OK ) {
+                       return true;
+               } elseif ( $securityStatus === AuthManager::SEC_REAUTH ) {
+                       $request = $this->getRequest();
+                       $title = SpecialPage::getTitleFor( 'Userlogin' );
+                       $query = [
+                               'returnto' => $this->getFullTitle()->getPrefixedDBkey(),
+                               'returntoquery' => wfArrayToCgi( array_diff_key( $request->getQueryValues(),
+                                       [ 'title' => true ] ) ),
+                               'force' => $level,
+                       ];
+                       $url = $title->getFullURL( $query, false, PROTO_HTTPS );
+
+                       $this->getOutput()->redirect( $url );
+                       return false;
+               }
+
+               $titleMessage = wfMessage( 'specialpage-securitylevel-not-allowed-title' );
+               $errorMessage = wfMessage( 'specialpage-securitylevel-not-allowed' );
+               throw new ErrorPageError( $titleMessage, $errorMessage );
+       }
+
        /**
         * Return an array of subpages beginning with $search that this special page will accept.
         *
@@ -342,7 +428,7 @@ class SpecialPage {
                        return [];
                }
 
-               $searchEngine = SearchEngine::create();
+               $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
                $searchEngine->setLimitOffset( $limit, $offset );
                $searchEngine->setNamespaces( [] );
                $result = $searchEngine->defaultPrefixSearch( $search );
@@ -458,6 +544,7 @@ class SpecialPage {
        public function execute( $subPage ) {
                $this->setHeaders();
                $this->checkPermissions();
+               $this->checkLoginSecurityLevel( $this->getLoginSecurityLevel() );
                $this->outputHeader();
        }
 
@@ -628,6 +715,7 @@ class SpecialPage {
        /**
         * Wrapper around wfMessage that sets the current context.
         *
+        * @since 1.16
         * @return Message
         * @see wfMessage
         */