Standardised file description headers, added @file
[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'] ) ) {
201 $vals['user'] = $file->getUser();
202 if ( !$file->getUser( 'id' ) ) {
203 $vals['anon'] = '';
204 }
205 }
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() );
210 }
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() );
216
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() );
222 } else {
223 $vals['thumbwidth'] = intval( $file->getWidth() );
224 $vals['thumbheight'] = intval( $file->getHeight() );
225 }
226
227 if ( isset( $prop['thumbmime'] ) ) {
228 $thumbFile = UnregisteredLocalFile::newFromPath( $mto->getPath(), false );
229 $vals['thumbmime'] = $thumbFile->getMimeType();
230 }
231 }
232 }
233 $vals['url'] = $file->getFullURL();
234 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
235 }
236 if ( isset( $prop['comment'] ) ) {
237 $vals['comment'] = $file->getDescription();
238 }
239 if ( isset( $prop['sha1'] ) ) {
240 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
241 }
242 if ( isset( $prop['metadata'] ) ) {
243 $metadata = $file->getMetadata();
244 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
245 }
246 if ( isset( $prop['mime'] ) ) {
247 $vals['mime'] = $file->getMimeType();
248 }
249
250 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
251 $vals['archivename'] = $file->getArchiveName();
252 }
253
254 if ( isset( $prop['bitdepth'] ) ) {
255 $vals['bitdepth'] = $file->getBitDepth();
256 }
257
258 return $vals;
259 }
260
261 public static function processMetaData( $metadata, $result ) {
262 $retval = array();
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 );
268 } else {
269 $r['value'] = $value;
270 }
271 $retval[] = $r;
272 }
273 }
274 $result->setIndexedTagName( $retval, 'metadata' );
275 return $retval;
276 }
277
278 public function getCacheMode( $params ) {
279 return 'public';
280 }
281
282 private function getContinueStr( $img ) {
283 return $img->getOriginalTitle()->getText() .
284 '|' . $img->getTimestamp();
285 }
286
287 public function getAllowedParams() {
288 return array(
289 'prop' => array(
290 ApiBase::PARAM_ISMULTI => true,
291 ApiBase::PARAM_DFLT => 'timestamp|user',
292 ApiBase::PARAM_TYPE => self::getPropertyNames()
293 ),
294 'limit' => array(
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
300 ),
301 'start' => array(
302 ApiBase::PARAM_TYPE => 'timestamp'
303 ),
304 'end' => array(
305 ApiBase::PARAM_TYPE => 'timestamp'
306 ),
307 'urlwidth' => array(
308 ApiBase::PARAM_TYPE => 'integer',
309 ApiBase::PARAM_DFLT => - 1
310 ),
311 'urlheight' => array(
312 ApiBase::PARAM_TYPE => 'integer',
313 ApiBase::PARAM_DFLT => - 1
314 ),
315 'continue' => null,
316 );
317 }
318
319 /**
320 * Returns all possible parameters to iiprop
321 */
322 public static function getPropertyNames() {
323 return array(
324 'timestamp',
325 'user',
326 'comment',
327 'url',
328 'size',
329 'dimensions', // For backwards compatibility with Allimages
330 'sha1',
331 'mime',
332 'thumbmime',
333 'metadata',
334 'archivename',
335 'bitdepth',
336 );
337 }
338
339 public function getParamDescription() {
340 $p = $this->getModulePrefix();
341 return array(
342 'prop' => array(
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',
356 ),
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',
364 );
365 }
366
367 public function getDescription() {
368 return 'Returns image information and upload history';
369 }
370
371 public function getPossibleErrors() {
372 return array_merge( parent::getPossibleErrors(), array(
373 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
374 ) );
375 }
376
377 protected function getExamples() {
378 return array(
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',
381 );
382 }
383
384 public function getVersion() {
385 return __CLASS__ . ': $Id$';
386 }
387 }