brion@svn.wikimedia.org schreef:
Revision: 35075 Author: brion Date: 2008-05-20 06:30:36 +0000 (Tue, 20 May 2008)
Log Message:
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
<snip>
// 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;
You're right. I never noticed, but list=allimages has aiprop=mime whereas prop=imageinfo doesn't. I'll fix that discrepancy (and any other discrepancies between aiprop and iiprop that are likely to exist) right away.
- function getThumbPath( $suffix = false ) {
return false; // hrmmm
- }
- function getThumbUrl( $suffix = false ) {
return false; // FLKDSJLKFDJS
- }
Did you know you can get these through the API as well with the iiurlwidth and iiurlheight parameters? The foreign wiki will do the scaling for you, and return the URL of a thumbnail and its dimensions.
- 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 );
Why do you use JSON here? Wouldn't it be better/faster/more intuitive/whatever to use format=php and unserialize()? Surely PHP can interpret its own format faster than JSON? Also, I though Http::get() was a faux pas, why use it here? Or is there no other option? As to the FIXME, it's kind of cryptical so I don't know exactly what you mean there, but the client API will normalize titles. Or are you worried about de requesting "Bild:Foo.jpg" from en?
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] );
}
}
}
I don't really get what the foreach is for: it's only one file, right? You can use reset() to get the first element of an array if it doesn't have index 0. Speaking of which, there should probably be some sanity check that verifies that $title is a legal title to prevent unnecessary requests or people sneaking in a '|' and not getting what they expect.
Roan Kattouw (Catrope)