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