Localisation updates: Adding/updating Persian translations
[mediawiki.git] / includes / filerepo / OldLocalFile.php
blob89e49c4c1d998a9aa58752076cd8ebde5d6be474
1 <?php
3 /**
4 * Class to represent a file in the oldimage table
6 * @ingroup FileRepo
7 */
8 class OldLocalFile extends LocalFile {
9 var $requestedTime, $archive_name;
11 const CACHE_VERSION = 1;
12 const MAX_CACHE_ROWS = 20;
14 static function newFromTitle( $title, $repo, $time = null ) {
15 # The null default value is only here to avoid an E_STRICT
16 if( $time === null )
17 throw new MWException( __METHOD__.' got null for $time parameter' );
18 return new self( $title, $repo, $time, null );
21 static function newFromArchiveName( $title, $repo, $archiveName ) {
22 return new self( $title, $repo, null, $archiveName );
25 static function newFromRow( $row, $repo ) {
26 $title = Title::makeTitle( NS_IMAGE, $row->oi_name );
27 $file = new self( $title, $repo, null, $row->oi_archive_name );
28 $file->loadFromRow( $row, 'oi_' );
29 return $file;
32 static function newFromKey( $sha1, $repo, $timestamp = false ) {
33 # Polymorphic function name to distinguish foreign and local fetches
34 $fname = get_class( $this ) . '::' . __FUNCTION__;
36 $conds = array( 'oi_sha1' => $sha1 );
37 if( $timestamp ) {
38 $conds['oi_timestamp'] = $timestamp;
40 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ), $conds, $fname );
41 if( $row ) {
42 return self::newFromRow( $row, $repo );
43 } else {
44 return false;
48 /**
49 * Fields in the oldimage table
51 static function selectFields() {
52 return array(
53 'oi_name',
54 'oi_archive_name',
55 'oi_size',
56 'oi_width',
57 'oi_height',
58 'oi_metadata',
59 'oi_bits',
60 'oi_media_type',
61 'oi_major_mime',
62 'oi_minor_mime',
63 'oi_description',
64 'oi_user',
65 'oi_user_text',
66 'oi_timestamp',
67 'oi_sha1',
71 /**
72 * @param Title $title
73 * @param FileRepo $repo
74 * @param string $time Timestamp or null to load by archive name
75 * @param string $archiveName Archive name or null to load by timestamp
77 function __construct( $title, $repo, $time, $archiveName ) {
78 parent::__construct( $title, $repo );
79 $this->requestedTime = $time;
80 $this->archive_name = $archiveName;
81 if ( is_null( $time ) && is_null( $archiveName ) ) {
82 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
86 function getCacheKey() {
87 return false;
90 function getArchiveName() {
91 if ( !isset( $this->archive_name ) ) {
92 $this->load();
94 return $this->archive_name;
97 function isOld() {
98 return true;
101 function isVisible() {
102 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
105 function loadFromDB() {
106 wfProfileIn( __METHOD__ );
107 $this->dataLoaded = true;
108 $dbr = $this->repo->getSlaveDB();
109 $conds = array( 'oi_name' => $this->getName() );
110 if ( is_null( $this->requestedTime ) ) {
111 $conds['oi_archive_name'] = $this->archive_name;
112 } else {
113 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
115 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
116 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
117 if ( $row ) {
118 $this->loadFromRow( $row, 'oi_' );
119 } else {
120 $this->fileExists = false;
122 wfProfileOut( __METHOD__ );
125 function getCacheFields( $prefix = 'img_' ) {
126 $fields = parent::getCacheFields( $prefix );
127 $fields[] = $prefix . 'archive_name';
128 $fields[] = $prefix . 'deleted';
129 return $fields;
132 function getRel() {
133 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
136 function getUrlRel() {
137 return 'archive/' . $this->getHashPath() . urlencode( $this->getArchiveName() );
140 function upgradeRow() {
141 wfProfileIn( __METHOD__ );
142 $this->loadFromFile();
144 # Don't destroy file info of missing files
145 if ( !$this->fileExists ) {
146 wfDebug( __METHOD__.": file does not exist, aborting\n" );
147 wfProfileOut( __METHOD__ );
148 return;
151 $dbw = $this->repo->getMasterDB();
152 list( $major, $minor ) = self::splitMime( $this->mime );
154 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
155 $dbw->update( 'oldimage',
156 array(
157 'oi_width' => $this->width,
158 'oi_height' => $this->height,
159 'oi_bits' => $this->bits,
160 'oi_media_type' => $this->media_type,
161 'oi_major_mime' => $major,
162 'oi_minor_mime' => $minor,
163 'oi_metadata' => $this->metadata,
164 'oi_sha1' => $this->sha1,
165 ), array(
166 'oi_name' => $this->getName(),
167 'oi_archive_name' => $this->archive_name ),
168 __METHOD__
170 wfProfileOut( __METHOD__ );
174 * int $field one of DELETED_* bitfield constants
175 * for file or revision rows
176 * @return bool
178 function isDeleted( $field ) {
179 return ($this->deleted & $field) == $field;
183 * Determine if the current user is allowed to view a particular
184 * field of this FileStore image file, if it's marked as deleted.
185 * @param int $field
186 * @return bool
188 function userCan( $field ) {
189 if( isset($this->deleted) && ($this->deleted & $field) == $field ) {
190 global $wgUser;
191 $permission = ( $this->deleted & File::DELETED_RESTRICTED ) == File::DELETED_RESTRICTED
192 ? 'suppressrevision'
193 : 'deleterevision';
194 wfDebug( "Checking for $permission due to $field match on $this->mDeleted\n" );
195 return $wgUser->isAllowed( $permission );
196 } else {
197 return true;