bb8aed8a091d5f300288c057ab6c48ad355b996e
[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
47 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
48 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
49
50 if ( $params['urlwidth'] != -1 ) {
51 $scale = array();
52 $scale['width'] = $params['urlwidth'];
53 $scale['height'] = $params['urlheight'];
54 } else {
55 $scale = null;
56 }
57
58 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
59 if (!empty($pageIds[NS_IMAGE])) {
60
61 $result = $this->getResult();
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[] = self::getInfo( $img, $prop, $result, $scale );
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[] = self::getInfo( $oldie, $prop, $result );
94 }
95 }
96
97 $result->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 static function getInfo($file, $prop, $result, $scale = null) {
113 $vals = array();
114 if( isset( $prop['timestamp'] ) )
115 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp());
116 if( isset( $prop['user'] ) ) {
117 $vals['user'] = $file->getUser();
118 if( !$file->getUser( 'id' ) )
119 $vals['anon'] = '';
120 }
121 if( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
122 $vals['size'] = intval( $file->getSize() );
123 $vals['width'] = intval( $file->getWidth() );
124 $vals['height'] = intval( $file->getHeight() );
125 }
126 if( isset( $prop['url'] ) ) {
127 if( !is_null( $scale ) && !$file->isOld() ) {
128 $thumb = $file->getThumbnail( $scale['width'], $scale['height'] );
129 if( $thumb )
130 {
131 $vals['thumburl'] = wfExpandUrl( $thumb->getURL() );
132 $vals['thumbwidth'] = $thumb->getWidth();
133 $vals['thumbheight'] = $thumb->getHeight();
134 }
135 }
136 $vals['url'] = $file->getFullURL();
137 }
138 if( isset( $prop['comment'] ) )
139 $vals['comment'] = $file->getDescription();
140 if( isset( $prop['sha1'] ) )
141 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
142 if( isset( $prop['metadata'] ) ) {
143 $metadata = $file->getMetadata();
144 $vals['metadata'] = $metadata ? unserialize( $metadata ) : null;
145 $result->setIndexedTagName_recursive( $vals['metadata'], 'meta' );
146 }
147 if( isset( $prop['mimetype'] ) )
148 $vals['mime'] = $file->getMimeType();
149
150 if( isset( $prop['archivename'] ) && $file->isOld() )
151 $vals['archivename'] = $file->getArchiveName();
152
153 return $vals;
154 }
155
156 public function getAllowedParams() {
157 return array (
158 'prop' => array (
159 ApiBase :: PARAM_ISMULTI => true,
160 ApiBase :: PARAM_DFLT => 'timestamp|user',
161 ApiBase :: PARAM_TYPE => array (
162 'timestamp',
163 'user',
164 'comment',
165 'url',
166 'size',
167 'sha1',
168 'mime',
169 'metadata',
170 'archivename'
171 )
172 ),
173 'limit' => array(
174 ApiBase :: PARAM_TYPE => 'limit',
175 ApiBase :: PARAM_DFLT => 1,
176 ApiBase :: PARAM_MIN => 1,
177 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
178 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
179 ),
180 'start' => array(
181 ApiBase :: PARAM_TYPE => 'timestamp'
182 ),
183 'end' => array(
184 ApiBase :: PARAM_TYPE => 'timestamp'
185 ),
186 'urlwidth' => array(
187 ApiBase :: PARAM_TYPE => 'integer',
188 ApiBase :: PARAM_DFLT => -1
189 ),
190 'urlheight' => array(
191 ApiBase :: PARAM_TYPE => 'integer',
192 ApiBase :: PARAM_DFLT => -1
193 )
194 );
195 }
196
197 public function getParamDescription() {
198 return array (
199 'prop' => 'What image information to get.',
200 'limit' => 'How many image revisions to return',
201 'start' => 'Timestamp to start listing from',
202 'end' => 'Timestamp to stop listing at',
203 '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.',
204 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
205 );
206 }
207
208 public function getDescription() {
209 return array (
210 'Returns image information and upload history'
211 );
212 }
213
214 protected function getExamples() {
215 return array (
216 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
217 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
218 );
219 }
220
221 public function getVersion() {
222 return __CLASS__ . ': $Id$';
223 }
224 }