As per mailing list: $wgOut->redirect auto expands; File::getFullUrl exists.
[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 $this->fld_archivename = isset($prop['archivename']);
54
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'];
60
61 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
62 if (!empty($pageIds[NS_IMAGE])) {
63 foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
64
65 $title = Title :: makeTitle(NS_IMAGE, $dbKey);
66 $img = wfFindFile($title);
67
68 $data = array();
69 if ( !$img ) {
70 $repository = '';
71 } else {
72
73 $repository = $img->getRepoName();
74
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);
80 }
81
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());
92 break;
93 }
94 $data[] = $this->getInfo($oldie);
95 }
96 }
97
98 $this->getResult()->addValue(array(
99 'query', 'pages', intval($pageId)),
100 'imagerepository', $repository
101 );
102 if (!empty($data))
103 $this->addPageSubItems($pageId, $data);
104 }
105 }
106 }
107
108 /**
109 * Get result information for an image revision
110 * @param File f The image
111 * @return array Result array
112 */
113 protected function getInfo($f) {
114 $vals = array();
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'))
120 $vals['anon'] = '';
121 }
122 if($this->fld_size) {
123 $vals['size'] = intval($f->getSize());
124 $vals['width'] = intval($f->getWidth());
125 $vals['height'] = intval($f->getHeight());
126 }
127 if($this->fld_url) {
128 if($this->scale && !$f->isOld()) {
129 $thumb = $f->getThumbnail($this->urlwidth, $this->urlheight);
130 if($thumb)
131 {
132 $vals['thumburl'] = wfExpandUrl( $thumb->getURL() );
133 $vals['thumbwidth'] = $thumb->getWidth();
134 $vals['thumbheight'] = $thumb->getHeight();
135 }
136 }
137 $vals['url'] = $f->getFullURL();
138 }
139 if($this->fld_comment)
140 $vals['comment'] = $f->getDescription();
141 if($this->fld_sha1)
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');
147 }
148 if($this->fld_archivename && $f->isOld())
149 $vals['archivename'] = $f->getArchiveName();
150
151 return $vals;
152 }
153
154 public function getAllowedParams() {
155 return array (
156 'prop' => array (
157 ApiBase :: PARAM_ISMULTI => true,
158 ApiBase :: PARAM_DFLT => 'timestamp|user',
159 ApiBase :: PARAM_TYPE => array (
160 'timestamp',
161 'user',
162 'comment',
163 'url',
164 'size',
165 'sha1',
166 'metadata',
167 'archivename'
168 )
169 ),
170 'limit' => 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
176 ),
177 'start' => array(
178 ApiBase :: PARAM_TYPE => 'timestamp'
179 ),
180 'end' => array(
181 ApiBase :: PARAM_TYPE => 'timestamp'
182 ),
183 'urlwidth' => array(
184 ApiBase :: PARAM_TYPE => 'integer',
185 ApiBase :: PARAM_DFLT => -1
186 ),
187 'urlheight' => array(
188 ApiBase :: PARAM_TYPE => 'integer',
189 ApiBase :: PARAM_DFLT => -1
190 )
191 );
192 }
193
194 public function getParamDescription() {
195 return array (
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',
202 );
203 }
204
205 public function getDescription() {
206 return array (
207 'Returns image information and upload history'
208 );
209 }
210
211 protected function getExamples() {
212 return array (
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',
215 );
216 }
217
218 public function getVersion() {
219 return __CLASS__ . ': $Id$';
220 }
221 }