More __METHOD__ in our madness
[lhc/web/wiklou.git] / includes / filerepo / file / OldLocalFile.php
1 <?php
2 /**
3 * Old file in the oldimage table
4 *
5 * @file
6 * @ingroup FileAbstraction
7 */
8
9 /**
10 * Class to represent a file in the oldimage table
11 *
12 * @ingroup FileAbstraction
13 */
14 class OldLocalFile extends LocalFile {
15 var $requestedTime, $archive_name;
16
17 const CACHE_VERSION = 1;
18 const MAX_CACHE_ROWS = 20;
19
20 static function newFromTitle( $title, $repo, $time = null ) {
21 # The null default value is only here to avoid an E_STRICT
22 if ( $time === null ) {
23 throw new MWException( __METHOD__.' got null for $time parameter' );
24 }
25 return new self( $title, $repo, $time, null );
26 }
27
28 static function newFromArchiveName( $title, $repo, $archiveName ) {
29 return new self( $title, $repo, null, $archiveName );
30 }
31
32 static function newFromRow( $row, $repo ) {
33 $title = Title::makeTitle( NS_FILE, $row->oi_name );
34 $file = new self( $title, $repo, null, $row->oi_archive_name );
35 $file->loadFromRow( $row, 'oi_' );
36 return $file;
37 }
38
39 /**
40 * Create a OldLocalFile from a SHA-1 key
41 * Do not call this except from inside a repo class.
42 *
43 * @param $sha1 string base-36 SHA-1
44 * @param $repo LocalRepo
45 * @param string|bool $timestamp MW_timestamp (optional)
46 *
47 * @return bool|OldLocalFile
48 */
49 static function newFromKey( $sha1, $repo, $timestamp = false ) {
50 $dbr = $repo->getSlaveDB();
51
52 $conds = array( 'oi_sha1' => $sha1 );
53 if ( $timestamp ) {
54 $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
55 }
56
57 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
58 if ( $row ) {
59 return self::newFromRow( $row, $repo );
60 } else {
61 return false;
62 }
63 }
64
65 /**
66 * Fields in the oldimage table
67 * @return array
68 */
69 static function selectFields() {
70 return array(
71 'oi_name',
72 'oi_archive_name',
73 'oi_size',
74 'oi_width',
75 'oi_height',
76 'oi_metadata',
77 'oi_bits',
78 'oi_media_type',
79 'oi_major_mime',
80 'oi_minor_mime',
81 'oi_description',
82 'oi_user',
83 'oi_user_text',
84 'oi_timestamp',
85 'oi_deleted',
86 'oi_sha1',
87 );
88 }
89
90 /**
91 * @param $title Title
92 * @param $repo FileRepo
93 * @param $time String: timestamp or null to load by archive name
94 * @param $archiveName String: archive name or null to load by timestamp
95 */
96 function __construct( $title, $repo, $time, $archiveName ) {
97 parent::__construct( $title, $repo );
98 $this->requestedTime = $time;
99 $this->archive_name = $archiveName;
100 if ( is_null( $time ) && is_null( $archiveName ) ) {
101 throw new MWException( __METHOD__.': must specify at least one of $time or $archiveName' );
102 }
103 }
104
105 function getCacheKey() {
106 return false;
107 }
108
109 function getArchiveName() {
110 if ( !isset( $this->archive_name ) ) {
111 $this->load();
112 }
113 return $this->archive_name;
114 }
115
116 function isOld() {
117 return true;
118 }
119
120 function isVisible() {
121 return $this->exists() && !$this->isDeleted(File::DELETED_FILE);
122 }
123
124 function loadFromDB() {
125 wfProfileIn( __METHOD__ );
126 $this->dataLoaded = true;
127 $dbr = $this->repo->getSlaveDB();
128 $conds = array( 'oi_name' => $this->getName() );
129 if ( is_null( $this->requestedTime ) ) {
130 $conds['oi_archive_name'] = $this->archive_name;
131 } else {
132 $conds[] = 'oi_timestamp = ' . $dbr->addQuotes( $dbr->timestamp( $this->requestedTime ) );
133 }
134 $row = $dbr->selectRow( 'oldimage', $this->getCacheFields( 'oi_' ),
135 $conds, __METHOD__, array( 'ORDER BY' => 'oi_timestamp DESC' ) );
136 if ( $row ) {
137 $this->loadFromRow( $row, 'oi_' );
138 } else {
139 $this->fileExists = false;
140 }
141 wfProfileOut( __METHOD__ );
142 }
143
144 function getCacheFields( $prefix = 'img_' ) {
145 $fields = parent::getCacheFields( $prefix );
146 $fields[] = $prefix . 'archive_name';
147 $fields[] = $prefix . 'deleted';
148 return $fields;
149 }
150
151 function getRel() {
152 return 'archive/' . $this->getHashPath() . $this->getArchiveName();
153 }
154
155 function getUrlRel() {
156 return 'archive/' . $this->getHashPath() . rawurlencode( $this->getArchiveName() );
157 }
158
159 function upgradeRow() {
160 wfProfileIn( __METHOD__ );
161 $this->loadFromFile();
162
163 # Don't destroy file info of missing files
164 if ( !$this->fileExists ) {
165 wfDebug( __METHOD__.": file does not exist, aborting\n" );
166 wfProfileOut( __METHOD__ );
167 return;
168 }
169
170 $dbw = $this->repo->getMasterDB();
171 list( $major, $minor ) = self::splitMime( $this->mime );
172
173 wfDebug(__METHOD__.': upgrading '.$this->archive_name." to the current schema\n");
174 $dbw->update( 'oldimage',
175 array(
176 'oi_width' => $this->width,
177 'oi_height' => $this->height,
178 'oi_bits' => $this->bits,
179 'oi_media_type' => $this->media_type,
180 'oi_major_mime' => $major,
181 'oi_minor_mime' => $minor,
182 'oi_metadata' => $this->metadata,
183 'oi_sha1' => $this->sha1,
184 ), array(
185 'oi_name' => $this->getName(),
186 'oi_archive_name' => $this->archive_name ),
187 __METHOD__
188 );
189 wfProfileOut( __METHOD__ );
190 }
191
192 /**
193 * @param $field Integer: one of DELETED_* bitfield constants
194 * for file or revision rows
195 * @return bool
196 */
197 function isDeleted( $field ) {
198 $this->load();
199 return ($this->deleted & $field) == $field;
200 }
201
202 /**
203 * Returns bitfield value
204 * @return int
205 */
206 function getVisibility() {
207 $this->load();
208 return (int)$this->deleted;
209 }
210
211 /**
212 * Determine if the current user is allowed to view a particular
213 * field of this image file, if it's marked as deleted.
214 *
215 * @param $field Integer
216 * @param $user User object to check, or null to use $wgUser
217 * @return bool
218 */
219 function userCan( $field, User $user = null ) {
220 $this->load();
221 return Revision::userCanBitfield( $this->deleted, $field, $user );
222 }
223
224 /**
225 * Upload a file directly into archive. Generally for Special:Import.
226 *
227 * @param $srcPath string File system path of the source file
228 * @param $archiveName string Full archive name of the file, in the form
229 * $timestamp!$filename, where $filename must match $this->getName()
230 *
231 * @return FileRepoStatus
232 */
233 function uploadOld( $srcPath, $archiveName, $timestamp, $comment, $user, $flags = 0 ) {
234 $this->lock();
235
236 $dstRel = 'archive/' . $this->getHashPath() . $archiveName;
237 $status = $this->publishTo( $srcPath, $dstRel,
238 $flags & File::DELETE_SOURCE ? FileRepo::DELETE_SOURCE : 0
239 );
240
241 if ( $status->isGood() ) {
242 if ( !$this->recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) ) {
243 $status->fatal( 'filenotfound', $srcPath );
244 }
245 }
246
247 $this->unlock();
248
249 return $status;
250 }
251
252 /**
253 * Record a file upload in the oldimage table, without adding log entries.
254 *
255 * @param $srcPath string File system path to the source file
256 * @param $archiveName string The archive name of the file
257 * @param $comment string Upload comment
258 * @param $user User User who did this upload
259 * @return bool
260 */
261 function recordOldUpload( $srcPath, $archiveName, $timestamp, $comment, $user ) {
262 $dbw = $this->repo->getMasterDB();
263 $dbw->begin( __METHOD__ );
264
265 $dstPath = $this->repo->getZonePath( 'public' ) . '/' . $this->getRel();
266 $props = $this->repo->getFileProps( $dstPath );
267 if ( !$props['fileExists'] ) {
268 return false;
269 }
270
271 $dbw->insert( 'oldimage',
272 array(
273 'oi_name' => $this->getName(),
274 'oi_archive_name' => $archiveName,
275 'oi_size' => $props['size'],
276 'oi_width' => intval( $props['width'] ),
277 'oi_height' => intval( $props['height'] ),
278 'oi_bits' => $props['bits'],
279 'oi_timestamp' => $dbw->timestamp( $timestamp ),
280 'oi_description' => $comment,
281 'oi_user' => $user->getId(),
282 'oi_user_text' => $user->getName(),
283 'oi_metadata' => $props['metadata'],
284 'oi_media_type' => $props['media_type'],
285 'oi_major_mime' => $props['major_mime'],
286 'oi_minor_mime' => $props['minor_mime'],
287 'oi_sha1' => $props['sha1'],
288 ), __METHOD__
289 );
290
291 $dbw->commit( __METHOD__ );
292
293 return true;
294 }
295
296 }