Followup r71831, it's not mutually exclusive!
[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 ) {
40 parent::__construct( $query, $moduleName, 'ii' );
41 }
42
43 public function execute() {
44 $params = $this->extractRequestParams();
45
46 $prop = array_flip( $params['prop'] );
47
48 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 ) {
49 $this->dieUsage( 'iiurlheight cannot be used without iiurlwidth', 'iiurlwidth' );
50 }
51
52 if ( $params['urlwidth'] != - 1 ) {
53 $scale = array();
54 $scale['width'] = $params['urlwidth'];
55 $scale['height'] = $params['urlheight'];
56 } else {
57 $scale = null;
58 }
59
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
64
65 $skip = false;
66 if ( !is_null( $params['continue'] ) ) {
67 $skip = true;
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' );
72 }
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] );
79 } else {
80 break;
81 }
82 }
83 }
84
85 $result = $this->getResult();
86 $images = RepoGroup::singleton()->findFiles( $titles );
87 foreach ( $images as $img ) {
88 // Skip redirects
89 if ( $img->getOriginalTitle()->isRedirect() ) {
90 continue;
91 }
92
93 $start = $skip ? $fromTimestamp : $params['start'];
94 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
95
96 $fit = $result->addValue(
97 array( 'query', 'pages', intval( $pageId ) ),
98 'imagerepository', $img->getRepoName()
99 );
100 if ( !$fit ) {
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() ) );
109 } else {
110 $this->setContinueEnumParameter( 'continue',
111 $this->getContinueStr( $img ) );
112 }
113 break;
114 }
115
116 // Get information about the current version first
117 // Check that the current version is within the start-end boundaries
118 $gotOne = false;
119 if (
120 ( is_null( $start ) || $img->getTimestamp() <= $start ) &&
121 ( is_null( $params['end'] ) || $img->getTimestamp() >= $params['end'] )
122 )
123 {
124 $gotOne = true;
125 $fit = $this->addPageSubItem( $pageId,
126 self::getInfo( $img, $prop, $result, $scale ) );
127 if ( !$fit ) {
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() ) );
132 } else {
133 $this->setContinueEnumParameter( 'continue',
134 $this->getContinueStr( $img ) );
135 }
136 break;
137 }
138 }
139
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() ) );
151 }
152 break;
153 }
154 $fit = $this->addPageSubItem( $pageId,
155 self::getInfo( $oldie, $prop, $result ) );
156 if ( !$fit ) {
157 if ( count( $pageIds[NS_IMAGE] ) == 1 ) {
158 $this->setContinueEnumParameter( 'start',
159 wfTimestamp( TS_ISO_8601, $oldie->getTimestamp() ) );
160 } else {
161 $this->setContinueEnumParameter( 'continue',
162 $this->getContinueStr( $oldie ) );
163 }
164 break;
165 }
166 }
167 if ( !$fit ) {
168 break;
169 }
170 $skip = false;
171 }
172
173 $data = $this->getResultData();
174 foreach ( $data['query']['pages'] as $pageid => $arr ) {
175 if ( !isset( $arr['imagerepository'] ) ) {
176 $result->addValue(
177 array( 'query', 'pages', $pageid ),
178 'imagerepository', ''
179 );
180 }
181 // The above can't fail because it doesn't increase the result size
182 }
183 }
184 }
185
186 /**
187 * Get result information for an image revision
188 *
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
194 */
195 static function getInfo( $file, $prop, $result, $scale = null ) {
196 $vals = array();
197 if ( isset( $prop['timestamp'] ) ) {
198 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
199 }
200 if ( isset( $prop['user'] ) || isset( $prop['userid'] ) ) {
201
202 if ( isset( $prop['user'] ) ) {
203 $vals['user'] = $file->getUser();
204 }
205 if ( isset( $prop['userid'] ) ) {
206 $vals['userid'] = $file->getUser( 'id' );
207 }
208 if ( !$file->getUser( 'id' ) ) {
209 $vals['anon'] = '';
210 }
211 }
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() );
216 }
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() );
222
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() );
228 } else {
229 $vals['thumbwidth'] = intval( $file->getWidth() );
230 $vals['thumbheight'] = intval( $file->getHeight() );
231 }
232
233 if ( isset( $prop['thumbmime'] ) ) {
234 $thumbFile = UnregisteredLocalFile::newFromPath( $mto->getPath(), false );
235 $vals['thumbmime'] = $thumbFile->getMimeType();
236 }
237 }
238 }
239 $vals['url'] = $file->getFullURL();
240 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
241 }
242 if ( isset( $prop['comment'] ) ) {
243 $vals['comment'] = $file->getDescription();
244 }
245 if ( isset( $prop['sha1'] ) ) {
246 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
247 }
248 if ( isset( $prop['metadata'] ) ) {
249 $metadata = $file->getMetadata();
250 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
251 }
252 if ( isset( $prop['mime'] ) ) {
253 $vals['mime'] = $file->getMimeType();
254 }
255
256 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
257 $vals['archivename'] = $file->getArchiveName();
258 }
259
260 if ( isset( $prop['bitdepth'] ) ) {
261 $vals['bitdepth'] = $file->getBitDepth();
262 }
263
264 return $vals;
265 }
266
267 public static function processMetaData( $metadata, $result ) {
268 $retval = array();
269 if ( is_array( $metadata ) ) {
270 foreach ( $metadata as $key => $value ) {
271 $r = array( 'name' => $key );
272 if ( is_array( $value ) ) {
273 $r['value'] = self::processMetaData( $value, $result );
274 } else {
275 $r['value'] = $value;
276 }
277 $retval[] = $r;
278 }
279 }
280 $result->setIndexedTagName( $retval, 'metadata' );
281 return $retval;
282 }
283
284 public function getCacheMode( $params ) {
285 return 'public';
286 }
287
288 private function getContinueStr( $img ) {
289 return $img->getOriginalTitle()->getText() .
290 '|' . $img->getTimestamp();
291 }
292
293 public function getAllowedParams() {
294 return array(
295 'prop' => array(
296 ApiBase::PARAM_ISMULTI => true,
297 ApiBase::PARAM_DFLT => 'timestamp|user',
298 ApiBase::PARAM_TYPE => self::getPropertyNames()
299 ),
300 'limit' => array(
301 ApiBase::PARAM_TYPE => 'limit',
302 ApiBase::PARAM_DFLT => 1,
303 ApiBase::PARAM_MIN => 1,
304 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
305 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
306 ),
307 'start' => array(
308 ApiBase::PARAM_TYPE => 'timestamp'
309 ),
310 'end' => array(
311 ApiBase::PARAM_TYPE => 'timestamp'
312 ),
313 'urlwidth' => array(
314 ApiBase::PARAM_TYPE => 'integer',
315 ApiBase::PARAM_DFLT => - 1
316 ),
317 'urlheight' => array(
318 ApiBase::PARAM_TYPE => 'integer',
319 ApiBase::PARAM_DFLT => - 1
320 ),
321 'continue' => null,
322 );
323 }
324
325 /**
326 * Returns all possible parameters to iiprop
327 */
328 public static function getPropertyNames() {
329 return array(
330 'timestamp',
331 'user',
332 'userid',
333 'comment',
334 'url',
335 'size',
336 'dimensions', // For backwards compatibility with Allimages
337 'sha1',
338 'mime',
339 'thumbmime',
340 'metadata',
341 'archivename',
342 'bitdepth',
343 );
344 }
345
346 public function getParamDescription() {
347 $p = $this->getModulePrefix();
348 return array(
349 'prop' => array(
350 'What image information to get:',
351 ' timestamp - Adds timestamp for the uploaded version',
352 ' user - Adds the user who uploaded the image version',
353 ' userid - Add the user id that uploaded the image version',
354 ' comment - Comment on the version',
355 ' url - Gives URL to the image and the description page',
356 ' size - Adds the size of the image in bytes and the height and width',
357 ' dimensions - Alias for size',
358 ' sha1 - Adds sha1 hash for the image',
359 ' mime - Adds MIME of the image',
360 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
361 ' metadata - Lists EXIF metadata for the version of the image',
362 ' archivename - Adds the file name of the archive version for non-latest versions',
363 ' bitdepth - Adds the bit depth of the version',
364 ),
365 'limit' => 'How many image revisions to return',
366 'start' => 'Timestamp to start listing from',
367 'end' => 'Timestamp to stop listing at',
368 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
369 'Only the current version of the image can be scaled' ),
370 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
371 'continue' => 'When more results are available, use this to continue',
372 );
373 }
374
375 public function getDescription() {
376 return 'Returns image information and upload history';
377 }
378
379 public function getPossibleErrors() {
380 return array_merge( parent::getPossibleErrors(), array(
381 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
382 ) );
383 }
384
385 protected function getExamples() {
386 return array(
387 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
388 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
389 );
390 }
391
392 public function getVersion() {
393 return __CLASS__ . ': $Id$';
394 }
395 }