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