* (bug 23473) - Give description of properties on all modules
[lhc/web/wiklou.git] / includes / api / ApiQueryImageInfo.php
1 <?php
2
3 /**
4 * Created on July 6, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * A query action to get image information and upload history.
33 *
34 * @ingroup API
35 */
36 class ApiQueryImageInfo extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ii' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44
45 $prop = array_flip( $params['prop'] );
46
47 if ( $params['urlheight'] != - 1 && $params['urlwidth'] == - 1 ) {
48 $this->dieUsage( 'iiurlheight cannot be used without iiurlwidth', 'iiurlwidth' );
49 }
50
51 if ( $params['urlwidth'] != - 1 ) {
52 $scale = array();
53 $scale['width'] = $params['urlwidth'];
54 $scale['height'] = $params['urlheight'];
55 } else {
56 $scale = null;
57 }
58
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
63
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' );
71 }
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;
80 }
81 }
82 }
83
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;
90 }
91
92 $start = $skip ? $fromTimestamp : $params['start'];
93 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
94
95 $fit = $result->addValue(
96 array( 'query', 'pages', intval( $pageId ) ),
97 'imagerepository', $img->getRepoName()
98 );
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 ) );
111 }
112 break;
113 }
114
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'] )
121 )
122 {
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 ) );
134 }
135 break;
136 }
137 }
138
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() ) );
150 }
151 break;
152 }
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 ) );
162 }
163 break;
164 }
165 }
166 if ( !$fit ) {
167 break;
168 }
169 $skip = false;
170 }
171
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', ''
178 );
179 }
180 // The above can't fail because it doesn't increase the result size
181 }
182 }
183 }
184
185 /**
186 * Get result information for an image revision
187 *
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
193 */
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() );
198 }
199 if ( isset( $prop['user'] ) ) {
200 $vals['user'] = $file->getUser();
201 if ( !$file->getUser( 'id' ) ) {
202 $vals['anon'] = '';
203 }
204 }
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() );
209 }
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() );
215
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() );
224 }
225
226 if ( isset( $prop['thumbmime'] ) ) {
227 $thumbFile = UnregisteredLocalFile::newFromPath( $mto->getPath(), false );
228 $vals['thumbmime'] = $thumbFile->getMimeType();
229 }
230 }
231 }
232 $vals['url'] = $file->getFullURL();
233 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
234 }
235 if ( isset( $prop['comment'] ) ) {
236 $vals['comment'] = $file->getDescription();
237 }
238 if ( isset( $prop['sha1'] ) ) {
239 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
240 }
241 if ( isset( $prop['metadata'] ) ) {
242 $metadata = $file->getMetadata();
243 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
244 }
245 if ( isset( $prop['mime'] ) ) {
246 $vals['mime'] = $file->getMimeType();
247 }
248
249 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
250 $vals['archivename'] = $file->getArchiveName();
251 }
252
253 if ( isset( $prop['bitdepth'] ) ) {
254 $vals['bitdepth'] = $file->getBitDepth();
255 }
256
257 return $vals;
258 }
259
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;
269 }
270 $retval[] = $r;
271 }
272 }
273 $result->setIndexedTagName( $retval, 'metadata' );
274 return $retval;
275 }
276
277 private function getContinueStr( $img ) {
278 return $img->getOriginalTitle()->getText() .
279 '|' . $img->getTimestamp();
280 }
281
282 public function getAllowedParams() {
283 return array(
284 'prop' => array(
285 ApiBase::PARAM_ISMULTI => true,
286 ApiBase::PARAM_DFLT => 'timestamp|user',
287 ApiBase::PARAM_TYPE => self::getPropertyNames()
288 ),
289 'limit' => array(
290 ApiBase::PARAM_TYPE => 'limit',
291 ApiBase::PARAM_DFLT => 1,
292 ApiBase::PARAM_MIN => 1,
293 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
294 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
295 ),
296 'start' => array(
297 ApiBase::PARAM_TYPE => 'timestamp'
298 ),
299 'end' => array(
300 ApiBase::PARAM_TYPE => 'timestamp'
301 ),
302 'urlwidth' => array(
303 ApiBase::PARAM_TYPE => 'integer',
304 ApiBase::PARAM_DFLT => - 1
305 ),
306 'urlheight' => array(
307 ApiBase::PARAM_TYPE => 'integer',
308 ApiBase::PARAM_DFLT => - 1
309 ),
310 'continue' => null,
311 );
312 }
313
314 /**
315 * Returns all possible parameters to iiprop
316 */
317 public static function getPropertyNames() {
318 return array(
319 'timestamp',
320 'user',
321 'comment',
322 'url',
323 'size',
324 'dimensions', // For backwards compatibility with Allimages
325 'sha1',
326 'mime',
327 'thumbmime',
328 'metadata',
329 'archivename',
330 'bitdepth',
331 );
332 }
333
334 public function getParamDescription() {
335 $p = $this->getModulePrefix();
336 return array(
337 'prop' => array(
338 'What image information to get:',
339 ' timestamp - Adds timestamp for the uploaded version',
340 ' user - Adds user for uploaded the image version',
341 ' comment - Comment on the version',
342 ' url - Gives URL to the image and the description page',
343 ' size - Adds the size of the image in bytes and the height and width',
344 ' dimensions - Alias for size',
345 ' sha1 - Adds sha1 hash for the image',
346 ' mime - Adds MIME of the image',
347 ' thumbmime - Adss MIME of the image thumbnail (requires url)',
348 ' metadata - Lists EXIF metadata for the version of the image',
349 ' archivename - Adds the file name of the archive version for non-latest versions',
350 ' bitdepth - Adds the bit depth of the version',
351 ),
352 'limit' => 'How many image revisions to return',
353 'start' => 'Timestamp to start listing from',
354 'end' => 'Timestamp to stop listing at',
355 'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
356 'Only the current version of the image can be scaled' ),
357 'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
358 'continue' => 'When more results are available, use this to continue',
359 );
360 }
361
362 public function getDescription() {
363 return 'Returns image information and upload history';
364 }
365
366 public function getPossibleErrors() {
367 return array_merge( parent::getPossibleErrors(), array(
368 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
369 ) );
370 }
371
372 protected function getExamples() {
373 return array(
374 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
375 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
376 );
377 }
378
379 public function getVersion() {
380 return __CLASS__ . ': $Id$';
381 }
382 }