Handle client disconnects in scoped timeout blocks.
[lhc/web/wiklou.git] / includes / FileDeleteForm.php
1 <?php
2 /**
3 * File deletion user interface.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Rob Church <robchur@gmail.com>
22 * @ingroup Media
23 */
24
25 /**
26 * File deletion user interface
27 *
28 * @ingroup Media
29 */
30 class FileDeleteForm {
31
32 /**
33 * @var Title
34 */
35 private $title = null;
36
37 /**
38 * @var File
39 */
40 private $file = null;
41
42 /**
43 * @var File
44 */
45 private $oldfile = null;
46 private $oldimage = '';
47
48 /**
49 * Constructor
50 *
51 * @param $file File object we're deleting
52 */
53 public function __construct( $file ) {
54 $this->title = $file->getTitle();
55 $this->file = $file;
56 }
57
58 /**
59 * Fulfil the request; shows the form or deletes the file,
60 * pending authentication, confirmation, etc.
61 */
62 public function execute() {
63 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
64
65 $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
66 if ( count( $permissionErrors ) ) {
67 throw new PermissionsError( 'delete', $permissionErrors );
68 }
69
70 if ( wfReadOnly() ) {
71 throw new ReadOnlyError;
72 }
73
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
76 }
77
78 $this->setHeaders();
79
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');
84
85 if( $this->oldimage ) {
86 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
87 }
88
89 if( !self::haveDeletableFile($this->file, $this->oldfile, $this->oldimage) ) {
90 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
91 $wgOut->addReturnTo( $this->title );
92 return;
93 }
94
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' );
99
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;
105 } else {
106 $reason = $deleteReasonList;
107 }
108
109 $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress, $wgUser );
110
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>' );
114 }
115 if( $status->ok ) {
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() );
121
122 if ( $wgUser->isLoggedIn() && $wgRequest->getCheck( 'wpWatch' ) != $wgUser->isWatched( $this->title ) ) {
123 if ( $wgRequest->getCheck( 'wpWatch' ) ) {
124 WatchAction::doWatch( $this->title, $wgUser );
125 } else {
126 WatchAction::doUnwatch( $this->title, $wgUser );
127 }
128 }
129 }
130 return;
131 }
132
133 $this->showForm();
134 $this->showLogEntries();
135 }
136
137 /**
138 * Really delete the file
139 *
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
147 */
148 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
149 if ( $user === null ) {
150 global $wgUser;
151 $user = $wgUser;
152 }
153
154 if( $oldimage ) {
155 $page = null;
156 $status = $file->deleteOld( $oldimage, $reason, $suppress );
157 if( $status->ok ) {
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;
163 }
164 $log->addEntry( 'delete', $title, $logComment );
165 }
166 } else {
167 $status = Status::newFatal( 'cannotdelete',
168 wfEscapeWikiText( $title->getPrefixedText() )
169 );
170 $page = WikiPage::factory( $title );
171 $dbw = wfGetDB( DB_MASTER );
172 try {
173 // delete the associated article first
174 $error = '';
175 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
176 // doDeleteArticleReal() returns a non-fatal error status if the page
177 // or revision is missing, so check for isOK() rather than isGood()
178 if ( $deleteStatus->isOK() ) {
179 $status = $file->delete( $reason, $suppress );
180 if( $status->isOK() ) {
181 $dbw->commit( __METHOD__ );
182 } else {
183 $dbw->rollback( __METHOD__ );
184 }
185 }
186 } catch ( MWException $e ) {
187 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
188 $dbw->rollback( __METHOD__ );
189 throw $e;
190 }
191 }
192
193 if ( $status->isOK() ) {
194 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
195 }
196
197 return $status;
198 }
199
200 /**
201 * Show the confirmation form
202 */
203 private function showForm() {
204 global $wgOut, $wgUser, $wgRequest;
205
206 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
207 $suppress = "<tr id=\"wpDeleteSuppressRow\">
208 <td></td>
209 <td class='mw-input'><strong>" .
210 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
211 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
212 "</strong></td>
213 </tr>";
214 } else {
215 $suppress = '';
216 }
217
218 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
219 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
220 'id' => 'mw-img-deleteconfirm' ) ) .
221 Xml::openElement( 'fieldset' ) .
222 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
223 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
224 $this->prepareMessage( 'filedelete-intro' ) .
225 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
226 "<tr>
227 <td class='mw-label'>" .
228 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
229 "</td>
230 <td class='mw-input'>" .
231 Xml::listDropDown( 'wpDeleteReasonList',
232 wfMsgForContent( 'filedelete-reason-dropdown' ),
233 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
234 "</td>
235 </tr>
236 <tr>
237 <td class='mw-label'>" .
238 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
239 "</td>
240 <td class='mw-input'>" .
241 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
242 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
243 "</td>
244 </tr>
245 {$suppress}";
246 if( $wgUser->isLoggedIn() ) {
247 $form .= "
248 <tr>
249 <td></td>
250 <td class='mw-input'>" .
251 Xml::checkLabel( wfMsg( 'watchthis' ),
252 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
253 "</td>
254 </tr>";
255 }
256 $form .= "
257 <tr>
258 <td></td>
259 <td class='mw-submit'>" .
260 Xml::submitButton( wfMsg( 'filedelete-submit' ),
261 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
262 "</td>
263 </tr>" .
264 Xml::closeElement( 'table' ) .
265 Xml::closeElement( 'fieldset' ) .
266 Xml::closeElement( 'form' );
267
268 if ( $wgUser->isAllowed( 'editinterface' ) ) {
269 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
270 $link = Linker::link(
271 $title,
272 wfMsgHtml( 'filedelete-edit-reasonlist' ),
273 array(),
274 array( 'action' => 'edit' )
275 );
276 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
277 }
278
279 $wgOut->addHTML( $form );
280 }
281
282 /**
283 * Show deletion log fragments pertaining to the current file
284 */
285 private function showLogEntries() {
286 global $wgOut;
287 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
288 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
289 }
290
291 /**
292 * Prepare a message referring to the file being deleted,
293 * showing an appropriate message depending upon whether
294 * it's a current file or an old version
295 *
296 * @param $message String: message base
297 * @return String
298 */
299 private function prepareMessage( $message ) {
300 global $wgLang;
301 if( $this->oldimage ) {
302 return wfMsgExt(
303 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
304 'parse',
305 wfEscapeWikiText( $this->title->getText() ),
306 $wgLang->date( $this->getTimestamp(), true ),
307 $wgLang->time( $this->getTimestamp(), true ),
308 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
309 } else {
310 return wfMsgExt(
311 $message,
312 'parse',
313 wfEscapeWikiText( $this->title->getText() )
314 );
315 }
316 }
317
318 /**
319 * Set headers, titles and other bits
320 */
321 private function setHeaders() {
322 global $wgOut;
323 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
324 $wgOut->setRobotPolicy( 'noindex,nofollow' );
325 $wgOut->addBacklinkSubtitle( $this->title );
326 }
327
328 /**
329 * Is the provided `oldimage` value valid?
330 *
331 * @return bool
332 */
333 public static function isValidOldSpec($oldimage) {
334 return strlen( $oldimage ) >= 16
335 && strpos( $oldimage, '/' ) === false
336 && strpos( $oldimage, '\\' ) === false;
337 }
338
339 /**
340 * Could we delete the file specified? If an `oldimage`
341 * value was provided, does it correspond to an
342 * existing, local, old version of this file?
343 *
344 * @param $file File
345 * @param $oldfile File
346 * @param $oldimage File
347 * @return bool
348 */
349 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
350 return $oldimage
351 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
352 : $file && $file->exists() && $file->isLocal();
353 }
354
355 /**
356 * Prepare the form action
357 *
358 * @return string
359 */
360 private function getAction() {
361 $q = array();
362 $q['action'] = 'delete';
363
364 if( $this->oldimage )
365 $q['oldimage'] = $this->oldimage;
366
367 return $this->title->getLocalUrl( $q );
368 }
369
370 /**
371 * Extract the timestamp of the old version
372 *
373 * @return string
374 */
375 private function getTimestamp() {
376 return $this->oldfile->getTimestamp();
377 }
378 }