Merge "Cosmetic change: add a new line after a section title."
[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 ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
123 WatchAction::doWatch( $this->title, $wgUser );
124 } elseif ( $this->title->userIsWatching() ) {
125 WatchAction::doUnwatch( $this->title, $wgUser );
126 }
127 }
128 return;
129 }
130
131 $this->showForm();
132 $this->showLogEntries();
133 }
134
135 /**
136 * Really delete the file
137 *
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
145 */
146 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
147 if ( $user === null ) {
148 global $wgUser;
149 $user = $wgUser;
150 }
151
152 if( $oldimage ) {
153 $page = null;
154 $status = $file->deleteOld( $oldimage, $reason, $suppress );
155 if( $status->ok ) {
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;
161 }
162 $log->addEntry( 'delete', $title, $logComment );
163 }
164 } else {
165 $status = Status::newFatal( 'cannotdelete',
166 wfEscapeWikiText( $title->getPrefixedText() )
167 );
168 $page = WikiPage::factory( $title );
169 $dbw = wfGetDB( DB_MASTER );
170 try {
171 // delete the associated article first
172 $error = '';
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__ );
177 } else {
178 $dbw->rollback( __METHOD__ );
179 }
180 }
181 } catch ( MWException $e ) {
182 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
183 $dbw->rollback( __METHOD__ );
184 throw $e;
185 }
186 }
187
188 if ( $status->isOK() ) {
189 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
190 }
191
192 return $status;
193 }
194
195 /**
196 * Show the confirmation form
197 */
198 private function showForm() {
199 global $wgOut, $wgUser, $wgRequest;
200
201 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
202 $suppress = "<tr id=\"wpDeleteSuppressRow\">
203 <td></td>
204 <td class='mw-input'><strong>" .
205 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
206 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
207 "</strong></td>
208 </tr>";
209 } else {
210 $suppress = '';
211 }
212
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' ) ) .
221 "<tr>
222 <td class='mw-label'>" .
223 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
224 "</td>
225 <td class='mw-input'>" .
226 Xml::listDropDown( 'wpDeleteReasonList',
227 wfMsgForContent( 'filedelete-reason-dropdown' ),
228 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
229 "</td>
230 </tr>
231 <tr>
232 <td class='mw-label'>" .
233 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
234 "</td>
235 <td class='mw-input'>" .
236 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
237 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
238 "</td>
239 </tr>
240 {$suppress}";
241 if( $wgUser->isLoggedIn() ) {
242 $form .= "
243 <tr>
244 <td></td>
245 <td class='mw-input'>" .
246 Xml::checkLabel( wfMsg( 'watchthis' ),
247 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
248 "</td>
249 </tr>";
250 }
251 $form .= "
252 <tr>
253 <td></td>
254 <td class='mw-submit'>" .
255 Xml::submitButton( wfMsg( 'filedelete-submit' ),
256 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
257 "</td>
258 </tr>" .
259 Xml::closeElement( 'table' ) .
260 Xml::closeElement( 'fieldset' ) .
261 Xml::closeElement( 'form' );
262
263 if ( $wgUser->isAllowed( 'editinterface' ) ) {
264 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
265 $link = Linker::link(
266 $title,
267 wfMsgHtml( 'filedelete-edit-reasonlist' ),
268 array(),
269 array( 'action' => 'edit' )
270 );
271 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
272 }
273
274 $wgOut->addHTML( $form );
275 }
276
277 /**
278 * Show deletion log fragments pertaining to the current file
279 */
280 private function showLogEntries() {
281 global $wgOut;
282 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
283 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
284 }
285
286 /**
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
290 *
291 * @param $message String: message base
292 * @return String
293 */
294 private function prepareMessage( $message ) {
295 global $wgLang;
296 if( $this->oldimage ) {
297 return wfMsgExt(
298 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
299 'parse',
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 ) );
304 } else {
305 return wfMsgExt(
306 $message,
307 'parse',
308 wfEscapeWikiText( $this->title->getText() )
309 );
310 }
311 }
312
313 /**
314 * Set headers, titles and other bits
315 */
316 private function setHeaders() {
317 global $wgOut;
318 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
319 $wgOut->setRobotPolicy( 'noindex,nofollow' );
320 $wgOut->addBacklinkSubtitle( $this->title );
321 }
322
323 /**
324 * Is the provided `oldimage` value valid?
325 *
326 * @return bool
327 */
328 public static function isValidOldSpec($oldimage) {
329 return strlen( $oldimage ) >= 16
330 && strpos( $oldimage, '/' ) === false
331 && strpos( $oldimage, '\\' ) === false;
332 }
333
334 /**
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?
338 *
339 * @param $file File
340 * @param $oldfile File
341 * @param $oldimage File
342 * @return bool
343 */
344 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
345 return $oldimage
346 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
347 : $file && $file->exists() && $file->isLocal();
348 }
349
350 /**
351 * Prepare the form action
352 *
353 * @return string
354 */
355 private function getAction() {
356 $q = array();
357 $q['action'] = 'delete';
358
359 if( $this->oldimage )
360 $q['oldimage'] = $this->oldimage;
361
362 return $this->title->getLocalUrl( $q );
363 }
364
365 /**
366 * Extract the timestamp of the old version
367 *
368 * @return string
369 */
370 private function getTimestamp() {
371 return $this->oldfile->getTimestamp();
372 }
373 }