Merge "Special:Upload should not crash on failing previews"
[mediawiki.git] / includes / FileDeleteForm.php
blobf850152050c0fa271eba198a0f1f1bf21554085e
1 <?php
2 /**
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
20 * @file
21 * @author Rob Church <robchur@gmail.com>
22 * @ingroup Media
25 /**
26 * File deletion user interface
28 * @ingroup Media
30 class FileDeleteForm {
32 /**
33 * @var Title
35 private $title = null;
37 /**
38 * @var File
40 private $file = null;
42 /**
43 * @var File
45 private $oldfile = null;
46 private $oldimage = '';
48 /**
49 * Constructor
51 * @param File $file File object we're deleting
53 public function __construct( $file ) {
54 $this->title = $file->getTitle();
55 $this->file = $file;
58 /**
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 );
70 if ( wfReadOnly() ) {
71 throw new ReadOnlyError;
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
78 $this->setHeaders();
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(
87 $this->title,
88 $this->oldimage
92 if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
93 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
94 $wgOut->addReturnTo( $this->title );
95 return;
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;
109 } else {
110 $reason = $deleteReasonList;
113 $status = self::doDelete(
114 $this->title,
115 $this->file,
116 $this->oldimage,
117 $reason,
118 $suppress,
119 $wgUser
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' )
126 . '</div>' );
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 );
137 return;
140 $this->showForm();
141 $this->showLogEntries();
145 * Really delete the file
147 * @param Title $title
148 * @param File $file
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
155 * @return Status
157 public static function doDelete( &$title, &$file, &$oldimage, $reason,
158 $suppress, User $user = null, $tags = []
160 if ( $user === null ) {
161 global $wgUser;
162 $user = $wgUser;
165 if ( $oldimage ) {
166 $page = null;
167 $status = $file->deleteOld( $oldimage, $reason, $suppress, $user );
168 if ( $status->ok ) {
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;
188 } else {
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
196 $error = '';
197 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error,
198 $user, $tags );
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__ );
206 } else {
207 // Page deleted but file still there? rollback page delete
208 wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
210 } else {
211 // Done; nothing changed
212 $dbw->endAtomic( __METHOD__ );
216 if ( $status->isOK() ) {
217 Hooks::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
220 return $status;
224 * Show the confirmation form
226 private function showForm() {
227 global $wgOut, $wgUser, $wgRequest;
229 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
230 $suppress = "<tr id=\"wpDeleteSuppressRow\">
231 <td></td>
232 <td class='mw-input'><strong>" .
233 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
234 'wpSuppress', 'wpSuppress', false, [ 'tabindex' => '3' ] ) .
235 "</strong></td>
236 </tr>";
237 } else {
238 $suppress = '';
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' ] ) .
249 "<tr>
250 <td class='mw-label'>" .
251 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
252 "</td>
253 <td class='mw-input'>" .
254 Xml::listDropDown(
255 'wpDeleteReasonList',
256 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
257 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
259 'wpReasonDropDown',
262 "</td>
263 </tr>
264 <tr>
265 <td class='mw-label'>" .
266 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
267 "</td>
268 <td class='mw-input'>" .
269 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
270 [ 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ] ) .
271 "</td>
272 </tr>
273 {$suppress}";
274 if ( $wgUser->isLoggedIn() ) {
275 $form .= "
276 <tr>
277 <td></td>
278 <td class='mw-input'>" .
279 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
280 'wpWatch', 'wpWatch', $checkWatch, [ 'tabindex' => '3' ] ) .
281 "</td>
282 </tr>";
284 $form .= "
285 <tr>
286 <td></td>
287 <td class='mw-submit'>" .
288 Xml::submitButton(
289 wfMessage( 'filedelete-submit' )->text(),
291 'name' => 'mw-filedelete-submit',
292 'id' => 'mw-filedelete-submit',
293 'tabindex' => '4'
296 "</td>
297 </tr>" .
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(
305 $title,
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() {
320 global $wgOut;
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
332 * @return string
334 private function prepareMessage( $message ) {
335 global $wgLang;
336 if ( $this->oldimage ) {
337 # Message keys used:
338 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
339 return wfMessage(
340 "{$message}-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();
345 } else {
346 return wfMessage(
347 $message,
348 wfEscapeWikiText( $this->title->getText() )
349 )->parseAsBlock();
354 * Set headers, titles and other bits
356 private function setHeaders() {
357 global $wgOut;
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
367 * @return bool
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?
380 * @param File $file
381 * @param File $oldfile
382 * @param File $oldimage
383 * @return bool
385 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
386 return $oldimage
387 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
388 : $file && $file->exists() && $file->isLocal();
392 * Prepare the form action
394 * @return string
396 private function getAction() {
397 $q = [];
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
410 * @return string
412 private function getTimestamp() {
413 return $this->oldfile->getTimestamp();