4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
14 private $title = null;
24 private $oldfile = null;
25 private $oldimage = '';
27 private $DeleteReason, $DeleteReasonList;
31 * @param $file File object we're deleting
33 public function __construct( $file ) {
34 $this->title
= $file->getTitle();
39 * Fulfil the request; shows the form or deletes the file,
40 * pending authentication, confirmation, etc.
42 public function execute() {
43 global $wgOut, $wgRequest, $wgUser;
45 $permissionErrors = $this->title
->getUserPermissionsErrors( 'delete', $wgUser );
46 if ( count( $permissionErrors ) ) {
47 throw new PermissionsError( 'delete', $permissionErrors );
51 throw new ReadOnlyError
;
56 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
57 $token = $wgRequest->getText( 'wpEditToken' );
58 # Flag to hide all contents of the archived revisions
59 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
61 if( $this->oldimage
) {
62 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
65 if( !self
::haveDeletableFile($this->file
, $this->oldfile
, $this->oldimage
) ) {
66 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
67 $wgOut->addReturnTo( $this->title
);
71 // Perform the deletion if appropriate
72 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
73 $this->DeleteReasonList
= $wgRequest->getText( 'wpDeleteReasonList' );
74 $this->DeleteReason
= $wgRequest->getText( 'wpReason' );
75 $reason = $this->DeleteReasonList
;
76 if ( $reason != 'other' && $this->DeleteReason
!= '') {
77 // Entry from drop down menu + additional comment
78 $reason .= wfMsgForContent( 'colon-separator' ) . $this->DeleteReason
;
79 } elseif ( $reason == 'other' ) {
80 $reason = $this->DeleteReason
;
83 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress );
85 if( !$status->isGood() ) {
86 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
87 $wgOut->addHTML( '<span class="error">' );
88 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
89 $wgOut->addHTML( '</span>' );
92 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
93 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
94 // Return to the main page if we just deleted all versions of the
95 // file, otherwise go back to the description page
96 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
102 $this->showLogEntries();
106 * Really delete the file
108 * @param $title Title object
109 * @param $file File object
110 * @param $oldimage String: archive name
111 * @param $reason String: reason of the deletion
112 * @param $suppress Boolean: whether to mark all deleted versions as restricted
114 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
117 $status = Status
::newFatal( 'error' );
120 $status = $file->deleteOld( $oldimage, $reason, $suppress );
122 // Need to do a log item
123 $log = new LogPage( 'delete' );
124 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
125 if( trim( $reason ) != '' ) {
126 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
128 $log->addEntry( 'delete', $title, $logComment );
131 $id = $title->getArticleID( Title
::GAID_FOR_UPDATE
);
132 $article = new Article( $title );
133 $dbw = wfGetDB( DB_MASTER
);
135 // delete the associated article first
136 if( $article->doDeleteArticle( $reason, $suppress, $id, false ) ) {
138 if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
139 WatchAction
::doWatch( $title, $wgUser );
140 } elseif ( $title->userIsWatching() ) {
141 WatchAction
::doUnwatch( $title, $wgUser );
143 $status = $file->delete( $reason, $suppress );
150 } catch ( MWException
$e ) {
151 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
156 if( $status->isGood() )
157 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
163 * Show the confirmation form
165 private function showForm() {
166 global $wgOut, $wgUser, $wgRequest;
168 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
169 $suppress = "<tr id=\"wpDeleteSuppressRow\">
171 <td class='mw-input'><strong>" .
172 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
173 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
180 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
181 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
182 'id' => 'mw-img-deleteconfirm' ) ) .
183 Xml
::openElement( 'fieldset' ) .
184 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
185 Html
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
186 $this->prepareMessage( 'filedelete-intro' ) .
187 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
189 <td class='mw-label'>" .
190 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
192 <td class='mw-input'>" .
193 Xml
::listDropDown( 'wpDeleteReasonList',
194 wfMsgForContent( 'filedelete-reason-dropdown' ),
195 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
199 <td class='mw-label'>" .
200 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
202 <td class='mw-input'>" .
203 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
204 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
208 if( $wgUser->isLoggedIn() ) {
212 <td class='mw-input'>" .
213 Xml
::checkLabel( wfMsg( 'watchthis' ),
214 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
221 <td class='mw-submit'>" .
222 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
223 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
226 Xml
::closeElement( 'table' ) .
227 Xml
::closeElement( 'fieldset' ) .
228 Xml
::closeElement( 'form' );
230 if ( $wgUser->isAllowed( 'editinterface' ) ) {
231 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
232 $link = Linker
::link(
234 wfMsgHtml( 'filedelete-edit-reasonlist' ),
236 array( 'action' => 'edit' )
238 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
241 $wgOut->addHTML( $form );
245 * Show deletion log fragments pertaining to the current file
247 private function showLogEntries() {
249 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
250 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
254 * Prepare a message referring to the file being deleted,
255 * showing an appropriate message depending upon whether
256 * it's a current file or an old version
258 * @param $message String: message base
261 private function prepareMessage( $message ) {
263 if( $this->oldimage
) {
265 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
267 wfEscapeWikiText( $this->title
->getText() ),
268 $wgLang->date( $this->getTimestamp(), true ),
269 $wgLang->time( $this->getTimestamp(), true ),
270 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) );
275 wfEscapeWikiText( $this->title
->getText() )
281 * Set headers, titles and other bits
283 private function setHeaders() {
285 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
286 $wgOut->setRobotPolicy( 'noindex,nofollow' );
287 $wgOut->setSubtitle( wfMsg(
288 'filedelete-backlink',
289 Linker
::linkKnown( $this->title
)
294 * Is the provided `oldimage` value valid?
298 public static function isValidOldSpec($oldimage) {
299 return strlen( $oldimage ) >= 16
300 && strpos( $oldimage, '/' ) === false
301 && strpos( $oldimage, '\\' ) === false;
305 * Could we delete the file specified? If an `oldimage`
306 * value was provided, does it correspond to an
307 * existing, local, old version of this file?
310 * @param $oldfile File
311 * @param $oldimage File
314 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
316 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
317 : $file && $file->exists() && $file->isLocal();
321 * Prepare the form action
325 private function getAction() {
327 $q['action'] = 'delete';
329 if( $this->oldimage
)
330 $q['oldimage'] = $this->oldimage
;
332 return $this->title
->getLocalUrl( $q );
336 * Extract the timestamp of the old version
340 private function getTimestamp() {
341 return $this->oldfile
->getTimestamp();