From 1365cd71fb2d5e69e2d9940850d3a9abdabea5ec Mon Sep 17 00:00:00 2001 From: Derk-Jan Hartman Date: Fri, 21 May 2010 15:59:01 +0000 Subject: [PATCH] Bug 22541. Support image redirects on Commons for ForeignAPIRepo getImageInfo() now requires fetchImageQuery() results as input. --- RELEASE-NOTES | 1 + includes/filerepo/ForeignAPIFile.php | 19 ++++++++++--- includes/filerepo/ForeignAPIRepo.php | 40 ++++++++++++---------------- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 2cd206d39d..30a271c6cf 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 diff --git a/includes/filerepo/ForeignAPIFile.php b/includes/filerepo/ForeignAPIFile.php index c46b1f8ff1..9987537725 100644 --- a/includes/filerepo/ForeignAPIFile.php +++ b/includes/filerepo/ForeignAPIFile.php @@ -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; } diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 264cb920f8..dbfdab070e 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -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 { -- 2.20.1