]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryFilearchive.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.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                 $this->addWhereRange( 'fa_name', $dir, $from, null );
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                 
110                         
111                 $limit = $params['limit'];
112                 $this->addOption( 'LIMIT', $limit + 1 );
113                 $this->addOption( 'ORDER BY', 'fa_name' .
114                                                 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
115
116                 $res = $this->select( __METHOD__ );
117
118                 $count = 0;
119                 $result = $this->getResult();
120                 foreach ( $res as $row ) {
121                         if ( ++$count > $limit ) {
122                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
123                                 // TODO: Security issue - if the user has no right to view next title, it will still be shown
124                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
125                                 break;
126                         }
127
128                         $file = array();
129                         $file['name'] = $row->fa_name;
130
131                         if ( $fld_sha1 ) {
132                                 $file['sha1'] = wfBaseConvert( $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                         if ( $fld_dimensions ) {
145                                 $file['height'] = $row->fa_height;
146                                 $file['width'] = $row->fa_width;
147                         }
148                         if ( $fld_description ) {
149                                 $file['description'] = $row->fa_description;
150                         }
151                         if ( $fld_metadata ) {
152                                 $file['metadata'] = $row->fa_metadata ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result ) : null;
153                         }
154                         if ( $fld_bitdepth ) {
155                                 $file['bitdepth'] = $row->fa_bits;
156                         }
157                         if ( $fld_mime ) {
158                                 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
159                         }
160                         
161                         if ( $row->fa_deleted & File::DELETED_FILE ) {
162                                 $file['filehidden'] = '';
163                         }
164                         if ( $row->fa_deleted & File::DELETED_COMMENT ) {
165                                 $file['commenthidden'] = '';
166                         }
167                         if ( $row->fa_deleted & File::DELETED_USER ) {
168                                 $file['userhidden'] = '';
169                         }
170                         if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
171                                 // This file is deleted for normal admins
172                                 $file['suppressed'] = '';
173                         }
174
175                         
176                         $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
177                         if ( !$fit ) {
178                                 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
179                                 break;
180                         }
181                 }
182
183                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
184         }
185
186         public function getAllowedParams() {
187                 return array (
188                         'from' => null,
189                         'prefix' => null,
190                         'limit' => array(
191                                 ApiBase::PARAM_DFLT => 10,
192                                 ApiBase::PARAM_TYPE => 'limit',
193                                 ApiBase::PARAM_MIN => 1,
194                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
195                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
196                         ),
197                         'dir' => array(
198                                 ApiBase::PARAM_DFLT => 'ascending',
199                                 ApiBase::PARAM_TYPE => array(
200                                         'ascending',
201                                         'descending'
202                                 )
203                         ),
204                         'prop' => array(
205                                 ApiBase::PARAM_DFLT => 'timestamp',
206                                 ApiBase::PARAM_ISMULTI => true,
207                                 ApiBase::PARAM_TYPE => array(
208                                         'sha1',
209                                         'timestamp',
210                                         'user',
211                                         'size',
212                                         'dimensions',
213                                         'description',
214                                         'mime',
215                                         'metadata',
216                                         'bitdepth'
217                                 ),
218                         ),
219                 );
220         }
221
222         public function getParamDescription() {
223                 return array(
224                         'from' => 'The image title to start enumerating from',
225                         'prefix' => 'Search for all image titles that begin with this value',
226                         'dir' => 'The direction in which to list',
227                         'limit' => 'How many total images to return',
228                         'prop' => array(
229                                 'What image information to get:',
230                                 ' sha1         - Adds sha1 hash for the image',
231                                 ' timestamp    - Adds timestamp for the uploaded version',
232                                 ' user         - Adds user who uploaded the image version',
233                                 ' size         - Adds the size of the image in bytes',
234                                 ' dimensions   - Adds the height and width of the image',
235                                 ' description  - Adds description the image version',
236                                 ' mime         - Adds MIME of the image',
237                                 ' metadata     - Lists EXIF metadata for the version of the image',
238                                 ' bitdepth     - Adds the bit depth of the version',
239             ),
240                 );
241         }
242
243         public function getDescription() {
244                 return 'Enumerate all deleted files sequentially';
245         }
246
247         public function getPossibleErrors() {
248                 return array_merge( parent::getPossibleErrors(), array(
249                         array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
250                 ) );
251         }
252
253         protected function getExamples() {
254                 return array(
255                         'Simple Use',
256                         ' Show a list of all deleted files',
257                         '  api.php?action=query&list=filearchive',
258                 );
259         }
260
261         public function getVersion() {
262                 return __CLASS__ . ': $Id$';
263         }
264 }