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
29 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
35 * Query module to enumerate all deleted files.
39 class ApiQueryFilearchive
extends ApiQueryBase
{
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'fa' );
45 public function execute() {
46 $user = $this->getUser();
47 // Before doing anything at all, let's check permissions
48 if ( !$user->isAllowed( 'deletedhistory' ) ) {
49 $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
54 $params = $this->extractRequestParams();
56 $prop = array_flip( $params['prop'] );
57 $fld_sha1 = isset( $prop['sha1'] );
58 $fld_timestamp = isset( $prop['timestamp'] );
59 $fld_user = isset( $prop['user'] );
60 $fld_size = isset( $prop['size'] );
61 $fld_dimensions = isset( $prop['dimensions'] );
62 $fld_description = isset( $prop['description'] ) ||
isset( $prop['parseddescription'] );
63 $fld_mime = isset( $prop['mime'] );
64 $fld_metadata = isset( $prop['metadata'] );
65 $fld_bitdepth = isset( $prop['bitdepth'] );
67 $this->addTables( 'filearchive' );
69 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
70 $this->addFieldsIf( 'fa_storage_key', $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_metadata', $fld_metadata );
77 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
80 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
81 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
82 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
83 $this->addWhereRange( 'fa_name', $dir, $from, $to );
84 if ( isset( $params['prefix'] ) ) {
85 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
88 $sha1Set = isset( $params['sha1'] );
89 $sha1base36Set = isset( $params['sha1base36'] );
90 if ( $sha1Set ||
$sha1base36Set ) {
93 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
98 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
99 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
101 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
102 } elseif ( $sha1base36Set ) {
103 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
104 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
106 $sha1 = $params['sha1base36'];
109 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
113 if ( !$user->isAllowed( 'suppressrevision' ) ) {
114 // Filter out revisions that the user is not allowed to see. There
115 // is no way to indicate that we have skipped stuff because the
116 // continuation parameter is fa_name
118 // Note that this field is unindexed. This should however not be
119 // a big problem as files with fa_deleted are rare
120 $this->addWhereFld( 'fa_deleted', 0 );
123 $limit = $params['limit'];
124 $this->addOption( 'LIMIT', $limit +
1 );
125 $this->addOption( 'ORDER BY', 'fa_name' .
126 ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
128 $res = $this->select( __METHOD__
);
131 $result = $this->getResult();
132 foreach ( $res as $row ) {
133 if ( ++
$count > $limit ) {
134 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
135 // TODO: Security issue - if the user has no right to view next title, it will still be shown
136 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
141 $file['name'] = $row->fa_name
;
142 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
143 self
::addTitleInfo( $file, $title );
146 $file['sha1'] = wfBaseConvert( LocalRepo
::getHashFromKey( $row->fa_storage_key
), 36, 16, 40 );
148 if ( $fld_timestamp ) {
149 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
152 $file['userid'] = $row->fa_user
;
153 $file['user'] = $row->fa_user_text
;
155 if ( $fld_size ||
$fld_dimensions ) {
156 $file['size'] = $row->fa_size
;
158 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
159 if ( $pageCount !== false ) {
160 $vals['pagecount'] = $pageCount;
163 $file['height'] = $row->fa_height
;
164 $file['width'] = $row->fa_width
;
166 if ( $fld_description ) {
167 $file['description'] = $row->fa_description
;
168 if ( isset( $prop['parseddescription'] ) ) {
169 $file['parseddescription'] = Linker
::formatComment(
170 $row->fa_description
, $title );
173 if ( $fld_metadata ) {
174 $file['metadata'] = $row->fa_metadata
175 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
178 if ( $fld_bitdepth ) {
179 $file['bitdepth'] = $row->fa_bits
;
182 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
185 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
186 $file['filehidden'] = '';
188 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
189 $file['commenthidden'] = '';
191 if ( $row->fa_deleted
& File
::DELETED_USER
) {
192 $file['userhidden'] = '';
194 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
195 // This file is deleted for normal admins
196 $file['suppressed'] = '';
200 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
202 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
207 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
210 public function getAllowedParams() {
216 ApiBase
::PARAM_DFLT
=> 10,
217 ApiBase
::PARAM_TYPE
=> 'limit',
218 ApiBase
::PARAM_MIN
=> 1,
219 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
220 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
223 ApiBase
::PARAM_DFLT
=> 'ascending',
224 ApiBase
::PARAM_TYPE
=> array(
230 'sha1base36' => null,
232 ApiBase
::PARAM_DFLT
=> 'timestamp',
233 ApiBase
::PARAM_ISMULTI
=> true,
234 ApiBase
::PARAM_TYPE
=> array(
250 public function getParamDescription() {
252 'from' => 'The image title to start enumerating from',
253 'to' => 'The image title to stop enumerating at',
254 'prefix' => 'Search for all image titles that begin with this value',
255 'dir' => 'The direction in which to list',
256 'limit' => 'How many images to return in total',
257 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
258 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
260 'What image information to get:',
261 ' sha1 - Adds SHA-1 hash for the image',
262 ' timestamp - Adds timestamp for the uploaded version',
263 ' user - Adds user who uploaded the image version',
264 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
265 ' dimensions - Alias for size',
266 ' description - Adds description the image version',
267 ' parseddescription - Parse the description on the version',
268 ' mime - Adds MIME of the image',
269 ' metadata - Lists EXIF metadata for the version of the image',
270 ' bitdepth - Adds the bit depth of the version',
275 public function getDescription() {
276 return 'Enumerate all deleted files sequentially';
279 public function getPossibleErrors() {
280 return array_merge( parent
::getPossibleErrors(), array(
281 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
282 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
283 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
284 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
288 public function getExamples() {
291 ' Show a list of all deleted files',
292 ' api.php?action=query&list=filearchive',
296 public function getVersion() {
297 return __CLASS__
. ': $Id$';