API: (bug 16858) Revamped list=deletedrevs to make listing deleted contributions...
[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 * @ingroup 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_FILE])) {
60
61 $result = $this->getResult();
62 $images = RepoGroup::singleton()->findFiles( array_keys( $pageIds[NS_FILE] ) );
63 foreach ( $images as $img ) {
64 $data = array();
65
66 // Get information about the current version first
67 // Check that the current version is within the start-end boundaries
68 if((is_null($params['start']) || $img->getTimestamp() <= $params['start']) &&
69 (is_null($params['end']) || $img->getTimestamp() >= $params['end'])) {
70 $data[] = self::getInfo( $img, $prop, $result, $scale );
71 }
72
73 // Now get the old revisions
74 // Get one more to facilitate query-continue functionality
75 $count = count($data);
76 $oldies = $img->getHistory($params['limit'] - $count + 1, $params['start'], $params['end']);
77 foreach($oldies as $oldie) {
78 if(++$count > $params['limit']) {
79 // We've reached the extra one which shows that there are additional pages to be had. Stop here...
80 // Only set a query-continue if there was only one title
81 if(count($pageIds[NS_FILE]) == 1)
82 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
83 break;
84 }
85 $data[] = self::getInfo( $oldie, $prop, $result );
86 }
87
88 $pageId = $pageIds[NS_FILE][ $img->getOriginalTitle()->getDBkey() ];
89 $result->addValue(
90 array( 'query', 'pages', intval( $pageId ) ),
91 'imagerepository', $img->getRepoName()
92 );
93 $this->addPageSubItems($pageId, $data);
94 }
95
96 $missing = array_diff( array_keys( $pageIds[NS_FILE] ), array_keys( $images ) );
97 foreach ( $missing as $title )
98 $result->addValue(
99 array( 'query', 'pages', intval( $pageIds[NS_FILE][$title] ) ),
100 'imagerepository', ''
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 static function getInfo($file, $prop, $result, $scale = null) {
111 $vals = array();
112 if( isset( $prop['timestamp'] ) )
113 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $file->getTimestamp());
114 if( isset( $prop['user'] ) ) {
115 $vals['user'] = $file->getUser();
116 if( !$file->getUser( 'id' ) )
117 $vals['anon'] = '';
118 }
119 if( isset( $prop['size'] ) || isset( $prop['dimensions'] ) ) {
120 $vals['size'] = intval( $file->getSize() );
121 $vals['width'] = intval( $file->getWidth() );
122 $vals['height'] = intval( $file->getHeight() );
123 }
124 if( isset( $prop['url'] ) ) {
125 if( !is_null( $scale ) && !$file->isOld() ) {
126 $mto = $file->transform( array( 'width' => $scale['width'], 'height' => $scale['height'] ) );
127 if( $mto && !$mto->isError() )
128 {
129 $vals['thumburl'] = $mto->getUrl();
130 $vals['thumbwidth'] = $mto->getWidth();
131 $vals['thumbheight'] = $mto->getHeight();
132 }
133 }
134 $vals['url'] = $file->getFullURL();
135 $vals['descriptionurl'] = wfExpandUrl( $file->getDescriptionUrl() );
136 }
137 if( isset( $prop['comment'] ) )
138 $vals['comment'] = $file->getDescription();
139 if( isset( $prop['sha1'] ) )
140 $vals['sha1'] = wfBaseConvert( $file->getSha1(), 36, 16, 40 );
141 if( isset( $prop['metadata'] ) ) {
142 $metadata = $file->getMetadata();
143 $vals['metadata'] = $metadata ? unserialize( $metadata ) : null;
144 $result->setIndexedTagName_recursive( $vals['metadata'], 'meta' );
145 }
146 if( isset( $prop['mime'] ) )
147 $vals['mime'] = $file->getMimeType();
148
149 if( isset( $prop['archivename'] ) && $file->isOld() )
150 $vals['archivename'] = $file->getArchiveName();
151
152 if( isset( $prop['bitdepth'] ) )
153 $vals['bitdepth'] = $file->getBitDepth();
154
155 return $vals;
156 }
157
158 public function getAllowedParams() {
159 return array (
160 'prop' => array (
161 ApiBase :: PARAM_ISMULTI => true,
162 ApiBase :: PARAM_DFLT => 'timestamp|user',
163 ApiBase :: PARAM_TYPE => array (
164 'timestamp',
165 'user',
166 'comment',
167 'url',
168 'size',
169 'sha1',
170 'mime',
171 'metadata',
172 'archivename',
173 'bitdepth',
174 )
175 ),
176 'limit' => array(
177 ApiBase :: PARAM_TYPE => 'limit',
178 ApiBase :: PARAM_DFLT => 1,
179 ApiBase :: PARAM_MIN => 1,
180 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
181 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
182 ),
183 'start' => array(
184 ApiBase :: PARAM_TYPE => 'timestamp'
185 ),
186 'end' => array(
187 ApiBase :: PARAM_TYPE => 'timestamp'
188 ),
189 'urlwidth' => array(
190 ApiBase :: PARAM_TYPE => 'integer',
191 ApiBase :: PARAM_DFLT => -1
192 ),
193 'urlheight' => array(
194 ApiBase :: PARAM_TYPE => 'integer',
195 ApiBase :: PARAM_DFLT => -1
196 )
197 );
198 }
199
200 public function getParamDescription() {
201 return array (
202 'prop' => 'What image information to get.',
203 'limit' => 'How many image revisions to return',
204 'start' => 'Timestamp to start listing from',
205 'end' => 'Timestamp to stop listing at',
206 'urlwidth' => array('If iiprop=url is set, a URL to an image scaled to this width will be returned.',
207 'Only the current version of the image can be scaled.'),
208 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
209 );
210 }
211
212 public function getDescription() {
213 return array (
214 'Returns image information and upload history'
215 );
216 }
217
218 protected function getExamples() {
219 return array (
220 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
221 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iilimit=50&iiend=20071231235959&iiprop=timestamp|user|url',
222 );
223 }
224
225 public function getVersion() {
226 return __CLASS__ . ': $Id$';
227 }
228 }