(bug 12953) prop=imageinfo should only set query-continue when there is only one...
[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 * @addtogroup 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 $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
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'];
59
60 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
61 if (!empty($pageIds[NS_IMAGE])) {
62 foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
63
64 $title = Title :: makeTitle(NS_IMAGE, $dbKey);
65 $img = wfFindFile($title);
66
67 $data = array();
68 if ( !$img ) {
69 $repository = '';
70 } else {
71
72 $repository = $img->getRepoName();
73
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);
79 }
80
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());
91 break;
92 }
93 $data[] = $this->getInfo($oldie);
94 }
95 }
96
97 $this->getResult()->addValue(array(
98 'query', 'pages', intval($pageId)),
99 'imagerepository', $repository
100 );
101 if (!empty($data))
102 $this->addPageSubItems($pageId, $data);
103 }
104 }
105 }
106
107 /**
108 * Get result information for an image revision
109 * @param File f The image
110 * @return array Result array
111 */
112 protected function getInfo($f) {
113 $vals = array();
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'))
119 $vals['anon'] = '';
120 }
121 if($this->fld_size) {
122 $vals['size'] = intval($f->getSize());
123 $vals['width'] = intval($f->getWidth());
124 $vals['height'] = intval($f->getHeight());
125 }
126 if($this->fld_url) {
127 if($this->scale && !$f->isOld()) {
128 $thumb = $f->getThumbnail($this->urlwidth, $this->urlheight);
129 if($thumb)
130 {
131 $vals['thumburl'] = $thumb->getURL();
132 $vals['thumbwidth'] = $thumb->getWidth();
133 $vals['thumbheight'] = $thumb->getHeight();
134 }
135 }
136 $vals['url'] = $f->getURL();
137 }
138 if($this->fld_comment)
139 $vals['comment'] = $f->getDescription();
140 if($this->fld_sha1)
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');
146 }
147 return $vals;
148 }
149
150 public function getAllowedParams() {
151 return array (
152 'prop' => array (
153 ApiBase :: PARAM_ISMULTI => true,
154 ApiBase :: PARAM_DFLT => 'timestamp|user',
155 ApiBase :: PARAM_TYPE => array (
156 'timestamp',
157 'user',
158 'comment',
159 'url',
160 'size',
161 'sha1',
162 'metadata'
163 )
164 ),
165 'limit' => 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
171 ),
172 'start' => array(
173 ApiBase :: PARAM_TYPE => 'timestamp'
174 ),
175 'end' => array(
176 ApiBase :: PARAM_TYPE => 'timestamp'
177 ),
178 'urlwidth' => array(
179 ApiBase :: PARAM_TYPE => 'integer',
180 ApiBase :: PARAM_DFLT => -1
181 ),
182 'urlheight' => array(
183 ApiBase :: PARAM_TYPE => 'integer',
184 ApiBase :: PARAM_DFLT => -1
185 )
186 );
187 }
188
189 public function getParamDescription() {
190 return array (
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',
197 );
198 }
199
200 public function getDescription() {
201 return array (
202 'Returns image information and upload history'
203 );
204 }
205
206 protected function getExamples() {
207 return array (
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',
210 );
211 }
212
213 public function getVersion() {
214 return __CLASS__ . ': $Id$';
215 }
216 }