ArchivedFile.php 14 KB

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