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 ) {
40 parent
::__construct( $query, $moduleName, 'ii' );
43 public function execute() {
44 $params = $this->extractRequestParams();
46 $prop = array_flip( $params['prop'] );
48 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 ) {
49 $this->dieUsage( 'iiurlheight cannot be used without iiurlwidth', 'iiurlwidth' );
52 if ( $params['urlwidth'] != - 1 ) {
54 $scale['width'] = $params['urlwidth'];
55 $scale['height'] = $params['urlheight'];
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
66 if ( !is_null( $params['continue'] ) ) {
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' );
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] );
85 $result = $this->getResult();
86 $images = RepoGroup
::singleton()->findFiles( $titles );
87 foreach ( $images as $img ) {
89 if ( $img->getOriginalTitle()->isRedirect() ) {
93 $start = $skip ?
$fromTimestamp : $params['start'];
94 $pageId = $pageIds[NS_IMAGE
][ $img->getOriginalTitle()->getDBkey() ];
96 $fit = $result->addValue(
97 array( 'query', 'pages', intval( $pageId ) ),
98 'imagerepository', $img->getRepoName()
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() ) );
110 $this->setContinueEnumParameter( 'continue',
111 $this->getContinueStr( $img ) );
116 // Get information about the current version first
117 // Check that the current version is within the start-end boundaries
120 ( is_null( $start ) ||
$img->getTimestamp() <= $start ) &&
121 ( is_null( $params['end'] ) ||
$img->getTimestamp() >= $params['end'] )
125 $fit = $this->addPageSubItem( $pageId,
126 self
::getInfo( $img, $prop, $result, $scale ) );
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() ) );
133 $this->setContinueEnumParameter( 'continue',
134 $this->getContinueStr( $img ) );
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() ) );
154 $fit = $this->addPageSubItem( $pageId,
155 self
::getInfo( $oldie, $prop, $result ) );
157 if ( count( $pageIds[NS_IMAGE
] ) == 1 ) {
158 $this->setContinueEnumParameter( 'start',
159 wfTimestamp( TS_ISO_8601
, $oldie->getTimestamp() ) );
161 $this->setContinueEnumParameter( 'continue',
162 $this->getContinueStr( $oldie ) );
173 $data = $this->getResultData();
174 foreach ( $data['query']['pages'] as $pageid => $arr ) {
175 if ( !isset( $arr['imagerepository'] ) ) {
177 array( 'query', 'pages', $pageid ),
178 'imagerepository', ''
181 // The above can't fail because it doesn't increase the result size
187 * Get result information for an image revision
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
195 static function getInfo( $file, $prop, $result, $scale = null ) {
197 if ( isset( $prop['timestamp'] ) ) {
198 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $file->getTimestamp() );
200 if ( isset( $prop['user'] ) ) {
201 $vals['user'] = $file->getUser();
202 if ( !$file->getUser( 'id' ) ) {
206 if ( isset( $prop['size'] ) ||
isset( $prop['dimensions'] ) ) {
207 $vals['size'] = intval( $file->getSize() );
208 $vals['width'] = intval( $file->getWidth() );
209 $vals['height'] = intval( $file->getHeight() );
211 if ( isset( $prop['url'] ) ) {
212 if ( !is_null( $scale ) && !$file->isOld() ) {
213 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
214 if ( $mto && !$mto->isError() ) {
215 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
217 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
218 // thumbnail sizes for the thumbnail actual size
219 if ( $mto->getUrl() !== $file->getUrl() ) {
220 $vals['thumbwidth'] = intval( $mto->getWidth() );
221 $vals['thumbheight'] = intval( $mto->getHeight() );
223 $vals['thumbwidth'] = intval( $file->getWidth() );
224 $vals['thumbheight'] = intval( $file->getHeight() );
227 if ( isset( $prop['thumbmime'] ) ) {
228 $thumbFile = UnregisteredLocalFile
::newFromPath( $mto->getPath(), false );
229 $vals['thumbmime'] = $thumbFile->getMimeType();
233 $vals['url'] = $file->getFullURL();
234 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
236 if ( isset( $prop['comment'] ) ) {
237 $vals['comment'] = $file->getDescription();
239 if ( isset( $prop['sha1'] ) ) {
240 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
242 if ( isset( $prop['metadata'] ) ) {
243 $metadata = $file->getMetadata();
244 $vals['metadata'] = $metadata ? self
::processMetaData( unserialize( $metadata ), $result ) : null;
246 if ( isset( $prop['mime'] ) ) {
247 $vals['mime'] = $file->getMimeType();
250 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
251 $vals['archivename'] = $file->getArchiveName();
254 if ( isset( $prop['bitdepth'] ) ) {
255 $vals['bitdepth'] = $file->getBitDepth();
261 public static function processMetaData( $metadata, $result ) {
263 if ( is_array( $metadata ) ) {
264 foreach ( $metadata as $key => $value ) {
265 $r = array( 'name' => $key );
266 if ( is_array( $value ) ) {
267 $r['value'] = self
::processMetaData( $value, $result );
269 $r['value'] = $value;
274 $result->setIndexedTagName( $retval, 'metadata' );
278 public function getCacheMode( $params ) {
282 private function getContinueStr( $img ) {
283 return $img->getOriginalTitle()->getText() .
284 '|' . $img->getTimestamp();
287 public function getAllowedParams() {
290 ApiBase
::PARAM_ISMULTI
=> true,
291 ApiBase
::PARAM_DFLT
=> 'timestamp|user',
292 ApiBase
::PARAM_TYPE
=> self
::getPropertyNames()
295 ApiBase
::PARAM_TYPE
=> 'limit',
296 ApiBase
::PARAM_DFLT
=> 1,
297 ApiBase
::PARAM_MIN
=> 1,
298 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
299 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
302 ApiBase
::PARAM_TYPE
=> 'timestamp'
305 ApiBase
::PARAM_TYPE
=> 'timestamp'
308 ApiBase
::PARAM_TYPE
=> 'integer',
309 ApiBase
::PARAM_DFLT
=> - 1
311 'urlheight' => array(
312 ApiBase
::PARAM_TYPE
=> 'integer',
313 ApiBase
::PARAM_DFLT
=> - 1
320 * Returns all possible parameters to iiprop
322 public static function getPropertyNames() {
329 'dimensions', // For backwards compatibility with Allimages
339 public function getParamDescription() {
340 $p = $this->getModulePrefix();
343 'What image information to get:',
344 ' timestamp - Adds timestamp for the uploaded version',
345 ' user - Adds user for uploaded the image version',
346 ' comment - Comment on the version',
347 ' url - Gives URL to the image and the description page',
348 ' size - Adds the size of the image in bytes and the height and width',
349 ' dimensions - Alias for size',
350 ' sha1 - Adds sha1 hash for the image',
351 ' mime - Adds MIME of the image',
352 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
353 ' metadata - Lists EXIF metadata for the version of the image',
354 ' archivename - Adds the file name of the archive version for non-latest versions',
355 ' bitdepth - Adds the bit depth of the version',
357 'limit' => 'How many image revisions to return',
358 'start' => 'Timestamp to start listing from',
359 'end' => 'Timestamp to stop listing at',
360 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
361 'Only the current version of the image can be scaled' ),
362 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
363 'continue' => 'When more results are available, use this to continue',
367 public function getDescription() {
368 return 'Returns image information and upload history';
371 public function getPossibleErrors() {
372 return array_merge( parent
::getPossibleErrors(), array(
373 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
377 protected function getExamples() {
379 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
380 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
384 public function getVersion() {
385 return __CLASS__
. ': $Id$';