OldLocalFile.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php
  2. /**
  3. * Old file in the oldimage 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 to represent a file in the oldimage table
  26. *
  27. * @ingroup FileAbstraction
  28. */
  29. class OldLocalFile extends LocalFile {
  30. /** @var string|int Timestamp */
  31. protected $requestedTime;
  32. /** @var string Archive name */
  33. protected $archive_name;
  34. const CACHE_VERSION = 1;
  35. const MAX_CACHE_ROWS = 20;
  36. /**
  37. * @param Title $title
  38. * @param FileRepo $repo
  39. * @param string|int|null $time
  40. * @return self
  41. * @throws MWException
  42. */
  43. static function newFromTitle( $title, $repo, $time = null ) {
  44. # The null default value is only here to avoid an E_STRICT
  45. if ( $time === null ) {
  46. throw new MWException( __METHOD__ . ' got null for $time parameter' );
  47. }
  48. return new self( $title, $repo, $time, null );
  49. }
  50. /**
  51. * @param Title $title
  52. * @param FileRepo $repo
  53. * @param string $archiveName
  54. * @return self
  55. */
  56. static function newFromArchiveName( $title, $repo, $archiveName ) {
  57. return new self( $title, $repo, null, $archiveName );
  58. }
  59. /**
  60. * @param stdClass $row
  61. * @param FileRepo $repo
  62. * @return self
  63. */
  64. static function newFromRow( $row, $repo ) {
  65. $title = Title::makeTitle( NS_FILE, $row->oi_name );
  66. $file = new self( $title, $repo, null, $row->oi_archive_name );
  67. $file->loadFromRow( $row, 'oi_' );
  68. return $file;
  69. }
  70. /**
  71. * Create a OldLocalFile from a SHA-1 key
  72. * Do not call this except from inside a repo class.
  73. *
  74. * @param string $sha1 Base-36 SHA-1
  75. * @param LocalRepo $repo
  76. * @param string|bool $timestamp MW_timestamp (optional)
  77. *
  78. * @return bool|OldLocalFile
  79. */
  80. static function newFromKey( $sha1, $repo, $timestamp = false ) {
  81. $dbr = $repo->getReplicaDB();
  82. $conds = [ 'oi_sha1' => $sha1 ];
  83. if ( $timestamp ) {
  84. $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
  85. }
  86. $fileQuery = self::getQueryInfo();
  87. $row = $dbr->selectRow(
  88. $fileQuery['tables'], $fileQuery['fields'], $conds, __METHOD__, [], $fileQuery['joins']
  89. );
  90. if ( $row ) {
  91. return self::newFromRow( $row, $repo );
  92. } else {
  93. return false;
  94. }
  95. }
  96. /**
  97. * Fields in the oldimage table
  98. * @deprecated since 1.31, use self::getQueryInfo() instead.
  99. * @return string[]
  100. */
  101. static function selectFields() {
  102. global $wgActorTableSchemaMigrationStage;
  103. wfDeprecated( __METHOD__, '1.31' );
  104. if ( $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH ) {
  105. // If code is using this instead of self::getQueryInfo(), there's a
  106. // decent chance it's going to try to directly access
  107. // $row->oi_user or $row->oi_user_text and we can't give it
  108. // useful values here once those aren't being written anymore.
  109. throw new BadMethodCallException(
  110. 'Cannot use ' . __METHOD__ . ' when $wgActorTableSchemaMigrationStage > MIGRATION_WRITE_BOTH'
  111. );
  112. }
  113. return [
  114. 'oi_name',
  115. 'oi_archive_name',
  116. 'oi_size',
  117. 'oi_width',
  118. 'oi_height',
  119. 'oi_metadata',
  120. 'oi_bits',
  121. 'oi_media_type',
  122. 'oi_major_mime',
  123. 'oi_minor_mime',
  124. 'oi_user',
  125. 'oi_user_text',
  126. 'oi_actor' => $wgActorTableSchemaMigrationStage > MIGRATION_OLD ? 'oi_actor' : 'NULL',
  127. 'oi_timestamp',
  128. 'oi_deleted',
  129. 'oi_sha1',
  130. ] + MediaWikiServices::getInstance()->getCommentStore()->getFields( 'oi_description' );
  131. }
  132. /**
  133. * Return the tables, fields, and join conditions to be selected to create
  134. * a new oldlocalfile object.
  135. * @since 1.31
  136. * @param string[] $options
  137. * - omit-lazy: Omit fields that are lazily cached.
  138. * @return array[] With three keys:
  139. * - tables: (string[]) to include in the `$table` to `IDatabase->select()`
  140. * - fields: (string[]) to include in the `$vars` to `IDatabase->select()`
  141. * - joins: (array) to include in the `$join_conds` to `IDatabase->select()`
  142. */
  143. public static function getQueryInfo( array $options = [] ) {
  144. $commentQuery = MediaWikiServices::getInstance()->getCommentStore()->getJoin( 'oi_description' );
  145. $actorQuery = ActorMigration::newMigration()->getJoin( 'oi_user' );
  146. $ret = [
  147. 'tables' => [ 'oldimage' ] + $commentQuery['tables'] + $actorQuery['tables'],
  148. 'fields' => [
  149. 'oi_name',
  150. 'oi_archive_name',
  151. 'oi_size',
  152. 'oi_width',
  153. 'oi_height',
  154. 'oi_bits',
  155. 'oi_media_type',
  156. 'oi_major_mime',
  157. 'oi_minor_mime',
  158. 'oi_timestamp',
  159. 'oi_deleted',
  160. 'oi_sha1',
  161. ] + $commentQuery['fields'] + $actorQuery['fields'],
  162. 'joins' => $commentQuery['joins'] + $actorQuery['joins'],
  163. ];
  164. if ( in_array( 'omit-nonlazy', $options, true ) ) {
  165. // Internal use only for getting only the lazy fields
  166. $ret['fields'] = [];
  167. }
  168. if ( !in_array( 'omit-lazy', $options, true ) ) {
  169. // Note: Keep this in sync with self::getLazyCacheFields()
  170. $ret['fields'][] = 'oi_metadata';
  171. }
  172. return $ret;
  173. }
  174. /**
  175. * @param Title $title
  176. * @param FileRepo $repo
  177. * @param string|int|null $time Timestamp or null to load by archive name
  178. * @param string|null $archiveName Archive name or null to load by timestamp
  179. * @throws MWException
  180. */
  181. function __construct( $title, $repo, $time, $archiveName ) {
  182. parent::__construct( $title, $repo );
  183. $this->requestedTime = $time;
  184. $this->archive_name = $archiveName;
  185. if ( is_null( $time ) && is_null( $archiveName ) ) {
  186. throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
  187. }
  188. }
  189. /**
  190. * @return bool
  191. */
  192. function getCacheKey() {
  193. return false;
  194. }
  195. /**
  196. * @return string
  197. */
  198. function getArchiveName() {
  199. if ( !isset( $this->archive_name ) ) {
  200. $this->load();
  201. }
  202. return $this->archive_name;
  203. }
  204. /**
  205. * @return bool
  206. */
  207. function isOld() {
  208. return true;
  209. }
  210. /**
  211. * @return bool
  212. */
  213. function isVisible() {
  214. return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
  215. }
  216. function loadFromDB( $flags = 0 ) {
  217. $this->dataLoaded = true;
  218. $dbr = ( $flags & self::READ_LATEST )
  219. ? $this->repo->getMasterDB()
  220. : $this->repo->getReplicaDB();
  221. $conds = [ 'oi_name' => $this->getName() ];
  222. if ( is_null( $this->requestedTime ) ) {
  223. $conds['oi_archive_name'] = $this->archive_name;
  224. } else {
  225. $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
  226. }
  227. $fileQuery = static::getQueryInfo();
  228. $row = $dbr->selectRow(
  229. $fileQuery['tables'],
  230. $fileQuery['fields'],
  231. $conds,
  232. __METHOD__,
  233. [ 'ORDER BY' => 'oi_timestamp DESC' ],
  234. $fileQuery['joins']
  235. );
  236. if ( $row ) {
  237. $this->loadFromRow( $row, 'oi_' );
  238. } else {
  239. $this->fileExists = false;
  240. }
  241. }
  242. /**
  243. * Load lazy file metadata from the DB
  244. */
  245. protected function loadExtraFromDB() {
  246. $this->extraDataLoaded = true;
  247. $dbr = $this->repo->getReplicaDB();
  248. $conds = [ 'oi_name' => $this->getName() ];
  249. if ( is_null( $this->requestedTime ) ) {
  250. $conds['oi_archive_name'] = $this->archive_name;
  251. } else {
  252. $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
  253. }
  254. $fileQuery = static::getQueryInfo( [ 'omit-nonlazy' ] );
  255. // In theory the file could have just been renamed/deleted...oh well
  256. $row = $dbr->selectRow(
  257. $fileQuery['tables'],
  258. $fileQuery['fields'],
  259. $conds,
  260. __METHOD__,
  261. [ 'ORDER BY' => 'oi_timestamp DESC' ],
  262. $fileQuery['joins']
  263. );
  264. if ( !$row ) { // fallback to master
  265. $dbr = $this->repo->getMasterDB();
  266. $row = $dbr->selectRow(
  267. $fileQuery['tables'],
  268. $fileQuery['fields'],
  269. $conds,
  270. __METHOD__,
  271. [ 'ORDER BY' => 'oi_timestamp DESC' ],
  272. $fileQuery['joins']
  273. );
  274. }
  275. if ( $row ) {
  276. foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
  277. $this->$name = $value;
  278. }
  279. } else {
  280. throw new MWException( "Could not find data for image '{$this->archive_name}'." );
  281. }
  282. }
  283. /** @inheritDoc */
  284. protected function getCacheFields( $prefix = 'img_' ) {
  285. $fields = parent::getCacheFields( $prefix );
  286. $fields[] = $prefix . 'archive_name';
  287. $fields[] = $prefix . 'deleted';
  288. return $fields;
  289. }
  290. /**
  291. * @return string
  292. */
  293. function getRel() {
  294. return 'archive/' . $this->getHashPath() . $this->getArchiveName();
  295. }
  296. /**
  297. * @return string
  298. */
  299. function getUrlRel() {
  300. return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
  301. }
  302. function upgradeRow() {
  303. $this->loadFromFile();
  304. # Don't destroy file info of missing files
  305. if ( !$this->fileExists ) {
  306. wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
  307. return;
  308. }
  309. $dbw = $this->repo->getMasterDB();
  310. list( $major, $minor ) = self::splitMime( $this->mime );
  311. wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
  312. $dbw->update( 'oldimage',
  313. [
  314. 'oi_size' => $this->size, // sanity
  315. 'oi_width' => $this->width,
  316. 'oi_height' => $this->height,
  317. 'oi_bits' => $this->bits,
  318. 'oi_media_type' => $this->media_type,
  319. 'oi_major_mime' => $major,
  320. 'oi_minor_mime' => $minor,
  321. 'oi_metadata' => $this->metadata,
  322. 'oi_sha1' => $this->sha1,
  323. ], [
  324. 'oi_name' => $this->getName(),
  325. 'oi_archive_name' => $this->archive_name ],
  326. __METHOD__
  327. );
  328. }
  329. /**
  330. * @param int $field One of DELETED_* bitfield constants for file or
  331. * revision rows
  332. * @return bool
  333. */
  334. function isDeleted( $field ) {
  335. $this->load();
  336. return ( $this->deleted & $field ) == $field;
  337. }
  338. /**
  339. * Returns bitfield value
  340. * @return int
  341. */
  342. function getVisibility() {
  343. $this->load();
  344. return (int)$this->deleted;
  345. }
  346. /**
  347. * Determine if the current user is allowed to view a particular
  348. * field of this image file, if it's marked as deleted.
  349. *
  350. * @param int $field
  351. * @param User|null $user User object to check, or null to use $wgUser
  352. * @return bool
  353. */
  354. function userCan( $field, User $user = null ) {
  355. $this->load();
  356. return Revision::userCanBitfield( $this->deleted, $field, $user );
  357. }
  358. /**
  359. * Upload a file directly into archive. Generally for Special:Import.
  360. *
  361. * @param string $srcPath File system path of the source file
  362. * @param string $archiveName Full archive name of the file, in the form
  363. * $timestamp!$filename, where $filename must match $this->getName()
  364. * @param string $timestamp
  365. * @param string $comment
  366. * @param User $user
  367. * @return Status
  368. */
  369. function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
  370. $this->lock();
  371. $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
  372. $status = $this->publishTo( $srcPath, $dstRel );
  373. if ( $status->isGood() ) {
  374. if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
  375. $status->fatal( 'filenotfound', $srcPath );
  376. }
  377. }
  378. $this->unlock();
  379. return $status;
  380. }
  381. /**
  382. * Record a file upload in the oldimage table, without adding log entries.
  383. *
  384. * @param string $srcPath File system path to the source file
  385. * @param string $archiveName The archive name of the file
  386. * @param string $timestamp
  387. * @param string $comment Upload comment
  388. * @param User $user User who did this upload
  389. * @return bool
  390. */
  391. protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
  392. $dbw = $this->repo->getMasterDB();
  393. $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
  394. $props = $this->repo->getFileProps( $dstPath );
  395. if ( !$props['fileExists'] ) {
  396. return false;
  397. }
  398. $commentFields = MediaWikiServices::getInstance()->getCommentStore()
  399. ->insert( $dbw, 'oi_description', $comment );
  400. $actorFields = ActorMigration::newMigration()->getInsertValues( $dbw, 'oi_user', $user );
  401. $dbw->insert( 'oldimage',
  402. [
  403. 'oi_name' => $this->getName(),
  404. 'oi_archive_name' => $archiveName,
  405. 'oi_size' => $props['size'],
  406. 'oi_width' => intval( $props['width'] ),
  407. 'oi_height' => intval( $props['height'] ),
  408. 'oi_bits' => $props['bits'],
  409. 'oi_timestamp' => $dbw->timestamp( $timestamp ),
  410. 'oi_metadata' => $props['metadata'],
  411. 'oi_media_type' => $props['media_type'],
  412. 'oi_major_mime' => $props['major_mime'],
  413. 'oi_minor_mime' => $props['minor_mime'],
  414. 'oi_sha1' => $props['sha1'],
  415. ] + $commentFields + $actorFields, __METHOD__
  416. );
  417. return true;
  418. }
  419. /**
  420. * If archive name is an empty string, then file does not "exist"
  421. *
  422. * This is the case for a couple files on Wikimedia servers where
  423. * the old version is "lost".
  424. * @return bool
  425. */
  426. public function exists() {
  427. $archiveName = $this->getArchiveName();
  428. if ( $archiveName === '' || !is_string( $archiveName ) ) {
  429. return false;
  430. }
  431. return parent::exists();
  432. }
  433. }