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