revert r113117 per CR
[mediawiki.git] / includes / FileDeleteForm.php
blob089acd03f31ac4881bb3b12957823a71ce62b8df
1 <?php
3 /**
4 * File deletion user interface
6 * @ingroup Media
7 * @author Rob Church <robchur@gmail.com>
8 */
9 class FileDeleteForm {
11 /**
12 * @var Title
14 private $title = null;
16 /**
17 * @var File
19 private $file = null;
21 /**
22 * @var File
24 private $oldfile = null;
25 private $oldimage = '';
27 /**
28 * Constructor
30 * @param $file File object we're deleting
32 public function __construct( $file ) {
33 $this->title = $file->getTitle();
34 $this->file = $file;
37 /**
38 * Fulfil the request; shows the form or deletes the file,
39 * pending authentication, confirmation, etc.
41 public function execute() {
42 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
44 $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
45 if ( count( $permissionErrors ) ) {
46 throw new PermissionsError( 'delete', $permissionErrors );
49 if ( wfReadOnly() ) {
50 throw new ReadOnlyError;
53 if ( $wgUploadMaintenance ) {
54 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
57 $this->setHeaders();
59 $this->oldimage = $wgRequest->getText( 'oldimage', false );
60 $token = $wgRequest->getText( 'wpEditToken' );
61 # Flag to hide all contents of the archived revisions
62 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
64 if( $this->oldimage ) {
65 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
68 if( !self::haveDeletableFile($this->file, $this->oldfile, $this->oldimage) ) {
69 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
70 $wgOut->addReturnTo( $this->title );
71 return;
74 // Perform the deletion if appropriate
75 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
76 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
77 $deleteReason = $wgRequest->getText( 'wpReason' );
79 if ( $deleteReasonList == 'other' ) {
80 $reason = $deleteReason;
81 } elseif ( $deleteReason != '' ) {
82 // Entry from drop down menu + additional comment
83 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
84 } else {
85 $reason = $deleteReasonList;
88 $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress, $wgUser );
90 if( !$status->isGood() ) {
91 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
92 $wgOut->addHTML( '<span class="error">' );
93 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
94 $wgOut->addHTML( '</span>' );
96 if( $status->ok ) {
97 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
98 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
99 // Return to the main page if we just deleted all versions of the
100 // file, otherwise go back to the description page
101 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
103 if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
104 WatchAction::doWatch( $this->title, $wgUser );
105 } elseif ( $this->title->userIsWatching() ) {
106 WatchAction::doUnwatch( $this->title, $wgUser );
109 return;
112 $this->showForm();
113 $this->showLogEntries();
117 * Really delete the file
119 * @param $title Title object
120 * @param File $file: file object
121 * @param $oldimage String: archive name
122 * @param $reason String: reason of the deletion
123 * @param $suppress Boolean: whether to mark all deleted versions as restricted
124 * @param $user User object performing the request
125 * @return bool|Status
127 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
128 if ( $user === null ) {
129 global $wgUser;
130 $user = $wgUser;
133 if( $oldimage ) {
134 $page = null;
135 $status = $file->deleteOld( $oldimage, $reason, $suppress );
136 if( $status->ok ) {
137 // Need to do a log item
138 $log = new LogPage( 'delete' );
139 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
140 if( trim( $reason ) != '' ) {
141 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
143 $log->addEntry( 'delete', $title, $logComment );
145 } else {
146 $status = Status::newFatal( 'cannotdelete',
147 wfEscapeWikiText( $title->getPrefixedText() )
149 $page = WikiPage::factory( $title );
150 $dbw = wfGetDB( DB_MASTER );
151 try {
152 // delete the associated article first
153 $error = '';
154 if ( $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user ) >= WikiPage::DELETE_SUCCESS ) {
155 $status = $file->delete( $reason, $suppress );
156 if( $status->isOK() ) {
157 $dbw->commit( __METHOD__ );
158 } else {
159 $dbw->rollback( __METHOD__ );
162 } catch ( MWException $e ) {
163 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
164 $dbw->rollback( __METHOD__ );
165 throw $e;
169 if ( $status->isOK() ) {
170 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
173 return $status;
177 * Show the confirmation form
179 private function showForm() {
180 global $wgOut, $wgUser, $wgRequest;
182 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
183 $suppress = "<tr id=\"wpDeleteSuppressRow\">
184 <td></td>
185 <td class='mw-input'><strong>" .
186 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
187 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
188 "</strong></td>
189 </tr>";
190 } else {
191 $suppress = '';
194 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
195 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
196 'id' => 'mw-img-deleteconfirm' ) ) .
197 Xml::openElement( 'fieldset' ) .
198 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
199 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
200 $this->prepareMessage( 'filedelete-intro' ) .
201 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
202 "<tr>
203 <td class='mw-label'>" .
204 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
205 "</td>
206 <td class='mw-input'>" .
207 Xml::listDropDown( 'wpDeleteReasonList',
208 wfMsgForContent( 'filedelete-reason-dropdown' ),
209 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
210 "</td>
211 </tr>
212 <tr>
213 <td class='mw-label'>" .
214 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
215 "</td>
216 <td class='mw-input'>" .
217 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
218 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
219 "</td>
220 </tr>
221 {$suppress}";
222 if( $wgUser->isLoggedIn() ) {
223 $form .= "
224 <tr>
225 <td></td>
226 <td class='mw-input'>" .
227 Xml::checkLabel( wfMsg( 'watchthis' ),
228 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
229 "</td>
230 </tr>";
232 $form .= "
233 <tr>
234 <td></td>
235 <td class='mw-submit'>" .
236 Xml::submitButton( wfMsg( 'filedelete-submit' ),
237 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
238 "</td>
239 </tr>" .
240 Xml::closeElement( 'table' ) .
241 Xml::closeElement( 'fieldset' ) .
242 Xml::closeElement( 'form' );
244 if ( $wgUser->isAllowed( 'editinterface' ) ) {
245 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
246 $link = Linker::link(
247 $title,
248 wfMsgHtml( 'filedelete-edit-reasonlist' ),
249 array(),
250 array( 'action' => 'edit' )
252 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
255 $wgOut->addHTML( $form );
259 * Show deletion log fragments pertaining to the current file
261 private function showLogEntries() {
262 global $wgOut;
263 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
264 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
268 * Prepare a message referring to the file being deleted,
269 * showing an appropriate message depending upon whether
270 * it's a current file or an old version
272 * @param $message String: message base
273 * @return String
275 private function prepareMessage( $message ) {
276 global $wgLang;
277 if( $this->oldimage ) {
278 return wfMsgExt(
279 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
280 'parse',
281 wfEscapeWikiText( $this->title->getText() ),
282 $wgLang->date( $this->getTimestamp(), true ),
283 $wgLang->time( $this->getTimestamp(), true ),
284 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
285 } else {
286 return wfMsgExt(
287 $message,
288 'parse',
289 wfEscapeWikiText( $this->title->getText() )
295 * Set headers, titles and other bits
297 private function setHeaders() {
298 global $wgOut;
299 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
300 $wgOut->setRobotPolicy( 'noindex,nofollow' );
301 $wgOut->addBacklinkSubtitle( $this->title );
305 * Is the provided `oldimage` value valid?
307 * @return bool
309 public static function isValidOldSpec($oldimage) {
310 return strlen( $oldimage ) >= 16
311 && strpos( $oldimage, '/' ) === false
312 && strpos( $oldimage, '\\' ) === false;
316 * Could we delete the file specified? If an `oldimage`
317 * value was provided, does it correspond to an
318 * existing, local, old version of this file?
320 * @param $file File
321 * @param $oldfile File
322 * @param $oldimage File
323 * @return bool
325 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
326 return $oldimage
327 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
328 : $file && $file->exists() && $file->isLocal();
332 * Prepare the form action
334 * @return string
336 private function getAction() {
337 $q = array();
338 $q['action'] = 'delete';
340 if( $this->oldimage )
341 $q['oldimage'] = $this->oldimage;
343 return $this->title->getLocalUrl( $q );
347 * Extract the timestamp of the old version
349 * @return string
351 private function getTimestamp() {
352 return $this->oldfile->getTimestamp();