A little refactoring of the input splitting/expansion:
[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 $history = $params['history'];
46
47 $prop = array_flip($params['prop']);
48 $fld_timestamp = isset($prop['timestamp']);
49 $fld_user = isset($prop['user']);
50 $fld_comment = isset($prop['comment']);
51 $fld_url = isset($prop['url']);
52 $fld_size = isset($prop['size']);
53 $fld_sha1 = isset($prop['sha1']);
54 $fld_metadata = isset($prop['metadata']);
55
56 if($params['urlheight'] != -1 && $params['urlwidth'] == -1)
57 $this->dieUsage("iiurlheight cannot be used without iiurlwidth", 'iiurlwidth');
58 $scale = $params['urlwidth'] != -1;
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 $isCur = true;
75 $count = 0;
76 while($line = $img->nextHistoryLine()) { // assignment
77 # FIXME: Limiting to 500 because it's unlimited right now
78 # 500+ image histories are scarce, but this has DoS potential
79 # FileRepo.php should be fixed
80 if($count++ == 500)
81 break;
82 $row = get_object_vars( $line );
83 $vals = array();
84 $prefix = $isCur ? 'img' : 'oi';
85
86 if ($fld_timestamp)
87 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row["${prefix}_timestamp"]);
88 if ($fld_user) {
89 $vals['user'] = $row["${prefix}_user_text"];
90 if(!$row["${prefix}_user"])
91 $vals['anon'] = '';
92 }
93 if ($fld_size) {
94 $vals['size'] = intval($row["{$prefix}_size"]);
95 $vals['width'] = intval($row["{$prefix}_width"]);
96 $vals['height'] = intval($row["{$prefix}_height"]);
97 }
98 if ($fld_url) {
99 if($scale && $isCur) {
100 $vals['url'] = $img->createThumb($params['urlwidth'], $params['urlheight']);
101 } else {
102 $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($row["oi_archive_name"]);
103 }
104 }
105 if ($fld_comment)
106 $vals['comment'] = $row["{$prefix}_description"];
107
108 if ($fld_sha1)
109 $vals['sha1'] = wfBaseConvert($row["{$prefix}_sha1"], 36, 16, 40);
110
111 if ($fld_metadata) {
112 $metadata = unserialize($row["{$prefix}_metadata"]);
113 $vals['metadata'] = $metadata ? $metadata : null;
114 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
115 }
116
117 $data[] = $vals;
118
119 if (!$history) // Stop after the first line.
120 break;
121
122 $isCur = false;
123 }
124
125 $img->resetHistory();
126 }
127
128 $this->getResult()->addValue(array(
129 'query', 'pages', intval($pageId)),
130 'imagerepository', $repository
131 );
132 if (!empty($data))
133 $this->addPageSubItems($pageId, $data);
134 }
135 }
136 }
137
138 protected function getAllowedParams() {
139 return array (
140 'prop' => array (
141 ApiBase :: PARAM_ISMULTI => true,
142 ApiBase :: PARAM_DFLT => 'timestamp|user',
143 ApiBase :: PARAM_TYPE => array (
144 'timestamp',
145 'user',
146 'comment',
147 'url',
148 'size',
149 'sha1',
150 'metadata'
151 )
152 ),
153 'history' => false,
154 'urlwidth' => array(
155 ApiBase :: PARAM_TYPE => 'integer',
156 ApiBase :: PARAM_DFLT => -1
157 ),
158 'urlheight' => array(
159 ApiBase :: PARAM_TYPE => 'integer',
160 ApiBase :: PARAM_DFLT => -1
161 )
162 );
163 }
164
165 protected function getParamDescription() {
166 return array (
167 'prop' => 'What image information to get.',
168 'history' => 'Include upload history',
169 '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.',
170 'urlheight' => 'Similar to iiurlwidth. Cannot be used without iiurlwidth',
171 );
172 }
173
174 protected function getDescription() {
175 return array (
176 'Returns image information and upload history'
177 );
178 }
179
180 protected function getExamples() {
181 return array (
182 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
183 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
184 );
185 }
186
187 public function getVersion() {
188 return __CLASS__ . ': $Id$';
189 }
190 }