f1a335e9db520bb329d681512b327392ec64803a
[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 && !$f->isOld()) {
121 $thumb = $f->getThumbnail($this->urlwidth, $this->urlheight);
122 if($thumb)
123 {
124 $vals['thumburl'] = $thumb->getURL();
125 $vals['thumbwidth'] = $thumb->getWidth();
126 $vals['thumbheight'] = $thumb->getHeight();
127 }
128 }
129 $vals['url'] = $f->getURL();
130 }
131 if($this->fld_comment)
132 $vals['comment'] = $f->getDescription();
133 if($this->fld_sha1)
134 $vals['sha1'] = wfBaseConvert($f->getSha1(), 36, 16, 40);
135 if($this->fld_metadata) {
136 $metadata = unserialize($f->getMetadata());
137 $vals['metadata'] = $metadata ? $metadata : null;
138 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
139 }
140 return $vals;
141 }
142
143 public function getAllowedParams() {
144 return array (
145 'prop' => array (
146 ApiBase :: PARAM_ISMULTI => true,
147 ApiBase :: PARAM_DFLT => 'timestamp|user',
148 ApiBase :: PARAM_TYPE => array (
149 'timestamp',
150 'user',
151 'comment',
152 'url',
153 'size',
154 'sha1',
155 'metadata'
156 )
157 ),
158 'limit' => array(
159 ApiBase :: PARAM_TYPE => 'limit',
160 ApiBase :: PARAM_DFLT => 1,
161 ApiBase :: PARAM_MIN => 1,
162 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
163 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
164 ),
165 'start' => array(
166 ApiBase :: PARAM_TYPE => 'timestamp'
167 ),
168 'end' => array(
169 ApiBase :: PARAM_TYPE => 'timestamp'
170 ),
171 'urlwidth' => array(
172 ApiBase :: PARAM_TYPE => 'integer',
173 ApiBase :: PARAM_DFLT => -1
174 ),
175 'urlheight' => array(
176 ApiBase :: PARAM_TYPE => 'integer',
177 ApiBase :: PARAM_DFLT => -1
178 )
179 );
180 }
181
182 public function getParamDescription() {
183 return array (
184 'prop' => 'What image information to get.',
185 'limit' => 'How many image revisions to return',
186 'start' => 'Timestamp to start listing from',
187 'end' => 'Timestamp to stop listing at',
188 '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.',
189 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
190 );
191 }
192
193 public function getDescription() {
194 return array (
195 'Returns image information and upload history'
196 );
197 }
198
199 protected function getExamples() {
200 return array (
201 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
202 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
203 );
204 }
205
206 public function getVersion() {
207 return __CLASS__ . ': $Id$';
208 }
209 }