From 98fa355b11f46aaf1eddf0ceb301a62cfcecafd3 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 21 May 2008 22:39:08 +0000 Subject: [PATCH] ForeignAPIRepo fixes: * Now supports thumbnails! * SVG seems to work * Comments now working in version info * Can fetch description pages without extra configuration (assuming standard script layout) * Asks server for mime type, but doesn't get it back yet --- includes/filerepo/ForeignAPIFile.php | 30 ++++++++++----- includes/filerepo/ForeignAPIRepo.php | 57 +++++++++++++++++++++++----- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/includes/filerepo/ForeignAPIFile.php b/includes/filerepo/ForeignAPIFile.php index 91522d4fb5..1608442d26 100644 --- a/includes/filerepo/ForeignAPIFile.php +++ b/includes/filerepo/ForeignAPIFile.php @@ -27,15 +27,19 @@ class ForeignAPIFile extends File { public function getPath() { return false; } - - function getThumbPath( $suffix = false ) { - return false; // hrmmm - } - - function getThumbUrl( $suffix = false ) { - return false; // FLKDSJLKFDJS + + function transform( $params, $flags = 0 ) { + $thumbUrl = $this->repo->getThumbUrl( + $this->getName(), + isset( $params['width'] ) ? $params['width'] : -1, + isset( $params['height'] ) ? $params['height'] : -1 ); + if( $thumbUrl ) { + wfDebug( __METHOD__ . " got remote thumb $thumbUrl\n" ); + return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );; + } + return false; } - + // Info we can get from API... public function getWidth( $page = 1 ) { return intval( $this->mInfo['width'] ); @@ -46,7 +50,7 @@ class ForeignAPIFile extends File { } public function getMetadata() { - return $this->mInfo['metadata']; + return serialize( (array)$this->mInfo['metadata'] ); } public function getSize() { @@ -61,7 +65,7 @@ class ForeignAPIFile extends File { return $this->mInfo['user']; } - public function getComment() { + public function getDescription() { return $this->mInfo['comment']; } @@ -73,4 +77,10 @@ class ForeignAPIFile extends File { function getMediaType() { return $this->mInfo['media_type']; } + + function getDescriptionUrl() { + return isset( $this->mInfo['descriptionurl'] ) + ? $this->mInfo['descriptionurl'] + : false; + } } diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index 3468f46fa4..b08c47a851 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -3,7 +3,16 @@ /** * A foreign repository with a remote MediaWiki with an API thingy * Very hacky and inefficient - * do not use :D + * do not use except for testing :D + * + * Example config: + * + * $wgForeignFileRepos[] = array( + * 'class' => 'ForeignAPIRepo', + * 'name' => 'shared', + * 'apibase' => 'http://en.wikipedia.org/w/api.php', + * 'fetchDescription' => true, // Optional + * ); * * @ingroup FileRepo */ @@ -11,6 +20,10 @@ class ForeignAPIRepo extends FileRepo { function __construct( $info ) { parent::__construct( $info ); $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php + if( !$this->scriptDirUrl ) { + // hack for description fetches + $this->scriptDirUrl = dirname( $this->mApiBase ); + } } function storeBatch( $triplets, $flags = 0 ) { @@ -32,25 +45,51 @@ class ForeignAPIRepo extends FileRepo { function newFile( $title, $time = false ) { return false; } - function findFile( $title, $time = false ) { + + protected function queryImage( $query ) { $url = $this->mApiBase . '?' . - wfArrayToCgi( array( - 'format' => 'json', - 'action' => 'query', - 'titles' => $title, // fixme -- canonical namespacea - 'prop' => 'imageinfo', - 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata' ) ); + wfArrayToCgi( + array_merge( $query, + array( + 'format' => 'json', + 'action' => 'query', + 'prop' => 'imageinfo' ) ) ); $json = Http::get( $url ); $data = json_decode( $json, true ); if( isset( $data['query']['pages'] ) ) { foreach( $data['query']['pages'] as $pageid => $info ) { if( isset( $info['imageinfo'][0] ) ) { - return new ForeignAPIFile( $title, $this, $info['imageinfo'][0] ); + return $info['imageinfo'][0]; } } } return false; } + + function findFile( $title, $time = false ) { + $info = $this->queryImage( array( + 'titles' => 'Image:' . $title->getText(), + 'prop' => 'imageinfo', + 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mimetype' ) ); + if( $info ) { + return new ForeignAPIFile( $title, $this, $info ); + } else { + return false; + } + } + + function getThumbUrl( $name, $width=-1, $height=-1 ) { + $info = $this->queryImage( array( + 'titles' => 'Image:' . $name, + 'iiprop' => 'url', + 'iiurlwidth' => $width, + 'iiurlheight' => $height ) ); + if( $info ) { + return $info['thumburl']; + } else { + return false; + } + } } -- 2.20.1