Part of Bug 19195 - Make user IDs more readily available with the API
[lhc/web/wiklou.git] / includes / api / ApiQueryImageInfo.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on July 6, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query action to get image information and upload history.
34 *
35 * @ingroup API
36 */
37 class ApiQueryImageInfo extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'ii' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45
46 $prop = array_flip( $params['prop'] );
47
48 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 ) {
49 $this->dieUsage( 'iiurlheight cannot be used without iiurlwidth', 'iiurlwidth' );
50 }
51
52 if ( $params['urlwidth'] != - 1 ) {
53 $scale = array();
54 $scale['width'] = $params['urlwidth'];
55 $scale['height'] = $params['urlheight'];
56 } else {
57 $scale = null;
58 }
59
60 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
61 if ( !empty( $pageIds[NS_FILE] ) ) {
62 $titles = array_keys( $pageIds[NS_FILE] );
63 asort( $titles ); // Ensure the order is always the same
64
65 $skip = false;
66 if ( !is_null( $params['continue'] ) ) {
67 $skip = true;
68 $cont = explode( '|', $params['continue'] );
69 if ( count( $cont ) != 2 ) {
70 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
71 'value returned by the previous query', '_badcontinue' );
72 }
73 $fromTitle = strval( $cont[0] );
74 $fromTimestamp = $cont[1];
75 // Filter out any titles before $fromTitle
76 foreach ( $titles as $key => $title ) {
77 if ( $title < $fromTitle ) {
78 unset( $titles[$key] );
79 } else {
80 break;
81 }
82 }
83 }
84
85 $result = $this->getResult();
86 $images = RepoGroup::singleton()->findFiles( $titles );
87 foreach ( $images as $img ) {
88 // Skip redirects
89 if ( $img->getOriginalTitle()->isRedirect() ) {
90 continue;
91 }
92
93 $start = $skip ? $fromTimestamp : $params['start'];
94 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
95
96 $fit = $result->addValue(
97 array( 'query', 'pages', intval( $pageId ) ),
98 'imagerepository', $img->getRepoName()
99 );
100 if ( !$fit ) {
101 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
102 // The user is screwed. imageinfo can't be solely
103 // responsible for exceeding the limit in this case,
104 // so set a query-continue that just returns the same
105 // thing again. When the violating queries have been
106 // out-continued, the result will get through
107 $this->setContinueEnumParameter( 'start',
108 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
109 } else {
110 $this->setContinueEnumParameter( 'continue',
111 $this->getContinueStr( $img ) );
112 }
113 break;
114 }
115
116 // Get information about the current version first
117 // Check that the current version is within the start-end boundaries
118 $gotOne = false;
119 if (
120 ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
121 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] )
122 )
123 {
124 $gotOne = true;
125 $fit = $this->addPageSubItem( $pageId,
126 self::getInfo( $img, $prop, $result, $scale ) );
127 if ( !$fit ) {
128 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
129 // See the 'the user is screwed' comment above
130 $this->setContinueEnumParameter( 'start',
131 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
132 } else {
133 $this->setContinueEnumParameter( 'continue',
134 $this->getContinueStr( $img ) );
135 }
136 break;
137 }
138 }
139
140 // Now get the old revisions
141 // Get one more to facilitate query-continue functionality
142 $count = ( $gotOne ? 1 : 0 );
143 $oldies = $img->getHistory( $params['limit'] - $count + 1, $start, $params['end'] );
144 foreach ( $oldies as $oldie ) {
145 if ( ++$count > $params['limit'] ) {
146 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
147 // Only set a query-continue if there was only one title
148 if ( count( $pageIds[NS_FILE] ) == 1 ) {
149 $this->setContinueEnumParameter( 'start',
150 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
151 }
152 break;
153 }
154 $fit = $this->addPageSubItem( $pageId,
155 self::getInfo( $oldie, $prop, $result ) );
156 if ( !$fit ) {
157 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
158 $this->setContinueEnumParameter( 'start',
159 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
160 } else {
161 $this->setContinueEnumParameter( 'continue',
162 $this->getContinueStr( $oldie ) );
163 }
164 break;
165 }
166 }
167 if ( !$fit ) {
168 break;
169 }
170 $skip = false;
171 }
172
173 $data = $this->getResultData();
174 foreach ( $data['query']['pages'] as $pageid => $arr ) {
175 if ( !isset( $arr['imagerepository'] ) ) {
176 $result->addValue(
177 array( 'query', 'pages', $pageid ),
178 'imagerepository', ''
179 );
180 }
181 // The above can't fail because it doesn't increase the result size
182 }
183 }
184 }
185
186 /**
187 * Get result information for an image revision
188 *
189 * @param $file File object
190 * @param $prop Array of properties to get (in the keys)
191 * @param $result ApiResult object
192 * @param $scale Array containing 'width' and 'height' items, or null
193 * @return Array: result array
194 */
195 static function getInfo( $file, $prop, $result, $scale = null ) {
196 $vals = array();
197 if ( isset( $prop['timestamp'] ) ) {
198 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
199 }
200 if ( isset( $prop['user'] ) || isset( $prop['userid'] ) ) {
201
202 if ( isset( $prop['user'] ) ) {
203 $vals['user'] = $file->getUser();
204 } else if ( isset( $prop['userid'] ) ) {
205 $vals['userid'] = $file->getUser( 'id' );
206 }
207 if ( !$file->getUser( 'id' ) ) {
208 $vals['anon'] = '';
209 }
210 }
211 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
212 $vals['size'] = intval( $file->getSize() );
213 $vals['width'] = intval( $file->getWidth() );
214 $vals['height'] = intval( $file->getHeight() );
215 }
216 if ( isset( $prop['url'] ) ) {
217 if ( !is_null( $scale ) && !$file->isOld() ) {
218 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
219 if ( $mto && !$mto->isError() ) {
220 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
221
222 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
223 // thumbnail sizes for the thumbnail actual size
224 if ( $mto->getUrl() !== $file->getUrl() ) {
225 $vals['thumbwidth'] = intval( $mto->getWidth() );
226 $vals['thumbheight'] = intval( $mto->getHeight() );
227 } else {
228 $vals['thumbwidth'] = intval( $file->getWidth() );
229 $vals['thumbheight'] = intval( $file->getHeight() );
230 }
231
232 if ( isset( $prop['thumbmime'] ) ) {
233 $thumbFile = UnregisteredLocalFile::newFromPath( $mto->getPath(), false );
234 $vals['thumbmime'] = $thumbFile->getMimeType();
235 }
236 }
237 }
238 $vals['url'] = $file->getFullURL();
239 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
240 }
241 if ( isset( $prop['comment'] ) ) {
242 $vals['comment'] = $file->getDescription();
243 }
244 if ( isset( $prop['sha1'] ) ) {
245 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
246 }
247 if ( isset( $prop['metadata'] ) ) {
248 $metadata = $file->getMetadata();
249 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
250 }
251 if ( isset( $prop['mime'] ) ) {
252 $vals['mime'] = $file->getMimeType();
253 }
254
255 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
256 $vals['archivename'] = $file->getArchiveName();
257 }
258
259 if ( isset( $prop['bitdepth'] ) ) {
260 $vals['bitdepth'] = $file->getBitDepth();
261 }
262
263 return $vals;
264 }
265
266 public static function processMetaData( $metadata, $result ) {
267 $retval = array();
268 if ( is_array( $metadata ) ) {
269 foreach ( $metadata as $key => $value ) {
270 $r = array( 'name' => $key );
271 if ( is_array( $value ) ) {
272 $r['value'] = self::processMetaData( $value, $result );
273 } else {
274 $r['value'] = $value;
275 }
276 $retval[] = $r;
277 }
278 }
279 $result->setIndexedTagName( $retval, 'metadata' );
280 return $retval;
281 }
282
283 public function getCacheMode( $params ) {
284 return 'public';
285 }
286
287 private function getContinueStr( $img ) {
288 return $img->getOriginalTitle()->getText() .
289 '|' . $img->getTimestamp();
290 }
291
292 public function getAllowedParams() {
293 return array(
294 'prop' => array(
295 ApiBase::PARAM_ISMULTI => true,
296 ApiBase::PARAM_DFLT => 'timestamp|user',
297 ApiBase::PARAM_TYPE => self::getPropertyNames()
298 ),
299 'limit' => array(
300 ApiBase::PARAM_TYPE => 'limit',
301 ApiBase::PARAM_DFLT => 1,
302 ApiBase::PARAM_MIN => 1,
303 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
304 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
305 ),
306 'start' => array(
307 ApiBase::PARAM_TYPE => 'timestamp'
308 ),
309 'end' => array(
310 ApiBase::PARAM_TYPE => 'timestamp'
311 ),
312 'urlwidth' => array(
313 ApiBase::PARAM_TYPE => 'integer',
314 ApiBase::PARAM_DFLT => - 1
315 ),
316 'urlheight' => array(
317 ApiBase::PARAM_TYPE => 'integer',
318 ApiBase::PARAM_DFLT => - 1
319 ),
320 'continue' => null,
321 );
322 }
323
324 /**
325 * Returns all possible parameters to iiprop
326 */
327 public static function getPropertyNames() {
328 return array(
329 'timestamp',
330 'user',
331 'userid',
332 'comment',
333 'url',
334 'size',
335 'dimensions', // For backwards compatibility with Allimages
336 'sha1',
337 'mime',
338 'thumbmime',
339 'metadata',
340 'archivename',
341 'bitdepth',
342 );
343 }
344
345 public function getParamDescription() {
346 $p = $this->getModulePrefix();
347 return array(
348 'prop' => array(
349 'What image information to get:',
350 ' timestamp - Adds timestamp for the uploaded version',
351 ' user - Adds the user who uploaded the image version',
352 ' userid - Add the user id that uploaded the image version',
353 ' comment - Comment on the version',
354 ' url - Gives URL to the image and the description page',
355 ' size - Adds the size of the image in bytes and the height and width',
356 ' dimensions - Alias for size',
357 ' sha1 - Adds sha1 hash for the image',
358 ' mime - Adds MIME of the image',
359 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
360 ' metadata - Lists EXIF metadata for the version of the image',
361 ' archivename - Adds the file name of the archive version for non-latest versions',
362 ' bitdepth - Adds the bit depth of the version',
363 ),
364 'limit' => 'How many image revisions to return',
365 'start' => 'Timestamp to start listing from',
366 'end' => 'Timestamp to stop listing at',
367 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
368 'Only the current version of the image can be scaled' ),
369 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
370 'continue' => 'When more results are available, use this to continue',
371 );
372 }
373
374 public function getDescription() {
375 return 'Returns image information and upload history';
376 }
377
378 public function getPossibleErrors() {
379 return array_merge( parent::getPossibleErrors(), array(
380 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
381 ) );
382 }
383
384 protected function getExamples() {
385 return array(
386 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
387 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
388 );
389 }
390
391 public function getVersion() {
392 return __CLASS__ . ': $Id$';
393 }
394 }