55 public static function newFromTitle( $title, $repo, $time =
null ) {
56 # The null default value is only here to avoid an E_STRICT
57 if ( $time ===
null ) {
58 throw new InvalidArgumentException( __METHOD__ .
' got null for $time parameter' );
73 return new static(
$title,
$repo,
null, $archiveName );
85 $file =
new static(
$title,
$repo,
null, $row->oi_archive_name );
86 $file->loadFromRow( $row,
'oi_' );
103 public static function newFromKey( $sha1, $repo, $timestamp =
false ) {
104 $dbr =
$repo->getReplicaDB();
105 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr );
107 $queryBuilder->where( [
'oi_sha1' =>
$sha1 ] );
109 $queryBuilder->andWhere( [
'oi_timestamp' => $dbr->
timestamp( $timestamp ) ] );
112 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
114 return static::newFromRow( $row,
$repo );
141 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
142 $queryInfo = FileSelectQueryBuilder::newForOldFile( $dbr, $options )->getQueryInfo();
144 'tables' => $queryInfo[
'tables'],
145 'fields' => $queryInfo[
'fields'],
146 'joins' => $queryInfo[
'join_conds'],
158 public function __construct( $title, $repo, $time, $archiveName ) {
160 $this->requestedTime = $time;
161 $this->archive_name = $archiveName;
162 if ( $time ===
null && $archiveName ===
null ) {
163 throw new LogicException( __METHOD__ .
': must specify at least one of $time or $archiveName' );
168 $this->archive_name = $row->{
"{$prefix}archive_name"};
169 $this->deleted = $row->{
"{$prefix}deleted"};
171 unset( $row->{
"{$prefix}archive_name"} );
172 unset( $row->{
"{$prefix}deleted"} );
173 parent::loadFromRow( $row, $prefix );
189 if ( $this->archive_name ===
null ) {
193 return $this->archive_name;
215 $this->dataLoaded =
true;
217 $dbr = ( $flags & IDBAccessObject::READ_LATEST )
218 ? $this->repo->getPrimaryDB()
219 : $this->repo->getReplicaDB();
220 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr, [] );
221 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
223 $this->loadFromRow( $row,
'oi_' );
225 $this->fileExists =
false;
234 $this->extraDataLoaded =
true;
235 $dbr = $this->repo->getReplicaDB();
236 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
239 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
242 $dbr = $this->repo->getPrimaryDB();
243 $queryBuilder = $this->buildQueryBuilderForLoad( $dbr );
244 $row = $queryBuilder->caller( __METHOD__ )->fetchRow();
248 foreach ( $this->unprefixRow( $row,
'oi_' ) as $name => $value ) {
249 $this->$name = $value;
252 throw new RuntimeException(
"Could not find data for image '{$this->archive_name}'." );
256 private function buildQueryBuilderForLoad(
IReadableDatabase $dbr, $options = [
'omit-nonlazy' ] ) {
257 $queryBuilder = FileSelectQueryBuilder::newForOldFile( $dbr, $options );
258 $queryBuilder->where( [
'oi_name' => $this->getName() ] )
259 ->orderBy(
'oi_timestamp', SelectQueryBuilder::SORT_DESC );
260 if ( $this->requestedTime ===
null ) {
261 $queryBuilder->andWhere( [
'oi_archive_name' => $this->archive_name ] );
263 $queryBuilder->andWhere( [
'oi_timestamp' => $dbr->
timestamp( $this->requestedTime ) ] );
265 return $queryBuilder;
273 $fields = parent::getCacheFields( $prefix );
274 $fields[] = $prefix .
'archive_name';
275 $fields[] = $prefix .
'deleted';
285 return $this->getArchiveRel( $this->getArchiveName() );
293 return $this->getArchiveRel( rawurlencode( $this->getArchiveName() ) );
300 $this->loadFromFile();
302 # Don't destroy file info of missing files
303 if ( !$this->fileExists ) {
304 wfDebug( __METHOD__ .
": file does not exist, aborting" );
309 $dbw = $this->repo->getPrimaryDB();
310 [ $major, $minor ] = self::splitMime( $this->mime );
311 $metadata = $this->getMetadataForDb( $dbw );
313 wfDebug( __METHOD__ .
': upgrading ' . $this->archive_name .
" to the current schema" );
314 $dbw->newUpdateQueryBuilder()
315 ->update(
'oldimage' )
317 'oi_size' => $this->size,
318 'oi_width' => $this->width,
319 'oi_height' => $this->height,
320 'oi_bits' => $this->bits,
321 'oi_media_type' => $this->media_type,
322 'oi_major_mime' => $major,
323 'oi_minor_mime' => $minor,
324 'oi_metadata' => $metadata,
325 'oi_sha1' => $this->sha1,
328 'oi_name' => $this->getName(),
329 'oi_archive_name' => $this->archive_name,
331 ->caller( __METHOD__ )->execute();
333 $migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get(
334 MainConfigNames::FileSchemaMigrationStage
337 $dbw->newUpdateQueryBuilder()
338 ->update(
'filerevision' )
340 'fr_size' => $this->size,
341 'fr_width' => $this->width,
342 'fr_height' => $this->height,
343 'fr_bits' => $this->bits,
344 'fr_metadata' => $metadata,
345 'fr_sha1' => $this->sha1,
348 'fr_file' => $this->acquireFileIdFromName(),
349 'fr_archive_name' => $this->archive_name,
351 ->caller( __METHOD__ )->execute();
369 return ( $this->deleted & $field ) == $field;
379 return (
int)$this->deleted;
393 return RevisionRecord::userCanBitfield(
410 $archiveName = $this->getArchiveName();
411 $dstRel = $this->getArchiveRel( $archiveName );
412 $status = $this->publishTo( $srcPath, $dstRel );
414 if ( $status->isGood() &&
415 !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user )
417 $status->fatal(
'filenotfound', $srcPath );
434 protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
435 $dbw = $this->repo->getPrimaryDB();
437 $services = MediaWikiServices::getInstance();
438 $mwProps =
new MWFileProps( $services->getMimeAnalyzer() );
439 $props = $mwProps->getPropsFromPath( $srcPath,
true );
440 if ( !$props[
'fileExists'] ) {
443 $this->setProps( $props );
445 $dbw->startAtomic( __METHOD__ );
447 $commentFields = $services->getCommentStore()
448 ->insert( $dbw,
'oi_description', $comment );
449 $actorId = $services->getActorNormalization()
450 ->acquireActorId( $user, $dbw );
451 $dbw->newInsertQueryBuilder()
452 ->insertInto(
'oldimage' )
454 'oi_name' => $this->getName(),
455 'oi_archive_name' => $archiveName,
456 'oi_size' => $props[
'size'],
457 'oi_width' => intval( $props[
'width'] ),
458 'oi_height' => intval( $props[
'height'] ),
459 'oi_bits' => $props[
'bits'],
460 'oi_actor' => $actorId,
461 'oi_timestamp' => $dbw->timestamp( $timestamp ),
462 'oi_metadata' => $this->getMetadataForDb( $dbw ),
463 'oi_media_type' => $props[
'media_type'],
464 'oi_major_mime' => $props[
'major_mime'],
465 'oi_minor_mime' => $props[
'minor_mime'],
466 'oi_sha1' => $props[
'sha1'],
468 ->caller( __METHOD__ )->execute();
470 $migrationStage = MediaWikiServices::getInstance()->getMainConfig()->get(
471 MainConfigNames::FileSchemaMigrationStage
474 $commentFields = $services->getCommentStore()
475 ->insert( $dbw,
'fr_description', $comment );
476 $dbw->newInsertQueryBuilder()
477 ->insertInto(
'filerevision' )
480 'fr_file' => $this->acquireFileIdFromName(),
481 'fr_size' => $this->size,
482 'fr_width' => intval( $this->width ),
483 'fr_height' => intval( $this->height ),
484 'fr_bits' => $this->bits,
485 'fr_actor' => $actorId,
487 'fr_timestamp' => $dbw->timestamp( $timestamp ),
488 'fr_metadata' => $this->getMetadataForDb( $dbw ),
489 'fr_sha1' => $this->sha1
491 ->caller( __METHOD__ )->execute();
494 $dbw->endAtomic( __METHOD__ );
507 $archiveName = $this->getArchiveName();
508 if ( $archiveName ===
'' || !is_string( $archiveName ) ) {
511 return parent::exists();
const SCHEMA_COMPAT_WRITE_NEW
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
FileRepo LocalRepo ForeignAPIRepo false $repo
Some member variables can be lazy-initialised using __get().
Title string false $title
Local file in the wiki's own database.
string $sha1
SHA-1 base 36 content hash.
MimeMagic helper wrapper.
A class containing constants representing the names of configuration variables.
Old file in the oldimage table.
static newFromArchiveName( $title, $repo, $archiveName)
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
string null $archive_name
Archive name.
uploadOld( $srcPath, $timestamp, $comment, UserIdentity $user)
Upload a file directly into archive.
static newFromRow( $row, $repo)
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new oldlocalfile object.
__construct( $title, $repo, $time, $archiveName)
reserializeMetadata()
Write the metadata back to the database with the current serialization format.
static newFromKey( $sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class.
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row.
getVisibility()
Returns bitfield value.
static newFromTitle( $title, $repo, $time=null)
exists()
If archive name is an empty string, then file does not "exist".
getCacheFields( $prefix='img_')
Returns the list of object properties that are included as-is in the cache.to override string[] 1....
userCan( $field, Authority $performer)
Determine if the current user is allowed to view a particular field of this image file,...
string int $requestedTime
Timestamp.
loadExtraFromDB()
Load lazy file metadata from the DB.