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