Fix OutputPage::parseInternal() by stripping <div> wrapper
[lhc/web/wiklou.git] / includes / OutputPage.php
index dd2f5ac..97d9d83 100644 (file)
@@ -323,6 +323,11 @@ class OutputPage extends ContextSource {
         */
        private $CSPNonce;
 
+       /**
+        * @var array A cache of the names of the cookies that will influence the cache
+        */
+       private static $cacheVaryCookies = null;
+
        /**
         * Constructor for OutputPage. This should not be called directly.
         * Instead a new RequestContext should be created and it will implicitly create
@@ -1748,13 +1753,95 @@ class OutputPage extends ContextSource {
         * @param bool $linestart Is this the start of a line?
         * @param bool $interface Is this text in the user interface language?
         * @throws MWException
+        * @deprecated since 1.32 due to untidy output; use
+        *    addWikiTextAsInterface() if $interface is default value or true,
+        *    or else addWikiTextAsContent() if $interface is false.
         */
        public function addWikiText( $text, $linestart = true, $interface = true ) {
-               $title = $this->getTitle(); // Work around E_STRICT
+               wfDeprecated( __METHOD__, '1.32' );
+               $title = $this->getTitle();
                if ( !$title ) {
                        throw new MWException( 'Title is null' );
                }
-               $this->addWikiTextTitle( $text, $title, $linestart, /*tidy*/false, $interface );
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/false, $interface );
+       }
+
+       /**
+        * Convert wikitext *in the user interface language* to HTML and
+        * add it to the buffer. The result will not be
+        * language-converted, as user interface messages are already
+        * localized into a specific variant.  Assumes that the current
+        * page title will be used if optional $title is not
+        * provided. Output will be tidy.
+        *
+        * @param string $text Wikitext in the user interface language
+        * @param bool $linestart Is this the start of a line? (Defaults to true)
+        * @param Title|null $title Optional title to use; default of `null`
+        *   means use current page title.
+        * @throws MWException if $title is not provided and OutputPage::getTitle()
+        *   is null
+        * @since 1.32
+        */
+       public function addWikiTextAsInterface(
+               $text, $linestart = true, Title $title = null
+       ) {
+               if ( $title === null ) {
+                       $title = $this->getTitle();
+               }
+               if ( !$title ) {
+                       throw new MWException( 'Title is null' );
+               }
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/true, /*interface*/true );
+       }
+
+       /**
+        * Convert wikitext *in the user interface language* to HTML and
+        * add it to the buffer with a `<div class="$wrapperClass">`
+        * wrapper.  The result will not be language-converted, as user
+        * interface messages as already localized into a specific
+        * variant.  The $text will be parsed in start-of-line context.
+        * Output will be tidy.
+        *
+        * @param string $wrapperClass The class attribute value for the <div>
+        *   wrapper in the output HTML
+        * @param string $text Wikitext in the user interface language
+        * @since 1.32
+        */
+       public function wrapWikiTextAsInterface(
+               $wrapperClass, $text
+       ) {
+               $this->addWikiTextTitleInternal(
+                       $text, $this->getTitle(),
+                       /*linestart*/true, /*tidy*/true, /*interface*/true,
+                       $wrapperClass
+               );
+       }
+
+       /**
+        * Convert wikitext *in the page content language* to HTML and add
+        * it to the buffer.  The result with be language-converted to the
+        * user's preferred variant.  Assumes that the current page title
+        * will be used if optional $title is not provided. Output will be
+        * tidy.
+        *
+        * @param string $text Wikitext in the page content language
+        * @param bool $linestart Is this the start of a line? (Defaults to true)
+        * @param Title|null $title Optional title to use; default of `null`
+        *   means use current page title.
+        * @throws MWException if $title is not provided and OutputPage::getTitle()
+        *   is null
+        * @since 1.32
+        */
+       public function addWikiTextAsContent(
+               $text, $linestart = true, Title $title = null
+       ) {
+               if ( $title === null ) {
+                       $title = $this->getTitle();
+               }
+               if ( !$title ) {
+                       throw new MWException( 'Title is null' );
+               }
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/true, /*interface*/false );
        }
 
        /**
@@ -1763,31 +1850,44 @@ class OutputPage extends ContextSource {
         * @param string $text Wikitext
         * @param Title $title
         * @param bool $linestart Is this the start of a line?
+        * @deprecated since 1.32 due to untidy output; use
+        *   addWikiTextAsInterface()
         */
        public function addWikiTextWithTitle( $text, Title $title, $linestart = true ) {
-               $this->addWikiTextTitle( $text, $title, $linestart );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/false, /*interface*/false );
        }
 
        /**
-        * Add wikitext with a custom Title object and tidy enabled.
+        * Add wikitext *in content language* with a custom Title object.
+        * Output will be tidy.
         *
-        * @param string $text Wikitext
+        * @param string $text Wikitext in content language
         * @param Title $title
         * @param bool $linestart Is this the start of a line?
+        * @deprecated since 1.32 to rename methods consistently; use
+        *   addWikiTextAsContent()
         */
        function addWikiTextTitleTidy( $text, Title $title, $linestart = true ) {
-               $this->addWikiTextTitle( $text, $title, $linestart, true );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/true, /*interface*/false );
        }
 
        /**
-        * Add wikitext with tidy enabled
+        * Add wikitext *in content language*. Output will be tidy.
         *
-        * @param string $text Wikitext
+        * @param string $text Wikitext in content language
         * @param bool $linestart Is this the start of a line?
+        * @deprecated since 1.32 to rename methods consistently; use
+        *   addWikiTextAsContent()
         */
        public function addWikiTextTidy( $text, $linestart = true ) {
+               wfDeprecated( __METHOD__, '1.32' );
                $title = $this->getTitle();
-               $this->addWikiTextTitleTidy( $text, $title, $linestart );
+               if ( !$title ) {
+                       throw new MWException( 'Title is null' );
+               }
+               $this->addWikiTextTitleInternal( $text, $title, $linestart, /*tidy*/true, /*interface*/false );
        }
 
        /**
@@ -1797,15 +1897,50 @@ class OutputPage extends ContextSource {
         * @param string $text Wikitext
         * @param Title $title
         * @param bool $linestart Is this the start of a line?
-        * @param bool $tidy Whether to use tidy
+        * @param bool $tidy Whether to use tidy.
+        *             Setting this to false (or omitting it) is deprecated
+        *             since 1.32; all wikitext should be tidied.
+        *             For backwards-compatibility with prior MW releases,
+        *             you may wish to invoke this method but set $tidy=true;
+        *             this will result in equivalent output to the non-deprecated
+        *             addWikiTextAsContent()/addWikiTextAsInterface() methods.
         * @param bool $interface Whether it is an interface message
         *   (for example disables conversion)
+        * @deprecated since 1.32, use addWikiTextAsContent() or
+        *   addWikiTextAsInterface() (depending on $interface)
         */
        public function addWikiTextTitle( $text, Title $title, $linestart,
                $tidy = false, $interface = false
+       ) {
+               wfDeprecated( __METHOD__, '1.32' );
+               return $this->addWikiTextTitleInternal( $text, $title, $linestart, $tidy, $interface );
+       }
+
+       /**
+        * Add wikitext with a custom Title object.
+        * Output is unwrapped.
+        *
+        * @param string $text Wikitext
+        * @param Title $title
+        * @param bool $linestart Is this the start of a line?
+        * @param bool $tidy Whether to use tidy.
+        *             Setting this to false (or omitting it) is deprecated
+        *             since 1.32; all wikitext should be tidied.
+        * @param bool $interface Whether it is an interface message
+        *   (for example disables conversion)
+        * @param string $wrapperClass if not empty, wraps the output in
+        *   a `<div class="$wrapperClass">`
+        * @private
+        */
+       private function addWikiTextTitleInternal(
+               $text, Title $title, $linestart, $tidy, $interface, $wrapperClass = null
        ) {
                global $wgParser;
 
+               if ( !$tidy ) {
+                       wfDeprecated( 'disabling tidy', '1.32' );
+               }
+
                $popts = $this->parserOptions();
                $oldTidy = $popts->setTidy( $tidy );
                $popts->setInterfaceMessage( (bool)$interface );
@@ -1819,7 +1954,7 @@ class OutputPage extends ContextSource {
 
                $this->addParserOutput( $parserOutput, [
                        'enableSectionEditLinks' => false,
-                       'wrapperDivClass' => '',
+                       'wrapperDivClass' => $wrapperClass ?? '',
                ] );
        }
 
@@ -1956,6 +2091,9 @@ class OutputPage extends ContextSource {
        /**
         * Parse wikitext and return the HTML.
         *
+        * @todo The output is wrapped in a <div> iff $interface is false; it's
+        * probably best to always strip the wrapper.
+        *
         * @param string $text
         * @param bool $linestart Is this the start of a line?
         * @param bool $interface Use interface language (instead of content language) while parsing
@@ -1966,6 +2104,45 @@ class OutputPage extends ContextSource {
         * @return string HTML
         */
        public function parse( $text, $linestart = true, $interface = false, $language = null ) {
+               return $this->parseInternal(
+                       $text, $linestart, $interface, $language
+               )->getText( [
+                       'enableSectionEditLinks' => false,
+               ] );
+       }
+
+       /**
+        * Parse wikitext, strip paragraph wrapper, and return the HTML.
+        *
+        * @param string $text
+        * @param bool $linestart Is this the start of a line?
+        * @param bool $interface Use interface language (instead of content language) while parsing
+        *   language sensitive magic words like GRAMMAR and PLURAL
+        * @return string HTML
+        */
+       public function parseInline( $text, $linestart = true, $interface = false ) {
+               $parsed = $this->parseInternal(
+                       $text, $linestart, $interface, /*language*/null
+               )->getText( [
+                       'enableSectionEditLinks' => false,
+                       'wrapperDivClass' => '', /* no wrapper div */
+               ] );
+               return Parser::stripOuterParagraph( $parsed );
+       }
+
+       /**
+        * Parse wikitext and return the HTML (internal implementation helper)
+        *
+        * @param string $text
+        * @param bool $linestart Is this the start of a line?
+        * @param bool $interface Use interface language (instead of content language) while parsing
+        *   language sensitive magic words like GRAMMAR and PLURAL.  This also disables
+        *   LanguageConverter.
+        * @param Language|null $language Target language object, will override $interface
+        * @throws MWException
+        * @return ParserOutput
+        */
+       private function parseInternal( $text, $linestart, $interface, $language ) {
                global $wgParser;
 
                if ( is_null( $this->getTitle() ) ) {
@@ -1992,23 +2169,7 @@ class OutputPage extends ContextSource {
                        $popts->setTargetLanguage( $oldLang );
                }
 
-               return $parserOutput->getText( [
-                       'enableSectionEditLinks' => false,
-               ] );
-       }
-
-       /**
-        * Parse wikitext, strip paragraphs, and return the HTML.
-        *
-        * @param string $text
-        * @param bool $linestart Is this the start of a line?
-        * @param bool $interface Use interface language (instead of content language) while parsing
-        *   language sensitive magic words like GRAMMAR and PLURAL
-        * @return string HTML
-        */
-       public function parseInline( $text, $linestart = true, $interface = false ) {
-               $parsed = $this->parse( $text, $linestart, $interface );
-               return Parser::stripOuterParagraph( $parsed );
+               return $parserOutput;
        }
 
        /**
@@ -2044,8 +2205,6 @@ class OutputPage extends ContextSource {
         * @param string|int|float|bool|null $mtime Last-Modified timestamp
         * @param int $minTTL Minimum TTL in seconds [default: 1 minute]
         * @param int $maxTTL Maximum TTL in seconds [default: $wgSquidMaxage]
-        * @return int TTL in seconds passed to lowerCdnMaxage() (may not be the same as the new
-        *  s-maxage)
         * @since 1.28
         */
        public function adaptCdnTTL( $mtime, $minTTL = 0, $maxTTL = 0 ) {
@@ -2056,13 +2215,11 @@ class OutputPage extends ContextSource {
                        return $minTTL; // entity does not exist
                }
 
-               $age = time() - wfTimestamp( TS_UNIX, $mtime );
+               $age = MWTimestamp::time() - wfTimestamp( TS_UNIX, $mtime );
                $adaptiveTTL = max( 0.9 * $age, $minTTL );
                $adaptiveTTL = min( $adaptiveTTL, $maxTTL );
 
                $this->lowerCdnMaxage( (int)$adaptiveTTL );
-
-               return $adaptiveTTL;
        }
 
        /**
@@ -2082,19 +2239,18 @@ class OutputPage extends ContextSource {
         * @return array
         */
        function getCacheVaryCookies() {
-               static $cookies;
-               if ( $cookies === null ) {
+               if ( self::$cacheVaryCookies === null ) {
                        $config = $this->getConfig();
-                       $cookies = array_merge(
+                       self::$cacheVaryCookies = array_values( array_unique( array_merge(
                                SessionManager::singleton()->getVaryCookies(),
                                [
                                        'forceHTTPS',
                                ],
                                $config->get( 'CacheVaryCookies' )
-                       );
-                       Hooks::run( 'GetCacheVaryCookies', [ $this, &$cookies ] );
+                       ) ) );
+                       Hooks::run( 'GetCacheVaryCookies', [ $this, &self::$cacheVaryCookies ] );
                }
-               return $cookies;
+               return self::$cacheVaryCookies;
        }
 
        /**
@@ -2178,8 +2334,12 @@ class OutputPage extends ContextSource {
         * Get a complete Key header
         *
         * @return string
+        * @deprecated in 1.32; the IETF spec for this header expired w/o becoming
+        *   a standard.
         */
        public function getKeyHeader() {
+               wfDeprecated( '$wgUseKeyHeader', '1.32' );
+
                $cvCookies = $this->getCacheVaryCookies();
 
                $cookiesOption = [];
@@ -2227,6 +2387,16 @@ class OutputPage extends ContextSource {
                                        continue;
                                }
 
+                               // XXX Note that this code is not strictly correct: we
+                               // do a case-insensitive match in
+                               // LanguageConverter::getHeaderVariant() while the
+                               // (abandoned, draft) spec for the `Key` header only
+                               // allows case-sensitive matches.  To match the logic
+                               // in LanguageConverter::getHeaderVariant() we should
+                               // also be looking at fallback variants and deprecated
+                               // mediawiki-internal codes, as well as BCP 47
+                               // normalized forms.
+
                                $aloption[] = "substr=$variant";
 
                                // IE and some other browsers use BCP 47 standards in their Accept-Language header,
@@ -2318,6 +2488,7 @@ class OutputPage extends ContextSource {
                                !$this->haveCacheVaryCookies()
                        ) {
                                if ( $config->get( 'UseESI' ) ) {
+                                       wfDeprecated( '$wgUseESI = true', '1.33' );
                                        # We'll purge the proxy cache explicitly, but require end user agents
                                        # to revalidate against the proxy on each visit.
                                        # Surrogate-Control controls our CDN, Cache-Control downstream caches
@@ -2631,7 +2802,7 @@ class OutputPage extends ContextSource {
                        }
                } else {
                        $this->prepareErrorPage( $this->msg( 'permissionserrors' ) );
-                       $this->addWikiText( $this->formatPermissionsErrorMessage( $errors, $action ) );
+                       $this->addWikiTextAsInterface( $this->formatPermissionsErrorMessage( $errors, $action ) );
                }
        }
 
@@ -3902,7 +4073,7 @@ class OutputPage extends ContextSource {
         *
         * Is equivalent to:
         *
-        *    $wgOut->addWikiText( "<div class='error'>\n"
+        *    $wgOut->addWikiTextAsInterface( "<div class='error'>\n"
         *        . wfMessage( 'some-error' )->plain() . "\n</div>" );
         *
         * The newline after the opening div is needed in some wikitext. See T21226.
@@ -3931,7 +4102,7 @@ class OutputPage extends ContextSource {
                        }
                        $s = str_replace( '$' . ( $n + 1 ), $this->msg( $name, $args )->plain(), $s );
                }
-               $this->addWikiText( $s );
+               $this->addWikiTextAsInterface( $s );
        }
 
        /**
@@ -3967,8 +4138,8 @@ class OutputPage extends ContextSource {
         * Helper function to setup the PHP implementation of OOUI to use in this request.
         *
         * @since 1.26
-        * @param String $skinName The Skin name to determine the correct OOUI theme
-        * @param String $dir Language direction
+        * @param string $skinName The Skin name to determine the correct OOUI theme
+        * @param string $dir Language direction
         */
        public static function setupOOUI( $skinName = 'default', $dir = 'ltr' ) {
                $themes = ResourceLoaderOOUIModule::getSkinThemeMap();