Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / FileDeleteForm.php
blob65d82b8782c1235fca19569f90a6fbbd3b5a2b69
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 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( $this->title, $this->oldimage );
89 if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
90 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
91 $wgOut->addReturnTo( $this->title );
92 return;
95 // Perform the deletion if appropriate
96 if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
97 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
98 $deleteReason = $wgRequest->getText( 'wpReason' );
100 if ( $deleteReasonList == 'other' ) {
101 $reason = $deleteReason;
102 } elseif ( $deleteReason != '' ) {
103 // Entry from drop down menu + additional comment
104 $reason = $deleteReasonList . wfMessage( 'colon-separator' )
105 ->inContentLanguage()->text() . $deleteReason;
106 } else {
107 $reason = $deleteReasonList;
110 $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress, $wgUser );
112 if ( !$status->isGood() ) {
113 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
114 $wgOut->addWikiText( '<div class="error">' . $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) . '</div>' );
116 if ( $status->ok ) {
117 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
118 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
119 // Return to the main page if we just deleted all versions of the
120 // file, otherwise go back to the description page
121 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
123 WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title, $wgUser );
125 return;
128 $this->showForm();
129 $this->showLogEntries();
133 * Really delete the file
135 * @param $title Title object
136 * @param File $file: file object
137 * @param string $oldimage archive name
138 * @param string $reason reason of the deletion
139 * @param $suppress Boolean: whether to mark all deleted versions as restricted
140 * @param $user User object performing the request
141 * @throws MWException
142 * @return bool|Status
144 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
145 if ( $user === null ) {
146 global $wgUser;
147 $user = $wgUser;
150 if ( $oldimage ) {
151 $page = null;
152 $status = $file->deleteOld( $oldimage, $reason, $suppress );
153 if ( $status->ok ) {
154 // Need to do a log item
155 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
156 if ( trim( $reason ) != '' ) {
157 $logComment .= wfMessage( 'colon-separator' )
158 ->inContentLanguage()->text() . $reason;
161 $logtype = $suppress ? 'suppress' : 'delete';
163 $logEntry = new ManualLogEntry( $logtype, 'delete' );
164 $logEntry->setPerformer( $user );
165 $logEntry->setTarget( $title );
166 $logEntry->setComment( $logComment );
167 $logid = $logEntry->insert();
168 $logEntry->publish( $logid );
170 } else {
171 $status = Status::newFatal( 'cannotdelete',
172 wfEscapeWikiText( $title->getPrefixedText() )
174 $page = WikiPage::factory( $title );
175 $dbw = wfGetDB( DB_MASTER );
176 try {
177 // delete the associated article first
178 $error = '';
179 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
180 // doDeleteArticleReal() returns a non-fatal error status if the page
181 // or revision is missing, so check for isOK() rather than isGood()
182 if ( $deleteStatus->isOK() ) {
183 $status = $file->delete( $reason, $suppress );
184 if ( $status->isOK() ) {
185 $dbw->commit( __METHOD__ );
186 } else {
187 $dbw->rollback( __METHOD__ );
190 } catch ( MWException $e ) {
191 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
192 $dbw->rollback( __METHOD__ );
193 throw $e;
197 if ( $status->isOK() ) {
198 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
201 return $status;
205 * Show the confirmation form
207 private function showForm() {
208 global $wgOut, $wgUser, $wgRequest;
210 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
211 $suppress = "<tr id=\"wpDeleteSuppressRow\">
212 <td></td>
213 <td class='mw-input'><strong>" .
214 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
215 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
216 "</strong></td>
217 </tr>";
218 } else {
219 $suppress = '';
222 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
223 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
224 'id' => 'mw-img-deleteconfirm' ) ) .
225 Xml::openElement( 'fieldset' ) .
226 Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
227 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
228 $this->prepareMessage( 'filedelete-intro' ) .
229 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
230 "<tr>
231 <td class='mw-label'>" .
232 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
233 "</td>
234 <td class='mw-input'>" .
235 Xml::listDropDown(
236 'wpDeleteReasonList',
237 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
238 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
240 'wpReasonDropDown',
243 "</td>
244 </tr>
245 <tr>
246 <td class='mw-label'>" .
247 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
248 "</td>
249 <td class='mw-input'>" .
250 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
251 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
252 "</td>
253 </tr>
254 {$suppress}";
255 if ( $wgUser->isLoggedIn() ) {
256 $form .= "
257 <tr>
258 <td></td>
259 <td class='mw-input'>" .
260 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
261 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
262 "</td>
263 </tr>";
265 $form .= "
266 <tr>
267 <td></td>
268 <td class='mw-submit'>" .
269 Xml::submitButton( wfMessage( 'filedelete-submit' )->text(),
270 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
271 "</td>
272 </tr>" .
273 Xml::closeElement( 'table' ) .
274 Xml::closeElement( 'fieldset' ) .
275 Xml::closeElement( 'form' );
277 if ( $wgUser->isAllowed( 'editinterface' ) ) {
278 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
279 $link = Linker::link(
280 $title,
281 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
282 array(),
283 array( 'action' => 'edit' )
285 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
288 $wgOut->addHTML( $form );
292 * Show deletion log fragments pertaining to the current file
294 private function showLogEntries() {
295 global $wgOut;
296 $deleteLogPage = new LogPage( 'delete' );
297 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
298 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
302 * Prepare a message referring to the file being deleted,
303 * showing an appropriate message depending upon whether
304 * it's a current file or an old version
306 * @param string $message message base
307 * @return String
309 private function prepareMessage( $message ) {
310 global $wgLang;
311 if ( $this->oldimage ) {
312 return wfMessage(
313 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
314 wfEscapeWikiText( $this->title->getText() ),
315 $wgLang->date( $this->getTimestamp(), true ),
316 $wgLang->time( $this->getTimestamp(), true ),
317 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
318 } else {
319 return wfMessage(
320 $message,
321 wfEscapeWikiText( $this->title->getText() )
322 )->parseAsBlock();
327 * Set headers, titles and other bits
329 private function setHeaders() {
330 global $wgOut;
331 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
332 $wgOut->setRobotPolicy( 'noindex,nofollow' );
333 $wgOut->addBacklinkSubtitle( $this->title );
337 * Is the provided `oldimage` value valid?
339 * @return bool
341 public static function isValidOldSpec( $oldimage ) {
342 return strlen( $oldimage ) >= 16
343 && strpos( $oldimage, '/' ) === false
344 && strpos( $oldimage, '\\' ) === false;
348 * Could we delete the file specified? If an `oldimage`
349 * value was provided, does it correspond to an
350 * existing, local, old version of this file?
352 * @param $file File
353 * @param $oldfile File
354 * @param $oldimage File
355 * @return bool
357 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
358 return $oldimage
359 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
360 : $file && $file->exists() && $file->isLocal();
364 * Prepare the form action
366 * @return string
368 private function getAction() {
369 $q = array();
370 $q['action'] = 'delete';
372 if ( $this->oldimage ) {
373 $q['oldimage'] = $this->oldimage;
376 return $this->title->getLocalURL( $q );
380 * Extract the timestamp of the old version
382 * @return string
384 private function getTimestamp() {
385 return $this->oldfile->getTimestamp();