Comments and code cleanup in OutputPage
[lhc/web/wiklou.git] / includes / OutputPage.php
index 5965cbe..cdf83f2 100644 (file)
@@ -55,9 +55,8 @@ class OutputPage extends ContextSource {
        protected $mCanonicalUrl = false;
 
        /**
-        * @var string Should be private - has getter and setter. Contains
-        *   the HTML title */
-       public $mPagetitle = '';
+        * @var string The contents of <h1> */
+       private $mPageTitle = '';
 
        /**
         * @var string Contains all of the "<body>" content. Should be private we
@@ -72,7 +71,7 @@ class OutputPage extends ContextSource {
         * @var bool Is the displayed content related to the source of the
         *   corresponding wiki article.
         */
-       private $mIsarticle = false;
+       private $mIsArticle = false;
 
        /** @var bool Stores "article flag" toggle. */
        private $mIsArticleRelated = true;
@@ -223,9 +222,6 @@ class OutputPage extends ContextSource {
         */
        public $mNoGallery = false;
 
-       /** @var string */
-       private $mPageTitleActionText = '';
-
        /** @var int Cache stuff. Looks like mEnableClientCache */
        protected $mCdnMaxage = 0;
        /** @var int Upper limit on mCdnMaxage */
@@ -259,6 +255,11 @@ class OutputPage extends ContextSource {
 
        private $mIndexPolicy = 'index';
        private $mFollowPolicy = 'follow';
+
+       /**
+        * @var array Headers that cause the cache to vary.  Key is header name, value is an array of
+        * options for the Key header.
+        */
        private $mVaryHeader = [
                'Accept-Encoding' => [ 'match=gzip' ],
        ];
@@ -404,18 +405,6 @@ class OutputPage extends ContextSource {
                return $this->mLinktags;
        }
 
-       /**
-        * Add a new \<link\> with "rel" attribute set to "meta"
-        *
-        * @param array $linkarr Associative array mapping attribute names to their
-        *                 values, both keys and values will be escaped, and the
-        *                 "rel" attribute will be automatically added
-        */
-       function addMetadataLink( array $linkarr ) {
-               $linkarr['rel'] = $this->getMetadataAttribute();
-               $this->addLink( $linkarr );
-       }
-
        /**
         * Set the URL to be used for the <link rel=canonical>. This should be used
         * in preference to addLink(), to avoid duplicate link tags.
@@ -436,22 +425,6 @@ class OutputPage extends ContextSource {
                return $this->mCanonicalUrl;
        }
 
-       /**
-        * Get the value of the "rel" attribute for metadata links
-        *
-        * @return string
-        */
-       public function getMetadataAttribute() {
-               # note: buggy CC software only reads first "meta" link
-               static $haveMeta = false;
-               if ( $haveMeta ) {
-                       return 'alternate meta';
-               } else {
-                       $haveMeta = true;
-                       return 'meta';
-               }
-       }
-
        /**
         * Add raw HTML to the list of scripts (including \<script\> tag, etc.)
         * Internal use only. Use OutputPage::addModules() or OutputPage::addJsConfigVars()
@@ -809,7 +782,7 @@ class OutputPage extends ContextSource {
                # this breaks strtotime().
                $clientHeader = preg_replace( '/;.*$/', '', $clientHeader );
 
-               Wikimedia\suppressWarnings(); // E_STRICT system time bitching
+               Wikimedia\suppressWarnings(); // E_STRICT system time warnings
                $clientHeaderTime = strtotime( $clientHeader );
                Wikimedia\restoreWarnings();
                if ( !$clientHeaderTime ) {
@@ -923,25 +896,6 @@ class OutputPage extends ContextSource {
                }
        }
 
-       /**
-        * Set the new value of the "action text", this will be added to the
-        * "HTML title", separated from it with " - ".
-        *
-        * @param string $text New value of the "action text"
-        */
-       public function setPageTitleActionText( $text ) {
-               $this->mPageTitleActionText = $text;
-       }
-
-       /**
-        * Get the value of the "action text"
-        *
-        * @return string
-        */
-       public function getPageTitleActionText() {
-               return $this->mPageTitleActionText;
-       }
-
        /**
         * "HTML title" means the contents of "<title>".
         * It is stored as plain, unescaped text and will be run through htmlspecialchars in the skin file.
@@ -992,7 +946,7 @@ class OutputPage extends ContextSource {
                # change "<script>foo&bar</script>" to "&lt;script&gt;foo&amp;bar&lt;/script&gt;"
                # but leave "<i>foobar</i>" alone
                $nameWithTags = Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $name ) );
-               $this->mPagetitle = $nameWithTags;
+               $this->mPageTitle = $nameWithTags;
 
                # change "<i>foo&amp;bar</i>" to "foo&bar"
                $this->setHTMLTitle(
@@ -1007,7 +961,7 @@ class OutputPage extends ContextSource {
         * @return string
         */
        public function getPageTitle() {
-               return $this->mPagetitle;
+               return $this->mPageTitle;
        }
 
        /**
@@ -1215,12 +1169,12 @@ class OutputPage extends ContextSource {
         * corresponding article on the wiki
         * Setting true will cause the change "article related" toggle to true
         *
-        * @param bool $v
+        * @param bool $newVal
         */
-       public function setArticleFlag( $v ) {
-               $this->mIsarticle = $v;
-               if ( $v ) {
-                       $this->mIsArticleRelated = $v;
+       public function setArticleFlag( $newVal ) {
+               $this->mIsArticle = $newVal;
+               if ( $newVal ) {
+                       $this->mIsArticleRelated = $newVal;
                }
        }
 
@@ -1231,19 +1185,19 @@ class OutputPage extends ContextSource {
         * @return bool
         */
        public function isArticle() {
-               return $this->mIsarticle;
+               return $this->mIsArticle;
        }
 
        /**
         * Set whether this page is related an article on the wiki
         * Setting false will cause the change of "article flag" toggle to false
         *
-        * @param bool $v
+        * @param bool $newVal
         */
-       public function setArticleRelated( $v ) {
-               $this->mIsArticleRelated = $v;
-               if ( !$v ) {
-                       $this->mIsarticle = false;
+       public function setArticleRelated( $newVal ) {
+               $this->mIsArticleRelated = $newVal;
+               if ( !$newVal ) {
+                       $this->mIsArticle = false;
                }
        }
 
@@ -1263,7 +1217,7 @@ class OutputPage extends ContextSource {
         *                               (e.g. 'fr:Test page')
         */
        public function addLanguageLinks( array $newLinkArray ) {
-               $this->mLanguageLinks += $newLinkArray;
+               $this->mLanguageLinks = array_merge( $this->mLanguageLinks, $newLinkArray );
        }
 
        /**
@@ -1293,7 +1247,7 @@ class OutputPage extends ContextSource {
        public function addCategoryLinks( array $categories ) {
                global $wgContLang;
 
-               if ( !is_array( $categories ) || count( $categories ) == 0 ) {
+               if ( !$categories ) {
                        return;
                }
 
@@ -1630,12 +1584,12 @@ class OutputPage extends ContextSource {
         * Set the revision ID which will be seen by the wiki text parser
         * for things such as embedded {{REVISIONID}} variable use.
         *
-        * @param int|null $revid An positive integer, or null
+        * @param int|null $revid A positive integer, or null
         * @return mixed Previous value
         */
        public function setRevisionId( $revid ) {
                $val = is_null( $revid ) ? null : intval( $revid );
-               return wfSetVar( $this->mRevisionId, $val );
+               return wfSetVar( $this->mRevisionId, $val, true );
        }
 
        /**
@@ -1655,7 +1609,7 @@ class OutputPage extends ContextSource {
         * @return mixed Previous value
         */
        public function setRevisionTimestamp( $timestamp ) {
-               return wfSetVar( $this->mRevisionTimestamp, $timestamp );
+               return wfSetVar( $this->mRevisionTimestamp, $timestamp, true );
        }
 
        /**
@@ -1671,7 +1625,7 @@ class OutputPage extends ContextSource {
        /**
         * Set the displayed file version
         *
-        * @param File|bool $file
+        * @param File|null $file
         * @return mixed Previous value
         */
        public function setFileVersion( $file ) {
@@ -1732,10 +1686,10 @@ class OutputPage extends ContextSource {
         * Add wikitext with a custom Title object
         *
         * @param string $text Wikitext
-        * @param Title &$title
+        * @param Title $title
         * @param bool $linestart Is this the start of a line?
         */
-       public function addWikiTextWithTitle( $text, &$title, $linestart = true ) {
+       public function addWikiTextWithTitle( $text, Title $title, $linestart = true ) {
                $this->addWikiTextTitle( $text, $title, $linestart );
        }
 
@@ -1743,10 +1697,10 @@ class OutputPage extends ContextSource {
         * Add wikitext with a custom Title object and tidy enabled.
         *
         * @param string $text Wikitext
-        * @param Title &$title
+        * @param Title $title
         * @param bool $linestart Is this the start of a line?
         */
-       function addWikiTextTitleTidy( $text, &$title, $linestart = true ) {
+       function addWikiTextTitleTidy( $text, Title $title, $linestart = true ) {
                $this->addWikiTextTitle( $text, $title, $linestart, true );
        }
 
@@ -1801,7 +1755,8 @@ class OutputPage extends ContextSource {
         * @param ParserOutput $parserOutput
         */
        public function addParserOutputMetadata( $parserOutput ) {
-               $this->mLanguageLinks += $parserOutput->getLanguageLinks();
+               $this->mLanguageLinks =
+                       array_merge( $this->mLanguageLinks, $parserOutput->getLanguageLinks() );
                $this->addCategoryLinks( $parserOutput->getCategories() );
                $this->setIndicators( $parserOutput->getIndicators() );
                $this->mNewSectionLink = $parserOutput->getNewSection();
@@ -1837,7 +1792,7 @@ class OutputPage extends ContextSource {
                foreach ( $parserOutput->getOutputHooks() as $hookInfo ) {
                        list( $hookName, $data ) = $hookInfo;
                        if ( isset( $parserOutputHooks[$hookName] ) ) {
-                               call_user_func( $parserOutputHooks[$hookName], $this, $parserOutput, $data );
+                               $parserOutputHooks[$hookName]( $this, $parserOutput, $data );
                        }
                }
 
@@ -1990,7 +1945,10 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Lower the value of the "s-maxage" part of the "Cache-control" HTTP header
+        * Set the value of the "s-maxage" part of the "Cache-control" HTTP header to $maxage if that is
+        * lower than the current s-maxage.  Either way, $maxage is now an upper limit on s-maxage, so
+        * that future calls to setCdnMaxage() will no longer be able to raise the s-maxage above
+        * $maxage.
         *
         * @param int $maxage Maximum cache time on the CDN, in seconds
         * @since 1.27
@@ -2008,9 +1966,10 @@ class OutputPage extends ContextSource {
         * TTL is 90% of the age of the object, subject to the min and max.
         *
         * @param string|int|float|bool|null $mtime Last-Modified timestamp
-        * @param int $minTTL Mimimum TTL in seconds [default: 1 minute]
+        * @param int $minTTL Minimum TTL in seconds [default: 1 minute]
         * @param int $maxTTL Maximum TTL in seconds [default: $wgSquidMaxage]
-        * @return int TTL in seconds
+        * @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 ) {
@@ -2033,16 +1992,16 @@ class OutputPage extends ContextSource {
        /**
         * Use enableClientCache(false) to force it to send nocache headers
         *
-        * @param bool $state
+        * @param bool|null $state New value, or null to not set the value
         *
-        * @return bool
+        * @return bool Old value
         */
        public function enableClientCache( $state ) {
                return wfSetVar( $this->mEnableClientCache, $state );
        }
 
        /**
-        * Get the list of cookies that will influence on the cache
+        * Get the list of cookie names that will influence the cache
         *
         * @return array
         */
@@ -2095,7 +2054,8 @@ class OutputPage extends ContextSource {
                if ( !is_array( $option ) ) {
                        $option = [];
                }
-               $this->mVaryHeader[$header] = array_unique( array_merge( $this->mVaryHeader[$header], $option ) );
+               $this->mVaryHeader[$header] =
+                       array_unique( array_merge( $this->mVaryHeader[$header], $option ) );
        }
 
        /**
@@ -2170,14 +2130,13 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * T23672: Add Accept-Language to Vary and Key headers
-        * if there's no 'variant' parameter existed in GET.
+        * T23672: Add Accept-Language to Vary and Key headers if there's no 'variant' parameter in GET.
         *
         * For example:
-        *   /w/index.php?title=Main_page should always be served; but
-        *   /w/index.php?title=Main_page&variant=zh-cn should never be served.
+        *   /w/index.php?title=Main_page will vary based on Accept-Language; but
+        *   /w/index.php?title=Main_page&variant=zh-cn will not.
         */
-       function addAcceptLanguage() {
+       private function addAcceptLanguage() {
                $title = $this->getTitle();
                if ( !$title instanceof Title ) {
                        return;
@@ -2190,16 +2149,15 @@ class OutputPage extends ContextSource {
                        foreach ( $variants as $variant ) {
                                if ( $variant === $lang->getCode() ) {
                                        continue;
-                               } else {
-                                       $aloption[] = 'substr=' . $variant;
-
-                                       // IE and some other browsers use BCP 47 standards in
-                                       // their Accept-Language header, like "zh-CN" or "zh-Hant".
-                                       // We should handle these too.
-                                       $variantBCP47 = LanguageCode::bcp47( $variant );
-                                       if ( $variantBCP47 !== $variant ) {
-                                               $aloption[] = 'substr=' . $variantBCP47;
-                                       }
+                               }
+
+                               $aloption[] = "substr=$variant";
+
+                               // IE and some other browsers use BCP 47 standards in their Accept-Language header,
+                               // like "zh-CN" or "zh-Hant".  We should handle these too.
+                               $variantBCP47 = LanguageCode::bcp47( $variant );
+                               if ( $variantBCP47 !== $variant ) {
+                                       $aloption[] = "substr=$variantBCP47";
                                }
                        }
                        $this->addVaryHeader( 'Accept-Language', $aloption );
@@ -2801,7 +2759,7 @@ class OutputPage extends ContextSource {
                                $this->rlClientContext = new DerivativeResourceLoaderContext( $this->rlClientContext );
                                $this->rlClientContext->setContentOverrideCallback( function ( Title $title ) {
                                        foreach ( $this->contentOverrideCallbacks as $callback ) {
-                                               $content = call_user_func( $callback, $title );
+                                               $content = $callback( $title );
                                                if ( $content !== null ) {
                                                        return $content;
                                                }