API: fix copyright symbol, coding style cleanup, more braces
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 * @param File f The image
188 * @return array Result array
189 */
190 static function getInfo( $file, $prop, $result, $scale = null ) {
191 $vals = array();
192 if ( isset( $prop['timestamp'] ) ) {
193 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $file->getTimestamp() );
194 }
195 if ( isset( $prop['user'] ) ) {
196 $vals['user'] = $file->getUser();
197 if ( !$file->getUser( 'id' ) ) {
198 $vals['anon'] = '';
199 }
200 }
201 if ( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
202 $vals['size'] = intval( $file->getSize() );
203 $vals['width'] = intval( $file->getWidth() );
204 $vals['height'] = intval( $file->getHeight() );
205 }
206 if ( isset( $prop['url'] ) ) {
207 if ( !is_null( $scale ) && !$file->isOld() ) {
208 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
209 if ( $mto && !$mto->isError() ) {
210 $vals['thumburl'] = wfExpandUrl( $mto->getUrl() );
211 $vals['thumbwidth'] = intval( $mto->getWidth() );
212 $vals['thumbheight'] = intval( $mto->getHeight() );
213 }
214 }
215 $vals['url'] = $file->getFullURL();
216 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
217 }
218 if ( isset( $prop['comment'] ) ) {
219 $vals['comment'] = $file->getDescription();
220 }
221 if ( isset( $prop['sha1'] ) ) {
222 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
223 }
224 if ( isset( $prop['metadata'] ) ) {
225 $metadata = $file->getMetadata();
226 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
227 }
228 if ( isset( $prop['mime'] ) ) {
229 $vals['mime'] = $file->getMimeType();
230 }
231
232 if ( isset( $prop['archivename'] ) && $file->isOld() ) {
233 $vals['archivename'] = $file->getArchiveName();
234 }
235
236 if ( isset( $prop['bitdepth'] ) ) {
237 $vals['bitdepth'] = $file->getBitDepth();
238 }
239
240 return $vals;
241 }
242
243 public static function processMetaData( $metadata, $result ) {
244 $retval = array();
245 if ( is_array( $metadata ) ) {
246 foreach ( $metadata as $key => $value ) {
247 $r = array( 'name' => $key );
248 if ( is_array( $value ) ) {
249 $r['value'] = self::processMetaData( $value, $result );
250 } else {
251 $r['value'] = $value;
252 }
253 $retval[] = $r;
254 }
255 }
256 $result->setIndexedTagName( $retval, 'metadata' );
257 return $retval;
258 }
259
260 private function getContinueStr( $img ) {
261 return $img->getOriginalTitle()->getText() .
262 '|' . $img->getTimestamp();
263 }
264
265 public function getAllowedParams() {
266 return array(
267 'prop' => array(
268 ApiBase::PARAM_ISMULTI => true,
269 ApiBase::PARAM_DFLT => 'timestamp|user',
270 ApiBase::PARAM_TYPE => self::getPropertyNames()
271 ),
272 'limit' => array(
273 ApiBase::PARAM_TYPE => 'limit',
274 ApiBase::PARAM_DFLT => 1,
275 ApiBase::PARAM_MIN => 1,
276 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
277 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
278 ),
279 'start' => array(
280 ApiBase::PARAM_TYPE => 'timestamp'
281 ),
282 'end' => array(
283 ApiBase::PARAM_TYPE => 'timestamp'
284 ),
285 'urlwidth' => array(
286 ApiBase::PARAM_TYPE => 'integer',
287 ApiBase::PARAM_DFLT => -1
288 ),
289 'urlheight' => array(
290 ApiBase::PARAM_TYPE => 'integer',
291 ApiBase::PARAM_DFLT => -1
292 ),
293 'continue' => null,
294 );
295 }
296
297 /**
298 * Returns all possible parameters to iiprop
299 */
300 public static function getPropertyNames() {
301 return array(
302 'timestamp',
303 'user',
304 'comment',
305 'url',
306 'size',
307 'dimensions', // For backwards compatibility with Allimages
308 'sha1',
309 'mime',
310 'metadata',
311 'archivename',
312 'bitdepth',
313 );
314 }
315
316 public function getParamDescription() {
317 return array(
318 'prop' => 'What image information to get.',
319 'limit' => 'How many image revisions to return',
320 'start' => 'Timestamp to start listing from',
321 'end' => 'Timestamp to stop listing at',
322 'urlwidth' => array( 'If iiprop=url is set, a URL to an image scaled to this width will be returned.',
323 'Only the current version of the image can be scaled.' ),
324 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
325 'continue' => 'When more results are available, use this to continue',
326 );
327 }
328
329 public function getDescription() {
330 return array(
331 'Returns image information and upload history'
332 );
333 }
334
335 public function getPossibleErrors() {
336 return array_merge( parent::getPossibleErrors(), array(
337 array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
338 ) );
339 }
340
341 protected function getExamples() {
342 return array(
343 'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
344 'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
345 );
346 }
347
348 public function getVersion() {
349 return __CLASS__ . ': $Id$';
350 }
351 }