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