4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
14 private $title = null;
24 private $oldfile = null;
25 private $oldimage = '';
30 * @param $file File object we're deleting
32 public function __construct( $file ) {
33 $this->title
= $file->getTitle();
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 );
50 throw new ReadOnlyError
;
53 if ( $wgUploadMaintenance ) {
54 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
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
);
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;
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>' );
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 );
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 ) {
133 $status = $file->deleteOld( $oldimage, $reason, $suppress );
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 );
144 $status = Status
::newFatal( 'cannotdelete',
145 wfEscapeWikiText( $title->getPrefixedText() )
147 $page = WikiPage
::factory( $title );
148 $dbw = wfGetDB( DB_MASTER
);
150 // delete the associated article first
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__
);
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__
);
167 if ( $status->isOK() ) {
168 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
175 * Show the confirmation form
177 private function showForm() {
178 global $wgOut, $wgUser, $wgRequest;
180 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
181 $suppress = "<tr id=\"wpDeleteSuppressRow\">
183 <td class='mw-input'><strong>" .
184 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
185 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
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' ) ) .
201 <td class='mw-label'>" .
202 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
204 <td class='mw-input'>" .
205 Xml
::listDropDown( 'wpDeleteReasonList',
206 wfMsgForContent( 'filedelete-reason-dropdown' ),
207 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
211 <td class='mw-label'>" .
212 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
214 <td class='mw-input'>" .
215 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
216 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
220 if( $wgUser->isLoggedIn() ) {
224 <td class='mw-input'>" .
225 Xml
::checkLabel( wfMsg( 'watchthis' ),
226 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
233 <td class='mw-submit'>" .
234 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
235 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
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(
246 wfMsgHtml( 'filedelete-edit-reasonlist' ),
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() {
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
273 private function prepareMessage( $message ) {
275 if( $this->oldimage
) {
277 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
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
) );
287 wfEscapeWikiText( $this->title
->getText() )
293 * Set headers, titles and other bits
295 private function setHeaders() {
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?
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?
319 * @param $oldfile File
320 * @param $oldimage File
323 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
325 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
326 : $file && $file->exists() && $file->isLocal();
330 * Prepare the form action
334 private function getAction() {
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
349 private function getTimestamp() {
350 return $this->oldfile
->getTimestamp();