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']);
46 $this->fld_timestamp
= isset($prop['timestamp']);
47 $this->fld_user
= isset($prop['user']);
48 $this->fld_comment
= isset($prop['comment']);
49 $this->fld_url
= isset($prop['url']);
50 $this->fld_size
= isset($prop['size']);
51 $this->fld_sha1
= isset($prop['sha1']);
52 $this->fld_metadata
= isset($prop['metadata']);
54 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
55 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
56 $this->scale
= ($params['urlwidth'] != -1);
57 $this->urlwidth
= $params['urlwidth'];
58 $this->urlheight
= $params['urlheight'];
60 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
61 if (!empty($pageIds[NS_IMAGE
])) {
62 foreach ($pageIds[NS_IMAGE
] as $dbKey => $pageId) {
64 $title = Title
:: makeTitle(NS_IMAGE
, $dbKey);
65 $img = wfFindFile($title);
72 $repository = $img->getRepoName();
74 // Get information about the current version first
75 // Check that the current version is within the start-end boundaries
76 if((is_null($params['start']) ||
$img->getTimestamp() <= $params['start']) &&
77 (is_null($params['end']) ||
$img->getTimestamp() >= $params['end'])) {
78 $data[] = $this->getInfo($img);
81 // Now get the old revisions
82 // Get one more to facilitate query-continue functionality
83 $count = count($data);
84 $oldies = $img->getHistory($params['limit'] - $count +
1, $params['start'], $params['end']);
85 foreach($oldies as $oldie) {
86 if(++
$count > $params['limit']) {
87 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
88 // Only set a query-continue if there was only one title
89 if(count($pageIds[NS_IMAGE
]) == 1)
90 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
93 $data[] = $this->getInfo($oldie);
97 $this->getResult()->addValue(array(
98 'query', 'pages', intval($pageId)),
99 'imagerepository', $repository
102 $this->addPageSubItems($pageId, $data);
108 * Get result information for an image revision
109 * @param File f The image
110 * @return array Result array
112 protected function getInfo($f) {
114 if($this->fld_timestamp
)
115 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $f->getTimestamp());
116 if($this->fld_user
) {
117 $vals['user'] = $f->getUser();
118 if(!$f->getUser('id'))
121 if($this->fld_size
) {
122 $vals['size'] = intval($f->getSize());
123 $vals['width'] = intval($f->getWidth());
124 $vals['height'] = intval($f->getHeight());
127 if($this->scale
&& !$f->isOld()) {
128 $thumb = $f->getThumbnail($this->urlwidth
, $this->urlheight
);
131 $vals['thumburl'] = $thumb->getURL();
132 $vals['thumbwidth'] = $thumb->getWidth();
133 $vals['thumbheight'] = $thumb->getHeight();
136 $vals['url'] = $f->getURL();
138 if($this->fld_comment
)
139 $vals['comment'] = $f->getDescription();
141 $vals['sha1'] = wfBaseConvert($f->getSha1(), 36, 16, 40);
142 if($this->fld_metadata
) {
143 $metadata = unserialize($f->getMetadata());
144 $vals['metadata'] = $metadata ?
$metadata : null;
145 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
150 public function getAllowedParams() {
153 ApiBase
:: PARAM_ISMULTI
=> true,
154 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
155 ApiBase
:: PARAM_TYPE
=> array (
166 ApiBase
:: PARAM_TYPE
=> 'limit',
167 ApiBase
:: PARAM_DFLT
=> 1,
168 ApiBase
:: PARAM_MIN
=> 1,
169 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
170 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
173 ApiBase
:: PARAM_TYPE
=> 'timestamp'
176 ApiBase
:: PARAM_TYPE
=> 'timestamp'
179 ApiBase
:: PARAM_TYPE
=> 'integer',
180 ApiBase
:: PARAM_DFLT
=> -1
182 'urlheight' => array(
183 ApiBase
:: PARAM_TYPE
=> 'integer',
184 ApiBase
:: PARAM_DFLT
=> -1
189 public function getParamDescription() {
191 'prop' => 'What image information to get.',
192 'limit' => 'How many image revisions to return',
193 'start' => 'Timestamp to start listing from',
194 'end' => 'Timestamp to stop listing at',
195 'urlwidth' => 'If iiprop=url is set, a URL to an image scaled to this width will be returned. Only the current version of the image can be scaled.',
196 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
200 public function getDescription() {
202 'Returns image information and upload history'
206 protected function getExamples() {
208 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
209 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
213 public function getVersion() {
214 return __CLASS__
. ': $Id$';