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