Got sick of testing local copies of Wikipedia articles with no images handy.
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 20 May 2008 06:30:36 +0000 (06:30 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 20 May 2008 06:30:36 +0000 (06:30 +0000)
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
includes/filerepo/ForeignAPIFile.php [new file with mode: 0644]
includes/filerepo/ForeignAPIRepo.php [new file with mode: 0644]
includes/filerepo/ForeignDBRepo.php

index a2e60e4..56881c1 100644 (file)
@@ -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 (file)
index 0000000..b395396
--- /dev/null
@@ -0,0 +1,73 @@
+<?php
+
+// Very hacky and inefficient
+// do not use :D
+
+class ForeignAPIFile extends File {
+       function __construct( $title, $repo, $info ) {
+               parent::__construct( $title, $repo );
+               
+               // For some reason API doesn't currently provide type info
+               $magic = MimeMagic::singleton();
+               $info['mime'] = $magic->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 (file)
index 0000000..b70f604
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+/**
+ * File repository with no files, for performance testing
+ */
+
+class ForeignAPIRepo extends FileRepo {
+       function __construct( $info ) {
+               parent::__construct( $info );
+               $this->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;
+       }
+}
index 017ded8..1168f2d 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 
 /**
- * A foreign repository with an accessible MediaWiki database
+ * A foreign repository with a remote MediaWiki with an API thingy
+ * Very hacky and inefficient
+ * do not use :D
  */
 
 class ForeignDBRepo extends LocalRepo {