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_metadata = isset( $prop['metadata'] );
60 $fld_bitdepth = isset( $prop['bitdepth'] );
62 $this->addTables( 'filearchive' );
64 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
65 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
66 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
67 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
68 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions ||
$fld_size );
69 $this->addFieldsIf( 'fa_description', $fld_description );
70 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
71 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
72 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
75 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
76 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
77 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
78 $this->addWhereRange( 'fa_name', $dir, $from, $to );
79 if ( isset( $params['prefix'] ) ) {
80 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
83 $sha1Set = isset( $params['sha1'] );
84 $sha1base36Set = isset( $params['sha1base36'] );
85 if ( $sha1Set ||
$sha1base36Set ) {
88 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
93 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
94 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
96 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
97 } elseif ( $sha1base36Set ) {
98 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
99 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
101 $sha1 = $params['sha1base36'];
104 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
108 if ( !$user->isAllowed( 'suppressrevision' ) ) {
109 // Filter out revisions that the user is not allowed to see. There
110 // is no way to indicate that we have skipped stuff because the
111 // continuation parameter is fa_name
113 // Note that this field is unindexed. This should however not be
114 // a big problem as files with fa_deleted are rare
115 $this->addWhereFld( 'fa_deleted', 0 );
118 $limit = $params['limit'];
119 $this->addOption( 'LIMIT', $limit +
1 );
120 $this->addOption( 'ORDER BY', 'fa_name' .
121 ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
123 $res = $this->select( __METHOD__
);
126 $result = $this->getResult();
127 foreach ( $res as $row ) {
128 if ( ++
$count > $limit ) {
129 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
130 // TODO: Security issue - if the user has no right to view next title, it will still be shown
131 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
136 $file['name'] = $row->fa_name
;
137 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
138 self
::addTitleInfo( $file, $title );
141 $file['sha1'] = wfBaseConvert( LocalRepo
::getHashFromKey( $row->fa_storage_key
), 36, 16, 40 );
143 if ( $fld_timestamp ) {
144 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
147 $file['userid'] = $row->fa_user
;
148 $file['user'] = $row->fa_user_text
;
150 if ( $fld_size ||
$fld_dimensions ) {
151 $file['size'] = $row->fa_size
;
153 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
154 if ( $pageCount !== false ) {
155 $vals['pagecount'] = $pageCount;
158 $file['height'] = $row->fa_height
;
159 $file['width'] = $row->fa_width
;
161 if ( $fld_description ) {
162 $file['description'] = $row->fa_description
;
163 if ( isset( $prop['parseddescription'] ) ) {
164 $file['parseddescription'] = Linker
::formatComment(
165 $row->fa_description
, $title );
168 if ( $fld_metadata ) {
169 $file['metadata'] = $row->fa_metadata
170 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
173 if ( $fld_bitdepth ) {
174 $file['bitdepth'] = $row->fa_bits
;
177 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
180 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
181 $file['filehidden'] = '';
183 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
184 $file['commenthidden'] = '';
186 if ( $row->fa_deleted
& File
::DELETED_USER
) {
187 $file['userhidden'] = '';
189 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
190 // This file is deleted for normal admins
191 $file['suppressed'] = '';
195 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
197 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
202 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
205 public function getAllowedParams() {
211 ApiBase
::PARAM_DFLT
=> 10,
212 ApiBase
::PARAM_TYPE
=> 'limit',
213 ApiBase
::PARAM_MIN
=> 1,
214 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
215 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
218 ApiBase
::PARAM_DFLT
=> 'ascending',
219 ApiBase
::PARAM_TYPE
=> array(
225 'sha1base36' => null,
227 ApiBase
::PARAM_DFLT
=> 'timestamp',
228 ApiBase
::PARAM_ISMULTI
=> true,
229 ApiBase
::PARAM_TYPE
=> array(
245 public function getParamDescription() {
247 'from' => 'The image title to start enumerating from',
248 'to' => 'The image title to stop enumerating at',
249 'prefix' => 'Search for all image titles that begin with this value',
250 'dir' => 'The direction in which to list',
251 'limit' => 'How many images to return in total',
252 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
253 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
255 'What image information to get:',
256 ' sha1 - Adds SHA-1 hash for the image',
257 ' timestamp - Adds timestamp for the uploaded version',
258 ' user - Adds user who uploaded the image version',
259 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
260 ' dimensions - Alias for size',
261 ' description - Adds description the image version',
262 ' parseddescription - Parse the description on the version',
263 ' mime - Adds MIME of the image',
264 ' metadata - Lists EXIF metadata for the version of the image',
265 ' bitdepth - Adds the bit depth of the version',
270 public function getDescription() {
271 return 'Enumerate all deleted files sequentially';
274 public function getPossibleErrors() {
275 return array_merge( parent
::getPossibleErrors(), array(
276 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
277 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
278 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
279 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
283 public function getExamples() {
285 'api.php?action=query&list=filearchive' => array(
287 'Show a list of all deleted files',
292 public function getVersion() {
293 return __CLASS__
. ': $Id$';