Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryFilearchive.php
blob6b926039dce92359d9a770b3ee883a5e332c7d3b
1 <?php
2 /**
3 * API for MediaWiki 1.12+
5 * Created on May 10, 2010
7 * Copyright © 2010 Sam Reed
8 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
9 * based on ApiQueryAllPages.php
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.
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.
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
26 * @file
29 /**
30 * Query module to enumerate all deleted files.
32 * @ingroup API
34 class ApiQueryFilearchive extends ApiQueryBase {
36 public function __construct( ApiQuery $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'fa' );
40 public function execute() {
41 $user = $this->getUser();
42 // Before doing anything at all, let's check permissions
43 if ( !$user->isAllowed( 'deletedhistory' ) ) {
44 $this->dieUsage(
45 'You don\'t have permission to view deleted file information',
46 'permissiondenied'
50 $db = $this->getDB();
52 $params = $this->extractRequestParams();
54 $prop = array_flip( $params['prop'] );
55 $fld_sha1 = isset( $prop['sha1'] );
56 $fld_timestamp = isset( $prop['timestamp'] );
57 $fld_user = isset( $prop['user'] );
58 $fld_size = isset( $prop['size'] );
59 $fld_dimensions = isset( $prop['dimensions'] );
60 $fld_description = isset( $prop['description'] ) || isset( $prop['parseddescription'] );
61 $fld_mime = isset( $prop['mime'] );
62 $fld_mediatype = isset( $prop['mediatype'] );
63 $fld_metadata = isset( $prop['metadata'] );
64 $fld_bitdepth = isset( $prop['bitdepth'] );
65 $fld_archivename = isset( $prop['archivename'] );
67 $this->addTables( 'filearchive' );
69 $this->addFields( ArchivedFile::selectFields() );
70 $this->addFields( array( 'fa_id', 'fa_name', 'fa_timestamp', 'fa_deleted' ) );
71 $this->addFieldsIf( 'fa_sha1', $fld_sha1 );
72 $this->addFieldsIf( array( 'fa_user', 'fa_user_text' ), $fld_user );
73 $this->addFieldsIf( array( 'fa_height', 'fa_width', 'fa_size' ), $fld_dimensions || $fld_size );
74 $this->addFieldsIf( 'fa_description', $fld_description );
75 $this->addFieldsIf( array( 'fa_major_mime', 'fa_minor_mime' ), $fld_mime );
76 $this->addFieldsIf( 'fa_media_type', $fld_mediatype );
77 $this->addFieldsIf( 'fa_metadata', $fld_metadata );
78 $this->addFieldsIf( 'fa_bits', $fld_bitdepth );
79 $this->addFieldsIf( 'fa_archive_name', $fld_archivename );
81 if ( !is_null( $params['continue'] ) ) {
82 $cont = explode( '|', $params['continue'] );
83 $this->dieContinueUsageIf( count( $cont ) != 3 );
84 $op = $params['dir'] == 'descending' ? '<' : '>';
85 $cont_from = $db->addQuotes( $cont[0] );
86 $cont_timestamp = $db->addQuotes( $db->timestamp( $cont[1] ) );
87 $cont_id = (int)$cont[2];
88 $this->dieContinueUsageIf( $cont[2] !== (string)$cont_id );
89 $this->addWhere( "fa_name $op $cont_from OR " .
90 "(fa_name = $cont_from AND " .
91 "(fa_timestamp $op $cont_timestamp OR " .
92 "(fa_timestamp = $cont_timestamp AND " .
93 "fa_id $op= $cont_id )))"
97 // Image filters
98 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
99 $from = ( $params['from'] === null ? null : $this->titlePartToKey( $params['from'], NS_FILE ) );
100 $to = ( $params['to'] === null ? null : $this->titlePartToKey( $params['to'], NS_FILE ) );
101 $this->addWhereRange( 'fa_name', $dir, $from, $to );
102 if ( isset( $params['prefix'] ) ) {
103 $this->addWhere( 'fa_name' . $db->buildLike(
104 $this->titlePartToKey( $params['prefix'], NS_FILE ),
105 $db->anyString() ) );
108 $sha1Set = isset( $params['sha1'] );
109 $sha1base36Set = isset( $params['sha1base36'] );
110 if ( $sha1Set || $sha1base36Set ) {
111 $sha1 = false;
112 if ( $sha1Set ) {
113 $sha1 = strtolower( $params['sha1'] );
114 if ( !$this->validateSha1Hash( $sha1 ) ) {
115 $this->dieUsage( 'The SHA1 hash provided is not valid', 'invalidsha1hash' );
117 $sha1 = wfBaseConvert( $sha1, 16, 36, 31 );
118 } elseif ( $sha1base36Set ) {
119 $sha1 = strtolower( $params['sha1base36'] );
120 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
121 $this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
124 if ( $sha1 ) {
125 $this->addWhereFld( 'fa_sha1', $sha1 );
129 // Exclude files this user can't view.
130 if ( !$user->isAllowed( 'deletedtext' ) ) {
131 $bitmask = File::DELETED_FILE;
132 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
133 $bitmask = File::DELETED_FILE | File::DELETED_RESTRICTED;
134 } else {
135 $bitmask = 0;
137 if ( $bitmask ) {
138 $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
141 $limit = $params['limit'];
142 $this->addOption( 'LIMIT', $limit + 1 );
143 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
144 $this->addOption( 'ORDER BY', array(
145 'fa_name' . $sort,
146 'fa_timestamp' . $sort,
147 'fa_id' . $sort,
148 ) );
150 $res = $this->select( __METHOD__ );
152 $count = 0;
153 $result = $this->getResult();
154 foreach ( $res as $row ) {
155 if ( ++$count > $limit ) {
156 // We've reached the one extra which shows that there are
157 // additional pages to be had. Stop here...
158 $this->setContinueEnumParameter(
159 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
161 break;
164 $file = array();
165 $file['id'] = $row->fa_id;
166 $file['name'] = $row->fa_name;
167 $title = Title::makeTitle( NS_FILE, $row->fa_name );
168 self::addTitleInfo( $file, $title );
170 if ( $fld_description &&
171 Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
173 $file['description'] = $row->fa_description;
174 if ( isset( $prop['parseddescription'] ) ) {
175 $file['parseddescription'] = Linker::formatComment(
176 $row->fa_description, $title );
179 if ( $fld_user &&
180 Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
182 $file['userid'] = $row->fa_user;
183 $file['user'] = $row->fa_user_text;
185 if ( $fld_sha1 ) {
186 $file['sha1'] = wfBaseConvert( $row->fa_sha1, 36, 16, 40 );
188 if ( $fld_timestamp ) {
189 $file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
191 if ( $fld_size || $fld_dimensions ) {
192 $file['size'] = $row->fa_size;
194 $pageCount = ArchivedFile::newFromRow( $row )->pageCount();
195 if ( $pageCount !== false ) {
196 $file['pagecount'] = $pageCount;
199 $file['height'] = $row->fa_height;
200 $file['width'] = $row->fa_width;
202 if ( $fld_mediatype ) {
203 $file['mediatype'] = $row->fa_media_type;
205 if ( $fld_metadata ) {
206 $file['metadata'] = $row->fa_metadata
207 ? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
208 : null;
210 if ( $fld_bitdepth ) {
211 $file['bitdepth'] = $row->fa_bits;
213 if ( $fld_mime ) {
214 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
216 if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
217 $file['archivename'] = $row->fa_archive_name;
220 if ( $row->fa_deleted & File::DELETED_FILE ) {
221 $file['filehidden'] = '';
223 if ( $row->fa_deleted & File::DELETED_COMMENT ) {
224 $file['commenthidden'] = '';
226 if ( $row->fa_deleted & File::DELETED_USER ) {
227 $file['userhidden'] = '';
229 if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
230 // This file is deleted for normal admins
231 $file['suppressed'] = '';
234 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $file );
235 if ( !$fit ) {
236 $this->setContinueEnumParameter(
237 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
239 break;
243 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'fa' );
246 public function getAllowedParams() {
247 return array(
248 'from' => null,
249 'to' => null,
250 'prefix' => null,
251 'dir' => array(
252 ApiBase::PARAM_DFLT => 'ascending',
253 ApiBase::PARAM_TYPE => array(
254 'ascending',
255 'descending'
258 'sha1' => null,
259 'sha1base36' => null,
260 'prop' => array(
261 ApiBase::PARAM_DFLT => 'timestamp',
262 ApiBase::PARAM_ISMULTI => true,
263 ApiBase::PARAM_TYPE => array(
264 'sha1',
265 'timestamp',
266 'user',
267 'size',
268 'dimensions',
269 'description',
270 'parseddescription',
271 'mime',
272 'mediatype',
273 'metadata',
274 'bitdepth',
275 'archivename',
278 'limit' => array(
279 ApiBase::PARAM_DFLT => 10,
280 ApiBase::PARAM_TYPE => 'limit',
281 ApiBase::PARAM_MIN => 1,
282 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
283 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
285 'continue' => array(
286 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
291 protected function getExamplesMessages() {
292 return array(
293 'action=query&list=filearchive'
294 => 'apihelp-query+filearchive-example-simple',
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/API:Filearchive';