Merge "set url used in createaccount-text to main page canonical url"
[lhc/web/wiklou.git] / includes / api / ApiQueryFilearchive.php
1 <?php
2 /**
3 * API for MediaWiki 1.12+
4 *
5 * Created on May 10, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all deleted files.
31 *
32 * @ingroup API
33 */
34 class ApiQueryFilearchive extends ApiQueryBase {
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'fa' );
38 }
39
40 public function execute() {
41 $user = $this->getUser();
42 // Before doing anything at all, let's check permissions
43 if ( !$user->isAllowed( 'deletedhistory' ) ) {
44 $this->dieUsage(
45 'You don\'t have permission to view deleted file information',
46 'permissiondenied'
47 );
48 }
49
50 $db = $this->getDB();
51
52 $params = $this->extractRequestParams();
53
54 $prop = array_flip( $params['prop'] );
55 $fld_sha1 = isset( $prop['sha1'] );
56 $fld_timestamp = isset( $prop['timestamp'] );
57 $fld_user = isset( $prop['user'] );
58 $fld_size = isset( $prop['size'] );
59 $fld_dimensions = isset( $prop['dimensions'] );
60 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
61 $fld_mime = isset( $prop['mime'] );
62 $fld_mediatype = isset( $prop['mediatype'] );
63 $fld_metadata = isset( $prop['metadata'] );
64 $fld_bitdepth = isset( $prop['bitdepth'] );
65 $fld_archivename = isset( $prop['archivename'] );
66
67 $this->addTables( 'filearchive' );
68
69 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
70 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
71 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
72 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
73 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
74 $this->addFieldsIf( 'fa_description', $fld_description );
75 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
76 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
77 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
78 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
79 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
80
81 if ( !is_null( $params['continue'] ) ) {
82 $cont = explode( '|', $params['continue'] );
83 $this->dieContinueUsageIf( count( $cont ) != 1 );
84 $op = $params['dir'] == 'descending' ? '<' : '>';
85 $cont_from = $db->addQuotes( $cont[0] );
86 $this->addWhere( "fa_name $op= $cont_from" );
87 }
88
89 // Image filters
90 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
91 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
92 if ( !is_null( $params['continue'] ) ) {
93 $from = $params['continue'];
94 }
95 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
96 $this->addWhereRange( 'fa_name', $dir, $from, $to );
97 if ( isset( $params['prefix'] ) ) {
98 $this->addWhere( 'fa_name' .
99 $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
100 }
101
102 $sha1Set = isset( $params['sha1'] );
103 $sha1base36Set = isset( $params['sha1base36'] );
104 if ( $sha1Set || $sha1base36Set ) {
105 $sha1 = false;
106 if ( $sha1Set ) {
107 $sha1 = strtolower( $params['sha1'] );
108 if ( !$this->validateSha1Hash( $sha1 ) ) {
109 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
110 }
111 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
112 } elseif ( $sha1base36Set ) {
113 $sha1 = strtolower( $params['sha1base36'] );
114 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
115 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
116 }
117 }
118 if ( $sha1 ) {
119 $this->addWhereFld( 'fa_sha1', $sha1 );
120 }
121 }
122
123 if ( !$user->isAllowed( 'suppressrevision' ) ) {
124 // Filter out revisions that the user is not allowed to see. There
125 // is no way to indicate that we have skipped stuff because the
126 // continuation parameter is fa_name
127
128 // Note that this field is unindexed. This should however not be
129 // a big problem as files with fa_deleted are rare
130 $this->addWhereFld( 'fa_deleted', 0 );
131 }
132
133 $limit = $params['limit'];
134 $this->addOption( 'LIMIT', $limit + 1 );
135 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
136 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
137
138 $res = $this->select( __METHOD__ );
139
140 $count = 0;
141 $result = $this->getResult();
142 foreach ( $res as $row ) {
143 if ( ++$count > $limit ) {
144 // We've reached the one extra which shows that there are
145 // additional pages to be had. Stop here...
146 $this->setContinueEnumParameter( 'continue', $row->fa_name );
147 break;
148 }
149
150 $file = array();
151 $file['name'] = $row->fa_name;
152 $title = Title::makeTitle( NS_FILE, $row->fa_name );
153 self::addTitleInfo( $file, $title );
154
155 if ( $fld_sha1 ) {
156 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
157 }
158 if ( $fld_timestamp ) {
159 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
160 }
161 if ( $fld_user ) {
162 $file['userid'] = $row->fa_user;
163 $file['user'] = $row->fa_user_text;
164 }
165 if ( $fld_size || $fld_dimensions ) {
166 $file['size'] = $row->fa_size;
167
168 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
169 if ( $pageCount !== false ) {
170 $vals['pagecount'] = $pageCount;
171 }
172
173 $file['height'] = $row->fa_height;
174 $file['width'] = $row->fa_width;
175 }
176 if ( $fld_description ) {
177 $file['description'] = $row->fa_description;
178 if ( isset( $prop['parseddescription'] ) ) {
179 $file['parseddescription'] = Linker::formatComment(
180 $row->fa_description, $title );
181 }
182 }
183 if ( $fld_mediatype ) {
184 $file['mediatype'] = $row->fa_media_type;
185 }
186 if ( $fld_metadata ) {
187 $file['metadata'] = $row->fa_metadata
188 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
189 : null;
190 }
191 if ( $fld_bitdepth ) {
192 $file['bitdepth'] = $row->fa_bits;
193 }
194 if ( $fld_mime ) {
195 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
196 }
197 if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
198 $file['archivename'] = $row->fa_archive_name;
199 }
200
201 if ( $row->fa_deleted & File::DELETED_FILE ) {
202 $file['filehidden'] = '';
203 }
204 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
205 $file['commenthidden'] = '';
206 }
207 if ( $row->fa_deleted & File::DELETED_USER ) {
208 $file['userhidden'] = '';
209 }
210 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
211 // This file is deleted for normal admins
212 $file['suppressed'] = '';
213 }
214
215 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
216 if ( !$fit ) {
217 $this->setContinueEnumParameter( 'continue', $row->fa_name );
218 break;
219 }
220 }
221
222 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
223 }
224
225 public function getAllowedParams() {
226 return array(
227 'from' => null,
228 'continue' => null,
229 'to' => null,
230 'prefix' => null,
231 'limit' => array(
232 ApiBase::PARAM_DFLT => 10,
233 ApiBase::PARAM_TYPE => 'limit',
234 ApiBase::PARAM_MIN => 1,
235 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
236 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
237 ),
238 'dir' => array(
239 ApiBase::PARAM_DFLT => 'ascending',
240 ApiBase::PARAM_TYPE => array(
241 'ascending',
242 'descending'
243 )
244 ),
245 'sha1' => null,
246 'sha1base36' => null,
247 'prop' => array(
248 ApiBase::PARAM_DFLT => 'timestamp',
249 ApiBase::PARAM_ISMULTI => true,
250 ApiBase::PARAM_TYPE => array(
251 'sha1',
252 'timestamp',
253 'user',
254 'size',
255 'dimensions',
256 'description',
257 'parseddescription',
258 'mime',
259 'mediatype',
260 'metadata',
261 'bitdepth',
262 'archivename',
263 ),
264 ),
265 );
266 }
267
268 public function getParamDescription() {
269 return array(
270 'from' => 'The image title to start enumerating from',
271 'continue' => 'When more results are available, use this to continue',
272 'to' => 'The image title to stop enumerating at',
273 'prefix' => 'Search for all image titles that begin with this value',
274 'dir' => 'The direction in which to list',
275 'limit' => 'How many images to return in total',
276 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
277 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
278 'prop' => array(
279 'What image information to get:',
280 ' sha1 - Adds SHA-1 hash for the image',
281 ' timestamp - Adds timestamp for the uploaded version',
282 ' user - Adds user who uploaded the image version',
283 ' size - Adds the size of the image in bytes and the height, ' .
284 'width and page count (if applicable)',
285 ' dimensions - Alias for size',
286 ' description - Adds description the image version',
287 ' parseddescription - Parse the description on the version',
288 ' mime - Adds MIME of the image',
289 ' mediatype - Adds the media type of the image',
290 ' metadata - Lists Exif metadata for the version of the image',
291 ' bitdepth - Adds the bit depth of the version',
292 ' archivename - Adds the file name of the archive version for non-latest versions'
293 ),
294 );
295 }
296
297 public function getResultProperties() {
298 return array(
299 '' => array(
300 'name' => 'string',
301 'ns' => 'namespace',
302 'title' => 'string',
303 'filehidden' => 'boolean',
304 'commenthidden' => 'boolean',
305 'userhidden' => 'boolean',
306 'suppressed' => 'boolean'
307 ),
308 'sha1' => array(
309 'sha1' => 'string'
310 ),
311 'timestamp' => array(
312 'timestamp' => 'timestamp'
313 ),
314 'user' => array(
315 'userid' => 'integer',
316 'user' => 'string'
317 ),
318 'size' => array(
319 'size' => 'integer',
320 'pagecount' => array(
321 ApiBase::PROP_TYPE => 'integer',
322 ApiBase::PROP_NULLABLE => true
323 ),
324 'height' => 'integer',
325 'width' => 'integer'
326 ),
327 'dimensions' => array(
328 'size' => 'integer',
329 'pagecount' => array(
330 ApiBase::PROP_TYPE => 'integer',
331 ApiBase::PROP_NULLABLE => true
332 ),
333 'height' => 'integer',
334 'width' => 'integer'
335 ),
336 'description' => array(
337 'description' => 'string'
338 ),
339 'parseddescription' => array(
340 'description' => 'string',
341 'parseddescription' => 'string'
342 ),
343 'metadata' => array(
344 'metadata' => 'string'
345 ),
346 'bitdepth' => array(
347 'bitdepth' => 'integer'
348 ),
349 'mime' => array(
350 'mime' => 'string'
351 ),
352 'mediatype' => array(
353 'mediatype' => 'string'
354 ),
355 'archivename' => array(
356 'archivename' => 'string'
357 ),
358 );
359 }
360
361 public function getDescription() {
362 return 'Enumerate all deleted files sequentially';
363 }
364
365 public function getPossibleErrors() {
366 return array_merge( parent::getPossibleErrors(), array(
367 array(
368 'code' => 'permissiondenied',
369 'info' => 'You don\'t have permission to view deleted file information'
370 ),
371 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
372 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA-1 hash provided is not valid' ),
373 array(
374 'code' => 'invalidsha1base36hash',
375 'info' => 'The SHA1Base36 hash provided is not valid'
376 ),
377 ) );
378 }
379
380 public function getExamples() {
381 return array(
382 'api.php?action=query&list=filearchive' => array(
383 'Simple Use',
384 'Show a list of all deleted files',
385 ),
386 );
387 }
388
389 public function getHelpUrls() {
390 return 'https://www.mediawiki.org/wiki/API:Filearchive';
391 }
392 }