(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / FileDeleteForm.php
blobd86c8d82f36ab126242256c3848767e879ef2b86
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->addWikiText( '<div class="error">' . $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) . '</div>' );
94 if( $status->ok ) {
95 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
96 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
97 // Return to the main page if we just deleted all versions of the
98 // file, otherwise go back to the description page
99 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
101 if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
102 WatchAction::doWatch( $this->title, $wgUser );
103 } elseif ( $this->title->userIsWatching() ) {
104 WatchAction::doUnwatch( $this->title, $wgUser );
107 return;
110 $this->showForm();
111 $this->showLogEntries();
115 * Really delete the file
117 * @param $title Title object
118 * @param File $file: file object
119 * @param $oldimage String: archive name
120 * @param $reason String: reason of the deletion
121 * @param $suppress Boolean: whether to mark all deleted versions as restricted
122 * @param $user User object performing the request
123 * @return bool|Status
125 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
126 if ( $user === null ) {
127 global $wgUser;
128 $user = $wgUser;
131 if( $oldimage ) {
132 $page = null;
133 $status = $file->deleteOld( $oldimage, $reason, $suppress );
134 if( $status->ok ) {
135 // Need to do a log item
136 $log = new LogPage( 'delete' );
137 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
138 if( trim( $reason ) != '' ) {
139 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
141 $log->addEntry( 'delete', $title, $logComment );
143 } else {
144 $status = Status::newFatal( 'cannotdelete',
145 wfEscapeWikiText( $title->getPrefixedText() )
147 $page = WikiPage::factory( $title );
148 $dbw = wfGetDB( DB_MASTER );
149 try {
150 // delete the associated article first
151 $error = '';
152 if ( $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user ) >= WikiPage::DELETE_SUCCESS ) {
153 $status = $file->delete( $reason, $suppress );
154 if( $status->isOK() ) {
155 $dbw->commit( __METHOD__ );
156 } else {
157 $dbw->rollback( __METHOD__ );
160 } catch ( MWException $e ) {
161 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
162 $dbw->rollback( __METHOD__ );
163 throw $e;
167 if ( $status->isOK() ) {
168 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
171 return $status;
175 * Show the confirmation form
177 private function showForm() {
178 global $wgOut, $wgUser, $wgRequest;
180 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
181 $suppress = "<tr id=\"wpDeleteSuppressRow\">
182 <td></td>
183 <td class='mw-input'><strong>" .
184 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
185 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
186 "</strong></td>
187 </tr>";
188 } else {
189 $suppress = '';
192 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
193 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
194 'id' => 'mw-img-deleteconfirm' ) ) .
195 Xml::openElement( 'fieldset' ) .
196 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
197 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
198 $this->prepareMessage( 'filedelete-intro' ) .
199 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
200 "<tr>
201 <td class='mw-label'>" .
202 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
203 "</td>
204 <td class='mw-input'>" .
205 Xml::listDropDown( 'wpDeleteReasonList',
206 wfMsgForContent( 'filedelete-reason-dropdown' ),
207 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
208 "</td>
209 </tr>
210 <tr>
211 <td class='mw-label'>" .
212 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
213 "</td>
214 <td class='mw-input'>" .
215 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
216 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
217 "</td>
218 </tr>
219 {$suppress}";
220 if( $wgUser->isLoggedIn() ) {
221 $form .= "
222 <tr>
223 <td></td>
224 <td class='mw-input'>" .
225 Xml::checkLabel( wfMsg( 'watchthis' ),
226 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
227 "</td>
228 </tr>";
230 $form .= "
231 <tr>
232 <td></td>
233 <td class='mw-submit'>" .
234 Xml::submitButton( wfMsg( 'filedelete-submit' ),
235 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
236 "</td>
237 </tr>" .
238 Xml::closeElement( 'table' ) .
239 Xml::closeElement( 'fieldset' ) .
240 Xml::closeElement( 'form' );
242 if ( $wgUser->isAllowed( 'editinterface' ) ) {
243 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
244 $link = Linker::link(
245 $title,
246 wfMsgHtml( 'filedelete-edit-reasonlist' ),
247 array(),
248 array( 'action' => 'edit' )
250 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
253 $wgOut->addHTML( $form );
257 * Show deletion log fragments pertaining to the current file
259 private function showLogEntries() {
260 global $wgOut;
261 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
262 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
266 * Prepare a message referring to the file being deleted,
267 * showing an appropriate message depending upon whether
268 * it's a current file or an old version
270 * @param $message String: message base
271 * @return String
273 private function prepareMessage( $message ) {
274 global $wgLang;
275 if( $this->oldimage ) {
276 return wfMsgExt(
277 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
278 'parse',
279 wfEscapeWikiText( $this->title->getText() ),
280 $wgLang->date( $this->getTimestamp(), true ),
281 $wgLang->time( $this->getTimestamp(), true ),
282 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
283 } else {
284 return wfMsgExt(
285 $message,
286 'parse',
287 wfEscapeWikiText( $this->title->getText() )
293 * Set headers, titles and other bits
295 private function setHeaders() {
296 global $wgOut;
297 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
298 $wgOut->setRobotPolicy( 'noindex,nofollow' );
299 $wgOut->addBacklinkSubtitle( $this->title );
303 * Is the provided `oldimage` value valid?
305 * @return bool
307 public static function isValidOldSpec($oldimage) {
308 return strlen( $oldimage ) >= 16
309 && strpos( $oldimage, '/' ) === false
310 && strpos( $oldimage, '\\' ) === false;
314 * Could we delete the file specified? If an `oldimage`
315 * value was provided, does it correspond to an
316 * existing, local, old version of this file?
318 * @param $file File
319 * @param $oldfile File
320 * @param $oldimage File
321 * @return bool
323 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
324 return $oldimage
325 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
326 : $file && $file->exists() && $file->isLocal();
330 * Prepare the form action
332 * @return string
334 private function getAction() {
335 $q = array();
336 $q['action'] = 'delete';
338 if( $this->oldimage )
339 $q['oldimage'] = $this->oldimage;
341 return $this->title->getLocalUrl( $q );
345 * Extract the timestamp of the old version
347 * @return string
349 private function getTimestamp() {
350 return $this->oldfile->getTimestamp();