4 * Created on July 6, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query action to get image information and upload history.
36 class ApiQueryImageInfo
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'ii');
42 public function execute() {
43 $params = $this->extractRequestParams();
45 $prop = array_flip($params['prop']);
47 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
48 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
50 if ( $params['urlwidth'] != -1 ) {
52 $scale['width'] = $params['urlwidth'];
53 $scale['height'] = $params['urlheight'];
58 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
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 ) {
65 if(!is_null($params['continue']))
66 if($cnt < $params['continue'])
68 $pageId = $pageIds[NS_IMAGE
][ $img->getOriginalTitle()->getDBkey() ];
70 $fit = $result->addValue(
71 array('query', 'pages', intval($pageId)),
72 'imagerepository', $img->getRepoName()
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()));
85 $this->setContinueEnumParameter('continue', $cnt);
89 // Get information about the current version first
90 // Check that the current version is within the start-end boundaries
92 if((is_null($params['start']) ||
$img->getTimestamp() <= $params['start']) &&
93 (is_null($params['end']) ||
$img->getTimestamp() >= $params['end'])) {
95 $fit = $this->addPageSubItem($pageId,
96 self
::getInfo( $img, $prop, $result, $scale));
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()));
104 $this->setContinueEnumParameter('continue', $cnt);
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()));
121 $fit = $this->addPageSubItem($pageId,
122 self
::getInfo($oldie, $prop, $result));
125 if(count($pageIds[NS_IMAGE
]) == 1)
126 $this->setContinueEnumParameter('start',
127 wfTimestamp(TS_ISO_8601
, $oldie->getTimestamp()));
129 $this->setContinueEnumParameter('continue', $cnt);
137 $missing = array_diff( array_keys( $pageIds[NS_FILE
] ), array_keys( $images ) );
138 foreach ($missing as $title) {
140 if(!is_null($params['continue']))
141 if($count < $params['continue'])
143 $fit = $result->addValue(
144 array('query', 'pages', intval($pageIds[NS_FILE
][$title])),
145 'imagerepository', ''
148 $this->setContinueEnumParameter('continue', $cnt);
154 * Get result information for an image revision
155 * @param File f The image
156 * @return array Result array
158 static function getInfo($file, $prop, $result, $scale = null) {
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' ) )
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() );
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() )
177 $vals['thumburl'] = $mto->getUrl();
178 $vals['thumbwidth'] = $mto->getWidth();
179 $vals['thumbheight'] = $mto->getHeight();
182 $vals['url'] = $file->getFullURL();
183 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
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;
193 if( isset( $prop['mime'] ) )
194 $vals['mime'] = $file->getMimeType();
196 if( isset( $prop['archivename'] ) && $file->isOld() )
197 $vals['archivename'] = $file->getArchiveName();
199 if( isset( $prop['bitdepth'] ) )
200 $vals['bitdepth'] = $file->getBitDepth();
205 public static function processMetaData($metadata, $result)
208 foreach($metadata as $key => $value)
210 $r = array('name' => $key);
212 $r['value'] = self
::processMetaData($value, $result);
214 $r['value'] = $value;
217 $result->setIndexedTagName($retval, 'metadata');
221 public function getAllowedParams() {
224 ApiBase
:: PARAM_ISMULTI
=> true,
225 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
226 ApiBase
:: PARAM_TYPE
=> 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
247 ApiBase
:: PARAM_TYPE
=> 'timestamp'
250 ApiBase
:: PARAM_TYPE
=> 'timestamp'
253 ApiBase
:: PARAM_TYPE
=> 'integer',
254 ApiBase
:: PARAM_DFLT
=> -1
256 'urlheight' => array(
257 ApiBase
:: PARAM_TYPE
=> 'integer',
258 ApiBase
:: PARAM_DFLT
=> -1
264 public function getParamDescription() {
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',
277 public function getDescription() {
279 'Returns image information and upload history'
283 protected function getExamples() {
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',
290 public function getVersion() {
291 return __CLASS__
. ': $Id$';