* API: BREAKING CHANGE: (bug 11430) Return fewer results than the limit in some cases...
[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 (C) 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 if ( $params['urlwidth'] != -1 ) {
51 $scale = array();
52 $scale['width'] = $params['urlwidth'];
53 $scale['height'] = $params['urlheight'];
54 } else {
55 $scale = null;
56 }
57
58 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
59 $cnt = 0;
60 if (!empty($pageIds[NS_FILE])) {
61 $result = $this->getResult();
62 $images = RepoGroup::singleton()->findFiles( array_keys( $pageIds[NS_FILE] ) );
63 foreach ( $images as $img ) {
64 $cnt++;
65 if(!is_null($params['continue']))
66 if($cnt < $params['continue'])
67 continue;
68 $pageId = $pageIds[NS_IMAGE][ $img->getOriginalTitle()->getDBkey() ];
69
70 $fit = $result->addValue(
71 array('query', 'pages', intval($pageId)),
72 'imagerepository', $img->getRepoName()
73 );
74 if(!$fit)
75 {
76 if(count($pageIds[NS_IMAGE]) == 1)
77 # The user is screwed. imageinfo can't be solely
78 # responsible for exceeding the limit in this case,
79 # so set a query-continue that just returns the same
80 # thing again. When the violating queries have been
81 # out-continued, the result will get through
82 $this->setContinueEnumParameter('start',
83 wfTimestamp(TS_ISO_8601, $img->getTimestamp()));
84 else
85 $this->setContinueEnumParameter('continue', $cnt);
86 break;
87 }
88
89 // Get information about the current version first
90 // Check that the current version is within the start-end boundaries
91 $gotOne = false;
92 if((is_null($params['start']) || $img->getTimestamp() <= $params['start']) &&
93 (is_null($params['end']) || $img->getTimestamp() >= $params['end'])) {
94 $gotOne = true;
95 $fit = $this->addPageSubItem($pageId,
96 self::getInfo( $img, $prop, $result, $scale));
97 if(!$fit)
98 {
99 if(count($pageIds[NS_IMAGE]) == 1)
100 # See the 'the user is screwed' comment above
101 $this->setContinueEnumParameter('start',
102 wfTimestamp(TS_ISO_8601, $img->getTimestamp()));
103 else
104 $this->setContinueEnumParameter('continue', $cnt);
105 break;
106 }
107 }
108
109 // Now get the old revisions
110 // Get one more to facilitate query-continue functionality
111 $count = ($gotOne ? 1 : 0);
112 $oldies = $img->getHistory($params['limit'] - $count + 1, $params['start'], $params['end']);
113 foreach($oldies as $oldie) {
114 if(++$count > $params['limit']) {
115 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
116 // Only set a query-continue if there was only one title
117 if(count($pageIds[NS_FILE]) == 1)
118 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $oldie->getTimestamp()));
119 break;
120 }
121 $fit = $this->addPageSubItem($pageId,
122 self::getInfo($oldie, $prop, $result));
123 if(!$fit)
124 {
125 if(count($pageIds[NS_IMAGE]) == 1)
126 $this->setContinueEnumParameter('start',
127 wfTimestamp(TS_ISO_8601, $oldie->getTimestamp()));
128 else
129 $this->setContinueEnumParameter('continue', $cnt);
130 break;
131 }
132 }
133 if(!$fit)
134 break;
135 }
136
137 $missing = array_diff( array_keys( $pageIds[NS_FILE] ), array_keys( $images ) );
138 foreach ($missing as $title) {
139 $cnt++;
140 if(!is_null($params['continue']))
141 if($count < $params['continue'])
142 continue;
143 $fit = $result->addValue(
144 array('query', 'pages', intval($pageIds[NS_FILE][$title])),
145 'imagerepository', ''
146 );
147 if(!$fit)
148 $this->setContinueEnumParameter('continue', $cnt);
149 }
150 }
151 }
152
153 /**
154 * Get result information for an image revision
155 * @param File f The image
156 * @return array Result array
157 */
158 static function getInfo($file, $prop, $result, $scale = null) {
159 $vals = array();
160 if( isset( $prop['timestamp'] ) )
161 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp());
162 if( isset( $prop['user'] ) ) {
163 $vals['user'] = $file->getUser();
164 if( !$file->getUser( 'id' ) )
165 $vals['anon'] = '';
166 }
167 if( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
168 $vals['size'] = intval( $file->getSize() );
169 $vals['width'] = intval( $file->getWidth() );
170 $vals['height'] = intval( $file->getHeight() );
171 }
172 if( isset( $prop['url'] ) ) {
173 if( !is_null( $scale ) && !$file->isOld() ) {
174 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
175 if( $mto && !$mto->isError() )
176 {
177 $vals['thumburl'] = $mto->getUrl();
178 $vals['thumbwidth'] = $mto->getWidth();
179 $vals['thumbheight'] = $mto->getHeight();
180 }
181 }
182 $vals['url'] = $file->getFullURL();
183 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
184 }
185 if( isset( $prop['comment'] ) )
186 $vals['comment'] = $file->getDescription();
187 if( isset( $prop['sha1'] ) )
188 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
189 if( isset( $prop['metadata'] ) ) {
190 $metadata = $file->getMetadata();
191 $vals['metadata'] = $metadata ? self::processMetaData( unserialize( $metadata ), $result ) : null;
192 }
193 if( isset( $prop['mime'] ) )
194 $vals['mime'] = $file->getMimeType();
195
196 if( isset( $prop['archivename'] ) && $file->isOld() )
197 $vals['archivename'] = $file->getArchiveName();
198
199 if( isset( $prop['bitdepth'] ) )
200 $vals['bitdepth'] = $file->getBitDepth();
201
202 return $vals;
203 }
204
205 public static function processMetaData($metadata, $result)
206 {
207 $retval = array();
208 foreach($metadata as $key => $value)
209 {
210 $r = array('name' => $key);
211 if(is_array($value))
212 $r['value'] = self::processMetaData($value, $result);
213 else
214 $r['value'] = $value;
215 $retval[] = $r;
216 }
217 $result->setIndexedTagName($retval, 'metadata');
218 return $retval;
219 }
220
221 public function getAllowedParams() {
222 return array (
223 'prop' => array (
224 ApiBase :: PARAM_ISMULTI => true,
225 ApiBase :: PARAM_DFLT => 'timestamp|user',
226 ApiBase :: PARAM_TYPE => array (
227 'timestamp',
228 'user',
229 'comment',
230 'url',
231 'size',
232 'sha1',
233 'mime',
234 'metadata',
235 'archivename',
236 'bitdepth',
237 )
238 ),
239 'limit' => array(
240 ApiBase :: PARAM_TYPE => 'limit',
241 ApiBase :: PARAM_DFLT => 1,
242 ApiBase :: PARAM_MIN => 1,
243 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
244 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
245 ),
246 'start' => array(
247 ApiBase :: PARAM_TYPE => 'timestamp'
248 ),
249 'end' => array(
250 ApiBase :: PARAM_TYPE => 'timestamp'
251 ),
252 'urlwidth' => array(
253 ApiBase :: PARAM_TYPE => 'integer',
254 ApiBase :: PARAM_DFLT => -1
255 ),
256 'urlheight' => array(
257 ApiBase :: PARAM_TYPE => 'integer',
258 ApiBase :: PARAM_DFLT => -1
259 ),
260 'continue' => null,
261 );
262 }
263
264 public function getParamDescription() {
265 return array (
266 'prop' => 'What image information to get.',
267 'limit' => 'How many image revisions to return',
268 'start' => 'Timestamp to start listing from',
269 'end' => 'Timestamp to stop listing at',
270 'urlwidth' => array('If iiprop=url is set, a URL to an image scaled to this width will be returned.',
271 'Only the current version of the image can be scaled.'),
272 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
273 'continue' => 'When more results are available, use this to continue',
274 );
275 }
276
277 public function getDescription() {
278 return array (
279 'Returns image information and upload history'
280 );
281 }
282
283 protected function getExamples() {
284 return array (
285 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
286 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
287 );
288 }
289
290 public function getVersion() {
291 return __CLASS__ . ': $Id$';
292 }
293 }