ApiQueryFilearchive::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 [email protected],
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
/**
30
 * Query module to enumerate all deleted files.
31
 *
32
 * @ingroup API
33
 */
34
class ApiQueryFilearchive extends ApiQueryBase {
35
36
	public function __construct( ApiQuery $query, $moduleName ) {
37
		parent::__construct( $query, $moduleName, 'fa' );
38
	}
39
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'
47
			);
48
		}
49
50
		$db = $this->getDB();
51
52
		$params = $this->extractRequestParams();
53
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'] );
66
67
		$this->addTables( 'filearchive' );
68
69
		$this->addFields( ArchivedFile::selectFields() );
70
		$this->addFields( [ 'fa_id', 'fa_name', 'fa_timestamp', 'fa_deleted' ] );
71
		$this->addFieldsIf( 'fa_sha1', $fld_sha1 );
72
		$this->addFieldsIf( [ 'fa_user', 'fa_user_text' ], $fld_user );
73
		$this->addFieldsIf( [ 'fa_height', 'fa_width', 'fa_size' ], $fld_dimensions || $fld_size );
74
		$this->addFieldsIf( 'fa_description', $fld_description );
75
		$this->addFieldsIf( [ '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 );
80
81 View Code Duplication
		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 )))"
94
			);
95
		}
96
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 View Code Duplication
		if ( isset( $params['prefix'] ) ) {
103
			$this->addWhere( 'fa_name' . $db->buildLike(
104
				$this->titlePartToKey( $params['prefix'], NS_FILE ),
105
				$db->anyString() ) );
106
		}
107
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' );
116
				}
117
				$sha1 = Wikimedia\base_convert( $sha1, 16, 36, 31 );
118 View Code Duplication
			} elseif ( $sha1base36Set ) {
119
				$sha1 = strtolower( $params['sha1base36'] );
120
				if ( !$this->validateSha1Base36Hash( $sha1 ) ) {
121
					$this->dieUsage( 'The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash' );
122
				}
123
			}
124
			if ( $sha1 ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sha1 of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
125
				$this->addWhereFld( 'fa_sha1', $sha1 );
126
			}
127
		}
128
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;
136
		}
137
		if ( $bitmask ) {
138
			$this->addWhere( $this->getDB()->bitAnd( 'fa_deleted', $bitmask ) . " != $bitmask" );
139
		}
140
141
		$limit = $params['limit'];
142
		$this->addOption( 'LIMIT', $limit + 1 );
143
		$sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
144
		$this->addOption( 'ORDER BY', [
145
			'fa_name' . $sort,
146
			'fa_timestamp' . $sort,
147
			'fa_id' . $sort,
148
		] );
149
150
		$res = $this->select( __METHOD__ );
151
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"
160
				);
161
				break;
162
			}
163
164
			$file = [];
165
			$file['id'] = (int)$row->fa_id;
166
			$file['name'] = $row->fa_name;
167
			$title = Title::makeTitle( NS_FILE, $row->fa_name );
168
			self::addTitleInfo( $file, $title );
169
170
			if ( $fld_description &&
171
				Revision::userCanBitfield( $row->fa_deleted, File::DELETED_COMMENT, $user )
172
			) {
173
				$file['description'] = $row->fa_description;
174
				if ( isset( $prop['parseddescription'] ) ) {
175
					$file['parseddescription'] = Linker::formatComment(
176
						$row->fa_description, $title );
177
				}
178
			}
179
			if ( $fld_user &&
180
				Revision::userCanBitfield( $row->fa_deleted, File::DELETED_USER, $user )
181
			) {
182
				$file['userid'] = (int)$row->fa_user;
183
				$file['user'] = $row->fa_user_text;
184
			}
185
			if ( $fld_sha1 ) {
186
				$file['sha1'] = Wikimedia\base_convert( $row->fa_sha1, 36, 16, 40 );
187
			}
188
			if ( $fld_timestamp ) {
189
				$file['timestamp'] = wfTimestamp( TS_ISO_8601, $row->fa_timestamp );
190
			}
191
			if ( $fld_size || $fld_dimensions ) {
192
				$file['size'] = $row->fa_size;
193
194
				$pageCount = ArchivedFile::newFromRow( $row )->pageCount();
195
				if ( $pageCount !== false ) {
196
					$file['pagecount'] = $pageCount;
197
				}
198
199
				$file['height'] = $row->fa_height;
200
				$file['width'] = $row->fa_width;
201
			}
202
			if ( $fld_mediatype ) {
203
				$file['mediatype'] = $row->fa_media_type;
204
			}
205
			if ( $fld_metadata ) {
206
				$file['metadata'] = $row->fa_metadata
207
					? ApiQueryImageInfo::processMetaData( unserialize( $row->fa_metadata ), $result )
208
					: null;
209
			}
210
			if ( $fld_bitdepth ) {
211
				$file['bitdepth'] = $row->fa_bits;
212
			}
213
			if ( $fld_mime ) {
214
				$file['mime'] = "$row->fa_major_mime/$row->fa_minor_mime";
215
			}
216
			if ( $fld_archivename && !is_null( $row->fa_archive_name ) ) {
217
				$file['archivename'] = $row->fa_archive_name;
218
			}
219
220
			if ( $row->fa_deleted & File::DELETED_FILE ) {
221
				$file['filehidden'] = true;
222
			}
223
			if ( $row->fa_deleted & File::DELETED_COMMENT ) {
224
				$file['commenthidden'] = true;
225
			}
226
			if ( $row->fa_deleted & File::DELETED_USER ) {
227
				$file['userhidden'] = true;
228
			}
229
			if ( $row->fa_deleted & File::DELETED_RESTRICTED ) {
230
				// This file is deleted for normal admins
231
				$file['suppressed'] = true;
232
			}
233
234
			$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $file );
235
			if ( !$fit ) {
236
				$this->setContinueEnumParameter(
237
					'continue', "$row->fa_name|$row->fa_timestamp|$row->fa_id"
238
				);
239
				break;
240
			}
241
		}
242
243
		$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'fa' );
244
	}
245
246
	public function getAllowedParams() {
247
		return [
248
			'from' => null,
249
			'to' => null,
250
			'prefix' => null,
251
			'dir' => [
252
				ApiBase::PARAM_DFLT => 'ascending',
253
				ApiBase::PARAM_TYPE => [
254
					'ascending',
255
					'descending'
256
				]
257
			],
258
			'sha1' => null,
259
			'sha1base36' => null,
260
			'prop' => [
261
				ApiBase::PARAM_DFLT => 'timestamp',
262
				ApiBase::PARAM_ISMULTI => true,
263
				ApiBase::PARAM_TYPE => [
264
					'sha1',
265
					'timestamp',
266
					'user',
267
					'size',
268
					'dimensions',
269
					'description',
270
					'parseddescription',
271
					'mime',
272
					'mediatype',
273
					'metadata',
274
					'bitdepth',
275
					'archivename',
276
				],
277
				ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
278
			],
279
			'limit' => [
280
				ApiBase::PARAM_DFLT => 10,
281
				ApiBase::PARAM_TYPE => 'limit',
282
				ApiBase::PARAM_MIN => 1,
283
				ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
284
				ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
285
			],
286
			'continue' => [
287
				ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
288
			],
289
		];
290
	}
291
292
	protected function getExamplesMessages() {
293
		return [
294
			'action=query&list=filearchive'
295
				=> 'apihelp-query+filearchive-example-simple',
296
		];
297
	}
298
299
	public function getHelpUrls() {
300
		return 'https://www.mediawiki.org/wiki/API:Filearchive';
301
	}
302
}
303