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