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 .= ': ' . $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 ) )
113 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
117 if( $status->isGood() )
118 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
124 * Show the confirmation form
126 private function showForm() {
127 global $wgOut, $wgUser, $wgRequest;
129 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
130 $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\">
132 <td class='mw-input'>" .
133 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
134 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
141 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(), 'id' => 'mw-img-deleteconfirm' ) ) .
142 Xml
::openElement( 'fieldset' ) .
143 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
144 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
145 $this->prepareMessage( 'filedelete-intro' ) .
146 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
148 <td class='mw-label'>" .
149 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
151 <td class='mw-input'>" .
152 Xml
::listDropDown( 'wpDeleteReasonList',
153 wfMsgForContent( 'filedelete-reason-dropdown' ),
154 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
158 <td class='mw-label'>" .
159 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
161 <td class='mw-input'>" .
162 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
163 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
169 <td class='mw-submit'>" .
170 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
171 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
174 Xml
::closeElement( 'table' ) .
175 Xml
::closeElement( 'fieldset' ) .
176 Xml
::closeElement( 'form' );
178 if ( $wgUser->isAllowed( 'editinterface' ) ) {
179 $skin = $wgUser->getSkin();
180 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
181 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
184 $wgOut->addHtml( $form );
188 * Show deletion log fragments pertaining to the current file
190 private function showLogEntries() {
192 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
193 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
197 * Prepare a message referring to the file being deleted,
198 * showing an appropriate message depending upon whether
199 * it's a current file or an old version
201 * @param string $message Message base
204 private function prepareMessage( $message ) {
206 if( $this->oldimage
) {
207 $url = $this->file
->getArchiveUrl( $this->oldimage
);
209 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
211 $this->title
->getText(),
212 $wgLang->date( $this->getTimestamp(), true ),
213 $wgLang->time( $this->getTimestamp(), true ),
214 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
219 $this->title
->getText()
225 * Set headers, titles and other bits
227 private function setHeaders() {
228 global $wgOut, $wgUser;
229 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
230 $wgOut->setRobotPolicy( 'noindex,nofollow' );
231 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
235 * Is the provided `oldimage` value valid?
239 public static function isValidOldSpec($oldimage) {
240 return strlen( $oldimage ) >= 16
241 && strpos( $oldimage, '/' ) === false
242 && strpos( $oldimage, '\\' ) === false;
246 * Could we delete the file specified? If an `oldimage`
247 * value was provided, does it correspond to an
248 * existing, local, old version of this file?
252 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
254 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
255 : $file && $file->exists() && $file->isLocal();
259 * Prepare the form action
263 private function getAction() {
265 $q[] = 'action=delete';
266 if( $this->oldimage
)
267 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
268 return $this->title
->getLocalUrl( implode( '&', $q ) );
272 * Extract the timestamp of the old version
276 private function getTimestamp() {
277 return $this->oldfile
->getTimestamp();