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>
26 * File deletion user interface
30 class FileDeleteForm
{
35 private $title = null;
45 private $oldfile = null;
46 private $oldimage = '';
51 * @param File $file File object we're deleting
53 public function __construct( $file ) {
54 $this->title
= $file->getTitle();
59 * Fulfil the request; shows the form or deletes the file,
60 * pending authentication, confirmation, etc.
62 public function execute() {
63 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
65 $permissionErrors = $this->title
->getUserPermissionsErrors( 'delete', $wgUser );
66 if ( count( $permissionErrors ) ) {
67 throw new PermissionsError( 'delete', $permissionErrors );
71 throw new ReadOnlyError
;
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
80 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
81 $token = $wgRequest->getText( 'wpEditToken' );
82 # Flag to hide all contents of the archived revisions
83 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
85 if ( $this->oldimage
) {
86 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName(
92 if ( !self
::haveDeletableFile( $this->file
, $this->oldfile
, $this->oldimage
) ) {
93 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
94 $wgOut->addReturnTo( $this->title
);
98 // Perform the deletion if appropriate
99 if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
100 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
101 $deleteReason = $wgRequest->getText( 'wpReason' );
103 if ( $deleteReasonList == 'other' ) {
104 $reason = $deleteReason;
105 } elseif ( $deleteReason != '' ) {
106 // Entry from drop down menu + additional comment
107 $reason = $deleteReasonList . wfMessage( 'colon-separator' )
108 ->inContentLanguage()->text() . $deleteReason;
110 $reason = $deleteReasonList;
113 $status = self
::doDelete(
122 if ( !$status->isGood() ) {
123 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
124 $wgOut->addWikiText( '<div class="error">' .
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 $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 $status->value
= $deleteStatus->value
; // log id
205 $dbw->endAtomic( __METHOD__
);
207 // Page deleted but file still there? rollback page delete
208 wfGetLBFactory()->rollbackMasterChanges( __METHOD__
);
211 // Done; nothing changed
212 $dbw->endAtomic( __METHOD__
);
216 if ( $status->isOK() ) {
217 Hooks
::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
224 * Show the confirmation form
226 private function showForm() {
227 global $wgOut, $wgUser, $wgRequest;
229 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
230 $suppress = "<tr id=\"wpDeleteSuppressRow\">
232 <td class='mw-input'><strong>" .
233 Xml
::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
234 'wpSuppress', 'wpSuppress', false, [ 'tabindex' => '3' ] ) .
241 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$wgUser->isWatched( $this->title
);
242 $form = Xml
::openElement( 'form', [ 'method' => 'post', 'action' => $this->getAction(),
243 'id' => 'mw-img-deleteconfirm' ] ) .
244 Xml
::openElement( 'fieldset' ) .
245 Xml
::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
246 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
247 $this->prepareMessage( 'filedelete-intro' ) .
248 Xml
::openElement( 'table', [ 'id' => 'mw-img-deleteconfirm-table' ] ) .
250 <td class='mw-label'>" .
251 Xml
::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
253 <td class='mw-input'>" .
255 'wpDeleteReasonList',
256 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
257 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
265 <td class='mw-label'>" .
266 Xml
::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
268 <td class='mw-input'>" .
269 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
270 [ 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ] ) .
274 if ( $wgUser->isLoggedIn() ) {
278 <td class='mw-input'>" .
279 Xml
::checkLabel( wfMessage( 'watchthis' )->text(),
280 'wpWatch', 'wpWatch', $checkWatch, [ 'tabindex' => '3' ] ) .
287 <td class='mw-submit'>" .
289 wfMessage( 'filedelete-submit' )->text(),
291 'name' => 'mw-filedelete-submit',
292 'id' => 'mw-filedelete-submit',
298 Xml
::closeElement( 'table' ) .
299 Xml
::closeElement( 'fieldset' ) .
300 Xml
::closeElement( 'form' );
302 if ( $wgUser->isAllowed( 'editinterface' ) ) {
303 $title = wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle();
304 $link = Linker
::linkKnown(
306 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
308 [ 'action' => 'edit' ]
310 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
313 $wgOut->addHTML( $form );
317 * Show deletion log fragments pertaining to the current file
319 private function showLogEntries() {
321 $deleteLogPage = new LogPage( 'delete' );
322 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
323 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
327 * Prepare a message referring to the file being deleted,
328 * showing an appropriate message depending upon whether
329 * it's a current file or an old version
331 * @param string $message Message base
334 private function prepareMessage( $message ) {
336 if ( $this->oldimage
) {
338 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
341 wfEscapeWikiText( $this->title
->getText() ),
342 $wgLang->date( $this->getTimestamp(), true ),
343 $wgLang->time( $this->getTimestamp(), true ),
344 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) )->parseAsBlock();
348 wfEscapeWikiText( $this->title
->getText() )
354 * Set headers, titles and other bits
356 private function setHeaders() {
358 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
359 $wgOut->setRobotPolicy( 'noindex,nofollow' );
360 $wgOut->addBacklinkSubtitle( $this->title
);
364 * Is the provided `oldimage` value valid?
366 * @param string $oldimage
369 public static function isValidOldSpec( $oldimage ) {
370 return strlen( $oldimage ) >= 16
371 && strpos( $oldimage, '/' ) === false
372 && strpos( $oldimage, '\\' ) === false;
376 * Could we delete the file specified? If an `oldimage`
377 * value was provided, does it correspond to an
378 * existing, local, old version of this file?
381 * @param File $oldfile
382 * @param File $oldimage
385 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
387 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
388 : $file && $file->exists() && $file->isLocal();
392 * Prepare the form action
396 private function getAction() {
398 $q['action'] = 'delete';
400 if ( $this->oldimage
) {
401 $q['oldimage'] = $this->oldimage
;
404 return $this->title
->getLocalURL( $q );
408 * Extract the timestamp of the old version
412 private function getTimestamp() {
413 return $this->oldfile
->getTimestamp();