ForeignAPIRepo.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * A foreign repository with a remote MediaWiki with an API thingy
  4. * Very hacky and inefficient
  5. * do not use except for testing :D
  6. *
  7. * Example config:
  8. *
  9. * $wgForeignFileRepos[] = array(
  10. * 'class' => 'ForeignAPIRepo',
  11. * 'name' => 'shared',
  12. * 'apibase' => 'http://en.wikipedia.org/w/api.php',
  13. * 'fetchDescription' => true, // Optional
  14. * 'descriptionCacheExpiry' => 3600,
  15. * );
  16. *
  17. * @ingroup FileRepo
  18. */
  19. class ForeignAPIRepo extends FileRepo {
  20. var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
  21. var $apiThumbCacheExpiry = 0;
  22. protected $mQueryCache = array();
  23. function __construct( $info ) {
  24. parent::__construct( $info );
  25. $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php
  26. if( !$this->scriptDirUrl ) {
  27. // hack for description fetches
  28. $this->scriptDirUrl = dirname( $this->mApiBase );
  29. }
  30. }
  31. /**
  32. * Per docs in FileRepo, this needs to return false if we don't support versioned
  33. * files. Well, we don't.
  34. */
  35. function newFile( $title, $time = false ) {
  36. if ( $time ) {
  37. return false;
  38. }
  39. return parent::newFile( $title, $time );
  40. }
  41. /**
  42. * No-ops
  43. */
  44. function storeBatch( $triplets, $flags = 0 ) {
  45. return false;
  46. }
  47. function storeTemp( $originalName, $srcPath ) {
  48. return false;
  49. }
  50. function publishBatch( $triplets, $flags = 0 ) {
  51. return false;
  52. }
  53. function deleteBatch( $sourceDestPairs ) {
  54. return false;
  55. }
  56. function getFileProps( $virtualUrl ) {
  57. return false;
  58. }
  59. protected function queryImage( $query ) {
  60. $data = $this->fetchImageQuery( $query );
  61. if( isset( $data['query']['pages'] ) ) {
  62. foreach( $data['query']['pages'] as $pageid => $info ) {
  63. if( isset( $info['imageinfo'][0] ) ) {
  64. return $info['imageinfo'][0];
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. protected function fetchImageQuery( $query ) {
  71. global $wgMemc;
  72. $url = $this->mApiBase .
  73. '?' .
  74. wfArrayToCgi(
  75. array_merge( $query,
  76. array(
  77. 'format' => 'json',
  78. 'action' => 'query' ) ) );
  79. if( !isset( $this->mQueryCache[$url] ) ) {
  80. $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
  81. $data = $wgMemc->get( $key );
  82. if( !$data ) {
  83. $data = Http::get( $url );
  84. if ( !$data ) {
  85. return null;
  86. }
  87. $wgMemc->set( $key, $data, 3600 );
  88. }
  89. if( count( $this->mQueryCache ) > 100 ) {
  90. // Keep the cache from growing infinitely
  91. $this->mQueryCache = array();
  92. }
  93. $this->mQueryCache[$url] = $data;
  94. }
  95. return json_decode( $this->mQueryCache[$url], true );
  96. }
  97. function getImageInfo( $title, $time = false ) {
  98. return $this->queryImage( array(
  99. 'titles' => 'Image:' . $title->getText(),
  100. 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
  101. 'prop' => 'imageinfo' ) );
  102. }
  103. function findBySha1( $hash ) {
  104. $results = $this->fetchImageQuery( array(
  105. 'aisha1base36' => $hash,
  106. 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
  107. 'list' => 'allimages', ) );
  108. $ret = array();
  109. if ( isset( $results['query']['allimages'] ) ) {
  110. foreach ( $results['query']['allimages'] as $img ) {
  111. $ret[] = new ForeignAPIFile( Title::makeTitle( NS_FILE, $img['name'] ), $this, $img );
  112. }
  113. }
  114. return $ret;
  115. }
  116. function getThumbUrl( $name, $width=-1, $height=-1 ) {
  117. $info = $this->queryImage( array(
  118. 'titles' => 'Image:' . $name,
  119. 'iiprop' => 'url',
  120. 'iiurlwidth' => $width,
  121. 'iiurlheight' => $height,
  122. 'prop' => 'imageinfo' ) );
  123. if( $info ) {
  124. wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
  125. return $info['thumburl'];
  126. } else {
  127. return false;
  128. }
  129. }
  130. function getThumbUrlFromCache( $name, $width, $height ) {
  131. global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
  132. if ( !$this->canCacheThumbs() ) {
  133. return $this->getThumbUrl( $name, $width, $height );
  134. }
  135. $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
  136. if ( $thumbUrl = $wgMemc->get($key) ) {
  137. wfDebug("Got thumb from local cache. $thumbUrl \n");
  138. return $thumbUrl;
  139. }
  140. else {
  141. $foreignUrl = $this->getThumbUrl( $name, $width, $height );
  142. // We need the same filename as the remote one :)
  143. $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
  144. $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
  145. if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
  146. wfMkdirParents($wgUploadDirectory . '/' . $path);
  147. }
  148. if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) {
  149. wfDebug( __METHOD__ . " could not write to thumb path\n" );
  150. return $foreignUrl;
  151. }
  152. $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
  153. $thumb = Http::get( $foreignUrl );
  154. # FIXME: Delete old thumbs that aren't being used. Maintenance script?
  155. file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
  156. $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
  157. wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
  158. return $localUrl;
  159. }
  160. }
  161. /**
  162. * Are we locally caching the thumbnails?
  163. * @return bool
  164. */
  165. public function canCacheThumbs() {
  166. return ( $this->apiThumbCacheExpiry > 0 );
  167. }
  168. }