Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / FileDeleteForm.php
blob9ce2f76cd4f0c3e19238edf92ab5a206afdd3708
1 <?php
2 /**
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
20 * @file
21 * @author Rob Church <robchur@gmail.com>
22 * @ingroup Media
25 /**
26 * File deletion user interface
28 * @ingroup Media
30 class FileDeleteForm {
32 /**
33 * @var Title
35 private $title = null;
37 /**
38 * @var File
40 private $file = null;
42 /**
43 * @var File
45 private $oldfile = null;
46 private $oldimage = '';
48 /**
49 * Constructor
51 * @param $file File object we're deleting
53 public function __construct( $file ) {
54 $this->title = $file->getTitle();
55 $this->file = $file;
58 /**
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 );
70 if ( wfReadOnly() ) {
71 throw new ReadOnlyError;
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
78 $this->setHeaders();
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 );
92 return;
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;
106 } else {
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>' );
116 if( $status->ok ) {
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 );
126 } else {
127 WatchAction::doUnwatch( $this->title, $wgUser );
131 return;
134 $this->showForm();
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 ) {
152 global $wgUser;
153 $user = $wgUser;
156 if( $oldimage ) {
157 $page = null;
158 $status = $file->deleteOld( $oldimage, $reason, $suppress );
159 if( $status->ok ) {
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 );
176 } else {
177 $status = Status::newFatal( 'cannotdelete',
178 wfEscapeWikiText( $title->getPrefixedText() )
180 $page = WikiPage::factory( $title );
181 $dbw = wfGetDB( DB_MASTER );
182 try {
183 // delete the associated article first
184 $error = '';
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__ );
192 } else {
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__ );
199 throw $e;
203 if ( $status->isOK() ) {
204 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
207 return $status;
211 * Show the confirmation form
213 private function showForm() {
214 global $wgOut, $wgUser, $wgRequest;
216 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
217 $suppress = "<tr id=\"wpDeleteSuppressRow\">
218 <td></td>
219 <td class='mw-input'><strong>" .
220 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
221 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
222 "</strong></td>
223 </tr>";
224 } else {
225 $suppress = '';
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' ) ) .
236 "<tr>
237 <td class='mw-label'>" .
238 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
239 "</td>
240 <td class='mw-input'>" .
241 Xml::listDropDown(
242 'wpDeleteReasonList',
243 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
244 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
246 'wpReasonDropDown',
249 "</td>
250 </tr>
251 <tr>
252 <td class='mw-label'>" .
253 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
254 "</td>
255 <td class='mw-input'>" .
256 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
257 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
258 "</td>
259 </tr>
260 {$suppress}";
261 if( $wgUser->isLoggedIn() ) {
262 $form .= "
263 <tr>
264 <td></td>
265 <td class='mw-input'>" .
266 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
267 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
268 "</td>
269 </tr>";
271 $form .= "
272 <tr>
273 <td></td>
274 <td class='mw-submit'>" .
275 Xml::submitButton( wfMessage( 'filedelete-submit' )->text(),
276 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
277 "</td>
278 </tr>" .
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(
286 $title,
287 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
288 array(),
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() {
301 global $wgOut;
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
313 * @return String
315 private function prepareMessage( $message ) {
316 global $wgLang;
317 if( $this->oldimage ) {
318 return wfMessage(
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();
324 } else {
325 return wfMessage(
326 $message,
327 wfEscapeWikiText( $this->title->getText() )
328 )->parseAsBlock();
333 * Set headers, titles and other bits
335 private function setHeaders() {
336 global $wgOut;
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?
345 * @return bool
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?
358 * @param $file File
359 * @param $oldfile File
360 * @param $oldimage File
361 * @return bool
363 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
364 return $oldimage
365 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
366 : $file && $file->exists() && $file->isLocal();
370 * Prepare the form action
372 * @return string
374 private function getAction() {
375 $q = array();
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
388 * @return string
390 private function getTimestamp() {
391 return $this->oldfile->getTimestamp();