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
30 * Query module to enumerate all deleted files.
34 class ApiQueryFilearchive
extends ApiQueryBase
{
36 public function __construct( ApiQuery
$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' ) ) {
45 'You don\'t have permission to view deleted file information',
52 $params = $this->extractRequestParams();
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'] );
67 $this->addTables( 'filearchive' );
69 $this->addFields( ArchivedFile
::selectFields() );
70 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
71 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
72 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
73 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
74 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions ||
$fld_size );
75 $this->addFieldsIf( 'fa_description', $fld_description );
76 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
77 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
78 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
79 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
80 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
82 if ( !is_null( $params['continue'] ) ) {
83 $cont = explode( '|', $params['continue'] );
84 $this->dieContinueUsageIf( count( $cont ) != 1 );
85 $op = $params['dir'] == 'descending' ?
'<' : '>';
86 $cont_from = $db->addQuotes( $cont[0] );
87 $this->addWhere( "fa_name $op= $cont_from" );
91 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
92 $from = ( $params['from'] === null ?
null : $this->titlePartToKey( $params['from'], NS_FILE
) );
93 if ( !is_null( $params['continue'] ) ) {
94 $from = $params['continue'];
96 $to = ( $params['to'] === null ?
null : $this->titlePartToKey( $params['to'], NS_FILE
) );
97 $this->addWhereRange( 'fa_name', $dir, $from, $to );
98 if ( isset( $params['prefix'] ) ) {
99 $this->addWhere( 'fa_name' . $db->buildLike(
100 $this->titlePartToKey( $params['prefix'], NS_FILE
),
101 $db->anyString() ) );
104 $sha1Set = isset( $params['sha1'] );
105 $sha1base36Set = isset( $params['sha1base36'] );
106 if ( $sha1Set ||
$sha1base36Set ) {
109 $sha1 = strtolower( $params['sha1'] );
110 if ( !$this->validateSha1Hash( $sha1 ) ) {
111 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
113 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
114 } elseif ( $sha1base36Set ) {
115 $sha1 = strtolower( $params['sha1base36'] );
116 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
117 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
121 $this->addWhereFld( 'fa_sha1', $sha1 );
125 // Exclude files this user can't view.
126 if ( !$user->isAllowed( 'deletedtext' ) ) {
127 $bitmask = File
::DELETED_FILE
;
128 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
129 $bitmask = File
::DELETED_FILE | File
::DELETED_RESTRICTED
;
134 $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
137 $limit = $params['limit'];
138 $this->addOption( 'LIMIT', $limit +
1 );
139 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
140 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
142 $res = $this->select( __METHOD__
);
145 $result = $this->getResult();
146 foreach ( $res as $row ) {
147 if ( ++
$count > $limit ) {
148 // We've reached the one extra which shows that there are
149 // additional pages to be had. Stop here...
150 $this->setContinueEnumParameter( 'continue', $row->fa_name
);
155 $file['name'] = $row->fa_name
;
156 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
157 self
::addTitleInfo( $file, $title );
159 if ( $fld_description &&
160 Revision
::userCanBitfield( $row->fa_deleted
, File
::DELETED_COMMENT
, $user )
162 $file['description'] = $row->fa_description
;
163 if ( isset( $prop['parseddescription'] ) ) {
164 $file['parseddescription'] = Linker
::formatComment(
165 $row->fa_description
, $title );
169 Revision
::userCanBitfield( $row->fa_deleted
, File
::DELETED_USER
, $user )
171 $file['userid'] = $row->fa_user
;
172 $file['user'] = $row->fa_user_text
;
175 $file['sha1'] = wfBaseConvert( $row->fa_sha1
, 36, 16, 40 );
177 if ( $fld_timestamp ) {
178 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
180 if ( $fld_size ||
$fld_dimensions ) {
181 $file['size'] = $row->fa_size
;
183 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
184 if ( $pageCount !== false ) {
185 $vals['pagecount'] = $pageCount;
188 $file['height'] = $row->fa_height
;
189 $file['width'] = $row->fa_width
;
191 if ( $fld_mediatype ) {
192 $file['mediatype'] = $row->fa_media_type
;
194 if ( $fld_metadata ) {
195 $file['metadata'] = $row->fa_metadata
196 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
199 if ( $fld_bitdepth ) {
200 $file['bitdepth'] = $row->fa_bits
;
203 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
205 if ( $fld_archivename && !is_null( $row->fa_archive_name
) ) {
206 $file['archivename'] = $row->fa_archive_name
;
209 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
210 $file['filehidden'] = '';
212 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
213 $file['commenthidden'] = '';
215 if ( $row->fa_deleted
& File
::DELETED_USER
) {
216 $file['userhidden'] = '';
218 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
219 // This file is deleted for normal admins
220 $file['suppressed'] = '';
223 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
225 $this->setContinueEnumParameter( 'continue', $row->fa_name
);
230 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
233 public function getAllowedParams() {
240 ApiBase
::PARAM_DFLT
=> 10,
241 ApiBase
::PARAM_TYPE
=> 'limit',
242 ApiBase
::PARAM_MIN
=> 1,
243 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
244 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
247 ApiBase
::PARAM_DFLT
=> 'ascending',
248 ApiBase
::PARAM_TYPE
=> array(
254 'sha1base36' => null,
256 ApiBase
::PARAM_DFLT
=> 'timestamp',
257 ApiBase
::PARAM_ISMULTI
=> true,
258 ApiBase
::PARAM_TYPE
=> array(
276 public function getParamDescription() {
278 'from' => 'The image title to start enumerating from',
279 'continue' => 'When more results are available, use this to continue',
280 'to' => 'The image title to stop enumerating at',
281 'prefix' => 'Search for all image titles that begin with this value',
282 'dir' => 'The direction in which to list',
283 'limit' => 'How many images to return in total',
284 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
285 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
287 'What image information to get:',
288 ' sha1 - Adds SHA-1 hash for the image',
289 ' timestamp - Adds timestamp for the uploaded version',
290 ' user - Adds user who uploaded the image version',
291 ' size - Adds the size of the image in bytes and the height, ' .
292 'width and page count (if applicable)',
293 ' dimensions - Alias for size',
294 ' description - Adds description the image version',
295 ' parseddescription - Parse the description on the version',
296 ' mime - Adds MIME of the image',
297 ' mediatype - Adds the media type of the image',
298 ' metadata - Lists Exif metadata for the version of the image',
299 ' bitdepth - Adds the bit depth of the version',
300 ' archivename - Adds the file name of the archive version for non-latest versions'
305 public function getResultProperties() {
311 'filehidden' => 'boolean',
312 'commenthidden' => 'boolean',
313 'userhidden' => 'boolean',
314 'suppressed' => 'boolean'
319 'timestamp' => array(
320 'timestamp' => 'timestamp'
323 'userid' => 'integer',
328 'pagecount' => array(
329 ApiBase
::PROP_TYPE
=> 'integer',
330 ApiBase
::PROP_NULLABLE
=> true
332 'height' => 'integer',
335 'dimensions' => array(
337 'pagecount' => array(
338 ApiBase
::PROP_TYPE
=> 'integer',
339 ApiBase
::PROP_NULLABLE
=> true
341 'height' => 'integer',
344 'description' => array(
345 'description' => 'string'
347 'parseddescription' => array(
348 'description' => 'string',
349 'parseddescription' => 'string'
352 'metadata' => 'string'
355 'bitdepth' => 'integer'
360 'mediatype' => array(
361 'mediatype' => 'string'
363 'archivename' => array(
364 'archivename' => 'string'
369 public function getDescription() {
370 return 'Enumerate all deleted files sequentially.';
373 public function getPossibleErrors() {
374 return array_merge( parent
::getPossibleErrors(), array(
376 'code' => 'permissiondenied',
377 'info' => 'You don\'t have permission to view deleted file information'
379 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
380 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA-1 hash provided is not valid' ),
382 'code' => 'invalidsha1base36hash',
383 'info' => 'The SHA1Base36 hash provided is not valid'
388 public function getExamples() {
390 'api.php?action=query&list=filearchive' => array(
392 'Show a list of all deleted files',
397 public function getHelpUrls() {
398 return 'https://www.mediawiki.org/wiki/API:Filearchive';