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 File 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();
93 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
97 $status = $file->deleteOld( $oldimage, $reason, $suppress );
99 // Need to do a log item
100 $log = new LogPage( 'delete' );
101 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
102 if( trim( $reason ) != '' )
103 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
104 $log->addEntry( 'delete', $title, $logComment );
107 $status = $file->delete( $reason, $suppress );
109 $id = $title->getArticleID( GAID_FOR_UPDATE
);
110 // Need to delete the associated article
111 $article = new Article( $title );
113 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason, &$error)) ) {
114 if( $article->doDeleteArticle( $reason, $suppress, $id ) ) {
116 if( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
118 } elseif( $title->userIsWatching() ) {
119 $article->doUnwatch();
121 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
126 if( $status->isGood() )
127 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
133 * Show the confirmation form
135 private function showForm() {
136 global $wgOut, $wgUser, $wgRequest;
138 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
139 $suppress = "<tr id=\"wpDeleteSuppressRow\">
141 <td class='mw-input'><strong>" .
142 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
143 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
150 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
151 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
152 'id' => 'mw-img-deleteconfirm' ) ) .
153 Xml
::openElement( 'fieldset' ) .
154 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
155 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
156 $this->prepareMessage( 'filedelete-intro' ) .
157 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
159 <td class='mw-label'>" .
160 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
162 <td class='mw-input'>" .
163 Xml
::listDropDown( 'wpDeleteReasonList',
164 wfMsgForContent( 'filedelete-reason-dropdown' ),
165 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
169 <td class='mw-label'>" .
170 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
172 <td class='mw-input'>" .
173 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
174 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
178 if( $wgUser->isLoggedIn() ) {
182 <td class='mw-input'>" .
183 Xml
::checkLabel( wfMsg( 'watchthis' ),
184 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
191 <td class='mw-submit'>" .
192 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
193 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
196 Xml
::closeElement( 'table' ) .
197 Xml
::closeElement( 'fieldset' ) .
198 Xml
::closeElement( 'form' );
200 if ( $wgUser->isAllowed( 'editinterface' ) ) {
201 $skin = $wgUser->getSkin();
202 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
205 wfMsgHtml( 'filedelete-edit-reasonlist' ),
207 array( 'action' => 'edit' )
209 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
212 $wgOut->addHTML( $form );
216 * Show deletion log fragments pertaining to the current file
218 private function showLogEntries() {
220 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
221 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
225 * Prepare a message referring to the file being deleted,
226 * showing an appropriate message depending upon whether
227 * it's a current file or an old version
229 * @param string $message Message base
232 private function prepareMessage( $message ) {
234 if( $this->oldimage
) {
235 $url = $this->file
->getArchiveUrl( $this->oldimage
);
237 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
239 $this->title
->getText(),
240 $wgLang->date( $this->getTimestamp(), true ),
241 $wgLang->time( $this->getTimestamp(), true ),
242 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
247 $this->title
->getText()
253 * Set headers, titles and other bits
255 private function setHeaders() {
256 global $wgOut, $wgUser;
257 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
258 $wgOut->setRobotPolicy( 'noindex,nofollow' );
259 $wgOut->setSubtitle( wfMsg(
260 'filedelete-backlink',
261 $wgUser->getSkin()->link(
266 array( 'known', 'noclasses' )
272 * Is the provided `oldimage` value valid?
276 public static function isValidOldSpec($oldimage) {
277 return strlen( $oldimage ) >= 16
278 && strpos( $oldimage, '/' ) === false
279 && strpos( $oldimage, '\\' ) === false;
283 * Could we delete the file specified? If an `oldimage`
284 * value was provided, does it correspond to an
285 * existing, local, old version of this file?
289 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
291 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
292 : $file && $file->exists() && $file->isLocal();
296 * Prepare the form action
300 private function getAction() {
302 $q['action'] = 'delete';
304 if( $this->oldimage
)
305 $q['oldimage'] = $this->oldimage
;
307 return $this->title
->getLocalUrl( $q );
311 * Extract the timestamp of the old version
315 private function getTimestamp() {
316 return $this->oldfile
->getTimestamp();