(bug 12875) Adding query-continue to prop=imageinfo. Patch by Bryan Tongh Minh.
[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($data) + 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 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
89 break;
90 }
91 $data[] = $this->getInfo($oldie);
92 }
93 }
94
95 $this->getResult()->addValue(array(
96 'query', 'pages', intval($pageId)),
97 'imagerepository', $repository
98 );
99 if (!empty($data))
100 $this->addPageSubItems($pageId, $data);
101 }
102 }
103 }
104
105 /**
106 * Get result information for an image revision
107 * @param File f The image
108 * @return array Result array
109 */
110 protected function getInfo($f) {
111 $vals = array();
112 if($this->fld_timestamp)
113 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $f->getTimestamp());
114 if($this->fld_user) {
115 $vals['user'] = $f->getUser();
116 if(!$f->getUser('id'))
117 $vals['anon'] = '';
118 }
119 if($this->fld_size) {
120 $vals['size'] = intval($f->getSize());
121 $vals['width'] = intval($f->getWidth());
122 $vals['height'] = intval($f->getHeight());
123 }
124 if($this->fld_url) {
125 if($this->scale && !$f->isOld()) {
126 $thumb = $f->getThumbnail($this->urlwidth, $this->urlheight);
127 if($thumb)
128 {
129 $vals['thumburl'] = $thumb->getURL();
130 $vals['thumbwidth'] = $thumb->getWidth();
131 $vals['thumbheight'] = $thumb->getHeight();
132 }
133 }
134 $vals['url'] = $f->getURL();
135 }
136 if($this->fld_comment)
137 $vals['comment'] = $f->getDescription();
138 if($this->fld_sha1)
139 $vals['sha1'] = wfBaseConvert($f->getSha1(), 36, 16, 40);
140 if($this->fld_metadata) {
141 $metadata = unserialize($f->getMetadata());
142 $vals['metadata'] = $metadata ? $metadata : null;
143 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
144 }
145 return $vals;
146 }
147
148 public function getAllowedParams() {
149 return array (
150 'prop' => array (
151 ApiBase :: PARAM_ISMULTI => true,
152 ApiBase :: PARAM_DFLT => 'timestamp|user',
153 ApiBase :: PARAM_TYPE => array (
154 'timestamp',
155 'user',
156 'comment',
157 'url',
158 'size',
159 'sha1',
160 'metadata'
161 )
162 ),
163 'limit' => array(
164 ApiBase :: PARAM_TYPE => 'limit',
165 ApiBase :: PARAM_DFLT => 1,
166 ApiBase :: PARAM_MIN => 1,
167 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
168 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
169 ),
170 'start' => array(
171 ApiBase :: PARAM_TYPE => 'timestamp'
172 ),
173 'end' => array(
174 ApiBase :: PARAM_TYPE => 'timestamp'
175 ),
176 'urlwidth' => array(
177 ApiBase :: PARAM_TYPE => 'integer',
178 ApiBase :: PARAM_DFLT => -1
179 ),
180 'urlheight' => array(
181 ApiBase :: PARAM_TYPE => 'integer',
182 ApiBase :: PARAM_DFLT => -1
183 )
184 );
185 }
186
187 public function getParamDescription() {
188 return array (
189 'prop' => 'What image information to get.',
190 'limit' => 'How many image revisions to return',
191 'start' => 'Timestamp to start listing from',
192 'end' => 'Timestamp to stop listing at',
193 '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.',
194 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
195 );
196 }
197
198 public function getDescription() {
199 return array (
200 'Returns image information and upload history'
201 );
202 }
203
204 protected function getExamples() {
205 return array (
206 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
207 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
208 );
209 }
210
211 public function getVersion() {
212 return __CLASS__ . ': $Id$';
213 }
214 }