MediaWiki  1.28.0
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->getSlaveDB();
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->getSlaveDB();
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  }
199 
203  protected function loadExtraFromDB() {
204 
205  $this->extraDataLoaded = true;
206  $dbr = $this->repo->getSlaveDB();
207  $conds = [ 'oi_name' => $this->getName() ];
208  if ( is_null( $this->requestedTime ) ) {
209  $conds['oi_archive_name'] = $this->archive_name;
210  } else {
211  $conds['oi_timestamp'] = $dbr->timestamp( $this->requestedTime );
212  }
213  // In theory the file could have just been renamed/deleted...oh well
214  $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
215  $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
216 
217  if ( !$row ) { // fallback to master
218  $dbr = $this->repo->getMasterDB();
219  $row = $dbr->selectRow( 'oldimage', $this->getLazyCacheFields( 'oi_' ),
220  $conds, __METHOD__, [ 'ORDER BY' => 'oi_timestamp DESC' ] );
221  }
222 
223  if ( $row ) {
224  foreach ( $this->unprefixRow( $row, 'oi_' ) as $name => $value ) {
225  $this->$name = $value;
226  }
227  } else {
228  throw new MWException( "Could not find data for image '{$this->archive_name}'." );
229  }
230 
231  }
232 
237  function getCacheFields( $prefix = 'img_' ) {
238  $fields = parent::getCacheFields( $prefix );
239  $fields[] = $prefix . 'archive_name';
240  $fields[] = $prefix . 'deleted';
241 
242  return $fields;
243  }
244 
248  function getRel() {
249  return 'archive/' . $this->getHashPath() . $this->getArchiveName();
250  }
251 
255  function getUrlRel() {
256  return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
257  }
258 
259  function upgradeRow() {
260  $this->loadFromFile();
261 
262  # Don't destroy file info of missing files
263  if ( !$this->fileExists ) {
264  wfDebug( __METHOD__ . ": file does not exist, aborting\n" );
265 
266  return;
267  }
268 
269  $dbw = $this->repo->getMasterDB();
270  list( $major, $minor ) = self::splitMime( $this->mime );
271 
272  wfDebug( __METHOD__ . ': upgrading ' . $this->archive_name . " to the current schema\n" );
273  $dbw->update( 'oldimage',
274  [
275  'oi_size' => $this->size, // sanity
276  'oi_width' => $this->width,
277  'oi_height' => $this->height,
278  'oi_bits' => $this->bits,
279  'oi_media_type' => $this->media_type,
280  'oi_major_mime' => $major,
281  'oi_minor_mime' => $minor,
282  'oi_metadata' => $this->metadata,
283  'oi_sha1' => $this->sha1,
284  ], [
285  'oi_name' => $this->getName(),
286  'oi_archive_name' => $this->archive_name ],
287  __METHOD__
288  );
289  }
290 
296  function isDeleted( $field ) {
297  $this->load();
298 
299  return ( $this->deleted & $field ) == $field;
300  }
301 
306  function getVisibility() {
307  $this->load();
308 
309  return (int)$this->deleted;
310  }
311 
320  function userCan( $field, User $user = null ) {
321  $this->load();
322 
323  return Revision::userCanBitfield( $this->deleted, $field, $user );
324  }
325 
337  function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user ) {
338  $this->lock();
339 
340  $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
341  $status = $this->publishTo( $srcPath, $dstRel );
342 
343  if ( $status->isGood() ) {
344  if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
345  $status->fatal( 'filenotfound', $srcPath );
346  }
347  }
348 
349  $this->unlock();
350 
351  return $status;
352  }
353 
364  protected function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
365  $dbw = $this->repo->getMasterDB();
366 
367  $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
368  $props = $this->repo->getFileProps( $dstPath );
369  if ( !$props['fileExists'] ) {
370  return false;
371  }
372 
373  $dbw->insert( 'oldimage',
374  [
375  'oi_name' => $this->getName(),
376  'oi_archive_name' => $archiveName,
377  'oi_size' => $props['size'],
378  'oi_width' => intval( $props['width'] ),
379  'oi_height' => intval( $props['height'] ),
380  'oi_bits' => $props['bits'],
381  'oi_timestamp' => $dbw->timestamp( $timestamp ),
382  'oi_description' => $comment,
383  'oi_user' => $user->getId(),
384  'oi_user_text' => $user->getName(),
385  'oi_metadata' => $props['metadata'],
386  'oi_media_type' => $props['media_type'],
387  'oi_major_mime' => $props['major_mime'],
388  'oi_minor_mime' => $props['minor_mime'],
389  'oi_sha1' => $props['sha1'],
390  ], __METHOD__
391  );
392 
393  return true;
394  }
395 
402  public function exists() {
403  $archiveName = $this->getArchiveName();
404  if ( $archiveName === '' || !is_string( $archiveName ) ) {
405  return false;
406  }
407  return parent::exists();
408  }
409 }
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
loadFromRow($row, $prefix= 'img_')
Load file metadata from a DB result row.
Definition: LocalFile.php:524
static newFromArchiveName($title, $repo, $archiveName)
width
$comment
unlock()
Decrement the lock reference count and end the atomic section if it reaches zero. ...
Definition: LocalFile.php:2037
string $sha1
SHA-1 base 36 content hash.
Definition: LocalFile.php:73
$value
const MAX_CACHE_ROWS
getName()
Return the name of this file.
Definition: File.php:296
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2703
string $name
The name of a file from its title object.
Definition: File.php:122
getCacheFields($prefix= 'img_')
unprefixRow($row, $prefix= 'img_')
Definition: LocalFile.php:463
int $user
User ID of uploader.
Definition: LocalFile.php:103
recordOldUpload($srcPath, $archiveName, $timestamp, $comment, $user)
Record a file upload in the oldimage table, without adding log entries.
static newFromTitle($title, $repo, $time=null)
isDeleted($field)
Title string bool $title
Definition: File.php:98
getHashPath()
Get the filename hash component of the directory including trailing slash, e.g.
Definition: File.php:1496
exists()
If archive name is an empty string, then file does not "exist".
Class to represent a file in the oldimage table.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
const DELETED_FILE
Definition: File.php:52
userCan($field, User $user=null)
Determine if the current user is allowed to view a particular field of this image file...
publishTo($src, $dstRel, $flags=0, array $options=[])
Move or copy a file to a specified location.
Definition: LocalFile.php:1607
static selectFields()
Fields in the oldimage table.
const NS_FILE
Definition: Defines.php:62
FileRepo LocalRepo ForeignAPIRepo bool $repo
Some member variables can be lazy-initialised using __get().
Definition: File.php:95
uploadOld($srcPath, $archiveName, $timestamp, $comment, $user)
Upload a file directly into archive.
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
static newFromRow($row, $repo)
const CACHE_VERSION
string $requestedTime
Timestamp.
string $archive_name
Archive name.
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
lock()
Start an atomic DB section and lock the image for update or increments a reference counter if the loc...
Definition: LocalFile.php:1992
__construct($title, $repo, $time, $archiveName)
int $deleted
Bitfield akin to rev_deleted.
Definition: LocalFile.php:82
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:43
loadFromDB($flags=0)
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
static newFromKey($sha1, $repo, $timestamp=false)
Create a OldLocalFile from a SHA-1 key Do not call this except from inside a repo class...
string $timestamp
Upload timestamp.
Definition: LocalFile.php:100
getLazyCacheFields($prefix= 'img_')
Definition: LocalFile.php:361
loadFromFile()
Load metadata from the file itself.
Definition: LocalFile.php:327
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, if it's marked as deleted.
Definition: Revision.php:1764
load($flags=0)
Load file metadata from cache or DB, unless already loaded.
Definition: LocalFile.php:542
getVisibility()
Returns bitfield value.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1749
loadExtraFromDB()
Load lazy file metadata from the DB.