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']);
53 $this->fld_archivename
= isset($prop['archivename']);
55 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
56 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
57 $this->scale
= ($params['urlwidth'] != -1);
58 $this->urlwidth
= $params['urlwidth'];
59 $this->urlheight
= $params['urlheight'];
61 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
62 if (!empty($pageIds[NS_IMAGE
])) {
63 foreach ($pageIds[NS_IMAGE
] as $dbKey => $pageId) {
65 $title = Title
:: makeTitle(NS_IMAGE
, $dbKey);
66 $img = wfFindFile($title);
73 $repository = $img->getRepoName();
75 // Get information about the current version first
76 // Check that the current version is within the start-end boundaries
77 if((is_null($params['start']) ||
$img->getTimestamp() <= $params['start']) &&
78 (is_null($params['end']) ||
$img->getTimestamp() >= $params['end'])) {
79 $data[] = $this->getInfo($img);
82 // Now get the old revisions
83 // Get one more to facilitate query-continue functionality
84 $count = count($data);
85 $oldies = $img->getHistory($params['limit'] - $count +
1, $params['start'], $params['end']);
86 foreach($oldies as $oldie) {
87 if(++
$count > $params['limit']) {
88 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
89 // Only set a query-continue if there was only one title
90 if(count($pageIds[NS_IMAGE
]) == 1)
91 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
94 $data[] = $this->getInfo($oldie);
98 $this->getResult()->addValue(array(
99 'query', 'pages', intval($pageId)),
100 'imagerepository', $repository
103 $this->addPageSubItems($pageId, $data);
109 * Get result information for an image revision
110 * @param File f The image
111 * @return array Result array
113 protected function getInfo($f) {
115 if($this->fld_timestamp
)
116 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $f->getTimestamp());
117 if($this->fld_user
) {
118 $vals['user'] = $f->getUser();
119 if(!$f->getUser('id'))
122 if($this->fld_size
) {
123 $vals['size'] = intval($f->getSize());
124 $vals['width'] = intval($f->getWidth());
125 $vals['height'] = intval($f->getHeight());
128 if($this->scale
&& !$f->isOld()) {
129 $thumb = $f->getThumbnail($this->urlwidth
, $this->urlheight
);
132 $vals['thumburl'] = wfExpandUrl( $thumb->getURL() );
133 $vals['thumbwidth'] = $thumb->getWidth();
134 $vals['thumbheight'] = $thumb->getHeight();
137 $vals['url'] = wfExpandUrl( $f->getURL() );
139 if($this->fld_comment
)
140 $vals['comment'] = $f->getDescription();
142 $vals['sha1'] = wfBaseConvert($f->getSha1(), 36, 16, 40);
143 if($this->fld_metadata
) {
144 $metadata = unserialize($f->getMetadata());
145 $vals['metadata'] = $metadata ?
$metadata : null;
146 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
148 if($this->fld_archivename
&& $f->isOld())
149 $vals['archivename'] = $f->getArchiveName();
154 public function getAllowedParams() {
157 ApiBase
:: PARAM_ISMULTI
=> true,
158 ApiBase
:: PARAM_DFLT
=> 'timestamp|user',
159 ApiBase
:: PARAM_TYPE
=> array (
171 ApiBase
:: PARAM_TYPE
=> 'limit',
172 ApiBase
:: PARAM_DFLT
=> 1,
173 ApiBase
:: PARAM_MIN
=> 1,
174 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
175 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
178 ApiBase
:: PARAM_TYPE
=> 'timestamp'
181 ApiBase
:: PARAM_TYPE
=> 'timestamp'
184 ApiBase
:: PARAM_TYPE
=> 'integer',
185 ApiBase
:: PARAM_DFLT
=> -1
187 'urlheight' => array(
188 ApiBase
:: PARAM_TYPE
=> 'integer',
189 ApiBase
:: PARAM_DFLT
=> -1
194 public function getParamDescription() {
196 'prop' => 'What image information to get.',
197 'limit' => 'How many image revisions to return',
198 'start' => 'Timestamp to start listing from',
199 'end' => 'Timestamp to stop listing at',
200 '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.',
201 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
205 public function getDescription() {
207 'Returns image information and upload history'
211 protected function getExamples() {
213 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
214 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
218 public function getVersion() {
219 return __CLASS__
. ': $Id$';