Partial revert of r37502, 37503: double-quoting of SQL strings
[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_IMAGE])) {
60
61 $result = $this->getResult();
62 $images = RepoGroup::singleton()->findFiles( array_keys( $pageIds[NS_IMAGE] ) );
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_IMAGE]) == 1)
82 $this->setContinueEnumParameter('start', $oldie->getTimestamp());
83 break;
84 }
85 $data[] = self::getInfo( $oldie, $prop, $result );
86 }
87
88 $pageId = $pageIds[NS_IMAGE][ $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_IMAGE] ), array_keys( $images ) );
97 foreach ( $missing as $title )
98 $result->addValue(
99 array( 'query', 'pages', intval( $pageIds[NS_IMAGE][$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 $thumb = $file->getThumbnail( $scale['width'], $scale['height'] );
127 if( $thumb )
128 {
129 $vals['thumburl'] = wfExpandUrl( $thumb->getURL() );
130 $vals['thumbwidth'] = $thumb->getWidth();
131 $vals['thumbheight'] = $thumb->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 return $vals;
153 }
154
155 public function getAllowedParams() {
156 return array (
157 'prop' => array (
158 ApiBase :: PARAM_ISMULTI => true,
159 ApiBase :: PARAM_DFLT => 'timestamp|user',
160 ApiBase :: PARAM_TYPE => array (
161 'timestamp',
162 'user',
163 'comment',
164 'url',
165 'size',
166 'sha1',
167 'mime',
168 'metadata',
169 'archivename'
170 )
171 ),
172 'limit' => array(
173 ApiBase :: PARAM_TYPE => 'limit',
174 ApiBase :: PARAM_DFLT => 1,
175 ApiBase :: PARAM_MIN => 1,
176 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
177 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
178 ),
179 'start' => array(
180 ApiBase :: PARAM_TYPE => 'timestamp'
181 ),
182 'end' => array(
183 ApiBase :: PARAM_TYPE => 'timestamp'
184 ),
185 'urlwidth' => array(
186 ApiBase :: PARAM_TYPE => 'integer',
187 ApiBase :: PARAM_DFLT => -1
188 ),
189 'urlheight' => array(
190 ApiBase :: PARAM_TYPE => 'integer',
191 ApiBase :: PARAM_DFLT => -1
192 )
193 );
194 }
195
196 public function getParamDescription() {
197 return array (
198 'prop' => 'What image information to get.',
199 'limit' => 'How many image revisions to return',
200 'start' => 'Timestamp to start listing from',
201 'end' => 'Timestamp to stop listing at',
202 'urlwidth' => array('If iiprop=url is set, a URL to an image scaled to this width will be returned.',
203 '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 }