ArchivedFile.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * Deleted file in the 'filearchive' table.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup FileAbstraction
  22. */
  23. /**
  24. * Class representing a row of the 'filearchive' table
  25. *
  26. * @ingroup FileAbstraction
  27. */
  28. class ArchivedFile {
  29. /** @var int Filearchive row ID */
  30. private $id;
  31. /** @var string File name */
  32. private $name;
  33. /** @var string FileStore storage group */
  34. private $group;
  35. /** @var string FileStore SHA-1 key */
  36. private $key;
  37. /** @var int File size in bytes */
  38. private $size;
  39. /** @var int Size in bytes */
  40. private $bits;
  41. /** @var int Width */
  42. private $width;
  43. /** @var int Height */
  44. private $height;
  45. /** @var string Metadata string */
  46. private $metadata;
  47. /** @var string MIME type */
  48. private $mime;
  49. /** @var string Media type */
  50. private $media_type;
  51. /** @var string Upload description */
  52. private $description;
  53. /** @var User|null Uploader */
  54. private $user;
  55. /** @var string Time of upload */
  56. private $timestamp;
  57. /** @var bool Whether or not all this has been loaded from the database (loadFromXxx) */
  58. private $dataLoaded;
  59. /** @var int Bitfield akin to rev_deleted */
  60. private $deleted;
  61. /** @var string SHA-1 hash of file content */
  62. private $sha1;
  63. /** @var int|false Number of pages of a multipage document, or false for
  64. * documents which aren't multipage documents
  65. */
  66. private $pageCount;
  67. /** @var string Original base filename */
  68. private $archive_name;
  69. /** @var MediaHandler */
  70. protected $handler;
  71. /** @var Title */
  72. protected $title; # image title
  73. /**
  74. * @throws MWException
  75. * @param Title $title
  76. * @param int $id
  77. * @param string $key
  78. * @param string $sha1
  79. */
  80. function __construct( $title, $id = 0, $key = '', $sha1 = '' ) {
  81. $this->id = -1;
  82. $this->title = false;
  83. $this->name = false;
  84. $this->group = 'deleted'; // needed for direct use of constructor
  85. $this->key = '';
  86. $this->size = 0;
  87. $this->bits = 0;
  88. $this->width = 0;
  89. $this->height = 0;
  90. $this->metadata = '';
  91. $this->mime = "unknown/unknown";
  92. $this->media_type = '';
  93. $this->description = '';
  94. $this->user = null;
  95. $this->timestamp = null;
  96. $this->deleted = 0;
  97. $this->dataLoaded = false;
  98. $this->exists = false;
  99. $this->sha1 = '';
  100. if ( $title instanceof Title ) {
  101. $this->title = File::normalizeTitle( $title, 'exception' );
  102. $this->name = $title->getDBkey();
  103. }
  104. if ( $id ) {
  105. $this->id = $id;
  106. }
  107. if ( $key ) {
  108. $this->key = $key;
  109. }
  110. if ( $sha1 ) {
  111. $this->sha1 = $sha1;
  112. }
  113. if ( !$id && !$key && !( $title instanceof Title ) && !$sha1 ) {
  114. throw new MWException( "No specifications provided to ArchivedFile constructor." );
  115. }
  116. }
  117. /**
  118. * Loads a file object from the filearchive table
  119. * @throws MWException
  120. * @return bool|null True on success or null
  121. */
  122. public function load() {
  123. if ( $this->dataLoaded ) {
  124. return true;
  125. }
  126. $conds = [];
  127. if ( $this->id > 0 ) {
  128. $conds['fa_id'] = $this->id;
  129. }
  130. if ( $this->key ) {
  131. $conds['fa_storage_group'] = $this->group;
  132. $conds['fa_storage_key'] = $this->key;
  133. }
  134. if ( $this->title ) {
  135. $conds['fa_name'] = $this->title->getDBkey();
  136. }
  137. if ( $this->sha1 ) {
  138. $conds['fa_sha1'] = $this->sha1;
  139. }
  140. if ( !count( $conds ) ) {
  141. throw new MWException( "No specific information for retrieving archived file" );
  142. }
  143. if ( !$this->title || $this->title->getNamespace() == NS_FILE ) {
  144. $this->dataLoaded = true; // set it here, to have also true on miss
  145. $dbr = wfGetDB( DB_REPLICA );
  146. $fileQuery = self::getQueryInfo();
  147. $row = $dbr->selectRow(
  148. $fileQuery['tables'],
  149. $fileQuery['fields'],
  150. $conds,
  151. __METHOD__,
  152. [ 'ORDER BY' => 'fa_timestamp DESC' ],
  153. $fileQuery['joins']
  154. );
  155. if ( !$row ) {
  156. // this revision does not exist?
  157. return null;
  158. }
  159. // initialize fields for filestore image object
  160. $this->loadFromRow( $row );
  161. } else {
  162. throw new MWException( 'This title does not correspond to an image page.' );
  163. }
  164. $this->exists = true;
  165. return true;
  166. }
  167. /**
  168. * Loads a file object from the filearchive table
  169. *
  170. * @param stdClass $row
  171. * @return ArchivedFile
  172. */
  173. public static function newFromRow( $row ) {
  174. $file = new ArchivedFile( Title::makeTitle( NS_FILE, $row->fa_name ) );
  175. $file->loadFromRow( $row );
  176. return $file;
  177. }
  178. /**
  179. * Fields in the filearchive table
  180. * @deprecated since 1.31, use self::getQueryInfo() instead.
  181. * @return string[]
  182. */
  183. static function selectFields() {
  184. global $wgActorTableSchemaMigrationStage;
  185. if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
  186. // If code is using this instead of self::getQueryInfo(), there's a
  187. // decent chance it's going to try to directly access
  188. // $row->fa_user or $row->fa_user_text and we can't give it
  189. // useful values here once those aren't being written anymore.
  190. throw new BadMethodCallException(
  191. 'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
  192. );
  193. }
  194. wfDeprecated( __METHOD__, '1.31' );
  195. return [
  196. 'fa_id',
  197. 'fa_name',
  198. 'fa_archive_name',
  199. 'fa_storage_key',
  200. 'fa_storage_group',
  201. 'fa_size',
  202. 'fa_bits',
  203. 'fa_width',
  204. 'fa_height',
  205. 'fa_metadata',
  206. 'fa_media_type',
  207. 'fa_major_mime',
  208. 'fa_minor_mime',
  209. 'fa_user',
  210. 'fa_user_text',
  211. 'fa_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ? 'fa_actor' : 'NULL',
  212. 'fa_timestamp',
  213. 'fa_deleted',
  214. 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
  215. 'fa_sha1',
  216. ] + CommentStore::getStore()->getFields( 'fa_description' );
  217. }
  218. /**
  219. * Return the tables, fields, and join conditions to be selected to create
  220. * a new archivedfile object.
  221. * @since 1.31
  222. * @return array[] With three keys:
  223. * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
  224. * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
  225. * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
  226. */
  227. public static function getQueryInfo() {
  228. $commentQuery = CommentStore::getStore()->getJoin( 'fa_description' );
  229. $actorQuery = ActorMigration::newMigration()->getJoin( 'fa_user' );
  230. return [
  231. 'tables' => [ 'filearchive' ] + $commentQuery['tables'] + $actorQuery['tables'],
  232. 'fields' => [
  233. 'fa_id',
  234. 'fa_name',
  235. 'fa_archive_name',
  236. 'fa_storage_key',
  237. 'fa_storage_group',
  238. 'fa_size',
  239. 'fa_bits',
  240. 'fa_width',
  241. 'fa_height',
  242. 'fa_metadata',
  243. 'fa_media_type',
  244. 'fa_major_mime',
  245. 'fa_minor_mime',
  246. 'fa_timestamp',
  247. 'fa_deleted',
  248. 'fa_deleted_timestamp', /* Used by LocalFileRestoreBatch */
  249. 'fa_sha1',
  250. ] + $commentQuery['fields'] + $actorQuery['fields'],
  251. 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
  252. ];
  253. }
  254. /**
  255. * Load ArchivedFile object fields from a DB row.
  256. *
  257. * @param stdClass $row Object database row
  258. * @since 1.21
  259. */
  260. public function loadFromRow( $row ) {
  261. $this->id = intval( $row->fa_id );
  262. $this->name = $row->fa_name;
  263. $this->archive_name = $row->fa_archive_name;
  264. $this->group = $row->fa_storage_group;
  265. $this->key = $row->fa_storage_key;
  266. $this->size = $row->fa_size;
  267. $this->bits = $row->fa_bits;
  268. $this->width = $row->fa_width;
  269. $this->height = $row->fa_height;
  270. $this->metadata = $row->fa_metadata;
  271. $this->mime = "$row->fa_major_mime/$row->fa_minor_mime";
  272. $this->media_type = $row->fa_media_type;
  273. $this->description = CommentStore::getStore()
  274. // Legacy because $row may have come from self::selectFields()
  275. ->getCommentLegacy( wfGetDB( DB_REPLICA ), 'fa_description', $row )->text;
  276. $this->user = User::newFromAnyId( $row->fa_user, $row->fa_user_text, $row->fa_actor );
  277. $this->timestamp = $row->fa_timestamp;
  278. $this->deleted = $row->fa_deleted;
  279. if ( isset( $row->fa_sha1 ) ) {
  280. $this->sha1 = $row->fa_sha1;
  281. } else {
  282. // old row, populate from key
  283. $this->sha1 = LocalRepo::getHashFromKey( $this->key );
  284. }
  285. if ( !$this->title ) {
  286. $this->title = Title::makeTitleSafe( NS_FILE, $row->fa_name );
  287. }
  288. }
  289. /**
  290. * Return the associated title object
  291. *
  292. * @return Title
  293. */
  294. public function getTitle() {
  295. if ( !$this->title ) {
  296. $this->load();
  297. }
  298. return $this->title;
  299. }
  300. /**
  301. * Return the file name
  302. *
  303. * @return string
  304. */
  305. public function getName() {
  306. if ( $this->name === false ) {
  307. $this->load();
  308. }
  309. return $this->name;
  310. }
  311. /**
  312. * @return int
  313. */
  314. public function getID() {
  315. $this->load();
  316. return $this->id;
  317. }
  318. /**
  319. * @return bool
  320. */
  321. public function exists() {
  322. $this->load();
  323. return $this->exists;
  324. }
  325. /**
  326. * Return the FileStore key
  327. * @return string
  328. */
  329. public function getKey() {
  330. $this->load();
  331. return $this->key;
  332. }
  333. /**
  334. * Return the FileStore key (overriding base File class)
  335. * @return string
  336. */
  337. public function getStorageKey() {
  338. return $this->getKey();
  339. }
  340. /**
  341. * Return the FileStore storage group
  342. * @return string
  343. */
  344. public function getGroup() {
  345. return $this->group;
  346. }
  347. /**
  348. * Return the width of the image
  349. * @return int
  350. */
  351. public function getWidth() {
  352. $this->load();
  353. return $this->width;
  354. }
  355. /**
  356. * Return the height of the image
  357. * @return int
  358. */
  359. public function getHeight() {
  360. $this->load();
  361. return $this->height;
  362. }
  363. /**
  364. * Get handler-specific metadata
  365. * @return string
  366. */
  367. public function getMetadata() {
  368. $this->load();
  369. return $this->metadata;
  370. }
  371. /**
  372. * Return the size of the image file, in bytes
  373. * @return int
  374. */
  375. public function getSize() {
  376. $this->load();
  377. return $this->size;
  378. }
  379. /**
  380. * Return the bits of the image file, in bytes
  381. * @return int
  382. */
  383. public function getBits() {
  384. $this->load();
  385. return $this->bits;
  386. }
  387. /**
  388. * Returns the MIME type of the file.
  389. * @return string
  390. */
  391. public function getMimeType() {
  392. $this->load();
  393. return $this->mime;
  394. }
  395. /**
  396. * Get a MediaHandler instance for this file
  397. * @return MediaHandler
  398. */
  399. function getHandler() {
  400. if ( !isset( $this->handler ) ) {
  401. $this->handler = MediaHandler::getHandler( $this->getMimeType() );
  402. }
  403. return $this->handler;
  404. }
  405. /**
  406. * Returns the number of pages of a multipage document, or false for
  407. * documents which aren't multipage documents
  408. * @return bool|int
  409. */
  410. function pageCount() {
  411. if ( !isset( $this->pageCount ) ) {
  412. // @FIXME: callers expect File objects
  413. if ( $this->getHandler() && $this->handler->isMultiPage( $this ) ) {
  414. $this->pageCount = $this->handler->pageCount( $this );
  415. } else {
  416. $this->pageCount = false;
  417. }
  418. }
  419. return $this->pageCount;
  420. }
  421. /**
  422. * Return the type of the media in the file.
  423. * Use the value returned by this function with the MEDIATYPE_xxx constants.
  424. * @return string
  425. */
  426. public function getMediaType() {
  427. $this->load();
  428. return $this->media_type;
  429. }
  430. /**
  431. * Return upload timestamp.
  432. *
  433. * @return string
  434. */
  435. public function getTimestamp() {
  436. $this->load();
  437. return wfTimestamp( TS_MW, $this->timestamp );
  438. }
  439. /**
  440. * Get the SHA-1 base 36 hash of the file
  441. *
  442. * @return string
  443. * @since 1.21
  444. */
  445. function getSha1() {
  446. $this->load();
  447. return $this->sha1;
  448. }
  449. /**
  450. * Returns ID or name of user who uploaded the file
  451. *
  452. * @note Prior to MediaWiki 1.23, this method always
  453. * returned the user id, and was inconsistent with
  454. * the rest of the file classes.
  455. * @param string $type 'text', 'id', or 'object'
  456. * @return int|string|User|null
  457. * @throws MWException
  458. * @since 1.31 added 'object'
  459. */
  460. public function getUser( $type = 'text' ) {
  461. $this->load();
  462. if ( $type === 'object' ) {
  463. return $this->user;
  464. } elseif ( $type === 'text' ) {
  465. return $this->user ? $this->user->getName() : '';
  466. } elseif ( $type === 'id' ) {
  467. return $this->user ? $this->user->getId() : 0;
  468. }
  469. throw new MWException( "Unknown type '$type'." );
  470. }
  471. /**
  472. * Return upload description.
  473. *
  474. * @return string|int
  475. */
  476. public function getDescription() {
  477. $this->load();
  478. if ( $this->isDeleted( File::DELETED_COMMENT ) ) {
  479. return 0;
  480. } else {
  481. return $this->description;
  482. }
  483. }
  484. /**
  485. * Return the user ID of the uploader.
  486. *
  487. * @return int
  488. */
  489. public function getRawUser() {
  490. return $this->getUser( 'id' );
  491. }
  492. /**
  493. * Return the user name of the uploader.
  494. *
  495. * @return string
  496. */
  497. public function getRawUserText() {
  498. return $this->getUser( 'text' );
  499. }
  500. /**
  501. * Return upload description.
  502. *
  503. * @return string
  504. */
  505. public function getRawDescription() {
  506. $this->load();
  507. return $this->description;
  508. }
  509. /**
  510. * Returns the deletion bitfield
  511. * @return int
  512. */
  513. public function getVisibility() {
  514. $this->load();
  515. return $this->deleted;
  516. }
  517. /**
  518. * for file or revision rows
  519. *
  520. * @param int $field One of DELETED_* bitfield constants
  521. * @return bool
  522. */
  523. public function isDeleted( $field ) {
  524. $this->load();
  525. return ( $this->deleted & $field ) == $field;
  526. }
  527. /**
  528. * Determine if the current user is allowed to view a particular
  529. * field of this FileStore image file, if it's marked as deleted.
  530. * @param int $field
  531. * @param null|User $user User object to check, or null to use $wgUser
  532. * @return bool
  533. */
  534. public function userCan( $field, User $user = null ) {
  535. $this->load();
  536. $title = $this->getTitle();
  537. return Revision::userCanBitfield( $this->deleted, $field, $user, $title ?: null );
  538. }
  539. }