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 WatchAction
::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title
, $wgUser );
129 $this->showLogEntries();
133 * Really delete the file
135 * @param $title Title object
136 * @param File $file: file object
137 * @param string $oldimage archive name
138 * @param string $reason reason of the deletion
139 * @param $suppress Boolean: whether to mark all deleted versions as restricted
140 * @param $user User object performing the request
141 * @throws MWException
142 * @return bool|Status
144 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User
$user = null ) {
145 if ( $user === null ) {
152 $status = $file->deleteOld( $oldimage, $reason, $suppress );
154 // Need to do a log item
155 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
156 if ( trim( $reason ) != '' ) {
157 $logComment .= wfMessage( 'colon-separator' )
158 ->inContentLanguage()->text() . $reason;
161 $logtype = $suppress ?
'suppress' : 'delete';
163 $logEntry = new ManualLogEntry( $logtype, 'delete' );
164 $logEntry->setPerformer( $user );
165 $logEntry->setTarget( $title );
166 $logEntry->setComment( $logComment );
167 $logid = $logEntry->insert();
168 $logEntry->publish( $logid );
171 $status = Status
::newFatal( 'cannotdelete',
172 wfEscapeWikiText( $title->getPrefixedText() )
174 $page = WikiPage
::factory( $title );
175 $dbw = wfGetDB( DB_MASTER
);
177 // delete the associated article first
179 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
180 // doDeleteArticleReal() returns a non-fatal error status if the page
181 // or revision is missing, so check for isOK() rather than isGood()
182 if ( $deleteStatus->isOK() ) {
183 $status = $file->delete( $reason, $suppress );
184 if ( $status->isOK() ) {
185 $dbw->commit( __METHOD__
);
187 $dbw->rollback( __METHOD__
);
190 } catch ( MWException
$e ) {
191 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
192 $dbw->rollback( __METHOD__
);
197 if ( $status->isOK() ) {
198 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
205 * Show the confirmation form
207 private function showForm() {
208 global $wgOut, $wgUser, $wgRequest;
210 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
211 $suppress = "<tr id=\"wpDeleteSuppressRow\">
213 <td class='mw-input'><strong>" .
214 Xml
::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
215 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
222 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) ||
$wgUser->isWatched( $this->title
);
223 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
224 'id' => 'mw-img-deleteconfirm' ) ) .
225 Xml
::openElement( 'fieldset' ) .
226 Xml
::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
227 Html
::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage
) ) .
228 $this->prepareMessage( 'filedelete-intro' ) .
229 Xml
::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
231 <td class='mw-label'>" .
232 Xml
::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
234 <td class='mw-input'>" .
236 'wpDeleteReasonList',
237 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
238 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
246 <td class='mw-label'>" .
247 Xml
::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
249 <td class='mw-input'>" .
250 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
251 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
255 if ( $wgUser->isLoggedIn() ) {
259 <td class='mw-input'>" .
260 Xml
::checkLabel( wfMessage( 'watchthis' )->text(),
261 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
268 <td class='mw-submit'>" .
269 Xml
::submitButton( wfMessage( 'filedelete-submit' )->text(),
270 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
273 Xml
::closeElement( 'table' ) .
274 Xml
::closeElement( 'fieldset' ) .
275 Xml
::closeElement( 'form' );
277 if ( $wgUser->isAllowed( 'editinterface' ) ) {
278 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Filedelete-reason-dropdown' );
279 $link = Linker
::link(
281 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
283 array( 'action' => 'edit' )
285 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
288 $wgOut->addHTML( $form );
292 * Show deletion log fragments pertaining to the current file
294 private function showLogEntries() {
296 $deleteLogPage = new LogPage( 'delete' );
297 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
298 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
);
302 * Prepare a message referring to the file being deleted,
303 * showing an appropriate message depending upon whether
304 * it's a current file or an old version
306 * @param string $message message base
309 private function prepareMessage( $message ) {
311 if ( $this->oldimage
) {
313 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
314 wfEscapeWikiText( $this->title
->getText() ),
315 $wgLang->date( $this->getTimestamp(), true ),
316 $wgLang->time( $this->getTimestamp(), true ),
317 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
), PROTO_CURRENT
) )->parseAsBlock();
321 wfEscapeWikiText( $this->title
->getText() )
327 * Set headers, titles and other bits
329 private function setHeaders() {
331 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title
->getText() ) );
332 $wgOut->setRobotPolicy( 'noindex,nofollow' );
333 $wgOut->addBacklinkSubtitle( $this->title
);
337 * Is the provided `oldimage` value valid?
341 public static function isValidOldSpec( $oldimage ) {
342 return strlen( $oldimage ) >= 16
343 && strpos( $oldimage, '/' ) === false
344 && strpos( $oldimage, '\\' ) === false;
348 * Could we delete the file specified? If an `oldimage`
349 * value was provided, does it correspond to an
350 * existing, local, old version of this file?
353 * @param $oldfile File
354 * @param $oldimage File
357 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
359 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
360 : $file && $file->exists() && $file->isLocal();
364 * Prepare the form action
368 private function getAction() {
370 $q['action'] = 'delete';
372 if ( $this->oldimage
) {
373 $q['oldimage'] = $this->oldimage
;
376 return $this->title
->getLocalURL( $q );
380 * Extract the timestamp of the old version
384 private function getTimestamp() {
385 return $this->oldfile
->getTimestamp();