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 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
126 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User
$user = null ) {
127 if ( $user === null ) {
134 $status = $file->deleteOld( $oldimage, $reason, $suppress );
136 // Need to do a log item
137 $log = new LogPage( 'delete' );
138 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
139 if( trim( $reason ) != '' ) {
140 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
142 $log->addEntry( 'delete', $title, $logComment );
145 $status = Status
::newFatal( 'error' );
146 $page = WikiPage
::factory( $title );
147 $dbw = wfGetDB( DB_MASTER
);
149 // delete the associated article first
151 if ( $page->doDeleteArticle( $reason, $suppress, 0, false, $error, $user ) ) {
152 $status = $file->delete( $reason, $suppress );
159 } catch ( MWException
$e ) {
160 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
166 if ( $status->isGood() ) {
167 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
174 * Show the confirmation form
176 private function showForm() {
177 global $wgOut, $wgUser, $wgRequest;
179 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
180 $suppress = "<tr id=\"wpDeleteSuppressRow\">
182 <td class='mw-input'><strong>" .
183 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
184 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
191 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
192 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
193 'id' => 'mw-img-deleteconfirm' ) ) .
194 Xml
::openElement( 'fieldset' ) .
195 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
196 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
197 $this->prepareMessage( 'filedelete-intro' ) .
198 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
200 <td class='mw-label'>" .
201 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
203 <td class='mw-input'>" .
204 Xml
::listDropDown( 'wpDeleteReasonList',
205 wfMsgForContent( 'filedelete-reason-dropdown' ),
206 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
210 <td class='mw-label'>" .
211 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
213 <td class='mw-input'>" .
214 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
215 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
219 if( $wgUser->isLoggedIn() ) {
223 <td class='mw-input'>" .
224 Xml
::checkLabel( wfMsg( 'watchthis' ),
225 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
232 <td class='mw-submit'>" .
233 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
234 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
237 Xml
::closeElement( 'table' ) .
238 Xml
::closeElement( 'fieldset' ) .
239 Xml
::closeElement( 'form' );
241 if ( $wgUser->isAllowed( 'editinterface' ) ) {
242 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
243 $link = Linker
::link(
245 wfMsgHtml( 'filedelete-edit-reasonlist' ),
247 array( 'action' => 'edit' )
249 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
252 $wgOut->addHTML( $form );
256 * Show deletion log fragments pertaining to the current file
258 private function showLogEntries() {
260 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
261 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
265 * Prepare a message referring to the file being deleted,
266 * showing an appropriate message depending upon whether
267 * it's a current file or an old version
269 * @param $message String: message base
272 private function prepareMessage( $message ) {
274 if( $this->oldimage
) {
276 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
278 wfEscapeWikiText( $this->title
->getText() ),
279 $wgLang->date( $this->getTimestamp(), true ),
280 $wgLang->time( $this->getTimestamp(), true ),
281 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) );
286 wfEscapeWikiText( $this->title
->getText() )
292 * Set headers, titles and other bits
294 private function setHeaders() {
296 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
297 $wgOut->setRobotPolicy( 'noindex,nofollow' );
298 $wgOut->addBacklinkSubtitle( $this->title
);
302 * Is the provided `oldimage` value valid?
306 public static function isValidOldSpec($oldimage) {
307 return strlen( $oldimage ) >= 16
308 && strpos( $oldimage, '/' ) === false
309 && strpos( $oldimage, '\\' ) === false;
313 * Could we delete the file specified? If an `oldimage`
314 * value was provided, does it correspond to an
315 * existing, local, old version of this file?
318 * @param $oldfile File
319 * @param $oldimage File
322 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
324 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
325 : $file && $file->exists() && $file->isLocal();
329 * Prepare the form action
333 private function getAction() {
335 $q['action'] = 'delete';
337 if( $this->oldimage
)
338 $q['oldimage'] = $this->oldimage
;
340 return $this->title
->getLocalUrl( $q );
344 * Extract the timestamp of the old version
348 private function getTimestamp() {
349 return $this->oldfile
->getTimestamp();