Fixed up some doxygen warnings
[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, $prefix = 'ii' ) {
40 // We allow a subclass to override the prefix, to create a related API module.
41 // Some other parts of MediaWiki construct this with a null $prefix, which used to be ignored when this only took two arguments
42 if ( is_null( $prefix ) ) {
43 $prefix = 'ii';
44 }
45 parent::__construct( $query, $moduleName, $prefix );
46 }
47
48 public function execute() {
49 $params = $this->extractRequestParams();
50
51 $prop = array_flip( $params['prop'] );
52
53 $scale = $this->getScale( $params );
54
55 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
56 if ( !empty( $pageIds[NS_FILE] ) ) {
57 $titles = array_keys( $pageIds[NS_FILE] );
58 asort( $titles ); // Ensure the order is always the same
59
60 $skip = false;
61 if ( !is_null( $params['continue'] ) ) {
62 $skip = true;
63 $cont = explode( '|', $params['continue'] );
64 if ( count( $cont ) != 2 ) {
65 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
66 'value returned by the previous query', '_badcontinue' );
67 }
68 $fromTitle = strval( $cont[0] );
69 $fromTimestamp = $cont[1];
70 // Filter out any titles before $fromTitle
71 foreach ( $titles as $key => $title ) {
72 if ( $title < $fromTitle ) {
73 unset( $titles[$key] );
74 } else {
75 break;
76 }
77 }
78 }
79
80 $result = $this->getResult();
81 $images = RepoGroup::singleton()->findFiles( $titles );
82 foreach ( $images as $img ) {
83 // Skip redirects
84 if ( $img->getOriginalTitle()->isRedirect() ) {
85 continue;
86 }
87
88 $start = $skip ? $fromTimestamp : $params['start'];
89 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
90
91 $fit = $result->addValue(
92 array( 'query', 'pages', intval( $pageId ) ),
93 'imagerepository', $img->getRepoName()
94 );
95 if ( !$fit ) {
96 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
97 // The user is screwed. imageinfo can't be solely
98 // responsible for exceeding the limit in this case,
99 // so set a query-continue that just returns the same
100 // thing again. When the violating queries have been
101 // out-continued, the result will get through
102 $this->setContinueEnumParameter( 'start',
103 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
104 } else {
105 $this->setContinueEnumParameter( 'continue',
106 $this->getContinueStr( $img ) );
107 }
108 break;
109 }
110
111 // Get information about the current version first
112 // Check that the current version is within the start-end boundaries
113 $gotOne = false;
114 if (
115 ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
116 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] )
117 )
118 {
119 $gotOne = true;
120 $fit = $this->addPageSubItem( $pageId,
121 self::getInfo( $img, $prop, $result, $scale ) );
122 if ( !$fit ) {
123 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
124 // See the 'the user is screwed' comment above
125 $this->setContinueEnumParameter( 'start',
126 wfTimestamp( TS_ISO_8601, $img->getTimestamp() ) );
127 } else {
128 $this->setContinueEnumParameter( 'continue',
129 $this->getContinueStr( $img ) );
130 }
131 break;
132 }
133 }
134
135 // Now get the old revisions
136 // Get one more to facilitate query-continue functionality
137 $count = ( $gotOne ? 1 : 0 );
138 $oldies = $img->getHistory( $params['limit'] - $count + 1, $start, $params['end'] );
139 foreach ( $oldies as $oldie ) {
140 if ( ++$count > $params['limit'] ) {
141 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
142 // Only set a query-continue if there was only one title
143 if ( count( $pageIds[NS_FILE] ) == 1 ) {
144 $this->setContinueEnumParameter( 'start',
145 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
146 }
147 break;
148 }
149 $fit = $this->addPageSubItem( $pageId,
150 self::getInfo( $oldie, $prop, $result ) );
151 if ( !$fit ) {
152 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
153 $this->setContinueEnumParameter( 'start',
154 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
155 } else {
156 $this->setContinueEnumParameter( 'continue',
157 $this->getContinueStr( $oldie ) );
158 }
159 break;
160 }
161 }
162 if ( !$fit ) {
163 break;
164 }
165 $skip = false;
166 }
167
168 $data = $this->getResultData();
169 foreach ( $data['query']['pages'] as $pageid => $arr ) {
170 if ( !isset( $arr['imagerepository'] ) ) {
171 $result->addValue(
172 array( 'query', 'pages', $pageid ),
173 'imagerepository', ''
174 );
175 }
176 // The above can't fail because it doesn't increase the result size
177 }
178 }
179 }
180
181 /**
182 * From parameters, construct a 'scale' array
183 * @param $params Array:
184 * @return Array or Null: key-val array of 'width' and 'height', or null
185 */
186 public function getScale( $params ) {
187 $p = $this->getModulePrefix();
188 if ( $params['urlheight'] != -1 && $params['urlwidth'] == -1 ) {
189 $this->dieUsage( "${p}urlheight cannot be used without {$p}urlwidth", "{$p}urlwidth" );
190 }
191
192 if ( $params['urlwidth'] != -1 ) {
193 $scale = array();
194 $scale['width'] = $params['urlwidth'];
195 $scale['height'] = $params['urlheight'];
196 } else {
197 $scale = null;
198 }
199 return $scale;
200 }
201
202
203 /**
204 * Get result information for an image revision
205 *
206 * @param $file File object
207 * @param $prop Array of properties to get (in the keys)
208 * @param $result ApiResult object
209 * @param $scale Array containing 'width' and 'height' items, or null
210 * @return Array: result array
211 */
212 static function getInfo( $file, $prop, $result, $scale = null ) {
213 $vals = array();
214 if ( isset( $prop['timestamp'] ) ) {
215 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
216 }
217 if ( isset( $prop['user'] ) || isset( $prop['userid'] ) ) {
218
219 if ( isset( $prop['user'] ) ) {
220 $vals['user'] = $file->getUser();
221 }
222 if ( isset( $prop['userid'] ) ) {
223 $vals['userid'] = $file->getUser( 'id' );
224 }
225 if ( !$file->getUser( 'id' ) ) {
226 $vals['anon'] = '';
227 }
228 }
229 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
230 $vals['size'] = intval( $file->getSize() );
231 $vals['width'] = intval( $file->getWidth() );
232 $vals['height'] = intval( $file->getHeight() );
233 }
234 if ( isset( $prop['url'] ) ) {
235 if ( !is_null( $scale ) && !$file->isOld() ) {
236 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
237 if ( $mto && !$mto->isError() ) {
238 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
239
240 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
241 // thumbnail sizes for the thumbnail actual size
242 if ( $mto->getUrl() !== $file->getUrl() ) {
243 $vals['thumbwidth'] = intval( $mto->getWidth() );
244 $vals['thumbheight'] = intval( $mto->getHeight() );
245 } else {
246 $vals['thumbwidth'] = intval( $file->getWidth() );
247 $vals['thumbheight'] = intval( $file->getHeight() );
248 }
249
250 if ( isset( $prop['thumbmime'] ) ) {
251 $thumbFile = UnregisteredLocalFile::newFromPath( $mto->getPath(), false );
252 $vals['thumbmime'] = $thumbFile->getMimeType();
253 }
254 }
255 }
256 $vals['url'] = $file->getFullURL();
257 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
258 }
259 if ( isset( $prop['comment'] ) ) {
260 $vals['comment'] = $file->getDescription();
261 }
262 if ( isset( $prop['parsedcomment'] ) ) {
263 global $wgUser;
264 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment(
265 $file->getDescription(), $file->getTitle() );
266 }
267
268 if ( isset( $prop['sha1'] ) ) {
269 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
270 }
271 if ( isset( $prop['metadata'] ) ) {
272 $metadata = $file->getMetadata();
273 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
274 }
275 if ( isset( $prop['mime'] ) ) {
276 $vals['mime'] = $file->getMimeType();
277 }
278
279 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
280 $vals['archivename'] = $file->getArchiveName();
281 }
282
283 if ( isset( $prop['bitdepth'] ) ) {
284 $vals['bitdepth'] = $file->getBitDepth();
285 }
286
287 return $vals;
288 }
289
290 /*
291 *
292 * @param $metadata Array
293 * @param $result ApiResult
294 * @return Array
295 */
296 public static function processMetaData( $metadata, $result ) {
297 $retval = array();
298 if ( is_array( $metadata ) ) {
299 foreach ( $metadata as $key => $value ) {
300 $r = array( 'name' => $key );
301 if ( is_array( $value ) ) {
302 $r['value'] = self::processMetaData( $value, $result );
303 } else {
304 $r['value'] = $value;
305 }
306 $retval[] = $r;
307 }
308 }
309 $result->setIndexedTagName( $retval, 'metadata' );
310 return $retval;
311 }
312
313 public function getCacheMode( $params ) {
314 return 'public';
315 }
316
317 private function getContinueStr( $img ) {
318 return $img->getOriginalTitle()->getText() .
319 '|' . $img->getTimestamp();
320 }
321
322 public function getAllowedParams() {
323 return array(
324 'prop' => array(
325 ApiBase::PARAM_ISMULTI => true,
326 ApiBase::PARAM_DFLT => 'timestamp|user',
327 ApiBase::PARAM_TYPE => self::getPropertyNames()
328 ),
329 'limit' => array(
330 ApiBase::PARAM_TYPE => 'limit',
331 ApiBase::PARAM_DFLT => 1,
332 ApiBase::PARAM_MIN => 1,
333 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
334 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
335 ),
336 'start' => array(
337 ApiBase::PARAM_TYPE => 'timestamp'
338 ),
339 'end' => array(
340 ApiBase::PARAM_TYPE => 'timestamp'
341 ),
342 'urlwidth' => array(
343 ApiBase::PARAM_TYPE => 'integer',
344 ApiBase::PARAM_DFLT => -1
345 ),
346 'urlheight' => array(
347 ApiBase::PARAM_TYPE => 'integer',
348 ApiBase::PARAM_DFLT => -1
349 ),
350 'continue' => null,
351 );
352 }
353
354 /**
355 * Returns all possible parameters to iiprop
356 */
357 public static function getPropertyNames() {
358 return array(
359 'timestamp',
360 'user',
361 'userid',
362 'comment',
363 'parsedcomment',
364 'url',
365 'size',
366 'dimensions', // For backwards compatibility with Allimages
367 'sha1',
368 'mime',
369 'thumbmime',
370 'metadata',
371 'archivename',
372 'bitdepth',
373 );
374 }
375
376
377 /**
378 * Return the API documentation for the parameters.
379 * @return {Array} parameter documentation.
380 */
381 public function getParamDescription() {
382 $p = $this->getModulePrefix();
383 return array(
384 'prop' => array(
385 'What image information to get:',
386 ' timestamp - Adds timestamp for the uploaded version',
387 ' user - Adds the user who uploaded the image version',
388 ' userid - Add the user id that uploaded the image version',
389 ' comment - Comment on the version',
390 ' parsedcomment - Parse the comment on the version',
391 ' url - Gives URL to the image and the description page',
392 ' size - Adds the size of the image in bytes and the height and width',
393 ' dimensions - Alias for size',
394 ' sha1 - Adds sha1 hash for the image',
395 ' mime - Adds MIME of the image',
396 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
397 ' metadata - Lists EXIF metadata for the version of the image',
398 ' archivename - Adds the file name of the archive version for non-latest versions',
399 ' bitdepth - Adds the bit depth of the version',
400 ),
401 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
402 'Only the current version of the image can be scaled' ),
403 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
404 'limit' => 'How many image revisions to return',
405 'start' => 'Timestamp to start listing from',
406 'end' => 'Timestamp to stop listing at',
407 'continue' => 'If the query response includes a continue value, use it here to get another page of results'
408 );
409 }
410
411 public function getDescription() {
412 return 'Returns image information and upload history';
413 }
414
415 public function getPossibleErrors() {
416 return array_merge( parent::getPossibleErrors(), array(
417 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
418 ) );
419 }
420
421 protected function getExamples() {
422 return array(
423 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
424 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
425 );
426 }
427
428 public function getVersion() {
429 return __CLASS__ . ': $Id$';
430 }
431 }