Skip to content

Instantly share code, notes, and snippets.

@reedy
Created January 28, 2013 01:21
Show Gist options
  • Save reedy/4651983 to your computer and use it in GitHub Desktop.
Save reedy/4651983 to your computer and use it in GitHub Desktop.
class GuildWarsForeignAPIRepo extends ForeignAPIRepo {
function fetchImageQuery( $query ) {
global $wgMemc;
$query = array_merge( $query,
array(
'format' => 'json',
'action' => 'query'
) );
if ( $this->mApiBase ) {
$url = wfAppendQuery( $this->mApiBase, $query );
} else {
$url = $this->makeUrl( $query, 'api' );
}
if( !isset( $this->mQueryCache[$url] ) ) {
$key = $this->getLocalCacheKey( 'GuildWarsForeignAPIRepo', 'Metadata', md5( $url ) );
$data = $wgMemc->get( $key );
if( !$data ) {
$data = self::httpGet( $url );
if ( !$data ) {
return null;
}
$wgMemc->set( $key, $data, 3600 );
}
if( count( $this->mQueryCache ) > 100 ) {
// Keep the cache from growing infinitely
$this->mQueryCache = array();
}
//shifchanges - this code supports embedding images that redirect to a non-image page.
//get the hostname of the site. this should be something like wiki.guildwars.com. we need this for the image URL.
preg_match('@^(?:http://)?([^/]+)@i',$url, $s_matches);
$s_host = $s_matches[1];
//get the filename of the image.
preg_match('/File:(.*?)"/i',$data, $s_filename);
$s_file = $s_filename[1];
//remove redirect data
preg_match('/,"redirects"(.*?)\]/',$data,$s_foo);
$s_redir = $s_foo[0];
//create image url
$s_imgurl = str_replace('/','\/',"http://" . $s_host . "/images/" . substr(md5($s_file),0,1) . "/" . substr(md5($s_file),0,2) . "/" . $s_file);
//create the description url
$s_descurl = str_replace('/','\/',"http://" . $s_host . "/wiki/File:" . $s_file);
//create file url for JSON string (escapes forward slashes)
$s_fileurl = "http://" . $s_host . "/images/" . substr(md5($s_file),0,1) . "/" . substr(md5($s_file),0,2) . "/" . $s_file;
//get the sha1 hash
$s_sha1 = sha1_file($s_fileurl);
//get the file size in bytes
$s_size = strlen(file_get_contents($s_fileurl));
//get image dimensions
$s_dim = getimagesize($s_fileurl);
$s_width = $s_dim[0];
$s_height = $s_dim[1];
//inject the URL into the string
$data = str_replace($s_redir,'',preg_replace('/"imagerepository":""/','"imagerepository":"local","imageinfo":[{"size":"' . $s_size . '","width":"' . $s_width . '","height":"' . $s_height . '","url":"' . $s_imgurl . '","descriptionurl":"' . $s_descurl . '","sha1":"' . $s_sha1 . '"}]',$data));
$this->mQueryCache[$url] = $data;
}
return FormatJson::decode( $this->mQueryCache[$url], true );
}
}
$wgForeignFileRepos[] = array(
'class' => 'GuildWarsForeignAPIRepo',
'name' => 'shared',
'apibase' => 'http://wiki.guildwars.com/api.php',
'fetchDescription' => true, // Optional
'descriptionCacheExpiry' => 43200, // 12 hours, optional (values are seconds)
'apiThumbCacheExpiry' => 43200, // 12 hours, optional, but required for local thumb caching
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment