4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
11 private $title = null;
14 private $oldfile = null;
15 private $oldimage = '';
20 * @param $file File object we're deleting
22 public function __construct( $file ) {
23 $this->title
= $file->getTitle();
28 * Fulfil the request; shows the form or deletes the file,
29 * pending authentication, confirmation, etc.
31 public function execute() {
32 global $wgOut, $wgRequest, $wgUser;
36 $wgOut->readOnlyPage();
39 $permission_errors = $this->title
->getUserPermissionsErrors('delete', $wgUser);
40 if (count($permission_errors)>0) {
41 $wgOut->showPermissionsErrorPage( $permission_errors );
45 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
46 $token = $wgRequest->getText( 'wpEditToken' );
47 # Flag to hide all contents of the archived revisions
48 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
50 if( $this->oldimage
&& !self
::isValidOldSpec($this->oldimage
) ) {
51 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage
) );
55 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
57 if( !self
::haveDeletableFile($this->file
, $this->oldfile
, $this->oldimage
) ) {
58 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
59 $wgOut->addReturnTo( $this->title
);
63 // Perform the deletion if appropriate
64 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
65 $this->DeleteReasonList
= $wgRequest->getText( 'wpDeleteReasonList' );
66 $this->DeleteReason
= $wgRequest->getText( 'wpReason' );
67 $reason = $this->DeleteReasonList
;
68 if ( $reason != 'other' && $this->DeleteReason
!= '') {
69 // Entry from drop down menu + additional comment
70 $reason .= wfMsgForContent( 'colon-separator' ) . $this->DeleteReason
;
71 } elseif ( $reason == 'other' ) {
72 $reason = $this->DeleteReason
;
75 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress );
77 if( !$status->isGood() )
78 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
80 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
81 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
82 // Return to the main page if we just deleted all versions of the
83 // file, otherwise go back to the description page
84 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
90 $this->showLogEntries();
94 * Really delete the file
96 * @param $title Title object
97 * @param $file File object
98 * @param $oldimage String: archive name
99 * @param $reason String: reason of the deletion
100 * @param $suppress Boolean: whether to mark all deleted versions as restricted
102 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
105 $status = Status
::newFatal( 'error' );
108 $status = $file->deleteOld( $oldimage, $reason, $suppress );
110 // Need to do a log item
111 $log = new LogPage( 'delete' );
112 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
113 if( trim( $reason ) != '' )
114 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
115 $log->addEntry( 'delete', $title, $logComment );
118 $id = $title->getArticleID( GAID_FOR_UPDATE
);
119 $article = new Article( $title );
121 $dbw = wfGetDB( DB_MASTER
);
123 if( wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, &$error ) ) ) {
124 // delete the associated article first
125 if( $article->doDeleteArticle( $reason, $suppress, $id, false ) ) {
127 if( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
129 } elseif( $title->userIsWatching() ) {
130 $article->doUnwatch();
132 $status = $file->delete( $reason, $suppress );
135 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $id ) );
141 } catch ( MWException
$e ) {
142 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
147 if( $status->isGood() )
148 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
154 * Show the confirmation form
156 private function showForm() {
157 global $wgOut, $wgUser, $wgRequest;
159 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
160 $suppress = "<tr id=\"wpDeleteSuppressRow\">
162 <td class='mw-input'><strong>" .
163 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
164 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
171 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
172 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
173 'id' => 'mw-img-deleteconfirm' ) ) .
174 Xml
::openElement( 'fieldset' ) .
175 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
176 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
177 $this->prepareMessage( 'filedelete-intro' ) .
178 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
180 <td class='mw-label'>" .
181 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
183 <td class='mw-input'>" .
184 Xml
::listDropDown( 'wpDeleteReasonList',
185 wfMsgForContent( 'filedelete-reason-dropdown' ),
186 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
190 <td class='mw-label'>" .
191 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
193 <td class='mw-input'>" .
194 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
195 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
199 if( $wgUser->isLoggedIn() ) {
203 <td class='mw-input'>" .
204 Xml
::checkLabel( wfMsg( 'watchthis' ),
205 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
212 <td class='mw-submit'>" .
213 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
214 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
217 Xml
::closeElement( 'table' ) .
218 Xml
::closeElement( 'fieldset' ) .
219 Xml
::closeElement( 'form' );
221 if ( $wgUser->isAllowed( 'editinterface' ) ) {
222 $skin = $wgUser->getSkin();
223 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
226 wfMsgHtml( 'filedelete-edit-reasonlist' ),
228 array( 'action' => 'edit' )
230 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
233 $wgOut->addHTML( $form );
237 * Show deletion log fragments pertaining to the current file
239 private function showLogEntries() {
241 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
242 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
246 * Prepare a message referring to the file being deleted,
247 * showing an appropriate message depending upon whether
248 * it's a current file or an old version
250 * @param $message String: message base
253 private function prepareMessage( $message ) {
255 if( $this->oldimage
) {
256 $url = $this->file
->getArchiveUrl( $this->oldimage
);
258 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
260 $this->title
->getText(),
261 $wgLang->date( $this->getTimestamp(), true ),
262 $wgLang->time( $this->getTimestamp(), true ),
263 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
268 $this->title
->getText()
274 * Set headers, titles and other bits
276 private function setHeaders() {
277 global $wgOut, $wgUser;
278 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
279 $wgOut->setRobotPolicy( 'noindex,nofollow' );
280 $wgOut->setSubtitle( wfMsg(
281 'filedelete-backlink',
282 $wgUser->getSkin()->link(
287 array( 'known', 'noclasses' )
293 * Is the provided `oldimage` value valid?
297 public static function isValidOldSpec($oldimage) {
298 return strlen( $oldimage ) >= 16
299 && strpos( $oldimage, '/' ) === false
300 && strpos( $oldimage, '\\' ) === false;
304 * Could we delete the file specified? If an `oldimage`
305 * value was provided, does it correspond to an
306 * existing, local, old version of this file?
310 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
312 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
313 : $file && $file->exists() && $file->isLocal();
317 * Prepare the form action
321 private function getAction() {
323 $q['action'] = 'delete';
325 if( $this->oldimage
)
326 $q['oldimage'] = $this->oldimage
;
328 return $this->title
->getLocalUrl( $q );
332 * Extract the timestamp of the old version
336 private function getTimestamp() {
337 return $this->oldfile
->getTimestamp();