3 * API for MediaWiki 1.12+
5 * Copyright © 2010 Sam Reed
6 * Copyright © 2008 Vasiliev Victor vasilvv@gmail.com,
7 * based on ApiQueryAllPages.php
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 namespace MediaWiki\Api
;
31 use MediaWiki\CommentFormatter\CommentFormatter
;
32 use MediaWiki\CommentFormatter\CommentItem
;
33 use MediaWiki\CommentStore\CommentStore
;
34 use MediaWiki\Revision\RevisionRecord
;
35 use MediaWiki\Title\Title
;
36 use MediaWiki\Title\TitleValue
;
37 use Wikimedia\ParamValidator\ParamValidator
;
38 use Wikimedia\ParamValidator\TypeDef\IntegerDef
;
39 use Wikimedia\Rdbms\IExpression
;
40 use Wikimedia\Rdbms\LikeValue
;
43 * Query module to enumerate all deleted files.
47 class ApiQueryFilearchive
extends ApiQueryBase
{
49 private CommentStore
$commentStore;
50 private CommentFormatter
$commentFormatter;
52 public function __construct(
55 CommentStore
$commentStore,
56 CommentFormatter
$commentFormatter
58 parent
::__construct( $query, $moduleName, 'fa' );
59 $this->commentStore
= $commentStore;
60 $this->commentFormatter
= $commentFormatter;
63 public function execute() {
64 $user = $this->getUser();
67 $params = $this->extractRequestParams();
69 $prop = array_fill_keys( $params['prop'], true );
70 $fld_sha1 = isset( $prop['sha1'] );
71 $fld_timestamp = isset( $prop['timestamp'] );
72 $fld_user = isset( $prop['user'] );
73 $fld_size = isset( $prop['size'] );
74 $fld_dimensions = isset( $prop['dimensions'] );
75 $fld_description = isset( $prop['description'] ) ||
isset( $prop['parseddescription'] );
76 $fld_parseddescription = isset( $prop['parseddescription'] );
77 $fld_mime = isset( $prop['mime'] );
78 $fld_mediatype = isset( $prop['mediatype'] );
79 $fld_metadata = isset( $prop['metadata'] );
80 $fld_bitdepth = isset( $prop['bitdepth'] );
81 $fld_archivename = isset( $prop['archivename'] );
83 if ( $fld_description && !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
84 $this->dieWithError( 'apierror-cantview-deleted-description', 'permissiondenied' );
86 if ( $fld_metadata && !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
87 $this->dieWithError( 'apierror-cantview-deleted-metadata', 'permissiondenied' );
90 $fileQuery = ArchivedFile
::getQueryInfo();
91 $this->addTables( $fileQuery['tables'] );
92 $this->addFields( $fileQuery['fields'] );
93 $this->addJoinConds( $fileQuery['joins'] );
95 if ( $params['continue'] !== null ) {
96 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'timestamp', 'int' ] );
97 $op = $params['dir'] == 'descending' ?
'<=' : '>=';
98 $this->addWhere( $db->buildComparison( $op, [
99 'fa_name' => $cont[0],
100 'fa_timestamp' => $db->timestamp( $cont[1] ),
106 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
107 $from = ( $params['from'] === null ?
null : $this->titlePartToKey( $params['from'], NS_FILE
) );
108 $to = ( $params['to'] === null ?
null : $this->titlePartToKey( $params['to'], NS_FILE
) );
109 $this->addWhereRange( 'fa_name', $dir, $from, $to );
110 if ( isset( $params['prefix'] ) ) {
115 new LikeValue( $this->titlePartToKey( $params['prefix'], NS_FILE
), $db->anyString() )
120 $sha1Set = isset( $params['sha1'] );
121 $sha1base36Set = isset( $params['sha1base36'] );
122 if ( $sha1Set ||
$sha1base36Set ) {
125 $sha1 = strtolower( $params['sha1'] );
126 if ( !$this->validateSha1Hash( $sha1 ) ) {
127 $this->dieWithError( 'apierror-invalidsha1hash' );
129 $sha1 = \Wikimedia\base_convert
( $sha1, 16, 36, 31 );
130 } elseif ( $sha1base36Set ) {
131 $sha1 = strtolower( $params['sha1base36'] );
132 if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
133 $this->dieWithError( 'apierror-invalidsha1base36hash' );
137 $this->addWhereFld( 'fa_sha1', $sha1 );
138 // Paranoia: avoid brute force searches (T19342)
139 if ( !$this->getAuthority()->isAllowed( 'deletedtext' ) ) {
140 $bitmask = File
::DELETED_FILE
;
141 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
142 $bitmask = File
::DELETED_FILE | File
::DELETED_RESTRICTED
;
147 $this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
152 $limit = $params['limit'];
153 $this->addOption( 'LIMIT', $limit +
1 );
154 $sort = ( $params['dir'] == 'descending' ?
' DESC' : '' );
155 $this->addOption( 'ORDER BY', [
157 'fa_timestamp' . $sort,
161 $res = $this->select( __METHOD__
);
163 // Format descriptions in a batch
164 $formattedDescriptions = [];
166 if ( $fld_parseddescription ) {
168 foreach ( $res as $row ) {
169 $desc = $this->commentStore
->getComment( 'fa_description', $row )->text
;
170 $descriptions[$row->fa_id
] = $desc;
171 $commentItems[$row->fa_id
] = ( new CommentItem( $desc ) )
172 ->selfLinkTarget( new TitleValue( NS_FILE
, $row->fa_name
) );
174 $formattedDescriptions = $this->commentFormatter
->createBatch()
175 ->comments( $commentItems )
180 $result = $this->getResult();
181 foreach ( $res as $row ) {
182 if ( ++
$count > $limit ) {
183 // We've reached the one extra which shows that there are
184 // additional pages to be had. Stop here...
185 $this->setContinueEnumParameter(
186 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
191 $exists = $row->fa_archive_name
!== '';
192 $canViewFile = RevisionRecord
::userCanBitfield( $row->fa_deleted
, File
::DELETED_FILE
, $user );
195 $file['id'] = (int)$row->fa_id
;
196 $file['name'] = $row->fa_name
;
197 $title = Title
::makeTitle( NS_FILE
, $row->fa_name
);
198 self
::addTitleInfo( $file, $title );
200 if ( $fld_description &&
201 RevisionRecord
::userCanBitfield( $row->fa_deleted
, File
::DELETED_COMMENT
, $user )
203 if ( isset( $prop['parseddescription'] ) ) {
204 $file['parseddescription'] = $formattedDescriptions[$row->fa_id
];
205 $file['description'] = $descriptions[$row->fa_id
];
207 $file['description'] = $this->commentStore
->getComment( 'fa_description', $row )->text
;
211 RevisionRecord
::userCanBitfield( $row->fa_deleted
, File
::DELETED_USER
, $user )
213 $file['userid'] = (int)$row->fa_user
;
214 $file['user'] = $row->fa_user_text
;
217 $file['filemissing'] = true;
219 if ( $fld_sha1 && $canViewFile && $exists ) {
220 $file['sha1'] = \Wikimedia\base_convert
( $row->fa_sha1
, 36, 16, 40 );
222 if ( $fld_timestamp ) {
223 $file['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->fa_timestamp
);
225 if ( ( $fld_size ||
$fld_dimensions ) && $canViewFile && $exists ) {
226 $file['size'] = $row->fa_size
;
228 $pageCount = ArchivedFile
::newFromRow( $row )->pageCount();
229 if ( $pageCount !== false ) {
230 $file['pagecount'] = $pageCount;
233 $file['height'] = $row->fa_height
;
234 $file['width'] = $row->fa_width
;
236 if ( $fld_mediatype && $canViewFile && $exists ) {
237 $file['mediatype'] = $row->fa_media_type
;
239 if ( $fld_metadata && $canViewFile && $exists ) {
240 $metadataArray = ArchivedFile
::newFromRow( $row )->getMetadataArray();
241 $file['metadata'] = $row->fa_metadata
242 ? ApiQueryImageInfo
::processMetaData( $metadataArray, $result )
245 if ( $fld_bitdepth && $canViewFile && $exists ) {
246 $file['bitdepth'] = $row->fa_bits
;
248 if ( $fld_mime && $canViewFile && $exists ) {
249 $file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
251 if ( $fld_archivename && $row->fa_archive_name
!== null ) {
252 $file['archivename'] = $row->fa_archive_name
;
255 if ( $row->fa_deleted
& File
::DELETED_FILE
) {
256 $file['filehidden'] = true;
258 if ( $row->fa_deleted
& File
::DELETED_COMMENT
) {
259 $file['commenthidden'] = true;
261 if ( $row->fa_deleted
& File
::DELETED_USER
) {
262 $file['userhidden'] = true;
264 if ( $row->fa_deleted
& File
::DELETED_RESTRICTED
) {
265 // This file is deleted for normal admins
266 $file['suppressed'] = true;
269 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $file );
271 $this->setContinueEnumParameter(
272 'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
278 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'fa' );
281 public function getAllowedParams() {
287 ParamValidator
::PARAM_DEFAULT
=> 'ascending',
288 ParamValidator
::PARAM_TYPE
=> [
294 'sha1base36' => null,
296 ParamValidator
::PARAM_DEFAULT
=> 'timestamp',
297 ParamValidator
::PARAM_ISMULTI
=> true,
298 ParamValidator
::PARAM_TYPE
=> [
312 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
315 ParamValidator
::PARAM_DEFAULT
=> 10,
316 ParamValidator
::PARAM_TYPE
=> 'limit',
317 IntegerDef
::PARAM_MIN
=> 1,
318 IntegerDef
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
319 IntegerDef
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
322 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
327 protected function getExamplesMessages() {
329 'action=query&list=filearchive'
330 => 'apihelp-query+filearchive-example-simple',
334 public function getHelpUrls() {
335 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filearchive';
339 /** @deprecated class alias since 1.43 */
340 class_alias( ApiQueryFilearchive
::class, 'ApiQueryFilearchive' );