Display translatable images in wiki language on image pages
authorMax Semenik <maxsem.wiki@gmail.com>
Wed, 9 Jan 2019 20:48:02 +0000 (12:48 -0800)
committerMax Semenik <maxsem.wiki@gmail.com>
Mon, 4 Feb 2019 23:57:37 +0000 (15:57 -0800)
We currently show SVGs in default languages unless overridden with
lang=... URL parameter (and we have UI for setting it). This change
makes it display thumbnails in wiki language, if translation is available.

Bug: T210814
Change-Id: Ieb0b5e9e27f45b71ef119bb3c1d3f2cd4d7100e5

RELEASE-NOTES-1.33
includes/page/ImagePage.php
tests/phpunit/data/media/translated.svg [new file with mode: 0644]
tests/phpunit/includes/page/ImagePageTest.php

index 5c4832a..de58ca7 100644 (file)
@@ -42,6 +42,8 @@ production.
   additional information about the authentication event.
 * TextContent::getText() was introduced as a replacement for
   Content::getNativeData() for text-based content models.
+* (T210814) SVGs are now by default displayed in wiki language on image
+  pages.
 
 === External library changes in 1.33 ===
 
index ae32f84..76b2de0 100644 (file)
@@ -20,6 +20,7 @@
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
 use Wikimedia\Rdbms\ResultWrapper;
 
 /**
@@ -287,20 +288,27 @@ class ImagePage extends Article {
                return parent::getEmptyPageParserOutput( $options );
        }
 
+       /**
+        * Returns language code to be used for dispaying the image, based on request context and
+        * languages available in the file.
+        *
+        * @param WebRequest $request
+        * @param File $file
+        * @return string|null
+        */
        private function getLanguageForRendering( WebRequest $request, File $file ) {
-               $handler = $this->displayImg->getHandler();
+               $handler = $file->getHandler();
                if ( !$handler ) {
                        return null;
                }
 
-               $requestLanguage = $request->getVal( 'lang' );
-               if ( !is_null( $requestLanguage ) ) {
-                       if ( $handler->validateParam( 'lang', $requestLanguage ) ) {
-                               return $requestLanguage;
-                       }
+               $config = MediaWikiServices::getInstance()->getMainConfig();
+               $requestLanguage = $request->getVal( 'lang', $config->get( 'LanguageCode' ) );
+               if ( $handler->validateParam( 'lang', $requestLanguage ) ) {
+                       return $file->getMatchedLanguage( $requestLanguage );
                }
 
-               return $handler->getDefaultRenderLanguage( $this->displayImg );
+               return $handler->getDefaultRenderLanguage( $file );
        }
 
        protected function openShowImage() {
diff --git a/tests/phpunit/data/media/translated.svg b/tests/phpunit/data/media/translated.svg
new file mode 100644 (file)
index 0000000..afd9fb4
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<svg xmlns="http://www.w3.org/2000/svg">
+       <g>
+               <switch>
+                       <text systemLanguage="ru"><tspan>RU</tspan></text>
+                       <text systemLanguage="de"><tspan>DE</tspan></text>
+                       <text>fallback</text>
+               </switch>
+       </g>
+</svg>
index 8e49bf9..7e43ce4 100644 (file)
@@ -1,7 +1,10 @@
 <?php
+
+use Wikimedia\TestingAccessWrapper;
+
 class ImagePageTest extends MediaWikiMediaTestCase {
 
-       function setUp() {
+       public function setUp() {
                $this->setMwGlobals( 'wgImageLimits', [
                        [ 320, 240 ],
                        [ 640, 480 ],
@@ -12,7 +15,7 @@ class ImagePageTest extends MediaWikiMediaTestCase {
                parent::setUp();
        }
 
-       function getImagePage( $filename ) {
+       public function getImagePage( $filename ) {
                $title = Title::makeTitleSafe( NS_FILE, $filename );
                $file = $this->dataFile( $filename );
                $iPage = new ImagePage( $title );
@@ -26,7 +29,7 @@ class ImagePageTest extends MediaWikiMediaTestCase {
         * @param array $dim Array [maxWidth, maxHeight, width, height]
         * @param array $expected Array [width, height] The width and height we expect to display at
         */
-       function testGetDisplayWidthHeight( $dim, $expected ) {
+       public function testGetDisplayWidthHeight( $dim, $expected ) {
                $iPage = $this->getImagePage( 'animated.gif' );
                $reflection = new ReflectionClass( $iPage );
                $reflMethod = $reflection->getMethod( 'getDisplayWidthHeight' );
@@ -36,7 +39,7 @@ class ImagePageTest extends MediaWikiMediaTestCase {
                $this->assertEquals( $actual, $expected );
        }
 
-       function providerGetDisplayWidthHeight() {
+       public function providerGetDisplayWidthHeight() {
                return [
                        [
                                [ 1024.0, 768.0, 600.0, 600.0 ],
@@ -71,7 +74,7 @@ class ImagePageTest extends MediaWikiMediaTestCase {
         * @param string $filename
         * @param int $expectedNumberThumbs How many thumbnails to show
         */
-       function testGetThumbSizes( $filename, $expectedNumberThumbs ) {
+       public function testGetThumbSizes( $filename, $expectedNumberThumbs ) {
                $iPage = $this->getImagePage( $filename );
                $reflection = new ReflectionClass( $iPage );
                $reflMethod = $reflection->getMethod( 'getThumbSizes' );
@@ -81,7 +84,7 @@ class ImagePageTest extends MediaWikiMediaTestCase {
                $this->assertEquals( count( $actual ), $expectedNumberThumbs );
        }
 
-       function providerGetThumbSizes() {
+       public function providerGetThumbSizes() {
                return [
                        [ 'animated.gif', 2 ],
                        [ 'Toll_Texas_1.svg', 1 ],
@@ -89,4 +92,41 @@ class ImagePageTest extends MediaWikiMediaTestCase {
                        [ 'jpeg-comment-binary.jpg', 2 ],
                ];
        }
+
+       /**
+        * @covers ImagePage::getLanguageForRendering()
+        * @dataProvider provideGetLanguageForRendering
+        *
+        * @param string|null $expected Expected language code
+        * @param string $wikiLangCode Wiki language code
+        * @param string|null $lang lang=... URL parameter
+        */
+       public function testGetLanguageForRendering( $expected = null, $wikiLangCode, $lang = null ) {
+               $params = [];
+               if ( $lang !== null ) {
+                       $params['lang'] = $lang;
+               }
+               $request = new FauxRequest( $params );
+               $this->setMwGlobals( 'wgLanguageCode', $wikiLangCode );
+
+               $page = $this->getImagePage( 'translated.svg' );
+               $page = TestingAccessWrapper::newFromObject( $page );
+
+               /** @var ImagePage $page */
+               $result = $page->getLanguageForRendering( $request, $page->getDisplayedFile() );
+               $this->assertEquals( $expected, $result );
+       }
+
+       public function provideGetLanguageForRendering() {
+               return [
+                       [ 'ru', 'ru' ],
+                       [ 'ru', 'ru', 'ru' ],
+                       [ null, 'en' ],
+                       [ null, 'fr' ],
+                       [ null, 'en', 'en' ],
+                       [ null, 'fr', 'fr' ],
+                       [ null, 'ru', 'en' ],
+                       [ 'de', 'ru', 'de' ],
+               ];
+       }
 }