3 * API for MediaWiki 1.8+
5 * Created on July 6, 2007
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to get image information and upload history.
37 class ApiQueryImageInfo extends ApiQueryBase {
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 ) ) {
45 parent::__construct( $query, $moduleName, $prefix );
48 public function execute() {
49 $params = $this->extractRequestParams();
51 $prop = array_flip( $params['prop'] );
53 $scale = $this->getScale( $params );
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
61 if ( !is_null( $params['continue'] ) ) {
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' );
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] );
80 $result = $this->getResult();
81 $images = RepoGroup::singleton()->findFiles( $titles );
82 foreach ( $images as $img ) {
84 if ( $img->getOriginalTitle()->isRedirect() ) {
88 $start = $skip ? $fromTimestamp : $params['start'];
89 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
91 $fit = $result->addValue(
92 array( 'query', 'pages', intval( $pageId ) ),
93 'imagerepository', $img->getRepoName()
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() ) );
105 $this->setContinueEnumParameter( 'continue',
106 $this->getContinueStr( $img ) );
111 // Get information about the current version first
112 // Check that the current version is within the start-end boundaries
115 ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
116 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] )
120 $fit = $this->addPageSubItem( $pageId,
121 self::getInfo( $img, $prop, $result, $scale ) );
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() ) );
128 $this->setContinueEnumParameter( 'continue',
129 $this->getContinueStr( $img ) );
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() ) );
149 $fit = $this->addPageSubItem( $pageId,
150 self::getInfo( $oldie, $prop, $result ) );
152 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
153 $this->setContinueEnumParameter( 'start',
154 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
156 $this->setContinueEnumParameter( 'continue',
157 $this->getContinueStr( $oldie ) );
168 $data = $this->getResultData();
169 foreach ( $data['query']['pages'] as $pageid => $arr ) {
170 if ( !isset( $arr['imagerepository'] ) ) {
172 array( 'query', 'pages', $pageid ),
173 'imagerepository', ''
176 // The above can't fail because it doesn't increase the result size
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
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" );
192 if ( $params['urlwidth'] != -1 ) {
194 $scale['width'] = $params['urlwidth'];
195 $scale['height'] = $params['urlheight'];
204 * Get result information for an image revision
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
212 static function getInfo( $file, $prop, $result, $scale = null ) {
214 // Timestamp is shown even if the file is revdelete'd in interface
216 if ( isset( $prop['timestamp'] ) ) {
217 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
220 $user = isset( $prop['user'] );
221 $userid = isset( $prop['userid'] );
223 if ( $user || $userid ) {
224 if ( $file->isDeleted( File::DELETED_USER ) ) {
225 $vals['userhidden'] = '';
228 $vals['user'] = $file->getUser();
231 $vals['userid'] = $file->getUser( 'id' );
233 if ( !$file->getUser( 'id' ) ) {
239 // This is shown even if the file is revdelete'd in interface
241 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
242 $vals['size'] = intval( $file->getSize() );
243 $vals['width'] = intval( $file->getWidth() );
244 $vals['height'] = intval( $file->getHeight() );
246 $pageCount = $file->pageCount();
247 if ( $pageCount !== false ) {
248 $vals['pagecount'] = $pageCount;
252 $pcomment = isset( $prop['parsedcomment'] );
253 $comment = isset( $prop['comment'] );
255 if ( $pcomment || $comment ) {
256 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
257 $vals['commenthidden'] = '';
261 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment(
262 $file->getDescription(), $file->getTitle() );
265 $vals['comment'] = $file->getDescription();
270 $url = isset( $prop['url'] );
271 $sha1 = isset( $prop['sha1'] );
272 $meta = isset( $prop['metadata'] );
273 $mime = isset( $prop['mime'] );
274 $archive = isset( $prop['archivename'] );
275 $bitdepth = isset( $prop['bitdepth'] );
277 if ( ( $url || $sha1 || $meta || $mime || $archive || $bitdepth )
278 && $file->isDeleted( File::DELETED_FILE ) ) {
279 $vals['filehidden'] = '';
281 //Early return, tidier than indenting all following things one level
286 if ( !is_null( $scale ) && !$file->isOld() ) {
287 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
288 if ( $mto && !$mto->isError() ) {
289 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
291 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
292 // thumbnail sizes for the thumbnail actual size
293 if ( $mto->getUrl() !== $file->getUrl() ) {
294 $vals['thumbwidth'] = intval( $mto->getWidth() );
295 $vals['thumbheight'] = intval( $mto->getHeight() );
297 $vals['thumbwidth'] = intval( $file->getWidth() );
298 $vals['thumbheight'] = intval( $file->getHeight() );
301 if ( isset( $prop['thumbmime'] ) && $file->getHandler() ) {
302 list( $ext, $mime ) = $file->getHandler()->getThumbType(
303 substr( $mto->getPath(), strrpos( $mto->getPath(), '.' ) + 1 ),
304 $file->getMimeType(), $thumbParams );
305 $vals['thumbmime'] = $mime;
307 } else if ( $mto && $mto->isError() ) {
308 $vals['thumberror'] = $mto->toText();
311 $vals['url'] = $file->getFullURL();
312 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
316 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
320 $metadata = $file->getMetadata();
321 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
325 $vals['mime'] = $file->getMimeType();
328 if ( $archive && $file->isOld() ) {
329 $vals['archivename'] = $file->getArchiveName();
333 $vals['bitdepth'] = $file->getBitDepth();
341 * @param $metadata Array
342 * @param $result ApiResult
345 public static function processMetaData( $metadata, $result ) {
347 if ( is_array( $metadata ) ) {
348 foreach ( $metadata as $key => $value ) {
349 $r = array( 'name' => $key );
350 if ( is_array( $value ) ) {
351 $r['value'] = self::processMetaData( $value, $result );
353 $r['value'] = $value;
358 $result->setIndexedTagName( $retval, 'metadata' );
362 public function getCacheMode( $params ) {
366 private function getContinueStr( $img ) {
367 return $img->getOriginalTitle()->getText() .
368 '|' . $img->getTimestamp();
371 public function getAllowedParams() {
374 ApiBase::PARAM_ISMULTI => true,
375 ApiBase::PARAM_DFLT => 'timestamp|user',
376 ApiBase::PARAM_TYPE => self::getPropertyNames()
379 ApiBase::PARAM_TYPE => 'limit',
380 ApiBase::PARAM_DFLT => 1,
381 ApiBase::PARAM_MIN => 1,
382 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
383 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
386 ApiBase::PARAM_TYPE => 'timestamp'
389 ApiBase::PARAM_TYPE => 'timestamp'
392 ApiBase::PARAM_TYPE => 'integer',
393 ApiBase::PARAM_DFLT => -1
395 'urlheight' => array(
396 ApiBase::PARAM_TYPE => 'integer',
397 ApiBase::PARAM_DFLT => -1
404 * Returns all possible parameters to iiprop
406 public static function getPropertyNames() {
415 'dimensions', // For backwards compatibility with Allimages
427 * Return the API documentation for the parameters.
428 * @return {Array} parameter documentation.
430 public function getParamDescription() {
431 $p = $this->getModulePrefix();
434 'What image information to get:',
435 ' timestamp - Adds timestamp for the uploaded version',
436 ' user - Adds the user who uploaded the image version',
437 ' userid - Add the user id that uploaded the image version',
438 ' comment - Comment on the version',
439 ' parsedcomment - Parse the comment on the version',
440 ' url - Gives URL to the image and the description page',
441 ' size - Adds the size of the image in bytes and the height and width',
442 ' dimensions - Alias for size',
443 ' sha1 - Adds sha1 hash for the image',
444 ' mime - Adds MIME of the image',
445 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
446 ' metadata - Lists EXIF metadata for the version of the image',
447 ' archivename - Adds the file name of the archive version for non-latest versions',
448 ' bitdepth - Adds the bit depth of the version',
450 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
451 'Only the current version of the image can be scaled' ),
452 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
453 'limit' => 'How many image revisions to return',
454 'start' => 'Timestamp to start listing from',
455 'end' => 'Timestamp to stop listing at',
456 'continue' => 'If the query response includes a continue value, use it here to get another page of results'
460 public function getDescription() {
461 return 'Returns image information and upload history';
464 public function getPossibleErrors() {
465 return array_merge( parent::getPossibleErrors(), array(
466 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
470 protected function getExamples() {
472 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
473 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
477 public function getVersion() {
478 return __CLASS__ . ': $Id$';