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( $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' );
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 if ( count( $cont ) != 1 ) {
81 $this->dieUsage( "Invalid continue param. You should pass the " .
82 "original value returned by the previous query", "_badcontinue" );
84 $op = $params['dir'] == 'descending' ?
'<' : '>';
85 $cont_from = $db->addQuotes( $cont[0] );
86 $this->addWhere( "fa_name $op= $cont_from" );
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'];
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' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
101 $sha1Set = isset( $params['sha1'] );
102 $sha1base36Set = isset( $params['sha1base36'] );
103 if ( $sha1Set ||
$sha1base36Set ) {
106 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
107 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
109 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
110 } elseif ( $sha1base36Set ) {
111 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
112 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
114 $sha1 = $params['sha1base36'];
117 $this->addWhereFld( 'fa_sha1', $sha1 );
121 if ( !$user->isAllowed( 'suppressrevision' ) ) {
122 // Filter out revisions that the user is not allowed to see. There
123 // is no way to indicate that we have skipped stuff because the
124 // continuation parameter is fa_name
126 // Note that this field is unindexed. This should however not be
127 // a big problem as files with fa_deleted are rare
128 $this->addWhereFld( 'fa_deleted', 0 );
131 $limit = $params['limit'];
132 $this->addOption( 'LIMIT', $limit +
1 );
133 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
134 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
136 $res = $this->select( __METHOD__
);
139 $result = $this->getResult();
140 foreach ( $res as $row ) {
141 if ( ++
$count > $limit ) {
142 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
143 $this->setContinueEnumParameter( 'continue', $row->fa_name
);
148 $file['name'] = $row->fa_name
;
149 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
150 self
::addTitleInfo( $file, $title );
153 $file['sha1'] = wfBaseConvert( $row->fa_sha1
, 36, 16, 40 );
155 if ( $fld_timestamp ) {
156 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
159 $file['userid'] = $row->fa_user
;
160 $file['user'] = $row->fa_user_text
;
162 if ( $fld_size ||
$fld_dimensions ) {
163 $file['size'] = $row->fa_size
;
165 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
166 if ( $pageCount !== false ) {
167 $vals['pagecount'] = $pageCount;
170 $file['height'] = $row->fa_height
;
171 $file['width'] = $row->fa_width
;
173 if ( $fld_description ) {
174 $file['description'] = $row->fa_description
;
175 if ( isset( $prop['parseddescription'] ) ) {
176 $file['parseddescription'] = Linker
::formatComment(
177 $row->fa_description
, $title );
180 if ( $fld_mediatype ) {
181 $file['mediatype'] = $row->fa_media_type
;
183 if ( $fld_metadata ) {
184 $file['metadata'] = $row->fa_metadata
185 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
188 if ( $fld_bitdepth ) {
189 $file['bitdepth'] = $row->fa_bits
;
192 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
194 if ( $fld_archivename && !is_null( $row->fa_archive_name
) ) {
195 $file['archivename'] = $row->fa_archive_name
;
198 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
199 $file['filehidden'] = '';
201 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
202 $file['commenthidden'] = '';
204 if ( $row->fa_deleted
& File
::DELETED_USER
) {
205 $file['userhidden'] = '';
207 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
208 // This file is deleted for normal admins
209 $file['suppressed'] = '';
213 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
215 $this->setContinueEnumParameter( 'continue', $row->fa_name
);
220 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
223 public function getAllowedParams() {
230 ApiBase
::PARAM_DFLT
=> 10,
231 ApiBase
::PARAM_TYPE
=> 'limit',
232 ApiBase
::PARAM_MIN
=> 1,
233 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
234 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
237 ApiBase
::PARAM_DFLT
=> 'ascending',
238 ApiBase
::PARAM_TYPE
=> array(
244 'sha1base36' => null,
246 ApiBase
::PARAM_DFLT
=> 'timestamp',
247 ApiBase
::PARAM_ISMULTI
=> true,
248 ApiBase
::PARAM_TYPE
=> array(
266 public function getParamDescription() {
268 'from' => 'The image title to start enumerating from',
269 'continue' => 'When more results are available, use this to continue',
270 'to' => 'The image title to stop enumerating at',
271 'prefix' => 'Search for all image titles that begin with this value',
272 'dir' => 'The direction in which to list',
273 'limit' => 'How many images to return in total',
274 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36",
275 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki)',
277 'What image information to get:',
278 ' sha1 - Adds SHA-1 hash for the image',
279 ' timestamp - Adds timestamp for the uploaded version',
280 ' user - Adds user who uploaded the image version',
281 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
282 ' dimensions - Alias for size',
283 ' description - Adds description the image version',
284 ' parseddescription - Parse the description on the version',
285 ' mime - Adds MIME of the image',
286 ' mediatype - Adds the media type of the image',
287 ' metadata - Lists EXIF metadata for the version of the image',
288 ' bitdepth - Adds the bit depth of the version',
289 ' archivename - Adds the file name of the archive version for non-latest versions'
294 public function getResultProperties() {
300 'filehidden' => 'boolean',
301 'commenthidden' => 'boolean',
302 'userhidden' => 'boolean',
303 'suppressed' => 'boolean'
308 'timestamp' => array(
309 'timestamp' => 'timestamp'
312 'userid' => 'integer',
317 'pagecount' => array(
318 ApiBase
::PROP_TYPE
=> 'integer',
319 ApiBase
::PROP_NULLABLE
=> true
321 'height' => 'integer',
324 'dimensions' => array(
326 'pagecount' => array(
327 ApiBase
::PROP_TYPE
=> 'integer',
328 ApiBase
::PROP_NULLABLE
=> true
330 'height' => 'integer',
333 'description' => array(
334 'description' => 'string'
336 'parseddescription' => array(
337 'description' => 'string',
338 'parseddescription' => 'string'
341 'metadata' => 'string'
344 'bitdepth' => 'integer'
349 'mediatype' => array(
350 'mediatype' => 'string'
352 'archivename' => array(
353 'archivename' => 'string'
358 public function getDescription() {
359 return 'Enumerate all deleted files sequentially';
362 public function getPossibleErrors() {
363 return array_merge( parent
::getPossibleErrors(), array(
364 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
365 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
366 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
367 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
368 array( 'code' => '_badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
372 public function getExamples() {
374 'api.php?action=query&list=filearchive' => array(
376 'Show a list of all deleted files',
381 public function getVersion() {
382 return __CLASS__
. ': $Id$';