Merge "(bug 260) Handle <pre> overflow automatically with a scroll bar"
[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 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
160 if( trim( $reason ) != '' ) {
161 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
162 }
163
164 $logtype = $suppress ? 'suppress' : 'delete';
165
166 $logEntry = new ManualLogEntry( $logtype, 'delete' );
167 $logEntry->setPerformer( $user );
168 $logEntry->setTarget( $title );
169 $logEntry->setComment( $logComment );
170 $logid = $logEntry->insert();
171 $logEntry->publish( $logid );
172 }
173 } else {
174 $status = Status::newFatal( 'cannotdelete',
175 wfEscapeWikiText( $title->getPrefixedText() )
176 );
177 $page = WikiPage::factory( $title );
178 $dbw = wfGetDB( DB_MASTER );
179 try {
180 // delete the associated article first
181 $error = '';
182 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
183 // doDeleteArticleReal() returns a non-fatal error status if the page
184 // or revision is missing, so check for isOK() rather than isGood()
185 if ( $deleteStatus->isOK() ) {
186 $status = $file->delete( $reason, $suppress );
187 if( $status->isOK() ) {
188 $dbw->commit( __METHOD__ );
189 } else {
190 $dbw->rollback( __METHOD__ );
191 }
192 }
193 } catch ( MWException $e ) {
194 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
195 $dbw->rollback( __METHOD__ );
196 throw $e;
197 }
198 }
199
200 if ( $status->isOK() ) {
201 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
202 }
203
204 return $status;
205 }
206
207 /**
208 * Show the confirmation form
209 */
210 private function showForm() {
211 global $wgOut, $wgUser, $wgRequest;
212
213 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
214 $suppress = "<tr id=\"wpDeleteSuppressRow\">
215 <td></td>
216 <td class='mw-input'><strong>" .
217 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
218 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
219 "</strong></td>
220 </tr>";
221 } else {
222 $suppress = '';
223 }
224
225 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
226 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
227 'id' => 'mw-img-deleteconfirm' ) ) .
228 Xml::openElement( 'fieldset' ) .
229 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
230 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
231 $this->prepareMessage( 'filedelete-intro' ) .
232 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
233 "<tr>
234 <td class='mw-label'>" .
235 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
236 "</td>
237 <td class='mw-input'>" .
238 Xml::listDropDown( 'wpDeleteReasonList',
239 wfMsgForContent( 'filedelete-reason-dropdown' ),
240 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
241 "</td>
242 </tr>
243 <tr>
244 <td class='mw-label'>" .
245 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
246 "</td>
247 <td class='mw-input'>" .
248 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
249 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
250 "</td>
251 </tr>
252 {$suppress}";
253 if( $wgUser->isLoggedIn() ) {
254 $form .= "
255 <tr>
256 <td></td>
257 <td class='mw-input'>" .
258 Xml::checkLabel( wfMsg( 'watchthis' ),
259 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
260 "</td>
261 </tr>";
262 }
263 $form .= "
264 <tr>
265 <td></td>
266 <td class='mw-submit'>" .
267 Xml::submitButton( wfMsg( 'filedelete-submit' ),
268 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
269 "</td>
270 </tr>" .
271 Xml::closeElement( 'table' ) .
272 Xml::closeElement( 'fieldset' ) .
273 Xml::closeElement( 'form' );
274
275 if ( $wgUser->isAllowed( 'editinterface' ) ) {
276 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
277 $link = Linker::link(
278 $title,
279 wfMsgHtml( 'filedelete-edit-reasonlist' ),
280 array(),
281 array( 'action' => 'edit' )
282 );
283 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
284 }
285
286 $wgOut->addHTML( $form );
287 }
288
289 /**
290 * Show deletion log fragments pertaining to the current file
291 */
292 private function showLogEntries() {
293 global $wgOut;
294 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
295 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
296 }
297
298 /**
299 * Prepare a message referring to the file being deleted,
300 * showing an appropriate message depending upon whether
301 * it's a current file or an old version
302 *
303 * @param $message String: message base
304 * @return String
305 */
306 private function prepareMessage( $message ) {
307 global $wgLang;
308 if( $this->oldimage ) {
309 return wfMsgExt(
310 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
311 'parse',
312 wfEscapeWikiText( $this->title->getText() ),
313 $wgLang->date( $this->getTimestamp(), true ),
314 $wgLang->time( $this->getTimestamp(), true ),
315 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
316 } else {
317 return wfMsgExt(
318 $message,
319 'parse',
320 wfEscapeWikiText( $this->title->getText() )
321 );
322 }
323 }
324
325 /**
326 * Set headers, titles and other bits
327 */
328 private function setHeaders() {
329 global $wgOut;
330 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
331 $wgOut->setRobotPolicy( 'noindex,nofollow' );
332 $wgOut->addBacklinkSubtitle( $this->title );
333 }
334
335 /**
336 * Is the provided `oldimage` value valid?
337 *
338 * @return bool
339 */
340 public static function isValidOldSpec($oldimage) {
341 return strlen( $oldimage ) >= 16
342 && strpos( $oldimage, '/' ) === false
343 && strpos( $oldimage, '\\' ) === false;
344 }
345
346 /**
347 * Could we delete the file specified? If an `oldimage`
348 * value was provided, does it correspond to an
349 * existing, local, old version of this file?
350 *
351 * @param $file File
352 * @param $oldfile File
353 * @param $oldimage File
354 * @return bool
355 */
356 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
357 return $oldimage
358 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
359 : $file && $file->exists() && $file->isLocal();
360 }
361
362 /**
363 * Prepare the form action
364 *
365 * @return string
366 */
367 private function getAction() {
368 $q = array();
369 $q['action'] = 'delete';
370
371 if( $this->oldimage )
372 $q['oldimage'] = $this->oldimage;
373
374 return $this->title->getLocalUrl( $q );
375 }
376
377 /**
378 * Extract the timestamp of the old version
379 *
380 * @return string
381 */
382 private function getTimestamp() {
383 return $this->oldfile->getTimestamp();
384 }
385 }