Fix vulnerability (hopefully): anyone with rollback privileges can mark the rollback...
[mediawiki.git] / includes / api / ApiQueryImageInfo.php
blob9d1026387c49e4d041ab32f82aea1db3425944f9
1 <?php
3 /*
4 * Created on July 6, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
31 /**
32 * A query action to get image information and upload history.
34 * @addtogroup API
36 class ApiQueryImageInfo extends ApiQueryBase {
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'ii');
42 public function execute() {
43 $params = $this->extractRequestParams();
45 $history = $params['history'];
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']);
56 $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
57 if (!empty($pageIds[NS_IMAGE])) {
58 foreach ($pageIds[NS_IMAGE] as $dbKey => $pageId) {
60 $title = Title :: makeTitle(NS_IMAGE, $dbKey);
61 $img = wfFindFile($title);
63 $data = array();
64 if ( !$img ) {
65 $repository = '';
66 } else {
68 $repository = $img->getRepoName();
70 $isCur = true;
71 while($line = $img->nextHistoryLine()) { // assignment
72 $row = get_object_vars( $line );
73 $vals = array();
74 $prefix = $isCur ? 'img' : 'oi';
76 if ($fld_timestamp)
77 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row["${prefix}_timestamp"]);
78 if ($fld_user) {
79 $vals['user'] = $row["${prefix}_user_text"];
80 if(!$row["${prefix}_user"])
81 $vals['anon'] = '';
83 if ($fld_size) {
84 $vals['size'] = intval($row["{$prefix}_size"]);
85 $vals['width'] = intval($row["{$prefix}_width"]);
86 $vals['height'] = intval($row["{$prefix}_height"]);
88 if ($fld_url)
89 $vals['url'] = $isCur ? $img->getURL() : $img->getArchiveUrl($row["oi_archive_name"]);
90 if ($fld_comment)
91 $vals['comment'] = $row["{$prefix}_description"];
93 if ($fld_sha1)
94 $vals['sha1'] = wfBaseConvert($row["{$prefix}_sha1"], 36, 16, 40);
96 if ($fld_metadata) {
97 $metadata = unserialize($row["{$prefix}_metadata"]);
98 $vals['metadata'] = $metadata ? $metadata : null;
99 $this->getResult()->setIndexedTagName_recursive($vals['metadata'], 'meta');
102 $data[] = $vals;
104 if (!$history) // Stop after the first line.
105 break;
107 $isCur = false;
110 $img->resetHistory();
113 $this->getResult()->addValue(array ('query', 'pages', intval($pageId)),
114 'imagerepository',
115 $repository);
116 if (!empty($data))
117 $this->addPageSubItems($pageId, $data);
122 protected function getAllowedParams() {
123 return array (
124 'prop' => array (
125 ApiBase :: PARAM_ISMULTI => true,
126 ApiBase :: PARAM_DFLT => 'timestamp|user',
127 ApiBase :: PARAM_TYPE => array (
128 'timestamp',
129 'user',
130 'comment',
131 'url',
132 'size',
133 'sha1',
134 'metadata'
137 'history' => false,
141 protected function getParamDescription() {
142 return array (
143 'prop' => 'What image information to get.',
144 'history' => 'Include upload history',
148 protected function getDescription() {
149 return array (
150 'Returns image information and upload history'
154 protected function getExamples() {
155 return array (
156 'api.php?action=query&titles=Image:Albert%20Einstein%20Head.jpg&prop=imageinfo',
157 'api.php?action=query&titles=Image:Test.jpg&prop=imageinfo&iihistory&iiprop=timestamp|user|url',
161 public function getVersion() {
162 return __CLASS__ . ': $Id$';