From 2c65904fb0d772b1260cec4deb3b20386015fa29 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 20 May 2008 06:30:36 +0000 Subject: [PATCH] Got sick of testing local copies of Wikipedia articles with no images handy. Threw together a really quick hack FileRepo class to grab images from the remote wiki using the MediaWiki API. Amazingly it sort of works -- be warned however: * no scaling seems to be done yet -- multi-megapixel images in your browser :D * no caching of lookups -- verrrry slow * lookups are done one at a time, not in any kind of batching * probably doesn't properly support lots and lots of things --- includes/AutoLoader.php | 2 + includes/filerepo/ForeignAPIFile.php | 73 ++++++++++++++++++++++++++++ includes/filerepo/ForeignAPIRepo.php | 53 ++++++++++++++++++++ includes/filerepo/ForeignDBRepo.php | 4 +- 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 includes/filerepo/ForeignAPIFile.php create mode 100644 includes/filerepo/ForeignAPIRepo.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index a2e60e49ba..56881c1477 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -302,6 +302,8 @@ function __autoload($className) { 'File' => 'includes/filerepo/File.php', 'FileRepo' => 'includes/filerepo/FileRepo.php', 'FileRepoStatus' => 'includes/filerepo/FileRepoStatus.php', + 'ForeignAPIFile' => 'includes/filerepo/ForeignAPIFile.php', + 'ForeignAPIRepo' => 'includes/filerepo/ForeignAPIRepo.php', 'ForeignDBFile' => 'includes/filerepo/ForeignDBFile.php', 'ForeignDBRepo' => 'includes/filerepo/ForeignDBRepo.php', 'ForeignDBViaLBRepo' => 'includes/filerepo/ForeignDBViaLBRepo.php', diff --git a/includes/filerepo/ForeignAPIFile.php b/includes/filerepo/ForeignAPIFile.php new file mode 100644 index 0000000000..b395396f1e --- /dev/null +++ b/includes/filerepo/ForeignAPIFile.php @@ -0,0 +1,73 @@ +guessTypesForExtension( $this->getExtension() ); + list( $info['major_mime'], $info['minor_mime'] ) = self::splitMime( $info['mime'] ); + $info['media_type'] = $magic->getMediaType( null, $info['mime'] ); + + $this->mInfo = $info; + } + + // Dummy functions... + public function exists() { + return true; + } + + public function getPath() { + return false; + } + + function getThumbPath( $suffix = false ) { + return false; // hrmmm + } + + function getThumbUrl( $suffix = false ) { + return false; // FLKDSJLKFDJS + } + + // Info we can get from API... + public function getWidth( $page = 1 ) { + return intval( $this->mInfo['width'] ); + } + + public function getHeight( $page = 1 ) { + return intval( $this->mInfo['height'] ); + } + + public function getMetadata() { + return $this->mInfo['metadata']; + } + + public function getSize() { + return intval( $this->mInfo['size'] ); + } + + public function getUrl() { + return $this->mInfo['url']; + } + + public function getUser( $method='text' ) { + return $this->mInfo['user']; + } + + public function getComment() { + return $this->mInfo['comment']; + } + + // Info we had to guess... + function getMimeType() { + return $this->mInfo['mime']; + } + + function getMediaType() { + return $this->mInfo['media_type']; + } +} diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php new file mode 100644 index 0000000000..b70f604bf0 --- /dev/null +++ b/includes/filerepo/ForeignAPIRepo.php @@ -0,0 +1,53 @@ +mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php + } + + function storeBatch( $triplets, $flags = 0 ) { + return false; + } + + function storeTemp( $originalName, $srcPath ) { + return false; + } + function publishBatch( $triplets, $flags = 0 ) { + return false; + } + function deleteBatch( $sourceDestPairs ) { + return false; + } + function getFileProps( $virtualUrl ) { + return false; + } + function newFile( $title, $time = false ) { + return false; + } + function findFile( $title, $time = false ) { + $url = $this->mApiBase . + '?' . + wfArrayToCgi( array( + 'format' => 'json', + 'action' => 'query', + 'titles' => $title, // fixme -- canonical namespacea + 'prop' => 'imageinfo', + 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata' ) ); + $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 false; + } +} diff --git a/includes/filerepo/ForeignDBRepo.php b/includes/filerepo/ForeignDBRepo.php index 017ded8cff..1168f2da0d 100644 --- a/includes/filerepo/ForeignDBRepo.php +++ b/includes/filerepo/ForeignDBRepo.php @@ -1,7 +1,9 @@