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() {
47 // Before doing anything at all, let's check permissions
48 if ( !$wgUser->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 );
74 $this->addFields( array( 'fa_user', 'fa_user_text' ) );
77 if ( $fld_dimensions ||
$fld_size ) {
78 $this->addFields( array( 'fa_height', 'fa_width', 'fa_size' ) );
81 $this->addFieldsIf( 'fa_description', $fld_description );
84 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
87 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
88 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
91 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
92 $from = ( is_null( $params['from'] ) ?
null : $this->titlePartToKey( $params['from'] ) );
93 $to = ( is_null( $params['to'] ) ?
null : $this->titlePartToKey( $params['to'] ) );
94 $this->addWhereRange( 'fa_name', $dir, $from, $to );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
99 if ( !$wgUser->isAllowed( 'suppressrevision' ) ) {
100 // Filter out revisions that the user is not allowed to see. There
101 // is no way to indicate that we have skipped stuff because the
102 // continuation parameter is fa_name
104 // Note that this field is unindexed. This should however not be
105 // a big problem as files with fa_deleted are rare
106 $this->addWhereFld( 'fa_deleted', 0 );
109 $limit = $params['limit'];
110 $this->addOption( 'LIMIT', $limit +
1 );
111 $this->addOption( 'ORDER BY', 'fa_name' .
112 ( $params['dir'] == 'descending' ?
' DESC' : '' ) );
114 $res = $this->select( __METHOD__
);
117 $result = $this->getResult();
118 foreach ( $res as $row ) {
119 if ( ++
$count > $limit ) {
120 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
121 // TODO: Security issue - if the user has no right to view next title, it will still be shown
122 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
127 $file['name'] = $row->fa_name
;
128 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
129 self
::addTitleInfo( $file, $title );
132 $file['sha1'] = wfBaseConvert( LocalRepo
::getHashFromKey( $row->fa_storage_key
), 36, 16, 40 );
134 if ( $fld_timestamp ) {
135 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
138 $file['userid'] = $row->fa_user
;
139 $file['user'] = $row->fa_user_text
;
141 if ( $fld_size ||
$fld_dimensions ) {
142 $file['size'] = $row->fa_size
;
144 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
145 if ( $pageCount !== false ) {
146 $vals['pagecount'] = $pageCount;
149 $file['height'] = $row->fa_height
;
150 $file['width'] = $row->fa_width
;
152 if ( $fld_description ) {
153 $file['description'] = $row->fa_description
;
154 if ( isset( $prop['parseddescription'] ) ) {
155 $file['parseddescription'] = $wgUser->getSkin()->formatComment(
156 $row->fa_description
, $title );
159 if ( $fld_metadata ) {
160 $file['metadata'] = $row->fa_metadata
161 ? ApiQueryImageInfo
::processMetaData( unserialize( $row->fa_metadata
), $result )
164 if ( $fld_bitdepth ) {
165 $file['bitdepth'] = $row->fa_bits
;
168 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
171 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
172 $file['filehidden'] = '';
174 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
175 $file['commenthidden'] = '';
177 if ( $row->fa_deleted
& File
::DELETED_USER
) {
178 $file['userhidden'] = '';
180 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
181 // This file is deleted for normal admins
182 $file['suppressed'] = '';
186 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
188 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name
) );
193 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
196 public function getAllowedParams() {
202 ApiBase
::PARAM_DFLT
=> 10,
203 ApiBase
::PARAM_TYPE
=> 'limit',
204 ApiBase
::PARAM_MIN
=> 1,
205 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
206 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
209 ApiBase
::PARAM_DFLT
=> 'ascending',
210 ApiBase
::PARAM_TYPE
=> array(
216 ApiBase
::PARAM_DFLT
=> 'timestamp',
217 ApiBase
::PARAM_ISMULTI
=> true,
218 ApiBase
::PARAM_TYPE
=> array(
234 public function getParamDescription() {
236 'from' => 'The image title to start enumerating from',
237 'to' => 'The image title to stop enumerating at',
238 'prefix' => 'Search for all image titles that begin with this value',
239 'dir' => 'The direction in which to list',
240 'limit' => 'How many images to return in total',
242 'What image information to get:',
243 ' sha1 - Adds SHA-1 hash for the image',
244 ' timestamp - Adds timestamp for the uploaded version',
245 ' user - Adds user who uploaded the image version',
246 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
247 ' dimensions - Alias for size',
248 ' description - Adds description the image version',
249 ' parseddescription - Parse the description on the version',
250 ' mime - Adds MIME of the image',
251 ' metadata - Lists EXIF metadata for the version of the image',
252 ' bitdepth - Adds the bit depth of the version',
257 public function getDescription() {
258 return 'Enumerate all deleted files sequentially';
261 public function getPossibleErrors() {
262 return array_merge( parent
::getPossibleErrors(), array(
263 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
267 protected function getExamples() {
270 ' Show a list of all deleted files',
271 ' api.php?action=query&list=filearchive',
275 public function getVersion() {
276 return __CLASS__
. ': $Id$';