]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryImageInfo.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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         public function getCacheMode( $params ) {
248                 return 'public';
249         }
250
251         private function getContinueStr( $img )
252         {
253                 return $img->getOriginalTitle()->getText() .
254                         '|' .  $img->getTimestamp();
255         }
256
257         public function getAllowedParams() {
258                 return array (
259                         'prop' => array (
260                                 ApiBase :: PARAM_ISMULTI => true,
261                                 ApiBase :: PARAM_DFLT => 'timestamp|user',
262                                 ApiBase :: PARAM_TYPE => self::getPropertyNames()
263                         ),
264                         'limit' => array(
265                                 ApiBase :: PARAM_TYPE => 'limit',
266                                 ApiBase :: PARAM_DFLT => 1,
267                                 ApiBase :: PARAM_MIN => 1,
268                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
269                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
270                         ),
271                         'start' => array(
272                                 ApiBase :: PARAM_TYPE => 'timestamp'
273                         ),
274                         'end' => array(
275                                 ApiBase :: PARAM_TYPE => 'timestamp'
276                         ),
277                         'urlwidth' => array(
278                                 ApiBase :: PARAM_TYPE => 'integer',
279                                 ApiBase :: PARAM_DFLT => - 1
280                         ),
281                         'urlheight' => array(
282                                 ApiBase :: PARAM_TYPE => 'integer',
283                                 ApiBase :: PARAM_DFLT => - 1
284                         ),
285                         'continue' => null,
286                 );
287         }
288         
289         /**
290          * Returns all possible parameters to iiprop
291          */
292         public static function getPropertyNames() {
293                 return array (
294                                         'timestamp',
295                                         'user',
296                                         'comment',
297                                         'url',
298                                         'size',
299                                         'dimensions', // For backwards compatibility with Allimages
300                                         'sha1',
301                                         'mime',
302                                         'metadata',
303                                         'archivename',
304                                         'bitdepth',
305                                 );
306         }
307
308         public function getParamDescription() {
309                 return array (
310                         'prop' => 'What image information to get.',
311                         'limit' => 'How many image revisions to return',
312                         'start' => 'Timestamp to start listing from',
313                         'end' => 'Timestamp to stop listing at',
314                         'urlwidth' => array( 'If iiprop=url is set, a URL to an image scaled to this width will be returned.',
315                                             'Only the current version of the image can be scaled.' ),
316                         'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
317                         'continue' => 'When more results are available, use this to continue',
318                 );
319         }
320
321         public function getDescription() {
322                 return array (
323                         'Returns image information and upload history'
324                 );
325         }
326         
327         public function getPossibleErrors() {
328                 return array_merge( parent::getPossibleErrors(), array(
329                         array( 'code' => 'iiurlwidth', 'info' => 'iiurlheight cannot be used without iiurlwidth' ),
330                 ) );
331         }
332
333         protected function getExamples() {
334                 return array (
335                         'api.php?action=query&titles=File:Albert%20Einstein%20Head.jpg&prop=imageinfo',
336                         'api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
337                 );
338         }
339
340         public function getVersion() {
341                 return __CLASS__ . ': $Id: ApiQueryImageInfo.php 69932 2010-07-26 08:03:21Z tstarling $';
342         }
343 }