Improve query building if's to cater for addition of columns
[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
77 if ( $fld_dimensions || $fld_size ) {
78 $this->addFields( array( 'fa_height', 'fa_width', 'fa_size' ) );
79 }
80
81 $this->addFieldsIf( 'fa_description', $fld_description );
82
83 if ( $fld_mime ) {
84 $this->addFields( array( 'fa_major_mime', 'fa_minor_mime' ) );
85 }
86
87 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
88 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
89
90 // Image filters
91 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
92 $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
93 $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
94 $this->addWhereRange( 'fa_name', $dir, $from, $to );
95 if ( isset( $params['prefix'] ) ) {
96 $this->addWhere( 'fa_name' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
97 }
98
99 if ( !$wgUser->isAllowed( 'suppressrevision' ) ) {
100 // Filter out revisions that the user is not allowed to see. There
101 // is no way to indicate that we have skipped stuff because the
102 // continuation parameter is fa_name
103
104 // Note that this field is unindexed. This should however not be
105 // a big problem as files with fa_deleted are rare
106 $this->addWhereFld( 'fa_deleted', 0 );
107 }
108
109 $limit = $params['limit'];
110 $this->addOption( 'LIMIT', $limit + 1 );
111 $this->addOption( 'ORDER BY', 'fa_name' .
112 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
113
114 $res = $this->select( __METHOD__ );
115
116 $count = 0;
117 $result = $this->getResult();
118 foreach ( $res as $row ) {
119 if ( ++$count > $limit ) {
120 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
121 // TODO: Security issue - if the user has no right to view next title, it will still be shown
122 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
123 break;
124 }
125
126 $file = array();
127 $file['name'] = $row->fa_name;
128 self::addTitleInfo( $file, Title::makeTitle( NS_FILE, $row->fa_name ) );
129
130 if ( $fld_sha1 ) {
131 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
132 }
133 if ( $fld_timestamp ) {
134 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
135 }
136 if ( $fld_user ) {
137 $file['userid'] = $row->fa_user;
138 $file['user'] = $row->fa_user_text;
139 }
140 if ( $fld_size || $fld_dimensions ) {
141 $file['size'] = $row->fa_size;
142
143 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
144 if ( $pageCount !== false ) {
145 $vals['pagecount'] = $pageCount;
146 }
147
148 $file['height'] = $row->fa_height;
149 $file['width'] = $row->fa_width;
150 }
151 if ( $fld_description ) {
152 $file['description'] = $row->fa_description;
153 }
154 if ( $fld_metadata ) {
155 $file['metadata'] = $row->fa_metadata
156 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
157 : null;
158 }
159 if ( $fld_bitdepth ) {
160 $file['bitdepth'] = $row->fa_bits;
161 }
162 if ( $fld_mime ) {
163 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
164 }
165
166 if ( $row->fa_deleted & File::DELETED_FILE ) {
167 $file['filehidden'] = '';
168 }
169 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
170 $file['commenthidden'] = '';
171 }
172 if ( $row->fa_deleted & File::DELETED_USER ) {
173 $file['userhidden'] = '';
174 }
175 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
176 // This file is deleted for normal admins
177 $file['suppressed'] = '';
178 }
179
180
181 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
182 if ( !$fit ) {
183 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
184 break;
185 }
186 }
187
188 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
189 }
190
191 public function getAllowedParams() {
192 return array (
193 'from' => null,
194 'to' => null,
195 'prefix' => null,
196 'limit' => array(
197 ApiBase::PARAM_DFLT => 10,
198 ApiBase::PARAM_TYPE => 'limit',
199 ApiBase::PARAM_MIN => 1,
200 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
201 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
202 ),
203 'dir' => array(
204 ApiBase::PARAM_DFLT => 'ascending',
205 ApiBase::PARAM_TYPE => array(
206 'ascending',
207 'descending'
208 )
209 ),
210 'prop' => array(
211 ApiBase::PARAM_DFLT => 'timestamp',
212 ApiBase::PARAM_ISMULTI => true,
213 ApiBase::PARAM_TYPE => array(
214 'sha1',
215 'timestamp',
216 'user',
217 'size',
218 'dimensions',
219 'description',
220 'mime',
221 'metadata',
222 'bitdepth'
223 ),
224 ),
225 );
226 }
227
228 public function getParamDescription() {
229 return array(
230 'from' => 'The image title to start enumerating from',
231 'to' => 'The image title to stop enumerating at',
232 'prefix' => 'Search for all image titles that begin with this value',
233 'dir' => 'The direction in which to list',
234 'limit' => 'How many images to return in total',
235 'prop' => array(
236 'What image information to get:',
237 ' sha1 - Adds SHA-1 hash for the image',
238 ' timestamp - Adds timestamp for the uploaded version',
239 ' user - Adds user who uploaded the image version',
240 ' size - Adds the size of the image in bytes and the height, width and page count (if applicable)',
241 ' dimensions - Alias for size',
242 ' description - Adds description the image version',
243 ' mime - Adds MIME of the image',
244 ' metadata - Lists EXIF metadata for the version of the image',
245 ' bitdepth - Adds the bit depth of the version',
246 ),
247 );
248 }
249
250 public function getDescription() {
251 return 'Enumerate all deleted files sequentially';
252 }
253
254 public function getPossibleErrors() {
255 return array_merge( parent::getPossibleErrors(), array(
256 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
257 ) );
258 }
259
260 protected function getExamples() {
261 return array(
262 'Simple Use',
263 ' Show a list of all deleted files',
264 ' api.php?action=query&list=filearchive',
265 );
266 }
267
268 public function getVersion() {
269 return __CLASS__ . ': $Id$';
270 }
271 }