Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / api / ApiQueryFilearchive.php
blobf53cd386732ff0582ea2672d368ae8719f7c92c0
1 <?php
2 /**
3 * API for MediaWiki 1.12+
5 * Created on May 10, 2010
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
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.
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.
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
26 * @file
29 /**
30 * Query module to enumerate all deleted files.
32 * @ingroup API
34 class ApiQueryFilearchive extends ApiQueryBase {
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'fa' );
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( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
47 $db = $this->getDB();
49 $params = $this->extractRequestParams();
51 $prop = array_flip( $params['prop'] );
52 $fld_sha1 = isset( $prop['sha1'] );
53 $fld_timestamp = isset( $prop['timestamp'] );
54 $fld_user = isset( $prop['user'] );
55 $fld_size = isset( $prop['size'] );
56 $fld_dimensions = isset( $prop['dimensions'] );
57 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
58 $fld_mime = isset( $prop['mime'] );
59 $fld_mediatype = isset( $prop['mediatype'] );
60 $fld_metadata = isset( $prop['metadata'] );
61 $fld_bitdepth = isset( $prop['bitdepth'] );
62 $fld_archivename = isset( $prop['archivename'] );
64 $this->addTables( 'filearchive' );
66 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
67 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
68 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
69 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
70 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
71 $this->addFieldsIf( 'fa_description', $fld_description );
72 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
73 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
74 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
75 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
76 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
78 if ( !is_null( $params['continue'] ) ) {
79 $cont = explode( '|', $params['continue'] );
80 $this->dieContinueUsageIf( count( $cont ) != 1 );
81 $op = $params['dir'] == 'descending' ? '<' : '>';
82 $cont_from = $db->addQuotes( $cont[0] );
83 $this->addWhere( "fa_name $op= $cont_from" );
86 // Image filters
87 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
88 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
89 if ( !is_null( $params['continue'] ) ) {
90 $from = $params['continue'];
92 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
93 $this->addWhereRange( 'fa_name', $dir, $from, $to );
94 if ( isset( $params['prefix'] ) ) {
95 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
98 $sha1Set = isset( $params['sha1'] );
99 $sha1base36Set = isset( $params['sha1base36'] );
100 if ( $sha1Set || $sha1base36Set ) {
101 $sha1 = false;
102 if ( $sha1Set ) {
103 $sha1 = strtolower( $params['sha1'] );
104 if ( !$this->validateSha1Hash( $sha1 ) ) {
105 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
107 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
108 } elseif ( $sha1base36Set ) {
109 $sha1 = strtolower( $params['sha1base36'] );
110 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
111 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
114 if ( $sha1 ) {
115 $this->addWhereFld( 'fa_sha1', $sha1 );
119 if ( !$user->isAllowed( 'suppressrevision' ) ) {
120 // Filter out revisions that the user is not allowed to see. There
121 // is no way to indicate that we have skipped stuff because the
122 // continuation parameter is fa_name
124 // Note that this field is unindexed. This should however not be
125 // a big problem as files with fa_deleted are rare
126 $this->addWhereFld( 'fa_deleted', 0 );
129 $limit = $params['limit'];
130 $this->addOption( 'LIMIT', $limit + 1 );
131 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
132 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
134 $res = $this->select( __METHOD__ );
136 $count = 0;
137 $result = $this->getResult();
138 foreach ( $res as $row ) {
139 if ( ++$count > $limit ) {
140 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
141 $this->setContinueEnumParameter( 'continue', $row->fa_name );
142 break;
145 $file = array();
146 $file['name'] = $row->fa_name;
147 $title = Title::makeTitle( NS_FILE, $row->fa_name );
148 self::addTitleInfo( $file, $title );
150 if ( $fld_sha1 ) {
151 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
153 if ( $fld_timestamp ) {
154 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
156 if ( $fld_user ) {
157 $file['userid'] = $row->fa_user;
158 $file['user'] = $row->fa_user_text;
160 if ( $fld_size || $fld_dimensions ) {
161 $file['size'] = $row->fa_size;
163 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
164 if ( $pageCount !== false ) {
165 $vals['pagecount'] = $pageCount;
168 $file['height'] = $row->fa_height;
169 $file['width'] = $row->fa_width;
171 if ( $fld_description ) {
172 $file['description'] = $row->fa_description;
173 if ( isset( $prop['parseddescription'] ) ) {
174 $file['parseddescription'] = Linker::formatComment(
175 $row->fa_description, $title );
178 if ( $fld_mediatype ) {
179 $file['mediatype'] = $row->fa_media_type;
181 if ( $fld_metadata ) {
182 $file['metadata'] = $row->fa_metadata
183 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
184 : null;
186 if ( $fld_bitdepth ) {
187 $file['bitdepth'] = $row->fa_bits;
189 if ( $fld_mime ) {
190 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
192 if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
193 $file['archivename'] = $row->fa_archive_name;
196 if ( $row->fa_deleted & File::DELETED_FILE ) {
197 $file['filehidden'] = '';
199 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
200 $file['commenthidden'] = '';
202 if ( $row->fa_deleted & File::DELETED_USER ) {
203 $file['userhidden'] = '';
205 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
206 // This file is deleted for normal admins
207 $file['suppressed'] = '';
210 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
211 if ( !$fit ) {
212 $this->setContinueEnumParameter( 'continue', $row->fa_name );
213 break;
217 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
220 public function getAllowedParams() {
221 return array(
222 'from' => null,
223 'continue' => null,
224 'to' => null,
225 'prefix' => null,
226 'limit' => array(
227 ApiBase::PARAM_DFLT => 10,
228 ApiBase::PARAM_TYPE => 'limit',
229 ApiBase::PARAM_MIN => 1,
230 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
231 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
233 'dir' => array(
234 ApiBase::PARAM_DFLT => 'ascending',
235 ApiBase::PARAM_TYPE => array(
236 'ascending',
237 'descending'
240 'sha1' => null,
241 'sha1base36' => null,
242 'prop' => array(
243 ApiBase::PARAM_DFLT => 'timestamp',
244 ApiBase::PARAM_ISMULTI => true,
245 ApiBase::PARAM_TYPE => array(
246 'sha1',
247 'timestamp',
248 'user',
249 'size',
250 'dimensions',
251 'description',
252 'parseddescription',
253 'mime',
254 'mediatype',
255 'metadata',
256 'bitdepth',
257 'archivename',
263 public function getParamDescription() {
264 return array(
265 'from' => 'The image title to start enumerating from',
266 'continue' => 'When more results are available, use this to continue',
267 'to' => 'The image title to stop enumerating at',
268 'prefix' => 'Search for all image titles that begin with this value',
269 'dir' => 'The direction in which to list',
270 'limit' => 'How many images to return in total',
271 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
272 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
273 'prop' => array(
274 'What image information to get:',
275 ' sha1 - Adds SHA-1 hash for the image',
276 ' timestamp - Adds timestamp for the uploaded version',
277 ' user - Adds user who uploaded the image version',
278 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
279 ' dimensions - Alias for size',
280 ' description - Adds description the image version',
281 ' parseddescription - Parse the description on the version',
282 ' mime - Adds MIME of the image',
283 ' mediatype - Adds the media type of the image',
284 ' metadata - Lists Exif metadata for the version of the image',
285 ' bitdepth - Adds the bit depth of the version',
286 ' archivename - Adds the file name of the archive version for non-latest versions'
291 public function getResultProperties() {
292 return array(
293 '' => array(
294 'name' => 'string',
295 'ns' => 'namespace',
296 'title' => 'string',
297 'filehidden' => 'boolean',
298 'commenthidden' => 'boolean',
299 'userhidden' => 'boolean',
300 'suppressed' => 'boolean'
302 'sha1' => array(
303 'sha1' => 'string'
305 'timestamp' => array(
306 'timestamp' => 'timestamp'
308 'user' => array(
309 'userid' => 'integer',
310 'user' => 'string'
312 'size' => array(
313 'size' => 'integer',
314 'pagecount' => array(
315 ApiBase::PROP_TYPE => 'integer',
316 ApiBase::PROP_NULLABLE => true
318 'height' => 'integer',
319 'width' => 'integer'
321 'dimensions' => array(
322 'size' => 'integer',
323 'pagecount' => array(
324 ApiBase::PROP_TYPE => 'integer',
325 ApiBase::PROP_NULLABLE => true
327 'height' => 'integer',
328 'width' => 'integer'
330 'description' => array(
331 'description' => 'string'
333 'parseddescription' => array(
334 'description' => 'string',
335 'parseddescription' => 'string'
337 'metadata' => array(
338 'metadata' => 'string'
340 'bitdepth' => array(
341 'bitdepth' => 'integer'
343 'mime' => array(
344 'mime' => 'string'
346 'mediatype' => array(
347 'mediatype' => 'string'
349 'archivename' => array(
350 'archivename' => 'string'
355 public function getDescription() {
356 return 'Enumerate all deleted files sequentially';
359 public function getPossibleErrors() {
360 return array_merge( parent::getPossibleErrors(), array(
361 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
362 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
363 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
364 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
365 ) );
368 public function getExamples() {
369 return array(
370 'api.php?action=query&list=filearchive' => array(
371 'Simple Use',
372 'Show a list of all deleted files',
377 public function getHelpUrls() {
378 return 'https://www.mediawiki.org/wiki/API:Filearchive';