<?php
+/**
+ * Old file in the oldimage table
+ *
+ * @file
+ * @ingroup FileRepo
+ */
/**
* Class to represent a file in the oldimage table
*
- * @addtogroup FileRepo
+ * @ingroup FileRepo
*/
class OldLocalFile extends LocalFile {
var $requestedTime, $archive_name;
}
static function newFromRow( $row, $repo ) {
- $title = Title::makeTitle( NS_IMAGE, $row->oi_name );
+ $title = Title::makeTitle( NS_FILE, $row->oi_name );
$file = new self( $title, $repo, null, $row->oi_archive_name );
$file->loadFromRow( $row, 'oi_' );
return $file;
}
/**
- * @param Title $title
- * @param FileRepo $repo
- * @param string $time Timestamp or null to load by archive name
- * @param string $archiveName Archive name or null to load by timestamp
+ * @param $sha1
+ * @param $repo LocalRepo
+ * @param bool $timestamp
+ * @return bool|OldLocalFile
+ */
+ static function newFromKey( $sha1, $repo, $timestamp = false ) {
+ $conds = array( 'oi_sha1' => $sha1 );
+ if( $timestamp ) {
+ $conds['oi_timestamp'] = $timestamp;
+ }
+ $dbr = $repo->getSlaveDB();
+ $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
+ if( $row ) {
+ return self::newFromRow( $row, $repo );
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Fields in the oldimage table
+ */
+ static function selectFields() {
+ return array(
+ 'oi_name',
+ 'oi_archive_name',
+ 'oi_size',
+ 'oi_width',
+ 'oi_height',
+ 'oi_metadata',
+ 'oi_bits',
+ 'oi_media_type',
+ 'oi_major_mime',
+ 'oi_minor_mime',
+ 'oi_description',
+ 'oi_user',
+ 'oi_user_text',
+ 'oi_timestamp',
+ 'oi_deleted',
+ 'oi_sha1',
+ );
+ }
+
+ /**
+ * @param $title Title
+ * @param $repo FileRepo
+ * @param $time String: timestamp or null to load by archive name
+ * @param $archiveName String: archive name or null to load by timestamp
*/
function __construct( $title, $repo, $time, $archiveName ) {
parent::__construct( $title, $repo );
function isOld() {
return true;
}
-
- function isVisible() {
+
+ function isVisible() {
return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
}
}
function getUrlRel() {
- return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
+ return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
}
-
+
function upgradeRow() {
wfProfileIn( __METHOD__ );
$this->loadFromFile();
-
+
# Don't destroy file info of missing files
if ( !$this->fileExists ) {
wfDebug( __METHOD__.": file does not exist, aborting\n" );
'oi_minor_mime' => $minor,
'oi_metadata' => $this->metadata,
'oi_sha1' => $this->sha1,
- ), array(
- 'oi_name' => $this->getName(),
+ ), array(
+ 'oi_name' => $this->getName(),
'oi_archive_name' => $this->archive_name ),
__METHOD__
);
wfProfileOut( __METHOD__ );
}
-
+
/**
- * int $field one of DELETED_* bitfield constants
- * for file or revision rows
+ * @param $field Integer: one of DELETED_* bitfield constants
+ * for file or revision rows
* @return bool
*/
function isDeleted( $field ) {
+ $this->load();
return ($this->deleted & $field) == $field;
}
-
+
+ /**
+ * Returns bitfield value
+ * @return int
+ */
+ function getVisibility() {
+ $this->load();
+ return (int)$this->deleted;
+ }
+
/**
* Determine if the current user is allowed to view a particular
- * field of this FileStore image file, if it's marked as deleted.
- * @param int $field
+ * field of this image file, if it's marked as deleted.
+ *
+ * @param $field Integer
* @return bool
*/
function userCan( $field ) {
- if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
- global $wgUser;
- $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
- ? 'hiderevision'
- : 'deleterevision';
- wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
- return $wgUser->isAllowed( $permission );
- } else {
- return true;
+ $this->load();
+ return Revision::userCanBitfield( $this->deleted, $field );
+ }
+
+ /**
+ * Upload a file directly into archive. Generally for Special:Import.
+ *
+ * @param $srcPath string File system path of the source file
+ * @param $archiveName string Full archive name of the file, in the form
+ * $timestamp!$filename, where $filename must match $this->getName()
+ *
+ * @return FileRepoStatus
+ */
+ function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
+ $this->lock();
+
+ $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
+ $status = $this->publishTo( $srcPath, $dstRel,
+ $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
+ );
+
+ if ( $status->isGood() ) {
+ if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
+ $status->fatal( 'filenotfound', $srcPath );
+ }
}
+
+ $this->unlock();
+
+ return $status;
}
+
+ /**
+ * Record a file upload in the oldimage table, without adding log entries.
+ *
+ * @param $srcPath string File system path to the source file
+ * @param $archiveName string The archive name of the file
+ * @param $comment string Upload comment
+ * @param $user User User who did this upload
+ * @return bool
+ */
+ function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
+ $dbw = $this->repo->getMasterDB();
+ $dbw->begin();
-}
+ $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
+ $props = self::getPropsFromPath( $dstPath );
+ if ( !$props['fileExists'] ) {
+ return false;
+ }
+ $dbw->insert( 'oldimage',
+ array(
+ 'oi_name' => $this->getName(),
+ 'oi_archive_name' => $archiveName,
+ 'oi_size' => $props['size'],
+ 'oi_width' => intval( $props['width'] ),
+ 'oi_height' => intval( $props['height'] ),
+ 'oi_bits' => $props['bits'],
+ 'oi_timestamp' => $dbw->timestamp( $timestamp ),
+ 'oi_description' => $comment,
+ 'oi_user' => $user->getId(),
+ 'oi_user_text' => $user->getName(),
+ 'oi_metadata' => $props['metadata'],
+ 'oi_media_type' => $props['media_type'],
+ 'oi_major_mime' => $props['major_mime'],
+ 'oi_minor_mime' => $props['minor_mime'],
+ 'oi_sha1' => $props['sha1'],
+ ), __METHOD__
+ );
+
+ $dbw->commit();
+ return true;
+ }
+
+}