ArchivedFile.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * @ingroup Media
  4. */
  5. class ArchivedFile
  6. {
  7. /**#@+
  8. * @private
  9. */
  10. var $id, # filearchive row ID
  11. $title, # image title
  12. $name, # image name
  13. $group, # FileStore storage group
  14. $key, # FileStore sha1 key
  15. $size, # file dimensions
  16. $bits, # size in bytes
  17. $width, # width
  18. $height, # height
  19. $metadata, # metadata string
  20. $mime, # mime type
  21. $media_type, # media type
  22. $description, # upload description
  23. $user, # user ID of uploader
  24. $user_text, # user name of uploader
  25. $timestamp, # time of upload
  26. $dataLoaded, # Whether or not all this has been loaded from the database (loadFromXxx)
  27. $deleted; # Bitfield akin to rev_deleted
  28. /**#@-*/
  29. function ArchivedFile( $title, $id=0, $key='' ) {
  30. $this->id = -1;
  31. $this->title = false;
  32. $this->name = false;
  33. $this->group = '';
  34. $this->key = '';
  35. $this->size = 0;
  36. $this->bits = 0;
  37. $this->width = 0;
  38. $this->height = 0;
  39. $this->metadata = '';
  40. $this->mime = "unknown/unknown";
  41. $this->media_type = '';
  42. $this->description = '';
  43. $this->user = 0;
  44. $this->user_text = '';
  45. $this->timestamp = NULL;
  46. $this->deleted = 0;
  47. $this->dataLoaded = false;
  48. if( is_object($title) ) {
  49. $this->title = $title;
  50. $this->name = $title->getDBkey();
  51. }
  52. if ($id)
  53. $this->id = $id;
  54. if ($key)
  55. $this->key = $key;
  56. if (!$id && !$key && !is_object($title))
  57. throw new MWException( "No specifications provided to ArchivedFile constructor." );
  58. }
  59. /**
  60. * Loads a file object from the filearchive table
  61. * @return ResultWrapper
  62. */
  63. public function load() {
  64. if ( $this->dataLoaded ) {
  65. return true;
  66. }
  67. $conds = array();
  68. if( $this->id > 0 )
  69. $conds['fa_id'] = $this->id;
  70. if( $this->key ) {
  71. $conds['fa_storage_group'] = $this->group;
  72. $conds['fa_storage_key'] = $this->key;
  73. }
  74. if( $this->title )
  75. $conds['fa_name'] = $this->title->getDBkey();
  76. if( !count($conds))
  77. throw new MWException( "No specific information for retrieving archived file" );
  78. if( !$this->title || $this->title->getNamespace() == NS_FILE ) {
  79. $dbr = wfGetDB( DB_SLAVE );
  80. $res = $dbr->select( 'filearchive',
  81. array(
  82. 'fa_id',
  83. 'fa_name',
  84. 'fa_archive_name',
  85. 'fa_storage_key',
  86. 'fa_storage_group',
  87. 'fa_size',
  88. 'fa_bits',
  89. 'fa_width',
  90. 'fa_height',
  91. 'fa_metadata',
  92. 'fa_media_type',
  93. 'fa_major_mime',
  94. 'fa_minor_mime',
  95. 'fa_description',
  96. 'fa_user',
  97. 'fa_user_text',
  98. 'fa_timestamp',
  99. 'fa_deleted' ),
  100. $conds,
  101. __METHOD__,
  102. array( 'ORDER BY' => 'fa_timestamp DESC' ) );
  103. if ( $dbr->numRows( $res ) == 0 ) {
  104. // this revision does not exist?
  105. return;
  106. }
  107. $ret = $dbr->resultObject( $res );
  108. $row = $ret->fetchObject();
  109. // initialize fields for filestore image object
  110. $this->id = intval($row->fa_id);
  111. $this->name = $row->fa_name;
  112. $this->archive_name = $row->fa_archive_name;
  113. $this->group = $row->fa_storage_group;
  114. $this->key = $row->fa_storage_key;
  115. $this->size = $row->fa_size;
  116. $this->bits = $row->fa_bits;
  117. $this->width = $row->fa_width;
  118. $this->height = $row->fa_height;
  119. $this->metadata = $row->fa_metadata;
  120. $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
  121. $this->media_type = $row->fa_media_type;
  122. $this->description = $row->fa_description;
  123. $this->user = $row->fa_user;
  124. $this->user_text = $row->fa_user_text;
  125. $this->timestamp = $row->fa_timestamp;
  126. $this->deleted = $row->fa_deleted;
  127. } else {
  128. throw new MWException( 'This title does not correspond to an image page.' );
  129. return;
  130. }
  131. $this->dataLoaded = true;
  132. return true;
  133. }
  134. /**
  135. * Loads a file object from the filearchive table
  136. * @return ResultWrapper
  137. */
  138. public static function newFromRow( $row ) {
  139. $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
  140. $file->id = intval($row->fa_id);
  141. $file->name = $row->fa_name;
  142. $file->archive_name = $row->fa_archive_name;
  143. $file->group = $row->fa_storage_group;
  144. $file->key = $row->fa_storage_key;
  145. $file->size = $row->fa_size;
  146. $file->bits = $row->fa_bits;
  147. $file->width = $row->fa_width;
  148. $file->height = $row->fa_height;
  149. $file->metadata = $row->fa_metadata;
  150. $file->mime = "$row->fa_major_mime/$row->fa_minor_mime";
  151. $file->media_type = $row->fa_media_type;
  152. $file->description = $row->fa_description;
  153. $file->user = $row->fa_user;
  154. $file->user_text = $row->fa_user_text;
  155. $file->timestamp = $row->fa_timestamp;
  156. $file->deleted = $row->fa_deleted;
  157. return $file;
  158. }
  159. /**
  160. * Return the associated title object
  161. * @public
  162. */
  163. public function getTitle() {
  164. return $this->title;
  165. }
  166. /**
  167. * Return the file name
  168. */
  169. public function getName() {
  170. return $this->name;
  171. }
  172. public function getID() {
  173. $this->load();
  174. return $this->id;
  175. }
  176. /**
  177. * Return the FileStore key
  178. */
  179. public function getKey() {
  180. $this->load();
  181. return $this->key;
  182. }
  183. /**
  184. * Return the FileStore storage group
  185. */
  186. public function getGroup() {
  187. return $file->group;
  188. }
  189. /**
  190. * Return the width of the image
  191. */
  192. public function getWidth() {
  193. $this->load();
  194. return $this->width;
  195. }
  196. /**
  197. * Return the height of the image
  198. */
  199. public function getHeight() {
  200. $this->load();
  201. return $this->height;
  202. }
  203. /**
  204. * Get handler-specific metadata
  205. */
  206. public function getMetadata() {
  207. $this->load();
  208. return $this->metadata;
  209. }
  210. /**
  211. * Return the size of the image file, in bytes
  212. * @public
  213. */
  214. public function getSize() {
  215. $this->load();
  216. return $this->size;
  217. }
  218. /**
  219. * Return the bits of the image file, in bytes
  220. * @public
  221. */
  222. public function getBits() {
  223. $this->load();
  224. return $this->bits;
  225. }
  226. /**
  227. * Returns the mime type of the file.
  228. */
  229. public function getMimeType() {
  230. $this->load();
  231. return $this->mime;
  232. }
  233. /**
  234. * Return the type of the media in the file.
  235. * Use the value returned by this function with the MEDIATYPE_xxx constants.
  236. */
  237. public function getMediaType() {
  238. $this->load();
  239. return $this->media_type;
  240. }
  241. /**
  242. * Return upload timestamp.
  243. */
  244. public function getTimestamp() {
  245. $this->load();
  246. return wfTimestamp( TS_MW, $this->timestamp );
  247. }
  248. /**
  249. * Return the user ID of the uploader.
  250. */
  251. public function getUser() {
  252. $this->load();
  253. if( $this->isDeleted( File::DELETED_USER ) ) {
  254. return 0;
  255. } else {
  256. return $this->user;
  257. }
  258. }
  259. /**
  260. * Return the user name of the uploader.
  261. */
  262. public function getUserText() {
  263. $this->load();
  264. if( $this->isDeleted( File::DELETED_USER ) ) {
  265. return 0;
  266. } else {
  267. return $this->user_text;
  268. }
  269. }
  270. /**
  271. * Return upload description.
  272. */
  273. public function getDescription() {
  274. $this->load();
  275. if( $this->isDeleted( File::DELETED_COMMENT ) ) {
  276. return 0;
  277. } else {
  278. return $this->description;
  279. }
  280. }
  281. /**
  282. * Return the user ID of the uploader.
  283. */
  284. public function getRawUser() {
  285. $this->load();
  286. return $this->user;
  287. }
  288. /**
  289. * Return the user name of the uploader.
  290. */
  291. public function getRawUserText() {
  292. $this->load();
  293. return $this->user_text;
  294. }
  295. /**
  296. * Return upload description.
  297. */
  298. public function getRawDescription() {
  299. $this->load();
  300. return $this->description;
  301. }
  302. /**
  303. * int $field one of DELETED_* bitfield constants
  304. * for file or revision rows
  305. * @return bool
  306. */
  307. public function isDeleted( $field ) {
  308. return ($this->deleted & $field) == $field;
  309. }
  310. /**
  311. * Determine if the current user is allowed to view a particular
  312. * field of this FileStore image file, if it's marked as deleted.
  313. * @param int $field
  314. * @return bool
  315. */
  316. public function userCan( $field ) {
  317. if( ($this->deleted & $field) == $field ) {
  318. global $wgUser;
  319. $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
  320. ? 'suppressrevision'
  321. : 'deleterevision';
  322. wfDebug( "Checking for $permission due to $field match on $this->deleted\n" );
  323. return $wgUser->isAllowed( $permission );
  324. } else {
  325. return true;
  326. }
  327. }
  328. }