* (bug 27595) sha1 search of list=filearchive does not work
[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' ) ) {
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 $sha1 = wfBaseConvert( $params['sha1'], 16, 36, 31 );
99 } elseif ( $sha1base36Set ) {
100 $sha1 = $params['sha1base36'];
101 }
102 if ( $sha1 ) {
103 $this->addWhere( 'fa_storage_key ' . $db->buildLike( $sha1, $db->anyString() ) );
104 }
105 }
106
107 if ( !$wgUser->isAllowed( 'suppressrevision' ) ) {
108 // Filter out revisions that the user is not allowed to see. There
109 // is no way to indicate that we have skipped stuff because the
110 // continuation parameter is fa_name
111
112 // Note that this field is unindexed. This should however not be
113 // a big problem as files with fa_deleted are rare
114 $this->addWhereFld( 'fa_deleted', 0 );
115 }
116
117 $limit = $params['limit'];
118 $this->addOption( 'LIMIT', $limit + 1 );
119 $this->addOption( 'ORDER BY', 'fa_name' .
120 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
121
122 $res = $this->select( __METHOD__ );
123
124 $count = 0;
125 $result = $this->getResult();
126 foreach ( $res as $row ) {
127 if ( ++$count > $limit ) {
128 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
129 // TODO: Security issue - if the user has no right to view next title, it will still be shown
130 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
131 break;
132 }
133
134 $file = array();
135 $file['name'] = $row->fa_name;
136 $title = Title::makeTitle( NS_FILE, $row->fa_name );
137 self::addTitleInfo( $file, $title );
138
139 if ( $fld_sha1 ) {
140 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
141 }
142 if ( $fld_timestamp ) {
143 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
144 }
145 if ( $fld_user ) {
146 $file['userid'] = $row->fa_user;
147 $file['user'] = $row->fa_user_text;
148 }
149 if ( $fld_size || $fld_dimensions ) {
150 $file['size'] = $row->fa_size;
151
152 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
153 if ( $pageCount !== false ) {
154 $vals['pagecount'] = $pageCount;
155 }
156
157 $file['height'] = $row->fa_height;
158 $file['width'] = $row->fa_width;
159 }
160 if ( $fld_description ) {
161 $file['description'] = $row->fa_description;
162 if ( isset( $prop['parseddescription'] ) ) {
163 $file['parseddescription'] = $wgUser->getSkin()->formatComment(
164 $row->fa_description, $title );
165 }
166 }
167 if ( $fld_metadata ) {
168 $file['metadata'] = $row->fa_metadata
169 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
170 : null;
171 }
172 if ( $fld_bitdepth ) {
173 $file['bitdepth'] = $row->fa_bits;
174 }
175 if ( $fld_mime ) {
176 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
177 }
178
179 if ( $row->fa_deleted & File::DELETED_FILE ) {
180 $file['filehidden'] = '';
181 }
182 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
183 $file['commenthidden'] = '';
184 }
185 if ( $row->fa_deleted & File::DELETED_USER ) {
186 $file['userhidden'] = '';
187 }
188 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
189 // This file is deleted for normal admins
190 $file['suppressed'] = '';
191 }
192
193
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
195 if ( !$fit ) {
196 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
197 break;
198 }
199 }
200
201 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
202 }
203
204 public function getAllowedParams() {
205 return array (
206 'from' => null,
207 'to' => null,
208 'prefix' => null,
209 'limit' => array(
210 ApiBase::PARAM_DFLT => 10,
211 ApiBase::PARAM_TYPE => 'limit',
212 ApiBase::PARAM_MIN => 1,
213 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
214 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
215 ),
216 'dir' => array(
217 ApiBase::PARAM_DFLT => 'ascending',
218 ApiBase::PARAM_TYPE => array(
219 'ascending',
220 'descending'
221 )
222 ),
223 'sha1' => null,
224 'sha1base36' => null,
225 'prop' => array(
226 ApiBase::PARAM_DFLT => 'timestamp',
227 ApiBase::PARAM_ISMULTI => true,
228 ApiBase::PARAM_TYPE => array(
229 'sha1',
230 'timestamp',
231 'user',
232 'size',
233 'dimensions',
234 'description',
235 'parseddescription',
236 'mime',
237 'metadata',
238 'bitdepth'
239 ),
240 ),
241 );
242 }
243
244 public function getParamDescription() {
245 return array(
246 'from' => 'The image title to start enumerating from',
247 'to' => 'The image title to stop enumerating at',
248 'prefix' => 'Search for all image titles that begin with this value',
249 'dir' => 'The direction in which to list',
250 'limit' => 'How many images to return in total',
251 'sha1' => "SHA1 hash of image. Overrides {$this->getModulePrefix()}sha1base36. Disabled in Miser Mode",
252 'sha1base36' => 'SHA1 hash of image in base 36 (used in MediaWiki). Disabled in Miser Mode',
253 'prop' => array(
254 'What image information to get:',
255 ' sha1 - Adds SHA-1 hash for the image',
256 ' timestamp - Adds timestamp for the uploaded version',
257 ' user - Adds user who uploaded the image version',
258 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
259 ' dimensions - Alias for size',
260 ' description - Adds description the image version',
261 ' parseddescription - Parse the description on the version',
262 ' mime - Adds MIME of the image',
263 ' metadata - Lists EXIF metadata for the version of the image',
264 ' bitdepth - Adds the bit depth of the version',
265 ),
266 );
267 }
268
269 public function getDescription() {
270 return 'Enumerate all deleted files sequentially';
271 }
272
273 public function getPossibleErrors() {
274 return array_merge( parent::getPossibleErrors(), array(
275 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
276 array( 'code' => 'hashsearchdisabled', 'info' => 'Search by hash disabled in Miser Mode' ),
277 ) );
278 }
279
280 protected function getExamples() {
281 return array(
282 'Simple Use',
283 ' Show a list of all deleted files',
284 ' api.php?action=query&list=filearchive',
285 );
286 }
287
288 public function getVersion() {
289 return __CLASS__ . ': $Id$';
290 }
291 }