MediaWiki  1.29.1
OldLocalFile.php
Go to the documentation of this file.
1 <?php
29 class OldLocalFile extends LocalFile {
31  protected $requestedTime;
32 
34  protected $archive_name;
35 
36  const CACHE_VERSION = 1;
37  const MAX_CACHE_ROWS = 20;
38 
46  static function newFromTitle( $title, $repo, $time = null ) {
47  # The null default value is only here to avoid an E_STRICT
48  if ( $time === null ) {
49  throw new MWException( __METHOD__ . ' got null for $time parameter' );
50  }
51 
52  return new self( $title, $repo, $time, null );
53  }
54 
61  static function newFromArchiveName( $title, $repo, $archiveName ) {
62  return new self( $title, $repo, null, $archiveName );
63  }
64 
70  static function newFromRow( $row, $repo ) {
71  $title = Title::makeTitle( NS_FILE, $row->oi_name );
72  $file = new self( $title, $repo, null, $row->oi_archive_name );
73  $file->loadFromRow( $row, 'oi_' );
74 
75  return $file;
76  }
77 
88  static function newFromKey( $sha1, $repo, $timestamp = false ) {
89  $dbr = $repo->getReplicaDB();
90 
91  $conds = [ 'oi_sha1' => $sha1 ];
92  if ( $timestamp ) {
93  $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
94  }
95 
96  $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
97  if ( $row ) {
98  return self::newFromRow( $row, $repo );
99  } else {
100  return false;
101  }
102  }
103 
108  static function selectFields() {
109  return [
110  'oi_name',
111  'oi_archive_name',
112  'oi_size',
113  'oi_width',
114  'oi_height',
115  'oi_metadata',
116  'oi_bits',
117  'oi_media_type',
118  'oi_major_mime',
119  'oi_minor_mime',
120  'oi_description',
121  'oi_user',
122  'oi_user_text',
123  'oi_timestamp',
124  'oi_deleted',
125  'oi_sha1',
126  ];
127  }
128 
136  function __construct( $title, $repo, $time, $archiveName ) {
137  parent::__construct( $title, $repo );
138  $this->requestedTime = $time;
139  $this->archive_name = $archiveName;
140  if ( is_null( $time ) && is_null( $archiveName ) ) {
141  throw new MWException( __METHOD__ . ': must specify at least one of $time or $archiveName' );
142  }
143  }
144 
148  function getCacheKey() {
149  return false;
150  }
151 
155  function getArchiveName() {
156  if ( !isset( $this->archive_name ) ) {
157  $this->load();
158  }
159 
160  return $this->archive_name;
161  }
162 
166  function isOld() {
167  return true;
168  }
169 
173  function isVisible() {
174  return $this->exists() && !$this->isDeleted( File::DELETED_FILE );
175  }
176 
177  function loadFromDB( $flags = 0 ) {
178  $this->dataLoaded = true;
179 
180  $dbr = ( $flags & self::READ_LATEST )
181  ? $this->repo->getMasterDB()
182  : $this->repo->getReplicaDB();
183 
184  $conds = [ 'oi_name' => $this->getName() ];
185  if ( is_null( $this->requestedTime ) ) {
186  $conds['oi_archive_name'] = $this->archive_name;
187  } else {
188  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
189  }
190  $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
191  $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
192  if ( $row ) {
193  $this->loadFromRow( $row, 'oi_' );
194  } else {
195  $this->fileExists = false;
196  }
197  }
198 
202  protected function loadExtraFromDB() {
203  $this->extraDataLoaded = true;
204  $dbr = $this->repo->getReplicaDB();
205  $conds = [ 'oi_name' => $this->getName() ];
206  if ( is_null( $this->requestedTime ) ) {
207  $conds['oi_archive_name'] = $this->archive_name;
208  } else {
209  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
210  }
211  // In theory the file could have just been renamed/deleted...oh well
212  $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
213  $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
214 
215  if ( !$row ) { // fallback to master
216  $dbr = $this->repo->getMasterDB();
217  $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
218  $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
219  }
220 
221  if ( $row ) {
222  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
223  $this->$name = $value;
224  }
225  } else {
226  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
227  }
228  }
229 
234  function getCacheFields( $prefix = 'img_' ) {
235  $fields = parent::getCacheFields( $prefix );
236  $fields[] = $prefix . 'archive_name';
237  $fields[] = $prefix . 'deleted';
238 
239  return $fields;
240  }
241 
245  function getRel() {
246  return 'archive/' . $this->getHashPath() . $this->getArchiveName();
247  }
248 
252  function getUrlRel() {
253  return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
254  }
255 
256  function upgradeRow() {
257  $this->loadFromFile();
258 
259  # Don't destroy file info of missing files
260  if ( !$this->fileExists ) {
261  wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
262 
263  return;
264  }
265 
266  $dbw = $this->repo->getMasterDB();
267  list( $major, $minor ) = self::splitMime( $this->mime );
268 
269  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
270  $dbw->update( 'oldimage',
271  [
272  'oi_size' => $this->size, // sanity
273  'oi_width' => $this->width,
274  'oi_height' => $this->height,
275  'oi_bits' => $this->bits,
276  'oi_media_type' => $this->media_type,
277  'oi_major_mime' => $major,
278  'oi_minor_mime' => $minor,
279  'oi_metadata' => $this->metadata,
280  'oi_sha1' => $this->sha1,
281  ], [
282  'oi_name' => $this->getName(),
283  'oi_archive_name' => $this->archive_name ],
284  __METHOD__
285  );
286  }
287 
293  function isDeleted( $field ) {
294  $this->load();
295 
296  return ( $this->deleted & $field ) == $field;
297  }
298 
303  function getVisibility() {
304  $this->load();
305 
306  return (int)$this->deleted;
307  }
308 
317  function userCan( $field, User $user = null ) {
318  $this->load();
319 
320  return Revision::userCanBitfield( $this->deleted, $field, $user );
321  }
322 
334  function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
335  $this->lock();
336 
337  $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
338  $status = $this->publishTo( $srcPath, $dstRel );
339 
340  if ( $status->isGood() ) {
341  if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
342  $status->fatal( 'filenotfound', $srcPath );
343  }
344  }
345 
346  $this->unlock();
347 
348  return $status;
349  }
350 
361  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
362  $dbw = $this->repo->getMasterDB();
363 
364  $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
365  $props = $this->repo->getFileProps( $dstPath );
366  if ( !$props['fileExists'] ) {
367  return false;
368  }
369 
370  $dbw->insert( 'oldimage',
371  [
372  'oi_name' => $this->getName(),
373  'oi_archive_name' => $archiveName,
374  'oi_size' => $props['size'],
375  'oi_width' => intval( $props['width'] ),
376  'oi_height' => intval( $props['height'] ),
377  'oi_bits' => $props['bits'],
378  'oi_timestamp' => $dbw->timestamp( $timestamp ),
379  'oi_description' => $comment,
380  'oi_user' => $user->getId(),
381  'oi_user_text' => $user->getName(),
382  'oi_metadata' => $props['metadata'],
383  'oi_media_type' => $props['media_type'],
384  'oi_major_mime' => $props['major_mime'],
385  'oi_minor_mime' => $props['minor_mime'],
386  'oi_sha1' => $props['sha1'],
387  ], __METHOD__
388  );
389 
390  return true;
391  }
392 
399  public function exists() {
400  $archiveName = $this->getArchiveName();
401  if ( $archiveName === '' || !is_string( $archiveName ) ) {
402  return false;
403  }
404  return parent::exists();
405  }
406 }
LocalFile\unprefixRow
unprefixRow( $row, $prefix='img_')
Definition: LocalFile.php:474
OldLocalFile\isOld
isOld()
Definition: OldLocalFile.php:166
File\$repo
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:96
OldLocalFile\CACHE_VERSION
const CACHE_VERSION
Definition: OldLocalFile.php:36
OldLocalFile\newFromKey
static newFromKey( $sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class.
Definition: OldLocalFile.php:88
LocalFile\unlock
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero.
Definition: LocalFile.php:2050
OldLocalFile\uploadOld
uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user)
Upload a file directly into archive.
Definition: OldLocalFile.php:334
Revision\userCanBitfield
static userCanBitfield( $bitfield, $field, User $user=null, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: Revision.php:1779
OldLocalFile\isDeleted
isDeleted( $field)
Definition: OldLocalFile.php:293
OldLocalFile\getVisibility
getVisibility()
Returns bitfield value.
Definition: OldLocalFile.php:303
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
LocalFile\$user
int $user
User ID of uploader.
Definition: LocalFile.php:105
NS_FILE
const NS_FILE
Definition: Defines.php:68
OldLocalFile\exists
exists()
If archive name is an empty string, then file does not "exist".
Definition: OldLocalFile.php:399
OldLocalFile\selectFields
static selectFields()
Fields in the oldimage table.
Definition: OldLocalFile.php:108
LocalFile\$sha1
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:75
File\splitMime
static splitMime( $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition: File.php:273
OldLocalFile\recordOldUpload
recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
Definition: OldLocalFile.php:361
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
OldLocalFile\$requestedTime
string $requestedTime
Timestamp.
Definition: OldLocalFile.php:31
LocalFile\publishTo
publishTo( $src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
Definition: LocalFile.php:1620
MWException
MediaWiki exception.
Definition: MWException.php:26
OldLocalFile\$archive_name
string $archive_name
Archive name.
Definition: OldLocalFile.php:34
OldLocalFile\upgradeRow
upgradeRow()
Fix assorted version-related problems with the image row by reloading it from the file.
Definition: OldLocalFile.php:256
LocalFile\loadFromRow
loadFromRow( $row, $prefix='img_')
Load file metadata from a DB result row.
Definition: LocalFile.php:535
LocalFile\$deleted
int $deleted
Bitfield akin to rev_deleted.
Definition: LocalFile.php:84
OldLocalFile\loadExtraFromDB
loadExtraFromDB()
Load lazy file metadata from the DB.
Definition: OldLocalFile.php:202
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1769
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
OldLocalFile\__construct
__construct( $title, $repo, $time, $archiveName)
Definition: OldLocalFile.php:136
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:999
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:45
$value
$value
Definition: styleTest.css.php:45
LocalFile\lock
lock()
Start an atomic DB section and lock the image for update or increments a reference counter if the loc...
Definition: LocalFile.php:2005
LocalFile\load
load( $flags=0)
Load file metadata from cache or DB, unless already loaded.
Definition: LocalFile.php:553
File\$title
Title string bool $title
Definition: File.php:99
File\getName
getName()
Return the name of this file.
Definition: File.php:297
LocalFile\$timestamp
string $timestamp
Upload timestamp.
Definition: LocalFile.php:102
OldLocalFile\isVisible
isVisible()
Definition: OldLocalFile.php:173
OldLocalFile\newFromRow
static newFromRow( $row, $repo)
Definition: OldLocalFile.php:70
OldLocalFile\getCacheFields
getCacheFields( $prefix='img_')
Definition: OldLocalFile.php:234
OldLocalFile\getRel
getRel()
Definition: OldLocalFile.php:245
OldLocalFile\getUrlRel
getUrlRel()
Definition: OldLocalFile.php:252
LocalFile\getLazyCacheFields
getLazyCacheFields( $prefix='img_')
Definition: LocalFile.php:372
OldLocalFile\newFromTitle
static newFromTitle( $title, $repo, $time=null)
Definition: OldLocalFile.php:46
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
LocalFile\loadFromFile
loadFromFile()
Load metadata from the file itself.
Definition: LocalFile.php:338
OldLocalFile\userCan
userCan( $field, User $user=null)
Determine if the current user is allowed to view a particular field of this image file,...
Definition: OldLocalFile.php:317
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
width
width
Definition: parserTests.txt:155
File\$name
string $name
The name of a file from its title object.
Definition: File.php:123
File\DELETED_FILE
const DELETED_FILE
Definition: File.php:53
OldLocalFile\getCacheKey
getCacheKey()
Definition: OldLocalFile.php:148
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
OldLocalFile\getArchiveName
getArchiveName()
Definition: OldLocalFile.php:155
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:29
OldLocalFile\newFromArchiveName
static newFromArchiveName( $title, $repo, $archiveName)
Definition: OldLocalFile.php:61
OldLocalFile\loadFromDB
loadFromDB( $flags=0)
Load file metadata from the DB.
Definition: OldLocalFile.php:177
File\getHashPath
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1497
$flags
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2749
OldLocalFile\MAX_CACHE_ROWS
const MAX_CACHE_ROWS
Definition: OldLocalFile.php:37