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 ) {
96 $status = $file->deleteOld( $oldimage, $reason, $suppress );
98 // Need to do a log item
99 $log = new LogPage( 'delete' );
100 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
101 if( trim( $reason ) != '' )
102 $logComment .= ": {$reason}";
103 $log->addEntry( 'delete', $title, $logComment );
106 $status = $file->delete( $reason, $suppress );
108 $id = $title->getArticleID( GAID_FOR_UPDATE
);
109 // Need to delete the associated article
110 $article = new Article( $title );
111 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)) ) {
112 if( $article->doDeleteArticle( $reason, $suppress, $id ) ) {
114 if( $wgRequest->getCheck( 'wpWatch' ) ) {
116 } elseif( $title->userIsWatching() ) {
117 $article->doUnwatch();
119 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
124 if( $status->isGood() )
125 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
131 * Show the confirmation form
133 private function showForm() {
134 global $wgOut, $wgUser, $wgRequest;
136 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
137 $suppress = "<tr id=\"wpDeleteSuppressRow\">
139 <td class='mw-input'>" .
140 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
141 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
148 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
149 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
150 'id' => 'mw-img-deleteconfirm' ) ) .
151 Xml
::openElement( 'fieldset' ) .
152 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
153 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
154 $this->prepareMessage( 'filedelete-intro' ) .
155 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
157 <td class='mw-label'>" .
158 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
160 <td class='mw-input'>" .
161 Xml
::listDropDown( 'wpDeleteReasonList',
162 wfMsgForContent( 'filedelete-reason-dropdown' ),
163 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
167 <td class='mw-label'>" .
168 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
170 <td class='mw-input'>" .
171 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
172 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
178 <td class='mw-input'>" .
179 Xml
::checkLabel( wfMsg( 'watchthis' ),
180 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
185 <td class='mw-submit'>" .
186 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
187 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
190 Xml
::closeElement( 'table' ) .
191 Xml
::closeElement( 'fieldset' ) .
192 Xml
::closeElement( 'form' );
194 if ( $wgUser->isAllowed( 'editinterface' ) ) {
195 $skin = $wgUser->getSkin();
196 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
197 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
200 $wgOut->addHTML( $form );
204 * Show deletion log fragments pertaining to the current file
206 private function showLogEntries() {
208 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
209 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
213 * Prepare a message referring to the file being deleted,
214 * showing an appropriate message depending upon whether
215 * it's a current file or an old version
217 * @param string $message Message base
220 private function prepareMessage( $message ) {
222 if( $this->oldimage
) {
223 $url = $this->file
->getArchiveUrl( $this->oldimage
);
225 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
227 $this->title
->getText(),
228 $wgLang->date( $this->getTimestamp(), true ),
229 $wgLang->time( $this->getTimestamp(), true ),
230 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
235 $this->title
->getText()
241 * Set headers, titles and other bits
243 private function setHeaders() {
244 global $wgOut, $wgUser;
245 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
246 $wgOut->setRobotPolicy( 'noindex,nofollow' );
247 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
251 * Is the provided `oldimage` value valid?
255 public static function isValidOldSpec($oldimage) {
256 return strlen( $oldimage ) >= 16
257 && strpos( $oldimage, '/' ) === false
258 && strpos( $oldimage, '\\' ) === false;
262 * Could we delete the file specified? If an `oldimage`
263 * value was provided, does it correspond to an
264 * existing, local, old version of this file?
268 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
270 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
271 : $file && $file->exists() && $file->isLocal();
275 * Prepare the form action
279 private function getAction() {
281 $q[] = 'action=delete';
282 if( $this->oldimage
)
283 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
284 return $this->title
->getLocalUrl( implode( '&', $q ) );
288 * Extract the timestamp of the old version
292 private function getTimestamp() {
293 return $this->oldfile
->getTimestamp();