Limiting image history listings to 500 for now. A hard-coded limit sucks, but an...
[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 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
57 if (!empty($pageIds[NS_IMAGE])) {
58 foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
59
60 $title = Title :: makeTitle(NS_IMAGE, $dbKey);
61 $img = wfFindFile($title);
62
63 $data = array();
64 if ( !$img ) {
65 $repository = '';
66 } else {
67
68 $repository = $img->getRepoName();
69
70 $isCur = true;
71 $count = 0;
72 while($line = $img->nextHistoryLine()) { // assignment
73 # FIXME: Limiting to 500 because it's unlimited right now
74 # 500+ image histories are scarce, but this has DoS potential
75 # FileRepo.php should be fixed
76 if($count++ == 500)
77 break;
78 $row = get_object_vars( $line );
79 $vals = array();
80 $prefix = $isCur ? 'img' : 'oi';
81
82 if ($fld_timestamp)
83 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row["${prefix}_timestamp"]);
84 if ($fld_user) {
85 $vals['user'] = $row["${prefix}_user_text"];
86 if(!$row["${prefix}_user"])
87 $vals['anon'] = '';
88 }
89 if ($fld_size) {
90 $vals['size'] = intval($row["{$prefix}_size"]);
91 $vals['width'] = intval($row["{$prefix}_width"]);
92 $vals['height'] = intval($row["{$prefix}_height"]);
93 }
94 if ($fld_url)
95 $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($row["oi_archive_name"]);
96 if ($fld_comment)
97 $vals['comment'] = $row["{$prefix}_description"];
98
99 if ($fld_sha1)
100 $vals['sha1'] = wfBaseConvert($row["{$prefix}_sha1"], 36, 16, 40);
101
102 if ($fld_metadata) {
103 $metadata = unserialize($row["{$prefix}_metadata"]);
104 $vals['metadata'] = $metadata ? $metadata : null;
105 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
106 }
107
108 $data[] = $vals;
109
110 if (!$history) // Stop after the first line.
111 break;
112
113 $isCur = false;
114 }
115
116 $img->resetHistory();
117 }
118
119 $this->getResult()->addValue(array(
120 'query', 'pages', intval($pageId)),
121 'imagerepository', $repository
122 );
123 if (!empty($data))
124 $this->addPageSubItems($pageId, $data);
125 }
126 }
127 }
128
129 protected function getAllowedParams() {
130 return array (
131 'prop' => array (
132 ApiBase :: PARAM_ISMULTI => true,
133 ApiBase :: PARAM_DFLT => 'timestamp|user',
134 ApiBase :: PARAM_TYPE => array (
135 'timestamp',
136 'user',
137 'comment',
138 'url',
139 'size',
140 'sha1',
141 'metadata'
142 )
143 ),
144 'history' => false,
145 );
146 }
147
148 protected function getParamDescription() {
149 return array (
150 'prop' => 'What image information to get.',
151 'history' => 'Include upload history',
152 );
153 }
154
155 protected function getDescription() {
156 return array (
157 'Returns image information and upload history'
158 );
159 }
160
161 protected function getExamples() {
162 return array (
163 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
164 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }