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;
44 $permissionErrors = $this->title
->getUserPermissionsErrors( 'delete', $wgUser );
45 if ( count( $permissionErrors ) ) {
46 throw new PermissionsError( 'delete', $permissionErrors );
50 throw new ReadOnlyError
;
55 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
56 $token = $wgRequest->getText( 'wpEditToken' );
57 # Flag to hide all contents of the archived revisions
58 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
60 if( $this->oldimage
) {
61 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
64 if( !self
::haveDeletableFile($this->file
, $this->oldfile
, $this->oldimage
) ) {
65 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
66 $wgOut->addReturnTo( $this->title
);
70 // Perform the deletion if appropriate
71 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
72 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
73 $deleteReason = $wgRequest->getText( 'wpReason' );
75 if ( $deleteReasonList == 'other' ) {
76 $reason = $deleteReason;
77 } elseif ( $deleteReason != '' ) {
78 // Entry from drop down menu + additional comment
79 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
81 $reason = $deleteReasonList;
84 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress );
86 if( !$status->isGood() ) {
87 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
88 $wgOut->addHTML( '<span class="error">' );
89 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
90 $wgOut->addHTML( '</span>' );
93 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
94 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
95 // Return to the main page if we just deleted all versions of the
96 // file, otherwise go back to the description page
97 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
103 $this->showLogEntries();
107 * Really delete the file
109 * @param $title Title object
110 * @param $file File object
111 * @param $oldimage String: archive name
112 * @param $reason String: reason of the deletion
113 * @param $suppress Boolean: whether to mark all deleted versions as restricted
115 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
118 $status = Status
::newFatal( 'error' );
121 $status = $file->deleteOld( $oldimage, $reason, $suppress );
123 // Need to do a log item
124 $log = new LogPage( 'delete' );
125 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
126 if( trim( $reason ) != '' ) {
127 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
129 $log->addEntry( 'delete', $title, $logComment );
132 $id = $title->getArticleID( Title
::GAID_FOR_UPDATE
);
133 $article = new Article( $title );
134 $dbw = wfGetDB( DB_MASTER
);
136 // delete the associated article first
137 if( $article->doDeleteArticle( $reason, $suppress, $id, false ) ) {
139 if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
140 WatchAction
::doWatch( $title, $wgUser );
141 } elseif ( $title->userIsWatching() ) {
142 WatchAction
::doUnwatch( $title, $wgUser );
144 $status = $file->delete( $reason, $suppress );
151 } catch ( MWException
$e ) {
152 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
157 if( $status->isGood() )
158 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
164 * Show the confirmation form
166 private function showForm() {
167 global $wgOut, $wgUser, $wgRequest;
169 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
170 $suppress = "<tr id=\"wpDeleteSuppressRow\">
172 <td class='mw-input'><strong>" .
173 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
174 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
181 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
182 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
183 'id' => 'mw-img-deleteconfirm' ) ) .
184 Xml
::openElement( 'fieldset' ) .
185 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
186 Html
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
187 $this->prepareMessage( 'filedelete-intro' ) .
188 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
190 <td class='mw-label'>" .
191 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
193 <td class='mw-input'>" .
194 Xml
::listDropDown( 'wpDeleteReasonList',
195 wfMsgForContent( 'filedelete-reason-dropdown' ),
196 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
200 <td class='mw-label'>" .
201 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
203 <td class='mw-input'>" .
204 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
205 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
209 if( $wgUser->isLoggedIn() ) {
213 <td class='mw-input'>" .
214 Xml
::checkLabel( wfMsg( 'watchthis' ),
215 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
222 <td class='mw-submit'>" .
223 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
224 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
227 Xml
::closeElement( 'table' ) .
228 Xml
::closeElement( 'fieldset' ) .
229 Xml
::closeElement( 'form' );
231 if ( $wgUser->isAllowed( 'editinterface' ) ) {
232 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
233 $link = Linker
::link(
235 wfMsgHtml( 'filedelete-edit-reasonlist' ),
237 array( 'action' => 'edit' )
239 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
242 $wgOut->addHTML( $form );
246 * Show deletion log fragments pertaining to the current file
248 private function showLogEntries() {
250 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
251 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
255 * Prepare a message referring to the file being deleted,
256 * showing an appropriate message depending upon whether
257 * it's a current file or an old version
259 * @param $message String: message base
262 private function prepareMessage( $message ) {
264 if( $this->oldimage
) {
266 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
268 wfEscapeWikiText( $this->title
->getText() ),
269 $wgLang->date( $this->getTimestamp(), true ),
270 $wgLang->time( $this->getTimestamp(), true ),
271 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) );
276 wfEscapeWikiText( $this->title
->getText() )
282 * Set headers, titles and other bits
284 private function setHeaders() {
286 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
287 $wgOut->setRobotPolicy( 'noindex,nofollow' );
288 $wgOut->setSubtitle( wfMsg(
289 'filedelete-backlink',
290 Linker
::linkKnown( $this->title
)
295 * Is the provided `oldimage` value valid?
299 public static function isValidOldSpec($oldimage) {
300 return strlen( $oldimage ) >= 16
301 && strpos( $oldimage, '/' ) === false
302 && strpos( $oldimage, '\\' ) === false;
306 * Could we delete the file specified? If an `oldimage`
307 * value was provided, does it correspond to an
308 * existing, local, old version of this file?
311 * @param $oldfile File
312 * @param $oldimage File
315 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
317 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
318 : $file && $file->exists() && $file->isLocal();
322 * Prepare the form action
326 private function getAction() {
328 $q['action'] = 'delete';
330 if( $this->oldimage
)
331 $q['oldimage'] = $this->oldimage
;
333 return $this->title
->getLocalUrl( $q );
337 * Extract the timestamp of the old version
341 private function getTimestamp() {
342 return $this->oldfile
->getTimestamp();