ForeignAPIFile.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Very hacky and inefficient
  4. * do not use :D
  5. *
  6. * @ingroup FileRepo
  7. */
  8. class ForeignAPIFile extends File {
  9. private $mExists;
  10. function __construct( $title, $repo, $info, $exists = false ) {
  11. parent::__construct( $title, $repo );
  12. $this->mInfo = $info;
  13. $this->mExists = $exists;
  14. }
  15. static function newFromTitle( $title, $repo ) {
  16. $info = $repo->getImageInfo( $title );
  17. if( $info ) {
  18. return new ForeignAPIFile( $title, $repo, $info, true );
  19. } else {
  20. return null;
  21. }
  22. }
  23. // Dummy functions...
  24. public function exists() {
  25. return $this->mExists;
  26. }
  27. public function getPath() {
  28. return false;
  29. }
  30. function transform( $params, $flags = 0 ) {
  31. if( !$this->canRender() ) {
  32. // show icon
  33. return parent::transform( $params, $flags );
  34. }
  35. $thumbUrl = $this->repo->getThumbUrlFromCache(
  36. $this->getName(),
  37. isset( $params['width'] ) ? $params['width'] : -1,
  38. isset( $params['height'] ) ? $params['height'] : -1 );
  39. if( $thumbUrl ) {
  40. return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );;
  41. }
  42. return false;
  43. }
  44. // Info we can get from API...
  45. public function getWidth( $page = 1 ) {
  46. return intval( @$this->mInfo['width'] );
  47. }
  48. public function getHeight( $page = 1 ) {
  49. return intval( @$this->mInfo['height'] );
  50. }
  51. public function getMetadata() {
  52. if ( isset( $this->mInfo['metadata'] ) ) {
  53. return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
  54. }
  55. return null;
  56. }
  57. public static function parseMetadata( $metadata ) {
  58. if( !is_array( $metadata ) ) {
  59. return $metadata;
  60. }
  61. $ret = array();
  62. foreach( $metadata as $meta ) {
  63. $ret[ $meta['name'] ] = self::parseMetadata( $meta['value'] );
  64. }
  65. return $ret;
  66. }
  67. public function getSize() {
  68. return intval( @$this->mInfo['size'] );
  69. }
  70. public function getUrl() {
  71. return strval( @$this->mInfo['url'] );
  72. }
  73. public function getUser( $method='text' ) {
  74. return strval( @$this->mInfo['user'] );
  75. }
  76. public function getDescription() {
  77. return strval( @$this->mInfo['comment'] );
  78. }
  79. function getSha1() {
  80. return wfBaseConvert( strval( @$this->mInfo['sha1'] ), 16, 36, 31 );
  81. }
  82. function getTimestamp() {
  83. return wfTimestamp( TS_MW, strval( @$this->mInfo['timestamp'] ) );
  84. }
  85. function getMimeType() {
  86. if( !isset( $this->mInfo['mime'] ) ) {
  87. $magic = MimeMagic::singleton();
  88. $this->mInfo['mime'] = $magic->guessTypesForExtension( $this->getExtension() );
  89. }
  90. return $this->mInfo['mime'];
  91. }
  92. /// @fixme May guess wrong on file types that can be eg audio or video
  93. function getMediaType() {
  94. $magic = MimeMagic::singleton();
  95. return $magic->getMediaType( null, $this->getMimeType() );
  96. }
  97. function getDescriptionUrl() {
  98. return isset( $this->mInfo['descriptionurl'] )
  99. ? $this->mInfo['descriptionurl']
  100. : false;
  101. }
  102. /**
  103. * Only useful if we're locally caching thumbs anyway...
  104. */
  105. function getThumbPath( $suffix = '' ) {
  106. if ( $this->repo->canCacheThumbs() ) {
  107. global $wgUploadDirectory;
  108. $path = $wgUploadDirectory . '/thumb/' . $this->getHashPath( $this->getName() );
  109. if ( $suffix ) {
  110. $path = $path . $suffix . '/';
  111. }
  112. return $path;
  113. }
  114. else {
  115. return null;
  116. }
  117. }
  118. function getThumbnails() {
  119. $files = array();
  120. $dir = $this->getThumbPath( $this->getName() );
  121. if ( is_dir( $dir ) ) {
  122. $handle = opendir( $dir );
  123. if ( $handle ) {
  124. while ( false !== ( $file = readdir($handle) ) ) {
  125. if ( $file{0} != '.' ) {
  126. $files[] = $file;
  127. }
  128. }
  129. closedir( $handle );
  130. }
  131. }
  132. return $files;
  133. }
  134. function purgeCache() {
  135. $this->purgeThumbnails();
  136. $this->purgeDescriptionPage();
  137. }
  138. function purgeDescriptionPage() {
  139. global $wgMemc, $wgContLang;
  140. $url = $this->repo->getDescriptionRenderUrl( $this->getName(), $wgContLang->getCode() );
  141. $key = wfMemcKey( 'RemoteFileDescription', 'url', md5($url) );
  142. $wgMemc->delete( $key );
  143. }
  144. function purgeThumbnails() {
  145. global $wgMemc;
  146. $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $this->getName() );
  147. $wgMemc->delete( $key );
  148. $files = $this->getThumbnails();
  149. $dir = $this->getThumbPath( $this->getName() );
  150. foreach ( $files as $file ) {
  151. unlink( $dir . $file );
  152. }
  153. if ( is_dir( $dir ) ) {
  154. rmdir( $dir ); // Might have already gone away, spews errors if we don't.
  155. }
  156. }
  157. }