3 * File deletion user interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Rob Church <robchur@gmail.com>
26 * File deletion user interface
30 class FileDeleteForm
{
35 private $title = null;
45 private $oldfile = null;
46 private $oldimage = '';
51 * @param $file File object we're deleting
53 public function __construct( $file ) {
54 $this->title
= $file->getTitle();
59 * Fulfil the request; shows the form or deletes the file,
60 * pending authentication, confirmation, etc.
62 public function execute() {
63 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
65 $permissionErrors = $this->title
->getUserPermissionsErrors( 'delete', $wgUser );
66 if ( count( $permissionErrors ) ) {
67 throw new PermissionsError( 'delete', $permissionErrors );
71 throw new ReadOnlyError
;
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
80 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
81 $token = $wgRequest->getText( 'wpEditToken' );
82 # Flag to hide all contents of the archived revisions
83 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
85 if( $this->oldimage
) {
86 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
89 if( !self
::haveDeletableFile( $this->file
, $this->oldfile
, $this->oldimage
) ) {
90 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
91 $wgOut->addReturnTo( $this->title
);
95 // Perform the deletion if appropriate
96 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
97 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
98 $deleteReason = $wgRequest->getText( 'wpReason' );
100 if ( $deleteReasonList == 'other' ) {
101 $reason = $deleteReason;
102 } elseif ( $deleteReason != '' ) {
103 // Entry from drop down menu + additional comment
104 $reason = $deleteReasonList . wfMessage( 'colon-separator' )
105 ->inContentLanguage()->text() . $deleteReason;
107 $reason = $deleteReasonList;
110 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress, $wgUser );
112 if( !$status->isGood() ) {
113 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
114 $wgOut->addWikiText( '<div class="error">' . $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) . '</div>' );
117 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
118 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
119 // Return to the main page if we just deleted all versions of the
120 // file, otherwise go back to the description page
121 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
123 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'wpWatch' ) != $wgUser->isWatched( $this->title
) ) {
124 if ( $wgRequest->getCheck( 'wpWatch' ) ) {
125 WatchAction
::doWatch( $this->title
, $wgUser );
127 WatchAction
::doUnwatch( $this->title
, $wgUser );
135 $this->showLogEntries();
139 * Really delete the file
141 * @param $title Title object
142 * @param File $file: file object
143 * @param string $oldimage archive name
144 * @param string $reason reason of the deletion
145 * @param $suppress Boolean: whether to mark all deleted versions as restricted
146 * @param $user User object performing the request
147 * @throws MWException
148 * @return bool|Status
150 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User
$user = null ) {
151 if ( $user === null ) {
158 $status = $file->deleteOld( $oldimage, $reason, $suppress );
160 // Need to do a log item
161 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
162 if( trim( $reason ) != '' ) {
163 $logComment .= wfMessage( 'colon-separator' )
164 ->inContentLanguage()->text() . $reason;
167 $logtype = $suppress ?
'suppress' : 'delete';
169 $logEntry = new ManualLogEntry( $logtype, 'delete' );
170 $logEntry->setPerformer( $user );
171 $logEntry->setTarget( $title );
172 $logEntry->setComment( $logComment );
173 $logid = $logEntry->insert();
174 $logEntry->publish( $logid );
177 $status = Status
::newFatal( 'cannotdelete',
178 wfEscapeWikiText( $title->getPrefixedText() )
180 $page = WikiPage
::factory( $title );
181 $dbw = wfGetDB( DB_MASTER
);
183 // delete the associated article first
185 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
186 // doDeleteArticleReal() returns a non-fatal error status if the page
187 // or revision is missing, so check for isOK() rather than isGood()
188 if ( $deleteStatus->isOK() ) {
189 $status = $file->delete( $reason, $suppress );
190 if( $status->isOK() ) {
191 $dbw->commit( __METHOD__
);
193 $dbw->rollback( __METHOD__
);
196 } catch ( MWException
$e ) {
197 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
198 $dbw->rollback( __METHOD__
);
203 if ( $status->isOK() ) {
204 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
211 * Show the confirmation form
213 private function showForm() {
214 global $wgOut, $wgUser, $wgRequest;
216 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
217 $suppress = "<tr id=\"wpDeleteSuppressRow\">
219 <td class='mw-input'><strong>" .
220 Xml
::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
221 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
228 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$wgUser->isWatched( $this->title
);
229 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
230 'id' => 'mw-img-deleteconfirm' ) ) .
231 Xml
::openElement( 'fieldset' ) .
232 Xml
::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
233 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
234 $this->prepareMessage( 'filedelete-intro' ) .
235 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
237 <td class='mw-label'>" .
238 Xml
::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
240 <td class='mw-input'>" .
242 'wpDeleteReasonList',
243 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
244 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
252 <td class='mw-label'>" .
253 Xml
::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
255 <td class='mw-input'>" .
256 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
257 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
261 if( $wgUser->isLoggedIn() ) {
265 <td class='mw-input'>" .
266 Xml
::checkLabel( wfMessage( 'watchthis' )->text(),
267 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
274 <td class='mw-submit'>" .
275 Xml
::submitButton( wfMessage( 'filedelete-submit' )->text(),
276 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
279 Xml
::closeElement( 'table' ) .
280 Xml
::closeElement( 'fieldset' ) .
281 Xml
::closeElement( 'form' );
283 if ( $wgUser->isAllowed( 'editinterface' ) ) {
284 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
285 $link = Linker
::link(
287 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
289 array( 'action' => 'edit' )
291 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
294 $wgOut->addHTML( $form );
298 * Show deletion log fragments pertaining to the current file
300 private function showLogEntries() {
302 $deleteLogPage = new LogPage( 'delete' );
303 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
304 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
308 * Prepare a message referring to the file being deleted,
309 * showing an appropriate message depending upon whether
310 * it's a current file or an old version
312 * @param string $message message base
315 private function prepareMessage( $message ) {
317 if( $this->oldimage
) {
319 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
320 wfEscapeWikiText( $this->title
->getText() ),
321 $wgLang->date( $this->getTimestamp(), true ),
322 $wgLang->time( $this->getTimestamp(), true ),
323 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) )->parseAsBlock();
327 wfEscapeWikiText( $this->title
->getText() )
333 * Set headers, titles and other bits
335 private function setHeaders() {
337 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
338 $wgOut->setRobotPolicy( 'noindex,nofollow' );
339 $wgOut->addBacklinkSubtitle( $this->title
);
343 * Is the provided `oldimage` value valid?
347 public static function isValidOldSpec( $oldimage ) {
348 return strlen( $oldimage ) >= 16
349 && strpos( $oldimage, '/' ) === false
350 && strpos( $oldimage, '\\' ) === false;
354 * Could we delete the file specified? If an `oldimage`
355 * value was provided, does it correspond to an
356 * existing, local, old version of this file?
359 * @param $oldfile File
360 * @param $oldimage File
363 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
365 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
366 : $file && $file->exists() && $file->isLocal();
370 * Prepare the form action
374 private function getAction() {
376 $q['action'] = 'delete';
378 if( $this->oldimage
) {
379 $q['oldimage'] = $this->oldimage
;
382 return $this->title
->getLocalURL( $q );
386 * Extract the timestamp of the old version
390 private function getTimestamp() {
391 return $this->oldfile
->getTimestamp();