Kill filtering by hash because the query is unindexed. We need a condition on fa_stor...
[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( 'fa_name' );
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 $limit = $params['limit'];
99 $this->addOption( 'LIMIT', $limit + 1 );
100 $this->addOption( 'ORDER BY', 'fa_name' .
101 ( $params['dir'] == 'descending' ? ' DESC' : '' ) );
102
103 $res = $this->select( __METHOD__ );
104
105 $count = 0;
106 $result = $this->getResult();
107 foreach ( $res as $row ) {
108 if ( ++$count > $limit ) {
109 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
110 // TODO: Security issue - if the user has no right to view next title, it will still be shown
111 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
112 break;
113 }
114
115 $file = array();
116 $file['name'] = $row->fa_name;
117 self::addTitleInfo( $file, Title::makeTitle( NS_FILE, $row->fa_name ) );
118
119 if ( $fld_sha1 ) {
120 $file['sha1'] = wfBaseConvert( LocalRepo::getHashFromKey( $row->fa_storage_key ), 36, 16, 40 );
121 }
122 if ( $fld_timestamp ) {
123 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
124 }
125 if ( $fld_user ) {
126 $file['userid'] = $row->fa_user;
127 $file['user'] = $row->fa_user_text;
128 }
129 if ( $fld_size ) {
130 $file['size'] = $row->fa_size;
131 }
132 if ( $fld_dimensions ) {
133 $file['height'] = $row->fa_height;
134 $file['width'] = $row->fa_width;
135 }
136 if ( $fld_description ) {
137 $file['description'] = $row->fa_description;
138 }
139 if ( $fld_metadata ) {
140 $file['metadata'] = $row->fa_metadata
141 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
142 : null;
143 }
144 if ( $fld_bitdepth ) {
145 $file['bitdepth'] = $row->fa_bits;
146 }
147 if ( $fld_mime ) {
148 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
149 }
150
151 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
152 if ( !$fit ) {
153 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->fa_name ) );
154 break;
155 }
156 }
157
158 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
159 }
160
161 public function getAllowedParams() {
162 return array (
163 'from' => null,
164 'prefix' => null,
165 'limit' => array(
166 ApiBase::PARAM_DFLT => 10,
167 ApiBase::PARAM_TYPE => 'limit',
168 ApiBase::PARAM_MIN => 1,
169 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
170 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
171 ),
172 'dir' => array(
173 ApiBase::PARAM_DFLT => 'ascending',
174 ApiBase::PARAM_TYPE => array(
175 'ascending',
176 'descending'
177 )
178 ),
179 'prop' => array(
180 ApiBase::PARAM_DFLT => 'timestamp',
181 ApiBase::PARAM_ISMULTI => true,
182 ApiBase::PARAM_TYPE => array(
183 'sha1',
184 'timestamp',
185 'user',
186 'size',
187 'dimensions',
188 'description',
189 'mime',
190 'metadata',
191 'bitdepth'
192 ),
193 ),
194 );
195 }
196
197 public function getParamDescription() {
198 return array(
199 'from' => 'The image title to start enumerating from',
200 'prefix' => 'Search for all image titles that begin with this value',
201 'dir' => 'The direction in which to list',
202 'limit' => 'How many total images to return',
203 'prop' => array(
204 'What image information to get:',
205 ' sha1 - Adds SHA-1 hash for the image',
206 ' timestamp - Adds timestamp for the uploaded version',
207 ' user - Adds user who uploaded the image version',
208 ' size - Adds the size of the image in bytes',
209 ' dimensions - Adds the height and width of the image',
210 ' description - Adds description the image version',
211 ' mime - Adds MIME of the image',
212 ' metadata - Lists EXIF metadata for the version of the image',
213 ' bitdepth - Adds the bit depth of the version',
214 ),
215 );
216 }
217
218 public function getDescription() {
219 return 'Enumerate all deleted files sequentially';
220 }
221
222 public function getPossibleErrors() {
223 return array_merge( parent::getPossibleErrors(), array(
224 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted file information' ),
225 ) );
226 }
227
228 protected function getExamples() {
229 return array(
230 'Simple Use',
231 ' Show a list of all deleted files',
232 ' api.php?action=query&list=filearchive',
233 );
234 }
235
236 public function getVersion() {
237 return __CLASS__ . ': $Id$';
238 }
239 }