MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/188272

Change subject: Add new thumbnailing parameters to mobileview
......................................................................

Add new thumbnailing parameters to mobileview

As requested at 
https://trello.com/c/XaPEs6Jl/8-add-width-constrained-article-image-parameter-to-mobileview-api

Change-Id: I27fbd7183a3ffb242d56fa2a800b2fe061893125
---
M i18n/en.json
M i18n/qqq.json
M includes/api/ApiMobileView.php
M tests/phpunit/api/ApiMobileViewTest.php
4 files changed, 158 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/72/188272/1

diff --git a/i18n/en.json b/i18n/en.json
index e166a83..3283889 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -371,7 +371,9 @@
        "apihelp-mobileview-param-onlyrequestedsections": "Return only 
requested sections even with $1prop=sections.",
        "apihelp-mobileview-param-offset": "Pretend all text result is one 
string, and return the substring starting at this point.",
        "apihelp-mobileview-param-maxlen": "Pretend all text result is one 
string, and limit result to this length.",
-       "apihelp-mobileview-param-thumbnail": "Maximum thumbnail dimensions.",
+       "apihelp-mobileview-param-thumbsize": "Maximum thumbnail dimensions.",
+       "apihelp-mobileview-param-thumbwidth": "Maximum thumbnail width.",
+       "apihelp-mobileview-param-thumbheight": "Maximum thumbnail height.",
        "apihelp-mobileview-example-1": "Get information about section 0 of 
[[Doom metal]]",
        "apihelp-mobileview-example-2": "Get information about section 0 and 
sections containing references of [[Candlemass]]",
        "apihelp-mobileview-example-3": "Get information about sections 1 and 
later and sections containing references of [[Candlemass]]",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index ed9b8fe..085913f 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -399,7 +399,9 @@
        "apihelp-mobileview-param-onlyrequestedsections": 
"{{doc-apihelp-param|mobileview|onlyrequestedsections}}",
        "apihelp-mobileview-param-offset": 
"{{doc-apihelp-param|mobileview|offset}}",
        "apihelp-mobileview-param-maxlen": 
"{{doc-apihelp-param|mobileview|maxlen}}",
-       "apihelp-mobileview-param-thumbnail": 
"{{doc-apihelp-param|mobileview|thumbnail}}",
+       "apihelp-mobileview-param-thumbsize": 
"{{doc-apihelp-param|mobileview|thumbsize}}",
+       "apihelp-mobileview-param-thumbwidth": 
"{{doc-apihelp-param|mobileview|thumbwidth}}",
+       "apihelp-mobileview-param-thumbheight": 
"{{doc-apihelp-param|mobileview|thumbheight}}",
        "apihelp-mobileview-example-1": "{{doc-apihelp-example|mobileview}}",
        "apihelp-mobileview-example-2": "{{doc-apihelp-example|mobileview}}",
        "apihelp-mobileview-example-3": "{{doc-apihelp-example|mobileview}}",
diff --git a/includes/api/ApiMobileView.php b/includes/api/ApiMobileView.php
index a9ab69b..b6de413 100644
--- a/includes/api/ApiMobileView.php
+++ b/includes/api/ApiMobileView.php
@@ -148,7 +148,7 @@
                        }
                }
                if ( $this->usePageImages ) {
-                       $this->addPageImage( $data, $prop, $params['thumbsize'] 
);
+                       $this->addPageImage( $data, $params, $prop );
                }
                $result = array();
                $missingSections = array();
@@ -259,12 +259,33 @@
                        $this->dieUsageMsg( array( 'invalidtitle', $name ) );
                }
                if ( $title->inNamespace( NS_FILE ) ) {
-                       $this->file = wfFindFile( $title );
+                       $this->file = $this->findFile( $title );
                }
                if ( !$title->exists() && !$this->file ) {
                        $this->dieUsageMsg( array( 'notanarticle', $name ) );
                }
                return $title;
+       }
+
+       /**
+        * Wrapper that returns a page image for a given title
+        *
+        * @param Title $title
+        * @return bool|File
+        */
+       protected function getPageImage( Title $title ) {
+               return PageImages::getPageImage( $title );
+       }
+
+       /**
+        * Wrapper for wfFindFile
+        *
+        * @param Title|string $title
+        * @param array $options
+        * @return bool|File
+        */
+       protected function findFile( $title, $options = array() ) {
+               return wfFindFile( $title, $options );
        }
 
        /**
@@ -529,7 +550,7 @@
                                $data['text'][] = $chunk;
                        }
                        if ( $this->usePageImages ) {
-                               $image = PageImages::getPageImage( $title );
+                               $image = $this->getPageImage( $title );
                                if ( $image ) {
                                        $data['image'] = 
$image->getTitle()->getText();
                                }
@@ -597,17 +618,23 @@
        /**
         * Adds Image information to Api result.
         * @param array $data whatever getData() returned
+        * @param array $params p[arameters to this API module
         * @param array $prop prop parameter value
-        * @param int $size thumbnail size
         */
-       private function addPageImage( array $data, array $prop, $size ) {
+       private function addPageImage( array $data, array $params, array $prop 
) {
                if ( !isset( $prop['image'] ) && !isset( $prop['thumb'] ) ) {
                        return;
                }
                if ( !isset( $data['image'] ) ) {
                        return;
                }
-               $file = wfFindFile( $data['image'] );
+               if ( isset( $params['thumbsize'] )
+                        && ( isset( $params['thumbwidth'] ) || isset( 
$params['thumbheight'] ) )
+               ) {
+                       $this->dieUsage( "`thumbsize' is mutually exclusive 
with `thumbwidth' and `thumbwidth'", 'toomanysizeparams' );
+               }
+
+               $file = $this->findFile( $data['image'] );
                if ( !$file ) {
                        return;
                }
@@ -624,7 +651,26 @@
                        );
                }
                if ( isset( $prop['thumb'] ) ) {
-                       $thumb = $file->transform( array( 'width' => $size, 
'height' => $size ) );
+                       $resize = array();
+                       if ( isset( $params['thumbsize'] ) ) {
+                               $resize['width'] = $resize['height'] = 
$params['thumbsize'];
+                       }
+                       if ( isset( $params['thumbwidth'] ) ) {
+                               $resize['width'] = $params['thumbwidth'];
+                       }
+                       if ( isset( $params['thumbheight'] ) ) {
+                               $resize['height'] = $params['thumbheight'];
+                       }
+                       if ( isset( $resize['width'] ) && !isset( 
$resize['height'] ) ) {
+                               $resize['height'] = $file->getHeight(); // 
Limit by width
+                       }
+                       if ( !isset( $resize['width'] ) && isset( 
$resize['height'] ) ) {
+                               $resize['width'] = $file->getWidth(); // Limit 
by width
+                       }
+                       if ( !$resize ) {
+                               $resize['width'] = $resize['height'] = 50; // 
Default
+                       }
+                       $thumb = $file->transform( $resize );
                        if ( !$thumb ) {
                                return;
                        }
@@ -732,10 +778,9 @@
                if ( $this->usePageImages ) {
                        $res['prop'][ApiBase::PARAM_TYPE][] = 'image';
                        $res['prop'][ApiBase::PARAM_TYPE][] = 'thumb';
-                       $res['thumbsize'] = array(
+                       $res['thumbsize'] = $res['thumbwidth'] = 
$res['thumbheight'] = array(
                                ApiBase::PARAM_TYPE => 'integer',
                                ApiBase::PARAM_MIN => 0,
-                               ApiBase::PARAM_DFLT => 50,
                        );
                }
                return $res;
@@ -791,7 +836,9 @@
                if ( $this->usePageImages ) {
                        $res['prop'][] = ' image           - information about 
an image associated with this page';
                        $res['prop'][] = ' thumb           - thumbnail of an 
image associated with this page';
-                       $res['thumbsize'] = 'Maximum thumbnail dimensions';
+                       $res['thumbsize'] = 'Maximum thumbnail dimensions 
(either width or height)';
+                       $res['thumbwidth'] = 'Maximum thumbnail width';
+                       $res['thumbheight'] = 'Maximum thumbnail height';
                }
                return $res;
        }
diff --git a/tests/phpunit/api/ApiMobileViewTest.php 
b/tests/phpunit/api/ApiMobileViewTest.php
index 1621aa5..d786273 100644
--- a/tests/phpunit/api/ApiMobileViewTest.php
+++ b/tests/phpunit/api/ApiMobileViewTest.php
@@ -1,6 +1,9 @@
 <?php
 
 class MockApiMobileView extends ApiMobileView {
+       /** @var PHPUnit_Framework_MockObject_MockObject */
+       public $mockFile;
+
        protected function makeTitle( $name ) {
                $t = Title::newFromText( $name );
                $row = new stdClass();
@@ -34,6 +37,14 @@
 
        public function getAllowedParams() {
                return array_merge( parent::getAllowedParams(), array( 'text' 
=> null ) );
+       }
+
+       protected function findFile( $title, $options = array() ) {
+               return $this->mockFile;
+       }
+
+       protected function getPageImage( Title $title ) {
+               return $this->findFile( $title );
        }
 }
 
@@ -107,7 +118,17 @@
                $request = new FauxRequest( $input );
                $context = new RequestContext();
                $context->setRequest( $request );
+               if ( !defined( 'PAGE_IMAGES_INSTALLED' ) ) {
+                       define( 'PAGE_IMAGES_INSTALLED', true );
+               }
                $api = new MockApiMobileView( new ApiMain( $context ), 
'mobileview' );
+
+               $api->mockFile = $this->getMock( 'MockFSFile', array( 
'getWidth', 'getHeight', 'getTitle', 'transform' ), array(), '', false );
+               $api->mockFile->method( 'getWidth' )->will( $this->returnValue( 
640 ) );
+               $api->mockFile->method( 'getHeight' )->will( 
$this->returnValue( 480 ) );
+               $api->mockFile->method( 'getTitle' )->will( $this->returnValue( 
Title::newFromText( 'File:Foo.jpg' ) ) );
+               $api->mockFile->method( 'transform' )->will( 
$this->returnCallback( array( $this, 'mockTransform' ) ) );
+
                $api->execute();
                $result = $api->getResultData();
                $this->assertTrue(
@@ -115,6 +136,15 @@
                        'API output should be encloded in mobileview element'
                );
                $this->assertArrayEquals( $expected, $result['mobileview'], 
false, true );
+       }
+
+       public function mockTransform( array $params ) {
+               $thumb = $this->getMock( 'MediaTransformOutput' );
+               $thumb->method( 'getUrl' )->will( $this->returnValue( 
'http://dummy' ) );
+               $thumb->method( 'getWidth' )->will( $this->returnValue( 
$params['width'] ) );
+               $thumb->method( 'getHeight' )->will( $this->returnValue( 
$params['height'] ) );
+
+               return $thumb;
        }
 
        public function provideView() {
@@ -207,6 +237,71 @@
                                        'pageprops' => array( 'notoc' => '' ),
                                ),
                        ),
+                       // Page image tests. Note that the dimensions are 
values passed to transform(),
+                       // not actual thumbnail dimensions.
+                       array(
+                               array(
+                                       'page' => 'Foo',
+                                       'text' => '',
+                                       'prop' => 'thumb',
+                               ),
+                               array(
+                                       'sections' => array(),
+                                       'thumb' => array(
+                                               'url' => 'http://dummy',
+                                               'width' => 50,
+                                               'height' => 50,
+                                       )
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'page' => 'Foo',
+                                       'text' => '',
+                                       'prop' => 'thumb',
+                                       'thumbsize' => 55,
+                               ),
+                               array(
+                                       'sections' => array(),
+                                       'thumb' => array(
+                                               'url' => 'http://dummy',
+                                               'width' => 55,
+                                               'height' => 55,
+                                       )
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'page' => 'Foo',
+                                       'text' => '',
+                                       'prop' => 'thumb',
+                                       'thumbwidth' => 100,
+                               ),
+                               array(
+                                       'sections' => array(),
+                                       'thumb' => array(
+                                               'url' => 'http://dummy',
+                                               'width' => 100,
+                                               'height' => 480,
+                                       )
+                               ),
+                       ),
+                       array(
+                               array(
+                                       'page' => 'Foo',
+                                       'text' => '',
+                                       'prop' => 'thumb',
+                                       'thumbheight' => 200,
+                               ),
+                               array(
+                                       'sections' => array(),
+                                       'thumb' => array(
+                                               'url' => 'http://dummy',
+                                               'width' => 640,
+                                               'height' => 200,
+                                       )
+                               ),
+                       ),
                );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/188272
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I27fbd7183a3ffb242d56fa2a800b2fe061893125
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to