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 . wfMsgForContent( 'colon-separator' ) . $deleteReason;
106 $reason = $deleteReasonList;
109 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress, $wgUser );
111 if( !$status->isGood() ) {
112 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
113 $wgOut->addWikiText( '<div class="error">' . $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) . '</div>' );
116 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
117 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
118 // Return to the main page if we just deleted all versions of the
119 // file, otherwise go back to the description page
120 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
122 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'wpWatch' ) != $wgUser->isWatched( $this->title
) ) {
123 if ( $wgRequest->getCheck( 'wpWatch' ) ) {
124 WatchAction
::doWatch( $this->title
, $wgUser );
126 WatchAction
::doUnwatch( $this->title
, $wgUser );
134 $this->showLogEntries();
138 * Really delete the file
140 * @param $title Title object
141 * @param File $file: file object
142 * @param $oldimage String: archive name
143 * @param $reason String: reason of the deletion
144 * @param $suppress Boolean: whether to mark all deleted versions as restricted
145 * @param $user User object performing the request
146 * @return bool|Status
148 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User
$user = null ) {
149 if ( $user === null ) {
156 $status = $file->deleteOld( $oldimage, $reason, $suppress );
158 // Need to do a log item
159 $log = new LogPage( 'delete' );
160 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
161 if( trim( $reason ) != '' ) {
162 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
164 $log->addEntry( 'delete', $title, $logComment );
167 $status = Status
::newFatal( 'cannotdelete',
168 wfEscapeWikiText( $title->getPrefixedText() )
170 $page = WikiPage
::factory( $title );
171 $dbw = wfGetDB( DB_MASTER
);
173 // delete the associated article first
175 if ( $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user ) >= WikiPage
::DELETE_SUCCESS
) {
176 $status = $file->delete( $reason, $suppress );
177 if( $status->isOK() ) {
178 $dbw->commit( __METHOD__
);
180 $dbw->rollback( __METHOD__
);
183 } catch ( MWException
$e ) {
184 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
185 $dbw->rollback( __METHOD__
);
190 if ( $status->isOK() ) {
191 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
198 * Show the confirmation form
200 private function showForm() {
201 global $wgOut, $wgUser, $wgRequest;
203 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
204 $suppress = "<tr id=\"wpDeleteSuppressRow\">
206 <td class='mw-input'><strong>" .
207 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
208 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
215 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$wgUser->isWatched( $this->title
);
216 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
217 'id' => 'mw-img-deleteconfirm' ) ) .
218 Xml
::openElement( 'fieldset' ) .
219 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
220 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
221 $this->prepareMessage( 'filedelete-intro' ) .
222 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
224 <td class='mw-label'>" .
225 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
227 <td class='mw-input'>" .
228 Xml
::listDropDown( 'wpDeleteReasonList',
229 wfMsgForContent( 'filedelete-reason-dropdown' ),
230 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
234 <td class='mw-label'>" .
235 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
237 <td class='mw-input'>" .
238 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
239 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
243 if( $wgUser->isLoggedIn() ) {
247 <td class='mw-input'>" .
248 Xml
::checkLabel( wfMsg( 'watchthis' ),
249 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
256 <td class='mw-submit'>" .
257 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
258 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
261 Xml
::closeElement( 'table' ) .
262 Xml
::closeElement( 'fieldset' ) .
263 Xml
::closeElement( 'form' );
265 if ( $wgUser->isAllowed( 'editinterface' ) ) {
266 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
267 $link = Linker
::link(
269 wfMsgHtml( 'filedelete-edit-reasonlist' ),
271 array( 'action' => 'edit' )
273 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
276 $wgOut->addHTML( $form );
280 * Show deletion log fragments pertaining to the current file
282 private function showLogEntries() {
284 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
285 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
289 * Prepare a message referring to the file being deleted,
290 * showing an appropriate message depending upon whether
291 * it's a current file or an old version
293 * @param $message String: message base
296 private function prepareMessage( $message ) {
298 if( $this->oldimage
) {
300 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
302 wfEscapeWikiText( $this->title
->getText() ),
303 $wgLang->date( $this->getTimestamp(), true ),
304 $wgLang->time( $this->getTimestamp(), true ),
305 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) );
310 wfEscapeWikiText( $this->title
->getText() )
316 * Set headers, titles and other bits
318 private function setHeaders() {
320 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
321 $wgOut->setRobotPolicy( 'noindex,nofollow' );
322 $wgOut->addBacklinkSubtitle( $this->title
);
326 * Is the provided `oldimage` value valid?
330 public static function isValidOldSpec($oldimage) {
331 return strlen( $oldimage ) >= 16
332 && strpos( $oldimage, '/' ) === false
333 && strpos( $oldimage, '\\' ) === false;
337 * Could we delete the file specified? If an `oldimage`
338 * value was provided, does it correspond to an
339 * existing, local, old version of this file?
342 * @param $oldfile File
343 * @param $oldimage File
346 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
348 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
349 : $file && $file->exists() && $file->isLocal();
353 * Prepare the form action
357 private function getAction() {
359 $q['action'] = 'delete';
361 if( $this->oldimage
)
362 $q['oldimage'] = $this->oldimage
;
364 return $this->title
->getLocalUrl( $q );
368 * Extract the timestamp of the old version
372 private function getTimestamp() {
373 return $this->oldfile
->getTimestamp();