Bug 22541. Support image redirects on Commons for ForeignAPIRepo
authorDerk-Jan Hartman <hartman@users.mediawiki.org>
Fri, 21 May 2010 15:59:01 +0000 (15:59 +0000)
committerDerk-Jan Hartman <hartman@users.mediawiki.org>
Fri, 21 May 2010 15:59:01 +0000 (15:59 +0000)
getImageInfo() now requires fetchImageQuery() results as input.

RELEASE-NOTES
includes/filerepo/ForeignAPIFile.php
includes/filerepo/ForeignAPIRepo.php

index 2cd206d..30a271c 100644 (file)
@@ -171,6 +171,7 @@ in a negative namespace (which is invalid).
 * (bug 23563) Old skins now support $wgUploadNavigationUrl and take into account upload rights.
 * (bug 1347) Render \phi in math using images, in order to create consistent and correct render results.
 * (bug 16573) Render \epsilon in math using images, in order to create consistent and correct render results.
+* (bug 22541) Support image redirects when using ForeignAPIRepo
 
 === API changes in 1.17 ===
 * (bug 22738) Allow filtering by action type on query=logevent
index c46b1f8..9987537 100644 (file)
@@ -17,9 +17,22 @@ class ForeignAPIFile extends File {
        }
        
        static function newFromTitle( $title, $repo ) {
-               $info = $repo->getImageInfo( $title );
-               if( $info ) {
-                       return new ForeignAPIFile( $title, $repo, $info, true );
+               $data = $repo->fetchImageQuery( array(
+                        'titles' => 'File:' . $title->getText(),
+                        'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
+                        'prop' => 'imageinfo' ) );
+
+               $info = $repo->getImageInfo( $data );
+
+               if( $data && $info) {
+                       if( isset( $data['query']['redirects'][0] ) ) {
+                               $newtitle = Title::newFromText( $data['query']['redirects'][0]['to']);
+                               $img = new ForeignAPIFile( $newtitle, $repo, $info, true );
+                               if( $img ) $img->redirectedFrom( $title->getDBkey() );
+                       } else {
+                               $img = new ForeignAPIFile( $title, $repo, $info, true );
+                       }
+                       return $img;
                } else {
                        return null;
                }
index 264cb92..dbfdab0 100644 (file)
@@ -103,20 +103,7 @@ class ForeignAPIRepo extends FileRepo {
                return false;
        }
 
-       protected function queryImage( $query ) {
-               $data = $this->fetchImageQuery( $query );
-
-               if( isset( $data['query']['pages'] ) ) {
-                       foreach( $data['query']['pages'] as $pageid => $info ) {
-                               if( isset( $info['imageinfo'][0] ) ) {
-                                       return $info['imageinfo'][0];
-                               }
-                       }
-               }
-               return false;
-       }
-
-       protected function fetchImageQuery( $query ) {
+       function fetchImageQuery( $query ) {
                global $wgMemc;
 
                $url = $this->mApiBase .
@@ -125,7 +112,8 @@ class ForeignAPIRepo extends FileRepo {
                                array_merge( $query,
                                        array(
                                                'format' => 'json',
-                                               'action' => 'query' ) ) );
+                                               'action' => 'query',
+                                               'redirects' => 'true' ) ) );
 
                if( !isset( $this->mQueryCache[$url] ) ) {
                        $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
@@ -147,11 +135,15 @@ class ForeignAPIRepo extends FileRepo {
                return FormatJson::decode( $this->mQueryCache[$url], true );
        }
 
-       function getImageInfo( $title, $time = false ) {
-               return $this->queryImage( array(
-                       'titles' => 'Image:' . $title->getText(),
-                       'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
-                       'prop' => 'imageinfo' ) );
+       function getImageInfo( $data ) {
+               if( $data && isset( $data['query']['pages'] ) ) {
+                       foreach( $data['query']['pages'] as $pageid => $info ) {
+                               if( isset( $info['imageinfo'][0] ) ) {
+                                       return $info['imageinfo'][0];
+                               }
+                       }
+               }
+               return false;
        }
 
        function findBySha1( $hash ) {
@@ -169,13 +161,15 @@ class ForeignAPIRepo extends FileRepo {
        }
 
        function getThumbUrl( $name, $width=-1, $height=-1 ) {
-               $info = $this->queryImage( array(
-                       'titles' => 'Image:' . $name,
+               $data = $this->fetchImageQuery( array(
+                       'titles' => 'File:' . $name,
                        'iiprop' => 'url',
                        'iiurlwidth' => $width,
                        'iiurlheight' => $height,
                        'prop' => 'imageinfo' ) );
-               if( $info && $info['thumburl'] ) {
+               $info = $this->getImageInfo( $data );
+
+               if( $data && $info && $info['thumburl'] ) {
                        wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
                        return $info['thumburl'];
                } else {