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 ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
123 WatchAction
::doWatch( $this->title
, $wgUser );
124 } elseif ( $this->title
->userIsWatching() ) {
125 WatchAction
::doUnwatch( $this->title
, $wgUser );
132 $this->showLogEntries();
136 * Really delete the file
138 * @param $title Title object
139 * @param File $file: file object
140 * @param $oldimage String: archive name
141 * @param $reason String: reason of the deletion
142 * @param $suppress Boolean: whether to mark all deleted versions as restricted
143 * @param $user User object performing the request
144 * @return bool|Status
146 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User
$user = null ) {
147 if ( $user === null ) {
154 $status = $file->deleteOld( $oldimage, $reason, $suppress );
156 // Need to do a log item
157 $log = new LogPage( 'delete' );
158 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
159 if( trim( $reason ) != '' ) {
160 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
162 $log->addEntry( 'delete', $title, $logComment );
165 $status = Status
::newFatal( 'cannotdelete',
166 wfEscapeWikiText( $title->getPrefixedText() )
168 $page = WikiPage
::factory( $title );
169 $dbw = wfGetDB( DB_MASTER
);
171 // delete the associated article first
173 if ( $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user ) >= WikiPage
::DELETE_SUCCESS
) {
174 $status = $file->delete( $reason, $suppress );
175 if( $status->isOK() ) {
176 $dbw->commit( __METHOD__
);
178 $dbw->rollback( __METHOD__
);
181 } catch ( MWException
$e ) {
182 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
183 $dbw->rollback( __METHOD__
);
188 if ( $status->isOK() ) {
189 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
196 * Show the confirmation form
198 private function showForm() {
199 global $wgOut, $wgUser, $wgRequest;
201 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
202 $suppress = "<tr id=\"wpDeleteSuppressRow\">
204 <td class='mw-input'><strong>" .
205 Xml
::checkLabel( wfMsg( 'revdelete-suppress' ),
206 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
213 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$this->title
->userIsWatching();
214 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
215 'id' => 'mw-img-deleteconfirm' ) ) .
216 Xml
::openElement( 'fieldset' ) .
217 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
218 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
219 $this->prepareMessage( 'filedelete-intro' ) .
220 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
222 <td class='mw-label'>" .
223 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
225 <td class='mw-input'>" .
226 Xml
::listDropDown( 'wpDeleteReasonList',
227 wfMsgForContent( 'filedelete-reason-dropdown' ),
228 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
232 <td class='mw-label'>" .
233 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
235 <td class='mw-input'>" .
236 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
237 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
241 if( $wgUser->isLoggedIn() ) {
245 <td class='mw-input'>" .
246 Xml
::checkLabel( wfMsg( 'watchthis' ),
247 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
254 <td class='mw-submit'>" .
255 Xml
::submitButton( wfMsg( 'filedelete-submit' ),
256 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
259 Xml
::closeElement( 'table' ) .
260 Xml
::closeElement( 'fieldset' ) .
261 Xml
::closeElement( 'form' );
263 if ( $wgUser->isAllowed( 'editinterface' ) ) {
264 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
265 $link = Linker
::link(
267 wfMsgHtml( 'filedelete-edit-reasonlist' ),
269 array( 'action' => 'edit' )
271 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
274 $wgOut->addHTML( $form );
278 * Show deletion log fragments pertaining to the current file
280 private function showLogEntries() {
282 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
283 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
287 * Prepare a message referring to the file being deleted,
288 * showing an appropriate message depending upon whether
289 * it's a current file or an old version
291 * @param $message String: message base
294 private function prepareMessage( $message ) {
296 if( $this->oldimage
) {
298 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
300 wfEscapeWikiText( $this->title
->getText() ),
301 $wgLang->date( $this->getTimestamp(), true ),
302 $wgLang->time( $this->getTimestamp(), true ),
303 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) );
308 wfEscapeWikiText( $this->title
->getText() )
314 * Set headers, titles and other bits
316 private function setHeaders() {
318 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
319 $wgOut->setRobotPolicy( 'noindex,nofollow' );
320 $wgOut->addBacklinkSubtitle( $this->title
);
324 * Is the provided `oldimage` value valid?
328 public static function isValidOldSpec($oldimage) {
329 return strlen( $oldimage ) >= 16
330 && strpos( $oldimage, '/' ) === false
331 && strpos( $oldimage, '\\' ) === false;
335 * Could we delete the file specified? If an `oldimage`
336 * value was provided, does it correspond to an
337 * existing, local, old version of this file?
340 * @param $oldfile File
341 * @param $oldimage File
344 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
346 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
347 : $file && $file->exists() && $file->isLocal();
351 * Prepare the form action
355 private function getAction() {
357 $q['action'] = 'delete';
359 if( $this->oldimage
)
360 $q['oldimage'] = $this->oldimage
;
362 return $this->title
->getLocalUrl( $q );
366 * Extract the timestamp of the old version
370 private function getTimestamp() {
371 return $this->oldfile
->getTimestamp();