* (bug 27585) add pagecount to list=filearchive
[lhc/web/wiklou.git] / includes / api / ApiQueryFilearchive.php
1 <?php
2 /**
3 * API for MediaWiki 1.12+
4 *
5 * Created on May 10, 2010
6 *
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllpages.php
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 *
26 * @file
27 */
28
29 if ( !defined( 'MEDIAWIKI' ) ) {
30 // Eclipse helper - will be ignored in production
31 require_once( 'ApiQueryBase.php' );
32 }
33
34 /**
35 * Query module to enumerate all deleted files.
36 *
37 * @ingroup API
38 */
39 class ApiQueryFilearchive extends ApiQueryBase {
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'fa' );
43 }
44
45 public function execute() {
46 global $wgUser;
47 // Before doing anything at all, let's check permissions
48 if ( !$wgUser->isAllowed( 'deletedhistory' ) ) {
49 $this->dieUsage( 'You don\'t have permission to view deleted file information', 'permissiondenied' );
50 }
51
52 $db = $this->getDB();
53
54 $params = $this->extractRequestParams();
55
56 $prop = array_flip( $params['prop'] );
57 $fld_sha1 = isset( $prop['sha1'] );
58 $fld_timestamp = isset( $prop['timestamp'] );
59 $fld_user = isset( $prop['user'] );
60 $fld_size = isset( $prop['size'] );
61 $fld_dimensions = isset( $prop['dimensions'] );
62 $fld_description = isset( $prop['description'] );
63 $fld_mime = isset( $prop['mime'] );
64 $fld_metadata = isset( $prop['metadata'] );
65 $fld_bitdepth = isset( $prop['bitdepth'] );
66
67 $this->addTables( 'filearchive' );
68
69 $this->addFields( array( 'fa_name', 'fa_deleted' ) );
70 $this->addFieldsIf( 'fa_storage_key', $fld_sha1 );
71 $this->addFieldsIf( 'fa_timestamp', $fld_timestamp );
72
73 if ( $fld_user ) {
74 $this->addFields( array( 'fa_user', 'fa_user_text' ) );
75 }
76 $this->addFieldsIf( 'fa_size', $fld_size );
77
78 if ( $fld_dimensions ) {
79 $this->addFields( array( 'fa_height', 'fa_width' ) );
80 }
81
82 $this->addFieldsIf( 'fa_description', $fld_description );
83
84 if ( $fld_mime ) {
85 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
86 }
87
88 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
89 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
90
91 // Image filters
92 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
93 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
94 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
95 $this->addWhereRange( 'fa_name', $dir, $from, $to );
96 if ( isset( $params['prefix'] ) ) {
97 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
98 }
99
100 if ( !$wgUser->isAllowed( 'suppressrevision' ) ) {
101 // Filter out revisions that the user is not allowed to see. There
102 // is no way to indicate that we have skipped stuff because the
103 // continuation parameter is fa_name
104
105 // Note that this field is unindexed. This should however not be
106 // a big problem as files with fa_deleted are rare
107 $this->addWhereFld( 'fa_deleted', 0 );
108 }
109
110 $limit = $params['limit'];
111 $this->addOption( 'LIMIT', $limit + 1 );
112 $this->addOption( 'ORDER BY', 'fa_name' .
113 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
114
115 $res = $this->select( __METHOD__ );
116
117 $count = 0;
118 $result = $this->getResult();
119 foreach ( $res as $row ) {
120 if ( ++$count > $limit ) {
121 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
122 // TODO: Security issue - if the user has no right to view next title, it will still be shown
123 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
124 break;
125 }
126
127 $file = array();
128 $file['name'] = $row->fa_name;
129 self::addTitleInfo( $file, Title::makeTitle( NS_FILE, $row->fa_name ) );
130
131 if ( $fld_sha1 ) {
132 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
133 }
134 if ( $fld_timestamp ) {
135 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
136 }
137 if ( $fld_user ) {
138 $file['userid'] = $row->fa_user;
139 $file['user'] = $row->fa_user_text;
140 }
141 if ( $fld_size ) {
142 $file['size'] = $row->fa_size;
143
144 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
145 if ( $pageCount !== false ) {
146 $vals['pagecount'] = $pageCount;
147 }
148 }
149 if ( $fld_dimensions ) {
150 $file['height'] = $row->fa_height;
151 $file['width'] = $row->fa_width;
152 }
153 if ( $fld_description ) {
154 $file['description'] = $row->fa_description;
155 }
156 if ( $fld_metadata ) {
157 $file['metadata'] = $row->fa_metadata
158 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
159 : null;
160 }
161 if ( $fld_bitdepth ) {
162 $file['bitdepth'] = $row->fa_bits;
163 }
164 if ( $fld_mime ) {
165 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
166 }
167
168 if ( $row->fa_deleted & File::DELETED_FILE ) {
169 $file['filehidden'] = '';
170 }
171 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
172 $file['commenthidden'] = '';
173 }
174 if ( $row->fa_deleted & File::DELETED_USER ) {
175 $file['userhidden'] = '';
176 }
177 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
178 // This file is deleted for normal admins
179 $file['suppressed'] = '';
180 }
181
182
183 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
184 if ( !$fit ) {
185 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
186 break;
187 }
188 }
189
190 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
191 }
192
193 public function getAllowedParams() {
194 return array (
195 'from' => null,
196 'to' => null,
197 'prefix' => null,
198 'limit' => array(
199 ApiBase::PARAM_DFLT => 10,
200 ApiBase::PARAM_TYPE => 'limit',
201 ApiBase::PARAM_MIN => 1,
202 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
203 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
204 ),
205 'dir' => array(
206 ApiBase::PARAM_DFLT => 'ascending',
207 ApiBase::PARAM_TYPE => array(
208 'ascending',
209 'descending'
210 )
211 ),
212 'prop' => array(
213 ApiBase::PARAM_DFLT => 'timestamp',
214 ApiBase::PARAM_ISMULTI => true,
215 ApiBase::PARAM_TYPE => array(
216 'sha1',
217 'timestamp',
218 'user',
219 'size',
220 'dimensions',
221 'description',
222 'mime',
223 'metadata',
224 'bitdepth'
225 ),
226 ),
227 );
228 }
229
230 public function getParamDescription() {
231 return array(
232 'from' => 'The image title to start enumerating from',
233 'to' => 'The image title to stop enumerating at',
234 'prefix' => 'Search for all image titles that begin with this value',
235 'dir' => 'The direction in which to list',
236 'limit' => 'How many images to return in total',
237 'prop' => array(
238 'What image information to get:',
239 ' sha1 - Adds SHA-1 hash for the image',
240 ' timestamp - Adds timestamp for the uploaded version',
241 ' user - Adds user who uploaded the image version',
242 ' size - Adds the size of the image in bytes',
243 ' dimensions - Adds the height and width of the image',
244 ' description - Adds description the image version',
245 ' mime - Adds MIME of the image',
246 ' metadata - Lists EXIF metadata for the version of the image',
247 ' bitdepth - Adds the bit depth of the version',
248 ),
249 );
250 }
251
252 public function getDescription() {
253 return 'Enumerate all deleted files sequentially';
254 }
255
256 public function getPossibleErrors() {
257 return array_merge( parent::getPossibleErrors(), array(
258 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
259 ) );
260 }
261
262 protected function getExamples() {
263 return array(
264 'Simple Use',
265 ' Show a list of all deleted files',
266 ' api.php?action=query&list=filearchive',
267 );
268 }
269
270 public function getVersion() {
271 return __CLASS__ . ': $Id$';
272 }
273 }