68155b0a76d1fe8a5a501657cd3963f225f5f251
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'] ) ||
isset( $prop['userid'] ) ) {
202 if ( isset( $prop['user'] ) ) {
203 $vals['user'] = $file->getUser();
205 if ( isset( $prop['userid'] ) ) {
206 $vals['userid'] = $file->getUser( 'id' );
208 if ( !$file->getUser( 'id' ) ) {
212 if ( isset( $prop['size'] ) ||
isset( $prop['dimensions'] ) ) {
213 $vals['size'] = intval( $file->getSize() );
214 $vals['width'] = intval( $file->getWidth() );
215 $vals['height'] = intval( $file->getHeight() );
217 if ( isset( $prop['url'] ) ) {
218 if ( !is_null( $scale ) && !$file->isOld() ) {
219 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
220 if ( $mto && !$mto->isError() ) {
221 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
223 // bug 23834 - If the URL's are the same, we haven't resized it, so shouldn't give the wanted
224 // thumbnail sizes for the thumbnail actual size
225 if ( $mto->getUrl() !== $file->getUrl() ) {
226 $vals['thumbwidth'] = intval( $mto->getWidth() );
227 $vals['thumbheight'] = intval( $mto->getHeight() );
229 $vals['thumbwidth'] = intval( $file->getWidth() );
230 $vals['thumbheight'] = intval( $file->getHeight() );
233 if ( isset( $prop['thumbmime'] ) ) {
234 $thumbFile = UnregisteredLocalFile
::newFromPath( $mto->getPath(), false );
235 $vals['thumbmime'] = $thumbFile->getMimeType();
239 $vals['url'] = $file->getFullURL();
240 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
242 if ( isset( $prop['comment'] ) ) {
243 $vals['comment'] = $file->getDescription();
245 if ( isset( $prop['sha1'] ) ) {
246 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
248 if ( isset( $prop['metadata'] ) ) {
249 $metadata = $file->getMetadata();
250 $vals['metadata'] = $metadata ? self
::processMetaData( unserialize( $metadata ), $result ) : null;
252 if ( isset( $prop['mime'] ) ) {
253 $vals['mime'] = $file->getMimeType();
256 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
257 $vals['archivename'] = $file->getArchiveName();
260 if ( isset( $prop['bitdepth'] ) ) {
261 $vals['bitdepth'] = $file->getBitDepth();
269 * param $metadata Array
270 * param $result ApiResult
272 public static function processMetaData( $metadata, $result ) {
274 if ( is_array( $metadata ) ) {
275 foreach ( $metadata as $key => $value ) {
276 $r = array( 'name' => $key );
277 if ( is_array( $value ) ) {
278 $r['value'] = self
::processMetaData( $value, $result );
280 $r['value'] = $value;
285 $result->setIndexedTagName( $retval, 'metadata' );
289 public function getCacheMode( $params ) {
293 private function getContinueStr( $img ) {
294 return $img->getOriginalTitle()->getText() .
295 '|' . $img->getTimestamp();
298 public function getAllowedParams() {
301 ApiBase
::PARAM_ISMULTI
=> true,
302 ApiBase
::PARAM_DFLT
=> 'timestamp|user',
303 ApiBase
::PARAM_TYPE
=> self
::getPropertyNames()
306 ApiBase
::PARAM_TYPE
=> 'limit',
307 ApiBase
::PARAM_DFLT
=> 1,
308 ApiBase
::PARAM_MIN
=> 1,
309 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
310 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
313 ApiBase
::PARAM_TYPE
=> 'timestamp'
316 ApiBase
::PARAM_TYPE
=> 'timestamp'
319 ApiBase
::PARAM_TYPE
=> 'integer',
320 ApiBase
::PARAM_DFLT
=> - 1
322 'urlheight' => array(
323 ApiBase
::PARAM_TYPE
=> 'integer',
324 ApiBase
::PARAM_DFLT
=> - 1
331 * Returns all possible parameters to iiprop
333 public static function getPropertyNames() {
341 'dimensions', // For backwards compatibility with Allimages
351 public function getParamDescription() {
352 $p = $this->getModulePrefix();
355 'What image information to get:',
356 ' timestamp - Adds timestamp for the uploaded version',
357 ' user - Adds the user who uploaded the image version',
358 ' userid - Add the user id that uploaded the image version',
359 ' comment - Comment on the version',
360 ' url - Gives URL to the image and the description page',
361 ' size - Adds the size of the image in bytes and the height and width',
362 ' dimensions - Alias for size',
363 ' sha1 - Adds sha1 hash for the image',
364 ' mime - Adds MIME of the image',
365 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
366 ' metadata - Lists EXIF metadata for the version of the image',
367 ' archivename - Adds the file name of the archive version for non-latest versions',
368 ' bitdepth - Adds the bit depth of the version',
370 'limit' => 'How many image revisions to return',
371 'start' => 'Timestamp to start listing from',
372 'end' => 'Timestamp to stop listing at',
373 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
374 'Only the current version of the image can be scaled' ),
375 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
376 'continue' => 'When more results are available, use this to continue',
380 public function getDescription() {
381 return 'Returns image information and upload history';
384 public function getPossibleErrors() {
385 return array_merge( parent
::getPossibleErrors(), array(
386 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
390 protected function getExamples() {
392 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
393 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
397 public function getVersion() {
398 return __CLASS__
. ': $Id$';