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