Call Linker methods statically
[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 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
32 }
33
34 /**
35 * Query module to enumerate all deleted files.
36 *
37 * @ingroup API
38 */
39 class ApiQueryFilearchive extends ApiQueryBase {
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'fa' );
43 }
44
45 public function execute() {
46 global $wgUser;
47 // Before doing anything at all, let's check permissions
48 if ( !$wgUser->isAllowed( 'deletedhistory' ) || $wgUser->isBlocked() ) {
49 $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
50 }
51
52 $db = $this->getDB();
53
54 $params = $this->extractRequestParams();
55
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'] );
66
67 $this->addTables( 'filearchive' );
68
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 );
78
79 // Image filters
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() ) );
86 }
87
88 $sha1Set = isset( $params['sha1'] );
89 $sha1base36Set = isset( $params['sha1base36'] );
90 if ( $sha1Set || $sha1base36Set ) {
91 global $wgMiserMode;
92 if ( $wgMiserMode ) {
93 $this->dieUsage( 'Search by hash disabled in Miser Mode', 'hashsearchdisabled' );
94 }
95
96 $sha1 = false;
97 if ( $sha1Set ) {
98 if ( !$this->validateSha1Hash( $params['sha1'] ) ) {
99 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
100 }
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' );
105 }
106 $sha1 = $params['sha1base36'];
107 }
108 if ( $sha1 ) {
109 $this->addWhere( 'fa_storage_key ' . $db->buildLike( "{$sha1}.", $db->anyString() ) );
110 }
111 }
112
113 if ( !$wgUser->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
117
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 );
121 }
122
123 $limit = $params['limit'];
124 $this->addOption( 'LIMIT', $limit + 1 );
125 $this->addOption( 'ORDER BY', 'fa_name' .
126 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
127
128 $res = $this->select( __METHOD__ );
129
130 $count = 0;
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 ) );
137 break;
138 }
139
140 $file = array();
141 $file['name'] = $row->fa_name;
142 $title = Title::makeTitle( NS_FILE, $row->fa_name );
143 self::addTitleInfo( $file, $title );
144
145 if ( $fld_sha1 ) {
146 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
147 }
148 if ( $fld_timestamp ) {
149 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
150 }
151 if ( $fld_user ) {
152 $file['userid'] = $row->fa_user;
153 $file['user'] = $row->fa_user_text;
154 }
155 if ( $fld_size || $fld_dimensions ) {
156 $file['size'] = $row->fa_size;
157
158 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
159 if ( $pageCount !== false ) {
160 $vals['pagecount'] = $pageCount;
161 }
162
163 $file['height'] = $row->fa_height;
164 $file['width'] = $row->fa_width;
165 }
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 );
171 }
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 'metadata',
244 'bitdepth'
245 ),
246 ),
247 );
248 }
249
250 public function getParamDescription() {
251 return array(
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',
259 'prop' => array(
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',
271 ),
272 );
273 }
274
275 public function getDescription() {
276 return 'Enumerate all deleted files sequentially';
277 }
278
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' ),
285 ) );
286 }
287
288 public function getExamples() {
289 return array(
290 'Simple Use',
291 ' Show a list of all deleted files',
292 ' api.php?action=query&list=filearchive',
293 );
294 }
295
296 public function getVersion() {
297 return __CLASS__ . ': $Id$';
298 }
299 }