Improve test coverage for OutputPage
[lhc/web/wiklou.git] / includes / OutputPage.php
index 4ce49fd..a1c5919 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
@@ -404,18 +403,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 +423,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()
@@ -469,7 +440,7 @@ class OutputPage extends ContextSource {
         * Internal use only. Use OutputPage::addModules() if possible.
         *
         * @param string $file URL to file (absolute path, protocol-relative, or full url)
-        * @param string $unused Previously used to change the cache-busting query parameter
+        * @param string|null $unused Previously used to change the cache-busting query parameter
         */
        public function addScriptFile( $file, $unused = null ) {
                if ( substr( $file, 0, 1 ) !== '/' && !preg_match( '#^[a-z]*://#i', $file ) ) {
@@ -809,7 +780,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 ) {
@@ -992,7 +963,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 +978,7 @@ class OutputPage extends ContextSource {
         * @return string
         */
        public function getPageTitle() {
-               return $this->mPagetitle;
+               return $this->mPageTitle;
        }
 
        /**
@@ -1929,7 +1900,7 @@ class OutputPage extends ContextSource {
         * @param bool $interface Use interface language ($wgLang instead of
         *   $wgContLang) while parsing language sensitive magic words like GRAMMAR and PLURAL.
         *   This also disables LanguageConverter.
-        * @param Language $language Target language object, will override $interface
+        * @param Language|null $language Target language object, will override $interface
         * @throws MWException
         * @return string HTML
         */
@@ -2533,7 +2504,7 @@ class OutputPage extends ContextSource {
         * Output a standard permission error page
         *
         * @param array $errors Error message keys or [key, param...] arrays
-        * @param string $action Action that was denied or null if unknown
+        * @param string|null $action Action that was denied or null if unknown
         */
        public function showPermissionsErrorPage( array $errors, $action = null ) {
                foreach ( $errors as $key => $error ) {
@@ -2623,7 +2594,7 @@ class OutputPage extends ContextSource {
         * Format a list of error messages
         *
         * @param array $errors Array of arrays returned by Title::getUserPermissionsErrors
-        * @param string $action Action that was denied or null if unknown
+        * @param string|null $action Action that was denied or null if unknown
         * @return string The wikitext error-messages, formatted into a list.
         */
        public function formatPermissionsErrorMessage( array $errors, $action = null ) {
@@ -2677,30 +2648,56 @@ class OutputPage extends ContextSource {
                }
        }
 
+       /**
+        * Output an error page
+        *
+        * @note FatalError exception class provides an alternative.
+        * @param string $message Error to output. Must be escaped for HTML.
+        */
        public function showFatalError( $message ) {
                $this->prepareErrorPage( $this->msg( 'internalerror' ) );
 
                $this->addHTML( $message );
        }
 
+       /**
+        * @deprecated 1.32 Use OutputPage::showFatalError or throw FatalError instead.
+        */
        public function showUnexpectedValueError( $name, $val ) {
-               $this->showFatalError( $this->msg( 'unexpected', $name, $val )->text() );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->showFatalError( $this->msg( 'unexpected', $name, $val )->escaped() );
        }
 
+       /**
+        * @deprecated 1.32 Use OutputPage::showFatalError or throw FatalError instead.
+        */
        public function showFileCopyError( $old, $new ) {
-               $this->showFatalError( $this->msg( 'filecopyerror', $old, $new )->text() );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->showFatalError( $this->msg( 'filecopyerror', $old, $new )->escaped() );
        }
 
+       /**
+        * @deprecated 1.32 Use OutputPage::showFatalError or throw FatalError instead.
+        */
        public function showFileRenameError( $old, $new ) {
-               $this->showFatalError( $this->msg( 'filerenameerror', $old, $new )->text() );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->showFatalError( $this->msg( 'filerenameerror', $old, $new )->escpaed() );
        }
 
+       /**
+        * @deprecated 1.32 Use OutputPage::showFatalError or throw FatalError instead.
+        */
        public function showFileDeleteError( $name ) {
-               $this->showFatalError( $this->msg( 'filedeleteerror', $name )->text() );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->showFatalError( $this->msg( 'filedeleteerror', $name )->escaped() );
        }
 
+       /**
+        * @deprecated 1.32 Use OutputPage::showFatalError or throw FatalError instead.
+        */
        public function showFileNotFoundError( $name ) {
-               $this->showFatalError( $this->msg( 'filenotfound', $name )->text() );
+               wfDeprecated( __METHOD__, '1.32' );
+               $this->showFatalError( $this->msg( 'filenotfound', $name )->escaped() );
        }
 
        /**
@@ -2708,7 +2705,7 @@ class OutputPage extends ContextSource {
         *
         * @param Title $title Title to link
         * @param array $query Query string parameters
-        * @param string $text Text of the link (input is not escaped)
+        * @param string|null $text Text of the link (input is not escaped)
         * @param array $options Options array to pass to Linker
         */
        public function addReturnTo( $title, array $query = [], $text = null, $options = [] ) {
@@ -2723,9 +2720,9 @@ class OutputPage extends ContextSource {
         * Add a "return to" link pointing to a specified title,
         * or the title indicated in the request, or else the main page
         *
-        * @param mixed $unused
-        * @param Title|string $returnto Title or String to return to
-        * @param string $returntoquery Query string for the return to link
+        * @param mixed|null $unused
+        * @param Title|string|null $returnto Title or String to return to
+        * @param string|null $returntoquery Query string for the return to link
         */
        public function returnToMain( $unused = null, $returnto = null, $returntoquery = null ) {
                if ( $returnto == null ) {
@@ -3055,7 +3052,7 @@ class OutputPage extends ContextSource {
         * Add one or more variables to be set in mw.config in JavaScript
         *
         * @param string|array $keys Key or array of key/value pairs
-        * @param mixed $value [optional] Value of the configuration variable
+        * @param mixed|null $value [optional] Value of the configuration variable
         */
        public function addJsConfigVars( $keys, $value = null ) {
                if ( is_array( $keys ) ) {
@@ -3504,6 +3501,13 @@ class OutputPage extends ContextSource {
                        ] );
                }
 
+               // Allow extensions to add, remove and/or otherwise manipulate these links
+               // If you want only to *add* <head> links, please use the addHeadItem()
+               // (or addHeadItems() for multiple items) method instead.
+               // This hook is provided as a last resort for extensions to modify these
+               // links before the output is sent to client.
+               Hooks::run( 'OutputPageAfterGetHeadLinksArray', [ &$tags, $this ] );
+
                return $tags;
        }
 
@@ -4011,7 +4015,7 @@ class OutputPage extends ContextSource {
         * @since 1.32
         */
        public function getCSPNonce() {
-               if ( !ContentSecurityPolicy::isEnabled( $this->getConfig() ) ) {
+               if ( !ContentSecurityPolicy::isNonceRequired( $this->getConfig() ) ) {
                        return false;
                }
                if ( $this->CSPNonce === null ) {