jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/325044 )

Change subject: API: Surface dimensions when requesting original image info
......................................................................


API: Surface dimensions when requesting original image info

It's useful for API consumers to have the dimensions of the original
image so that they know the bounds within which they can safely rewrite
the thumb URL (bearing in mind the prerendered widths[1], in the case of
WMF wiki consumers).

This change adds an 'original' property to the page object, containing
the original image source URL along with its width and height, when
original image info is requested.

A warning is added when original image info is requested, noting the
format change and warning the consumer that the original image URL will
no longer be provided within the 'thumbnail' property in a future release.

[1] 
https://github.com/wikimedia/operations-mediawiki-config/blob/master/wmf-config/InitialiseSettings.php#L17535

Bug: T152163
Change-Id: I9d937f73a974dfb099b93552405531464b8ad3ae
---
M i18n/en.json
M i18n/qqq.json
M includes/ApiQueryPageImages.php
M tests/phpunit/ApiQueryPageImagesTest.php
4 files changed, 69 insertions(+), 10 deletions(-)

Approvals:
  jenkins-bot: Verified
  Jdlrobson: Looks good to me, approved



diff --git a/i18n/en.json b/i18n/en.json
index a0b48e8..2e55fe5 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -7,10 +7,11 @@
        "pageimages-info-label": "Page image",
        "apihelp-query+pageimages-description": "Returns information about 
images on the page, such as thumbnail and presence of photos.",
        "apihelp-query+pageimages-example-1": "Get name and 
<kbd>100</kbd>-pixel thumbnail of an image on the <kbd>Albert Einstein</kbd> 
page.",
-       "apihelp-query+pageimages-param-prop": "Which information to 
return:\n;thumbnail:URL and dimensions of image associated with page, if 
any.\n;name:Image title.",
+       "apihelp-query+pageimages-param-prop": "Which information to 
return:\n;thumbnail:URL and dimensions of thumbnail image associated with page, 
if any.\n;original:URL and original dimensions of image associated with page, 
if any.\n;name:Image title.",
        "apihelp-query+pageimages-param-thumbsize": "Maximum thumbnail 
dimension.",
        "apihelp-query+pageimages-param-limit": "Properties of how many pages 
to return.",
        "apihelp-query+pageimages-param-license": "Limit page images to a 
certain license type",
        "apihelp-query+pageimages-paramvalue-license-free": "only free images",
-       "apihelp-query+pageimages-paramvalue-license-any": "best image, whether 
free or non-free."
+       "apihelp-query+pageimages-paramvalue-license-any": "best image, whether 
free or non-free.",
+       "apiwarn-query+pageimages-original-in-thumbnail-deprecated": "The 
original image's source URL is now provided with its dimensions in its own 
property for the page object. It will be removed from the 'thumbnail' property 
in a future release."
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 4aa4243..433f026 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -15,5 +15,6 @@
        "apihelp-query+pageimages-param-limit": 
"{{doc-apihelp-param|query+pageimages|limit}}",
        "apihelp-query+pageimages-param-license": 
"{{doc-apihelp-param|query+pageimages|license}}",
        "apihelp-query+pageimages-paramvalue-license-free": 
"{{doc-apihelp-paramvalue|query+pageimages|license|free}}",
-       "apihelp-query+pageimages-paramvalue-license-any": 
"{{doc-apihelp-paramvalue|query+pageimages|license|any}}"
+       "apihelp-query+pageimages-paramvalue-license-any": 
"{{doc-apihelp-paramvalue|query+pageimages|license|any}}",
+       "apiwarn-query+pageimages-original-in-thumbnail-deprecated": "A message 
explaining that the API response format has changed and the 'original' property 
will be removed from the 'thumbnail' object in a future release."
 }
diff --git a/includes/ApiQueryPageImages.php b/includes/ApiQueryPageImages.php
index 4939278..13486b4 100644
--- a/includes/ApiQueryPageImages.php
+++ b/includes/ApiQueryPageImages.php
@@ -187,6 +187,8 @@
                if ( isset( $prop['thumbnail'] ) || isset( $prop['original'] ) 
) {
                        $file = wfFindFile( $fileName );
                        if ( $file ) {
+                               $vals['thumbnail'] = [];
+
                                if ( isset( $prop['thumbnail'] ) ) {
                                        $thumb = $file->transform( [ 'width' => 
$size, 'height' => $size ] );
                                        if ( $thumb && $thumb->getUrl() ) {
@@ -204,13 +206,25 @@
 
                                if ( isset( $prop['original'] ) ) {
                                        $original_url = wfExpandUrl( 
$file->getUrl(), PROTO_CURRENT );
-                                       if ( isset( $vals['thumbnail'] ) ) {
-                                               $vals['thumbnail']['original'] 
= $original_url;
+
+                                       $vals['original'] = [
+                                               'source' => $original_url,
+                                               'width' => $file->getWidth(),
+                                               'height' => $file->getHeight()
+                                       ];
+
+                                       // DEPRECATED: The original image URL 
was originally provided as a property of
+                                       // the "thumbnail" object, but we now 
provide the original image info in its own
+                                       // object on the same level as the 
thumbnail.
+                                       //
+                                       // Remove this functionality, and the 
warning, after a reasonable amount of time
+                                       // has passed since December 2016.
+                                       if ( is_callable( [ $this, 'addWarning' 
] ) ) {
+                                               $this->addWarning( 
'apiwarn-query+pageimages-original-in-thumbnail-deprecated' );
                                        } else {
-                                               $vals['thumbnail'] = [
-                                                       'original' => 
$original_url,
-                                               ];
+                                               $this->setWarning( "The 
original image's source URL is now provided with its dimensions in its own 
property for the page object. It will be removed from the 'thumbnail' property 
in a future release." );
                                        }
+                                       $vals['thumbnail']['original'] = 
$original_url;
                                }
                        }
                }
@@ -273,7 +287,7 @@
                        'prop' => [ 'What information to return',
                                ' thumbnail - URL and dimensions of image 
associated with page, if any',
                                ' name - image title',
-                               ' original - URL to the image original',
+                               ' original - URL and dimensions of the original 
image',
                        ],
                        'thumbsize' => 'Maximum thumbnail dimension',
                        'limit' => 'Properties of how many pages to return',
diff --git a/tests/phpunit/ApiQueryPageImagesTest.php 
b/tests/phpunit/ApiQueryPageImagesTest.php
index de22c8a..a29f484 100644
--- a/tests/phpunit/ApiQueryPageImagesTest.php
+++ b/tests/phpunit/ApiQueryPageImagesTest.php
@@ -169,7 +169,7 @@
                $mock = TestingAccessWrapper::newFromObject(
                        $this->getMockBuilder( ApiQueryPageImages::class )
                                ->disableOriginalConstructor()
-                               -> setMethods( [ 'extractRequestParams', 
'getTitles', 'setContinueParameter', 'dieUsage',
+                               ->setMethods( [ 'extractRequestParams', 
'getTitles', 'setContinueParameter', 'dieUsage',
                                        'addTables', 'addFields', 'addWhere', 
'select', 'setResultValues' ] )
                                ->getMock()
                );
@@ -188,6 +188,9 @@
                                $mock->expects( $this->exactly( 1 ) )
                                ->method( 'dieUsage' );
                }
+
+               $originalRequested = in_array( 'original', 
$requestParams['prop'] );
+               $this->assertTrue( $this->hasExpectedProperties( $queryResults, 
$originalRequested ) );
 
                $license = isset( $requestParams['license'] ) ? 
$requestParams['license'] : 'free';
                if ( $license == ApiQueryPageImages::PARAM_LICENSE_ANY ) {
@@ -270,6 +273,16 @@
                                ],
                                2
                        ],
+                       [
+                               ['prop' => ['thumbnail', 'original'], 
'thumbsize' => 510, 'limit' => 10, 'license' => 'free'],
+                               [Title::newFromText( 'Page 1' ), 
Title::newFromText( 'Page 2' )],
+                               [0, 1],
+                               [
+                                       (object) ['pp_page' => 0, 'pp_value' => 
'A_Free.jpg', 'pp_value_original' => 'A_Free_original.jpg', 'pp_original_width' 
=> 80, 'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE],
+                                       (object) ['pp_page' => 1, 'pp_value' => 
'B_Free.jpg', 'pp_value_original' => 'B_Free_original.jpg', 'pp_original_width' 
=> 80, 'pp_original_height' => 80, 'pp_propname' => PageImages::PROP_NAME_FREE],
+                               ],
+                               2
+                       ],
                ];
        }
 
@@ -289,4 +302,34 @@
                        [ 'any', [ \PageImages::PROP_NAME_FREE, 
\PageImages::PROP_NAME ] ]
                ];
        }
+
+       private function hasExpectedProperties( $queryResults, 
$originalRequested ) {
+               if ( $originalRequested ) {
+                       return $this->allResultsHaveProperty( $queryResults, 
'pp_value_original' )
+                               && $this->allResultsHaveProperty( 
$queryResults, 'pp_original_width' )
+                               && $this->allResultsHaveProperty( 
$queryResults, 'pp_original_height' );
+               } else {
+                       return $this->noResultsHaveProperty( $queryResults, 
'pp_value_original' )
+                               && $this->noResultsHaveProperty( $queryResults, 
'pp_original_width' )
+                               && $this->noResultsHaveProperty( $queryResults, 
'pp_original_height' );
+               }
+       }
+
+       private function noResultsHaveProperty( $queryResults, $propName ) {
+               foreach( $queryResults as $result ) {
+                       if ( property_exists( $result, $propName ) ) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       private function allResultsHaveProperty( $queryResults, $propName ) {
+               foreach( $queryResults as $result ) {
+                       if ( !property_exists( $result, $propName ) ) {
+                               return false;
+                       }
+               }
+               return true;
+       }
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9d937f73a974dfb099b93552405531464b8ad3ae
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/PageImages
Gerrit-Branch: master
Gerrit-Owner: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Bmansurov <bmansu...@wikimedia.org>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Pmiazga <pmia...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to