mediawiki.js: Show line number of caller of mw.log.warn and .error
[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 $status->value = $logid;
185 }
186 } else {
187 $status = Status::newFatal( 'cannotdelete',
188 wfEscapeWikiText( $title->getPrefixedText() )
189 );
190 $page = WikiPage::factory( $title );
191 $dbw = wfGetDB( DB_MASTER );
192 try {
193 // delete the associated article first
194 $error = '';
195 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
196 // doDeleteArticleReal() returns a non-fatal error status if the page
197 // or revision is missing, so check for isOK() rather than isGood()
198 if ( $deleteStatus->isOK() ) {
199 $status = $file->delete( $reason, $suppress, $user );
200 if ( $status->isOK() ) {
201 $dbw->commit( __METHOD__ );
202 $status->value = $deleteStatus->value; // log id
203 } else {
204 $dbw->rollback( __METHOD__ );
205 }
206 }
207 } catch ( Exception $e ) {
208 // Rollback before returning to prevent UI from displaying
209 // incorrect "View or restore N deleted edits?"
210 $dbw->rollback( __METHOD__ );
211 throw $e;
212 }
213 }
214
215 if ( $status->isOK() ) {
216 Hooks::run( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
217 }
218
219 return $status;
220 }
221
222 /**
223 * Show the confirmation form
224 */
225 private function showForm() {
226 global $wgOut, $wgUser, $wgRequest;
227
228 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
229 $suppress = "<tr id=\"wpDeleteSuppressRow\">
230 <td></td>
231 <td class='mw-input'><strong>" .
232 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
233 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
234 "</strong></td>
235 </tr>";
236 } else {
237 $suppress = '';
238 }
239
240 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
241 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
242 'id' => 'mw-img-deleteconfirm' ) ) .
243 Xml::openElement( 'fieldset' ) .
244 Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
245 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
246 $this->prepareMessage( 'filedelete-intro' ) .
247 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
248 "<tr>
249 <td class='mw-label'>" .
250 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
251 "</td>
252 <td class='mw-input'>" .
253 Xml::listDropDown(
254 'wpDeleteReasonList',
255 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
256 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
257 '',
258 'wpReasonDropDown',
259 1
260 ) .
261 "</td>
262 </tr>
263 <tr>
264 <td class='mw-label'>" .
265 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
266 "</td>
267 <td class='mw-input'>" .
268 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
269 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
270 "</td>
271 </tr>
272 {$suppress}";
273 if ( $wgUser->isLoggedIn() ) {
274 $form .= "
275 <tr>
276 <td></td>
277 <td class='mw-input'>" .
278 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
279 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
280 "</td>
281 </tr>";
282 }
283 $form .= "
284 <tr>
285 <td></td>
286 <td class='mw-submit'>" .
287 Xml::submitButton(
288 wfMessage( 'filedelete-submit' )->text(),
289 array(
290 'name' => 'mw-filedelete-submit',
291 'id' => 'mw-filedelete-submit',
292 'tabindex' => '4'
293 )
294 ) .
295 "</td>
296 </tr>" .
297 Xml::closeElement( 'table' ) .
298 Xml::closeElement( 'fieldset' ) .
299 Xml::closeElement( 'form' );
300
301 if ( $wgUser->isAllowed( 'editinterface' ) ) {
302 $title = wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle();
303 $link = Linker::linkKnown(
304 $title,
305 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
306 array(),
307 array( 'action' => 'edit' )
308 );
309 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
310 }
311
312 $wgOut->addHTML( $form );
313 }
314
315 /**
316 * Show deletion log fragments pertaining to the current file
317 */
318 private function showLogEntries() {
319 global $wgOut;
320 $deleteLogPage = new LogPage( 'delete' );
321 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
322 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
323 }
324
325 /**
326 * Prepare a message referring to the file being deleted,
327 * showing an appropriate message depending upon whether
328 * it's a current file or an old version
329 *
330 * @param string $message Message base
331 * @return string
332 */
333 private function prepareMessage( $message ) {
334 global $wgLang;
335 if ( $this->oldimage ) {
336 # Message keys used:
337 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
338 return wfMessage(
339 "{$message}-old",
340 wfEscapeWikiText( $this->title->getText() ),
341 $wgLang->date( $this->getTimestamp(), true ),
342 $wgLang->time( $this->getTimestamp(), true ),
343 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
344 } else {
345 return wfMessage(
346 $message,
347 wfEscapeWikiText( $this->title->getText() )
348 )->parseAsBlock();
349 }
350 }
351
352 /**
353 * Set headers, titles and other bits
354 */
355 private function setHeaders() {
356 global $wgOut;
357 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
358 $wgOut->setRobotPolicy( 'noindex,nofollow' );
359 $wgOut->addBacklinkSubtitle( $this->title );
360 }
361
362 /**
363 * Is the provided `oldimage` value valid?
364 *
365 * @param string $oldimage
366 * @return bool
367 */
368 public static function isValidOldSpec( $oldimage ) {
369 return strlen( $oldimage ) >= 16
370 && strpos( $oldimage, '/' ) === false
371 && strpos( $oldimage, '\\' ) === false;
372 }
373
374 /**
375 * Could we delete the file specified? If an `oldimage`
376 * value was provided, does it correspond to an
377 * existing, local, old version of this file?
378 *
379 * @param File $file
380 * @param File $oldfile
381 * @param File $oldimage
382 * @return bool
383 */
384 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
385 return $oldimage
386 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
387 : $file && $file->exists() && $file->isLocal();
388 }
389
390 /**
391 * Prepare the form action
392 *
393 * @return string
394 */
395 private function getAction() {
396 $q = array();
397 $q['action'] = 'delete';
398
399 if ( $this->oldimage ) {
400 $q['oldimage'] = $this->oldimage;
401 }
402
403 return $this->title->getLocalURL( $q );
404 }
405
406 /**
407 * Extract the timestamp of the old version
408 *
409 * @return string
410 */
411 private function getTimestamp() {
412 return $this->oldfile->getTimestamp();
413 }
414 }