3 * File deletion user interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Rob Church <robchur@gmail.com>
24 use MediaWiki\MediaWikiServices
;
27 * File deletion user interface
31 class FileDeleteForm
{
36 private $title = null;
46 private $oldfile = null;
47 private $oldimage = '';
50 * @param File $file File object we're deleting
52 public function __construct( $file ) {
53 $this->title
= $file->getTitle();
58 * Fulfil the request; shows the form or deletes the file,
59 * pending authentication, confirmation, etc.
61 public function execute() {
62 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
64 $permissionErrors = $this->title
->getUserPermissionsErrors( 'delete', $wgUser );
65 if ( count( $permissionErrors ) ) {
66 throw new PermissionsError( 'delete', $permissionErrors );
70 throw new ReadOnlyError
;
73 if ( $wgUploadMaintenance ) {
74 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
79 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
80 $token = $wgRequest->getText( 'wpEditToken' );
81 # Flag to hide all contents of the archived revisions
82 $suppress = $wgRequest->getCheck( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
84 if ( $this->oldimage
) {
85 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName(
91 if ( !self
::haveDeletableFile( $this->file
, $this->oldfile
, $this->oldimage
) ) {
92 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
93 $wgOut->addReturnTo( $this->title
);
97 // Perform the deletion if appropriate
98 if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
99 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
100 $deleteReason = $wgRequest->getText( 'wpReason' );
102 if ( $deleteReasonList == 'other' ) {
103 $reason = $deleteReason;
104 } elseif ( $deleteReason != '' ) {
105 // Entry from drop down menu + additional comment
106 $reason = $deleteReasonList . wfMessage( 'colon-separator' )
107 ->inContentLanguage()->text() . $deleteReason;
109 $reason = $deleteReasonList;
112 $status = self
::doDelete(
121 if ( !$status->isGood() ) {
122 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
123 $wgOut->wrapWikiTextAsInterface(
125 $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' )
128 if ( $status->isOK() ) {
129 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
130 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
131 // Return to the main page if we just deleted all versions of the
132 // file, otherwise go back to the description page
133 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
135 WatchAction
::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title
, $wgUser );
141 $this->showLogEntries();
145 * Really delete the file
147 * @param Title &$title
149 * @param string &$oldimage Archive name
150 * @param string $reason Reason of the deletion
151 * @param bool $suppress Whether to mark all deleted versions as restricted
152 * @param User|null $user User object performing the request
153 * @param array $tags Tags to apply to the deletion action
154 * @throws MWException
157 public static function doDelete( &$title, &$file, &$oldimage, $reason,
158 $suppress, User
$user = null, $tags = []
160 if ( $user === null ) {
167 $status = $file->deleteOld( $oldimage, $reason, $suppress, $user );
169 // Need to do a log item
170 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
171 if ( trim( $reason ) != '' ) {
172 $logComment .= wfMessage( 'colon-separator' )
173 ->inContentLanguage()->text() . $reason;
176 $logtype = $suppress ?
'suppress' : 'delete';
178 $logEntry = new ManualLogEntry( $logtype, 'delete' );
179 $logEntry->setPerformer( $user );
180 $logEntry->setTarget( $title );
181 $logEntry->setComment( $logComment );
182 $logEntry->setTags( $tags );
183 $logid = $logEntry->insert();
184 $logEntry->publish( $logid );
186 $status->value
= $logid;
189 $status = Status
::newFatal( 'cannotdelete',
190 wfEscapeWikiText( $title->getPrefixedText() )
192 $page = WikiPage
::factory( $title );
193 $dbw = wfGetDB( DB_MASTER
);
194 $dbw->startAtomic( __METHOD__
);
195 // delete the associated article first
197 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error,
199 // doDeleteArticleReal() returns a non-fatal error status if the page
200 // or revision is missing, so check for isOK() rather than isGood()
201 if ( $deleteStatus->isOK() ) {
202 $status = $file->delete( $reason, $suppress, $user );
203 if ( $status->isOK() ) {
204 if ( $deleteStatus->value
=== null ) {
205 // No log ID from doDeleteArticleReal(), probably
206 // because the page/revision didn't exist, so create
208 $logtype = $suppress ?
'suppress' : 'delete';
209 $logEntry = new ManualLogEntry( $logtype, 'delete' );
210 $logEntry->setPerformer( $user );
211 $logEntry->setTarget( clone $title );
212 $logEntry->setComment( $reason );
213 $logEntry->setTags( $tags );
214 $logid = $logEntry->insert();
215 $dbw->onTransactionPreCommitOrIdle(
216 function () use ( $logEntry, $logid ) {
217 $logEntry->publish( $logid );
221 $status->value
= $logid;
223 $status->value
= $deleteStatus->value
; // log id
225 $dbw->endAtomic( __METHOD__
);
227 // Page deleted but file still there? rollback page delete
228 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
229 $lbFactory->rollbackMasterChanges( __METHOD__
);
232 $dbw->endAtomic( __METHOD__
);
236 if ( $status->isOK() ) {
237 Hooks
::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
244 * Show the confirmation form
246 private function showForm() {
247 global $wgOut, $wgUser, $wgRequest;
249 $wgOut->addModules( 'mediawiki.action.delete.file' );
251 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$wgUser->isWatched( $this->title
);
253 $wgOut->enableOOUI();
255 $options = Xml
::listDropDownOptions(
256 $wgOut->msg( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
257 [ 'other' => $wgOut->msg( 'filedelete-reason-otherlist' )->inContentLanguage()->text() ]
259 $options = Xml
::listDropDownOptionsOoui( $options );
261 $fields[] = new OOUI\
LabelWidget( [ 'label' => new OOUI\
HtmlSnippet(
262 $this->prepareMessage( 'filedelete-intro' ) ) ]
265 $fields[] = new OOUI\
FieldLayout(
266 new OOUI\
DropdownInputWidget( [
267 'name' => 'wpDeleteReasonList',
268 'inputId' => 'wpDeleteReasonList',
272 'options' => $options,
275 'label' => $wgOut->msg( 'filedelete-comment' )->text(),
280 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
281 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
282 // Unicode codepoints.
283 $fields[] = new OOUI\
FieldLayout(
284 new OOUI\
TextInputWidget( [
285 'name' => 'wpReason',
286 'inputId' => 'wpReason',
288 'maxLength' => CommentStore
::COMMENT_CHARACTER_LIMIT
,
290 'value' => $wgRequest->getText( 'wpReason' ),
294 'label' => $wgOut->msg( 'filedelete-otherreason' )->text(),
299 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
300 $fields[] = new OOUI\
FieldLayout(
301 new OOUI\
CheckboxInputWidget( [
302 'name' => 'wpSuppress',
303 'inputId' => 'wpSuppress',
308 'label' => $wgOut->msg( 'revdelete-suppress' )->text(),
315 if ( $wgUser->isLoggedIn() ) {
316 $fields[] = new OOUI\
FieldLayout(
317 new OOUI\
CheckboxInputWidget( [
319 'inputId' => 'wpWatch',
321 'selected' => $checkWatch,
324 'label' => $wgOut->msg( 'watchthis' )->text(),
331 $fields[] = new OOUI\
FieldLayout(
332 new OOUI\
ButtonInputWidget( [
333 'name' => 'mw-filedelete-submit',
334 'inputId' => 'mw-filedelete-submit',
336 'value' => $wgOut->msg( 'filedelete-submit' )->text(),
337 'label' => $wgOut->msg( 'filedelete-submit' )->text(),
338 'flags' => [ 'primary', 'destructive' ],
346 $fieldset = new OOUI\
FieldsetLayout( [
347 'label' => $wgOut->msg( 'filedelete-legend' )->text(),
351 $form = new OOUI\
FormLayout( [
353 'action' => $this->getAction(),
354 'id' => 'mw-img-deleteconfirm',
356 $form->appendContent(
358 new OOUI\
HtmlSnippet(
359 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) )
364 new OOUI\
PanelLayout( [
365 'classes' => [ 'deletepage-wrapper' ],
373 if ( $wgUser->isAllowed( 'editinterface' ) ) {
374 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
375 $link = $linkRenderer->makeKnownLink(
376 $wgOut->msg( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle(),
377 wfMessage( 'filedelete-edit-reasonlist' )->text(),
379 [ 'action' => 'edit' ]
381 $wgOut->addHTML( '<p class="mw-filedelete-editreasons">' . $link . '</p>' );
386 * Show deletion log fragments pertaining to the current file
388 private function showLogEntries() {
390 $deleteLogPage = new LogPage( 'delete' );
391 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
392 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
396 * Prepare a message referring to the file being deleted,
397 * showing an appropriate message depending upon whether
398 * it's a current file or an old version
400 * @param string $message Message base
403 private function prepareMessage( $message ) {
405 if ( $this->oldimage
) {
407 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
410 wfEscapeWikiText( $this->title
->getText() ),
411 $wgLang->date( $this->getTimestamp(), true ),
412 $wgLang->time( $this->getTimestamp(), true ),
413 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) )->parseAsBlock();
417 wfEscapeWikiText( $this->title
->getText() )
423 * Set headers, titles and other bits
425 private function setHeaders() {
427 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
428 $wgOut->setRobotPolicy( 'noindex,nofollow' );
429 $wgOut->addBacklinkSubtitle( $this->title
);
433 * Is the provided `oldimage` value valid?
435 * @param string $oldimage
438 public static function isValidOldSpec( $oldimage ) {
439 return strlen( $oldimage ) >= 16
440 && strpos( $oldimage, '/' ) === false
441 && strpos( $oldimage, '\\' ) === false;
445 * Could we delete the file specified? If an `oldimage`
446 * value was provided, does it correspond to an
447 * existing, local, old version of this file?
450 * @param File &$oldfile
451 * @param File $oldimage
454 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
456 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
457 : $file && $file->exists() && $file->isLocal();
461 * Prepare the form action
465 private function getAction() {
467 $q['action'] = 'delete';
469 if ( $this->oldimage
) {
470 $q['oldimage'] = $this->oldimage
;
473 return $this->title
->getLocalURL( $q );
477 * Extract the timestamp of the old version
481 private function getTimestamp() {
482 return $this->oldfile
->getTimestamp();