Merge "(bug 19195) Make user IDs more readily available with the API"
[lhc/web/wiklou.git] / includes / api / ApiQueryFilearchive.php
1 <?php
2 /**
3 * API for MediaWiki 1.12+
4 *
5 * Created on May 10, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
10 *
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.
15 *
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.
20 *
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
25 *
26 * @file
27 */
28
29 /**
30 * Query module to enumerate all deleted files.
31 *
32 * @ingroup API
33 */
34 class ApiQueryFilearchive extends ApiQueryBase {
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'fa' );
38 }
39
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' );
45 }
46
47 $db = $this->getDB();
48
49 $params = $this->extractRequestParams();
50
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
63 $this->addTables( 'filearchive' );
64
65 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
66 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
67 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
68 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
69 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
70 $this->addFieldsIf( 'fa_description', $fld_description );
71 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
72 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
73 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
74 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
75
76 // Image filters
77 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
78 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
79 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
80 $this->addWhereRange( 'fa_name', $dir, $from, $to );
81 if ( isset( $params['prefix'] ) ) {
82 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
83 }
84
85 $sha1Set = isset( $params['sha1'] );
86 $sha1base36Set = isset( $params['sha1base36'] );
87 if ( $sha1Set || $sha1base36Set ) {
88 global $wgMiserMode;
89 if ( $wgMiserMode ) {
90 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
91 }
92
93 $sha1 = false;
94 if ( $sha1Set ) {
95 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
96 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
97 }
98 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
99 } elseif ( $sha1base36Set ) {
100 if ( !$this->validateSha1Base36Hash( $params['sha1base36'] ) ) {
101 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
102 }
103 $sha1 = $params['sha1base36'];
104 }
105 if ( $sha1 ) {
106 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
107 }
108 }
109
110 if ( !$user->isAllowed( 'suppressrevision' ) ) {
111 // Filter out revisions that the user is not allowed to see. There
112 // is no way to indicate that we have skipped stuff because the
113 // continuation parameter is fa_name
114
115 // Note that this field is unindexed. This should however not be
116 // a big problem as files with fa_deleted are rare
117 $this->addWhereFld( 'fa_deleted', 0 );
118 }
119
120 $limit = $params['limit'];
121 $this->addOption( 'LIMIT', $limit + 1 );
122 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
123 $this->addOption( 'ORDER BY', 'fa_name' . $sort );
124
125 $res = $this->select( __METHOD__ );
126
127 $count = 0;
128 $result = $this->getResult();
129 foreach ( $res as $row ) {
130 if ( ++$count > $limit ) {
131 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
132 // TODO: Security issue - if the user has no right to view next title, it will still be shown
133 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
134 break;
135 }
136
137 $file = array();
138 $file['name'] = $row->fa_name;
139 $title = Title::makeTitle( NS_FILE, $row->fa_name );
140 self::addTitleInfo( $file, $title );
141
142 if ( $fld_sha1 ) {
143 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
144 }
145 if ( $fld_timestamp ) {
146 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
147 }
148 if ( $fld_user ) {
149 $file['userid'] = $row->fa_user;
150 $file['user'] = $row->fa_user_text;
151 }
152 if ( $fld_size || $fld_dimensions ) {
153 $file['size'] = $row->fa_size;
154
155 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
156 if ( $pageCount !== false ) {
157 $vals['pagecount'] = $pageCount;
158 }
159
160 $file['height'] = $row->fa_height;
161 $file['width'] = $row->fa_width;
162 }
163 if ( $fld_description ) {
164 $file['description'] = $row->fa_description;
165 if ( isset( $prop['parseddescription'] ) ) {
166 $file['parseddescription'] = Linker::formatComment(
167 $row->fa_description, $title );
168 }
169 }
170 if ( $fld_mediatype ) {
171 $file['mediatype'] = $row->fa_media_type;
172 }
173 if ( $fld_metadata ) {
174 $file['metadata'] = $row->fa_metadata
175 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
176 : null;
177 }
178 if ( $fld_bitdepth ) {
179 $file['bitdepth'] = $row->fa_bits;
180 }
181 if ( $fld_mime ) {
182 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
183 }
184
185 if ( $row->fa_deleted & File::DELETED_FILE ) {
186 $file['filehidden'] = '';
187 }
188 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
189 $file['commenthidden'] = '';
190 }
191 if ( $row->fa_deleted & File::DELETED_USER ) {
192 $file['userhidden'] = '';
193 }
194 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
195 // This file is deleted for normal admins
196 $file['suppressed'] = '';
197 }
198
199
200 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
201 if ( !$fit ) {
202 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
203 break;
204 }
205 }
206
207 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
208 }
209
210 public function getAllowedParams() {
211 return array (
212 'from' => null,
213 'to' => null,
214 'prefix' => null,
215 'limit' => array(
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
221 ),
222 'dir' => array(
223 ApiBase::PARAM_DFLT => 'ascending',
224 ApiBase::PARAM_TYPE => array(
225 'ascending',
226 'descending'
227 )
228 ),
229 'sha1' => null,
230 'sha1base36' => null,
231 'prop' => array(
232 ApiBase::PARAM_DFLT => 'timestamp',
233 ApiBase::PARAM_ISMULTI => true,
234 ApiBase::PARAM_TYPE => array(
235 'sha1',
236 'timestamp',
237 'user',
238 'size',
239 'dimensions',
240 'description',
241 'parseddescription',
242 'mime',
243 'mediatype',
244 'metadata',
245 'bitdepth'
246 ),
247 ),
248 );
249 }
250
251 public function getParamDescription() {
252 return array(
253 'from' => 'The image title to start enumerating from',
254 'to' => 'The image title to stop enumerating at',
255 'prefix' => 'Search for all image titles that begin with this value',
256 'dir' => 'The direction in which to list',
257 'limit' => 'How many images to return in total',
258 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
259 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
260 'prop' => array(
261 'What image information to get:',
262 ' sha1 - Adds SHA-1 hash for the image',
263 ' timestamp - Adds timestamp for the uploaded version',
264 ' user - Adds user who uploaded the image version',
265 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
266 ' dimensions - Alias for size',
267 ' description - Adds description the image version',
268 ' parseddescription - Parse the description on the version',
269 ' mime - Adds MIME of the image',
270 ' mediatype - Adds the media type of the image',
271 ' metadata - Lists EXIF metadata for the version of the image',
272 ' bitdepth - Adds the bit depth of the version',
273 ),
274 );
275 }
276
277 public function getDescription() {
278 return 'Enumerate all deleted files sequentially';
279 }
280
281 public function getPossibleErrors() {
282 return array_merge( parent::getPossibleErrors(), array(
283 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
284 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
285 array( 'code' => 'invalidsha1hash', 'info' => 'The SHA1 hash provided is not valid' ),
286 array( 'code' => 'invalidsha1base36hash', 'info' => 'The SHA1Base36 hash provided is not valid' ),
287 ) );
288 }
289
290 public function getExamples() {
291 return array(
292 'api.php?action=query&list=filearchive' => array(
293 'Simple Use',
294 'Show a list of all deleted files',
295 ),
296 );
297 }
298
299 public function getVersion() {
300 return __CLASS__ . ': $Id$';
301 }
302 }