Merge "Hard-deprecate ParserOutput stateful transform methods"
[lhc/web/wiklou.git] / includes / page / ImagePage.php
index 6a751ac..b5ff805 100644 (file)
@@ -115,23 +115,11 @@ class ImagePage extends Article {
 
                if ( $this->getTitle()->getNamespace() == NS_FILE && $this->mPage->getFile()->getRedirected() ) {
                        if ( $this->getTitle()->getDBkey() == $this->mPage->getFile()->getName() || $diff !== null ) {
-                               // mTitle is the same as the redirect target so ask Article
-                               // to perform the redirect for us.
                                $request->setVal( 'diffonly', 'true' );
-                               parent::view();
-                               return;
-                       } else {
-                               // mTitle is not the same as the redirect target so it is
-                               // probably the redirect page itself. Fake the redirect symbol
-                               $out->setPageTitle( $this->getTitle()->getPrefixedText() );
-                               $out->addHTML( $this->viewRedirect(
-                                       Title::makeTitle( NS_FILE, $this->mPage->getFile()->getName() ),
-                                       /* $appendSubtitle */ true,
-                                       /* $forceKnown */ true )
-                               );
-                               $this->mPage->doViewUpdates( $this->getContext()->getUser(), $this->getOldID() );
-                               return;
                        }
+
+                       parent::view();
+                       return;
                }
 
                if ( $wgShowEXIF && $this->displayImg->exists() ) {
@@ -263,18 +251,20 @@ class ImagePage extends Article {
        protected function makeMetadataTable( $metadata ) {
                $r = "<div class=\"mw-imagepage-section-metadata\">";
                $r .= $this->getContext()->msg( 'metadata-help' )->plain();
-               $r .= "<table id=\"mw_metadata\" class=\"mw_metadata\">\n";
+               // Intial state is collapsed
+               // see filepage.css and mediawiki.action.view.metadata module.
+               $r .= "<table id=\"mw_metadata\" class=\"mw_metadata collapsed\">\n";
                foreach ( $metadata as $type => $stuff ) {
                        foreach ( $stuff as $v ) {
-                               # @todo FIXME: Why is this using escapeId for a class?!
-                               $class = Sanitizer::escapeId( $v['id'] );
+                               $class = str_replace( ' ', '_', $v['id'] );
                                if ( $type == 'collapsed' ) {
-                                       // Handled by mediawiki.action.view.metadata module.
-                                       $class .= ' collapsable';
+                                       $class .= ' mw-metadata-collapsible';
                                }
-                               $r .= "<tr class=\"$class\">\n";
-                               $r .= "<th>{$v['name']}</th>\n";
-                               $r .= "<td>{$v['value']}</td>\n</tr>";
+                               $r .= Html::rawElement( 'tr',
+                                       [ 'class' => $class ],
+                                       Html::rawElement( 'th', [], $v['name'] )
+                                               . Html::rawElement( 'td', [], $v['value'] )
+                               );
                        }
                }
                $r .= "</table>\n</div>\n";
@@ -296,6 +286,22 @@ class ImagePage extends Article {
                return parent::getContentObject();
        }
 
+       private function getLanguageForRendering( WebRequest $request, File $file ) {
+               $handler = $this->displayImg->getHandler();
+               if ( !$handler ) {
+                       return null;
+               }
+
+               $requestLanguage = $request->getVal( 'lang' );
+               if ( !is_null( $requestLanguage ) ) {
+                       if ( $handler->validateParam( 'lang', $requestLanguage ) ) {
+                               return $requestLanguage;
+                       }
+               }
+
+               return $handler->getDefaultRenderLanguage( $this->displayImg );
+       }
+
        protected function openShowImage() {
                global $wgEnableUploads, $wgSend404Code, $wgSVGMaxSize;
 
@@ -320,14 +326,9 @@ class ImagePage extends Article {
                                $params = [ 'page' => $page ];
                        }
 
-                       $renderLang = $request->getVal( 'lang' );
+                       $renderLang = $this->getLanguageForRendering( $request, $this->displayImg );
                        if ( !is_null( $renderLang ) ) {
-                               $handler = $this->displayImg->getHandler();
-                               if ( $handler && $handler->validateParam( 'lang', $renderLang ) ) {
-                                       $params['lang'] = $renderLang;
-                               } else {
-                                       $renderLang = null;
-                               }
+                               $params['lang'] = $renderLang;
                        }
 
                        $width_orig = $this->displayImg->getWidth( $page );
@@ -538,13 +539,13 @@ class ImagePage extends Article {
                                // The dirmark, however, must not be immediately adjacent
                                // to the filename, because it can get copied with it.
                                // See T27277.
-                               // @codingStandardsIgnoreStart Ignore long line
+                               // phpcs:disable Generic.Files.LineLength
                                $out->addWikiText( <<<EOT
 <div class="fullMedia"><span class="dangerousLink">{$medialink}</span> $dirmark<span class="fileInfo">$longDesc</span></div>
 <div class="mediaWarning">$warning</div>
 EOT
                                );
-                               // @codingStandardsIgnoreEnd
+                               // phpcs:enable
                        } else {
                                $out->addWikiText( <<<EOT
 <div class="fullMedia">{$medialink} {$dirmark}<span class="fileInfo">$longDesc</span>
@@ -555,12 +556,7 @@ EOT
 
                        $renderLangOptions = $this->displayImg->getAvailableLanguages();
                        if ( count( $renderLangOptions ) >= 1 ) {
-                               $currentLanguage = $renderLang;
-                               $defaultLang = $this->displayImg->getDefaultRenderLanguage();
-                               if ( is_null( $currentLanguage ) ) {
-                                       $currentLanguage = $defaultLang;
-                               }
-                               $out->addHTML( $this->doRenderLangOpt( $renderLangOptions, $currentLanguage, $defaultLang ) );
+                               $out->addHTML( $this->doRenderLangOpt( $renderLangOptions, $renderLang ) );
                        }
 
                        // Add cannot animate thumbnail warning
@@ -594,7 +590,7 @@ EOT
                                # Show deletion log to be consistent with normal articles
                                LogEventsList::showLogExtract(
                                        $out,
-                                       [ 'delete', 'move' ],
+                                       [ 'delete', 'move', 'protect' ],
                                        $this->getTitle()->getPrefixedText(),
                                        '',
                                        [ 'lim' => 10,
@@ -632,11 +628,11 @@ EOT
        /**
         * Make the text under the image to say what size preview
         *
-        * @param $params array parameters for thumbnail
-        * @param $sizeLinkBigImagePreview HTML for the current size
+        * @param array $params parameters for thumbnail
+        * @param string $sizeLinkBigImagePreview HTML for the current size
         * @return string HTML output
         */
-       private function getThumbPrevText( $params, $sizeLinkBigImagePreview ) {
+       protected function getThumbPrevText( $params, $sizeLinkBigImagePreview ) {
                if ( $sizeLinkBigImagePreview ) {
                        // Show a different message of preview is different format from original.
                        $previewTypeDiffers = false;
@@ -674,7 +670,7 @@ EOT
         * @param int $height
         * @return string
         */
-       private function makeSizeLink( $params, $width, $height ) {
+       protected function makeSizeLink( $params, $width, $height ) {
                $params['width'] = $width;
                $params['height'] = $height;
                $thumbnail = $this->displayImg->transform( $params );
@@ -974,7 +970,7 @@ EOT
                                $fromSrc = $this->getContext()->msg(
                                        'shared-repo-from',
                                        $file->getRepo()->getDisplayName()
-                               )->text();
+                               )->escaped();
                        }
                        $out->addHTML( "<li>{$link} {$fromSrc}</li>\n" );
                }
@@ -1058,60 +1054,31 @@ EOT
         * Output a drop-down box for language options for the file
         *
         * @param array $langChoices Array of string language codes
-        * @param string $curLang Language code file is being viewed in.
-        * @param string $defaultLang Language code that image is rendered in by default
+        * @param string $renderLang Language code for the language we want the file to rendered in.
         * @return string HTML to insert underneath image.
         */
-       protected function doRenderLangOpt( array $langChoices, $curLang, $defaultLang ) {
+       protected function doRenderLangOpt( array $langChoices, $renderLang ) {
                global $wgScript;
-               sort( $langChoices );
-               $curLang = wfBCP47( $curLang );
-               $defaultLang = wfBCP47( $defaultLang );
                $opts = '';
-               $haveCurrentLang = false;
-               $haveDefaultLang = false;
-
-               // We make a list of all the language choices in the file.
-               // Additionally if the default language to render this file
-               // is not included as being in this file (for example, in svgs
-               // usually the fallback content is the english content) also
-               // include a choice for that. Last of all, if we're viewing
-               // the file in a language not on the list, add it as a choice.
+
+               $matchedRenderLang = $this->displayImg->getMatchedLanguage( $renderLang );
+
                foreach ( $langChoices as $lang ) {
-                       $code = wfBCP47( $lang );
-                       $name = Language::fetchLanguageName( $code, $this->getContext()->getLanguage()->getCode() );
-                       if ( $name !== '' ) {
-                               $display = $this->getContext()->msg( 'img-lang-opt', $code, $name )->text();
-                       } else {
-                               $display = $code;
-                       }
-                       $opts .= "\n" . Xml::option( $display, $code, $curLang === $code );
-                       if ( $curLang === $code ) {
-                               $haveCurrentLang = true;
-                       }
-                       if ( $defaultLang === $code ) {
-                               $haveDefaultLang = true;
-                       }
-               }
-               if ( !$haveDefaultLang ) {
-                       // Its hard to know if the content is really in the default language, or
-                       // if its just unmarked content that could be in any language.
-                       $opts = Xml::option(
-                                       $this->getContext()->msg( 'img-lang-default' )->text(),
-                               $defaultLang,
-                               $defaultLang === $curLang
-                       ) . $opts;
-               }
-               if ( !$haveCurrentLang && $defaultLang !== $curLang ) {
-                       $name = Language::fetchLanguageName( $curLang, $this->getContext()->getLanguage()->getCode() );
-                       if ( $name !== '' ) {
-                               $display = $this->getContext()->msg( 'img-lang-opt', $curLang, $name )->text();
-                       } else {
-                               $display = $curLang;
-                       }
-                       $opts = Xml::option( $display, $curLang, true ) . $opts;
+                       $opts .= $this->createXmlOptionStringForLanguage(
+                               $lang,
+                               $matchedRenderLang === $lang
+                       );
                }
 
+               // Allow for the default case in an svg <switch> that is displayed if no
+               // systemLanguage attribute matches
+               $opts .= "\n" .
+                       Xml::option(
+                               $this->getContext()->msg( 'img-lang-default' )->text(),
+                               'und',
+                               is_null( $matchedRenderLang )
+                       );
+
                $select = Html::rawElement(
                        'select',
                        [ 'id' => 'mw-imglangselector', 'name' => 'lang' ],
@@ -1130,6 +1097,27 @@ EOT
                return $langSelectLine;
        }
 
+       /**
+        * @param $lang string
+        * @param $selected bool
+        * @return string
+        */
+       private function createXmlOptionStringForLanguage( $lang, $selected ) {
+               $code = LanguageCode::bcp47( $lang );
+               $name = Language::fetchLanguageName( $code, $this->getContext()->getLanguage()->getCode() );
+               if ( $name !== '' ) {
+                       $display = $this->getContext()->msg( 'img-lang-opt', $code, $name )->text();
+               } else {
+                       $display = $code;
+               }
+               return "\n" .
+                       Xml::option(
+                               $display,
+                               $lang,
+                               $selected
+                       );
+       }
+
        /**
         * Get the width and height to display image at.
         *