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->addHTML( '<span class="error">' );
93 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
94 $wgOut->addHTML( '</span>' );
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 );
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 ) {
135 $status = $file->deleteOld( $oldimage, $reason, $suppress );
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 );
146 $status = Status
::newFatal( 'cannotdelete',
147 wfEscapeWikiText( $title->getPrefixedText() )
149 $page = WikiPage
::factory( $title );
150 $dbw = wfGetDB( DB_MASTER
);
152 // delete the associated article first
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__
);
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__
);
169 if ( $status->isOK() ) {
170 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
177 * Show the confirmation form
179 private function showForm() {
180 global $wgOut, $wgUser, $wgRequest;
182 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
183 $suppress = "<tr id=\"wpDeleteSuppressRow\">
185 <td class='mw-input'><strong>" .
186 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
187 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
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' ) ) .
203 <td class='mw-label'>" .
204 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
206 <td class='mw-input'>" .
207 Xml
::listDropDown( 'wpDeleteReasonList',
208 wfMsgForContent( 'filedelete-reason-dropdown' ),
209 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
213 <td class='mw-label'>" .
214 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
216 <td class='mw-input'>" .
217 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
218 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
222 if( $wgUser->isLoggedIn() ) {
226 <td class='mw-input'>" .
227 Xml
::checkLabel( wfMsg( 'watchthis' ),
228 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
235 <td class='mw-submit'>" .
236 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
237 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
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(
248 wfMsgHtml( 'filedelete-edit-reasonlist' ),
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() {
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
275 private function prepareMessage( $message ) {
277 if( $this->oldimage
) {
279 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
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
) );
289 wfEscapeWikiText( $this->title
->getText() )
295 * Set headers, titles and other bits
297 private function setHeaders() {
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?
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?
321 * @param $oldfile File
322 * @param $oldimage File
325 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
327 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
328 : $file && $file->exists() && $file->isLocal();
332 * Prepare the form action
336 private function getAction() {
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
351 private function getTimestamp() {
352 return $this->oldfile
->getTimestamp();