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