Merge "Bumping minimum supported version of PHPUnit to 3.6.7"
[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_metadata = isset( $prop['metadata'] );
60 $fld_bitdepth = isset( $prop['bitdepth'] );
61
62 $this->addTables( 'filearchive' );
63
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 );
73
74 // Image filters
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() ) );
81 }
82
83 $sha1Set = isset( $params['sha1'] );
84 $sha1base36Set = isset( $params['sha1base36'] );
85 if ( $sha1Set || $sha1base36Set ) {
86 global $wgMiserMode;
87 if ( $wgMiserMode ) {
88 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
89 }
90
91 $sha1 = false;
92 if ( $sha1Set ) {
93 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
94 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
95 }
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' );
100 }
101 $sha1 = $params['sha1base36'];
102 }
103 if ( $sha1 ) {
104 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
105 }
106 }
107
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
112
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 );
116 }
117
118 $limit = $params['limit'];
119 $this->addOption( 'LIMIT', $limit + 1 );
120 $this->addOption( 'ORDER BY', 'fa_name' .
121 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
122
123 $res = $this->select( __METHOD__ );
124
125 $count = 0;
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 ) );
132 break;
133 }
134
135 $file = array();
136 $file['name'] = $row->fa_name;
137 $title = Title::makeTitle( NS_FILE, $row->fa_name );
138 self::addTitleInfo( $file, $title );
139
140 if ( $fld_sha1 ) {
141 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
142 }
143 if ( $fld_timestamp ) {
144 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
145 }
146 if ( $fld_user ) {
147 $file['userid'] = $row->fa_user;
148 $file['user'] = $row->fa_user_text;
149 }
150 if ( $fld_size || $fld_dimensions ) {
151 $file['size'] = $row->fa_size;
152
153 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
154 if ( $pageCount !== false ) {
155 $vals['pagecount'] = $pageCount;
156 }
157
158 $file['height'] = $row->fa_height;
159 $file['width'] = $row->fa_width;
160 }
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 );
166 }
167 }
168 if ( $fld_metadata ) {
169 $file['metadata'] = $row->fa_metadata
170 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
171 : null;
172 }
173 if ( $fld_bitdepth ) {
174 $file['bitdepth'] = $row->fa_bits;
175 }
176 if ( $fld_mime ) {
177 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
178 }
179
180 if ( $row->fa_deleted & File::DELETED_FILE ) {
181 $file['filehidden'] = '';
182 }
183 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
184 $file['commenthidden'] = '';
185 }
186 if ( $row->fa_deleted & File::DELETED_USER ) {
187 $file['userhidden'] = '';
188 }
189 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
190 // This file is deleted for normal admins
191 $file['suppressed'] = '';
192 }
193
194
195 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
196 if ( !$fit ) {
197 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
198 break;
199 }
200 }
201
202 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
203 }
204
205 public function getAllowedParams() {
206 return array (
207 'from' => null,
208 'to' => null,
209 'prefix' => null,
210 'limit' => array(
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
216 ),
217 'dir' => array(
218 ApiBase::PARAM_DFLT => 'ascending',
219 ApiBase::PARAM_TYPE => array(
220 'ascending',
221 'descending'
222 )
223 ),
224 'sha1' => null,
225 'sha1base36' => null,
226 'prop' => array(
227 ApiBase::PARAM_DFLT => 'timestamp',
228 ApiBase::PARAM_ISMULTI => true,
229 ApiBase::PARAM_TYPE => array(
230 'sha1',
231 'timestamp',
232 'user',
233 'size',
234 'dimensions',
235 'description',
236 'parseddescription',
237 'mime',
238 'metadata',
239 'bitdepth'
240 ),
241 ),
242 );
243 }
244
245 public function getParamDescription() {
246 return array(
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',
254 'prop' => array(
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',
266 ),
267 );
268 }
269
270 public function getDescription() {
271 return 'Enumerate all deleted files sequentially';
272 }
273
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' ),
280 ) );
281 }
282
283 public function getExamples() {
284 return array(
285 'api.php?action=query&list=filearchive' => array(
286 'Simple Use',
287 'Show a list of all deleted files',
288 ),
289 );
290 }
291
292 public function getVersion() {
293 return __CLASS__ . ': $Id$';
294 }
295 }