Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / FileDeleteForm.php
blob98f8283813508a40799515dcb74559705a75cca5
1 <?php
2 /**
3 * File deletion user interface.
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.
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.
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
20 * @file
21 * @author Rob Church <robchur@gmail.com>
22 * @ingroup Media
25 /**
26 * File deletion user interface
28 * @ingroup Media
30 class FileDeleteForm {
32 /**
33 * @var Title
35 private $title = null;
37 /**
38 * @var File
40 private $file = null;
42 /**
43 * @var File
45 private $oldfile = null;
46 private $oldimage = '';
48 /**
49 * Constructor
51 * @param File $file File object we're deleting
53 public function __construct( $file ) {
54 $this->title = $file->getTitle();
55 $this->file = $file;
58 /**
59 * Fulfil the request; shows the form or deletes the file,
60 * pending authentication, confirmation, etc.
62 public function execute() {
63 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
65 $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
66 if ( count( $permissionErrors ) ) {
67 throw new PermissionsError( 'delete', $permissionErrors );
70 if ( wfReadOnly() ) {
71 throw new ReadOnlyError;
74 if ( $wgUploadMaintenance ) {
75 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
78 $this->setHeaders();
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' );
85 if ( $this->oldimage ) {
86 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
87 $this->title,
88 $this->oldimage
92 if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
93 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
94 $wgOut->addReturnTo( $this->title );
95 return;
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' );
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;
113 $status = self::doDelete(
114 $this->title,
115 $this->file,
116 $this->oldimage,
117 $reason,
118 $suppress,
119 $wgUser
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>' );
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() );
135 WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title, $wgUser );
137 return;
140 $this->showForm();
141 $this->showLogEntries();
145 * Really delete the file
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
156 public static function doDelete( &$title, &$file, &$oldimage, $reason,
157 $suppress, User $user = null
159 if ( $user === null ) {
160 global $wgUser;
161 $user = $wgUser;
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;
175 $logtype = $suppress ? 'suppress' : 'delete';
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 );
184 $status->value = $logid;
186 } else {
187 $status = Status::newFatal( 'cannotdelete',
188 wfEscapeWikiText( $title->getPrefixedText() )
190 $page = WikiPage::factory( $title );
191 $dbw = wfGetDB( DB_MASTER );
192 try {
193 $dbw->startAtomic( __METHOD__ );
194 // delete the associated article first
195 $error = '';
196 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
197 // doDeleteArticleReal() returns a non-fatal error status if the page
198 // or revision is missing, so check for isOK() rather than isGood()
199 if ( $deleteStatus->isOK() ) {
200 $status = $file->delete( $reason, $suppress, $user );
201 if ( $status->isOK() ) {
202 $status->value = $deleteStatus->value; // log id
203 $dbw->endAtomic( __METHOD__ );
204 } else {
205 // Page deleted but file still there? rollback page delete
206 $dbw->rollback( __METHOD__ );
208 } else {
209 // Done; nothing changed
210 $dbw->endAtomic( __METHOD__ );
212 } catch ( Exception $e ) {
213 // Rollback before returning to prevent UI from displaying
214 // incorrect "View or restore N deleted edits?"
215 $dbw->rollback( __METHOD__ );
216 throw $e;
220 if ( $status->isOK() ) {
221 Hooks::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
224 return $status;
228 * Show the confirmation form
230 private function showForm() {
231 global $wgOut, $wgUser, $wgRequest;
233 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
234 $suppress = "<tr id=\"wpDeleteSuppressRow\">
235 <td></td>
236 <td class='mw-input'><strong>" .
237 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
238 'wpSuppress', 'wpSuppress', false, [ 'tabindex' => '3' ] ) .
239 "</strong></td>
240 </tr>";
241 } else {
242 $suppress = '';
245 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
246 $form = Xml::openElement( 'form', [ 'method' => 'post', 'action' => $this->getAction(),
247 'id' => 'mw-img-deleteconfirm' ] ) .
248 Xml::openElement( 'fieldset' ) .
249 Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
250 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
251 $this->prepareMessage( 'filedelete-intro' ) .
252 Xml::openElement( 'table', [ 'id' => 'mw-img-deleteconfirm-table' ] ) .
253 "<tr>
254 <td class='mw-label'>" .
255 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
256 "</td>
257 <td class='mw-input'>" .
258 Xml::listDropDown(
259 'wpDeleteReasonList',
260 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
261 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
263 'wpReasonDropDown',
266 "</td>
267 </tr>
268 <tr>
269 <td class='mw-label'>" .
270 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
271 "</td>
272 <td class='mw-input'>" .
273 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
274 [ 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ] ) .
275 "</td>
276 </tr>
277 {$suppress}";
278 if ( $wgUser->isLoggedIn() ) {
279 $form .= "
280 <tr>
281 <td></td>
282 <td class='mw-input'>" .
283 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
284 'wpWatch', 'wpWatch', $checkWatch, [ 'tabindex' => '3' ] ) .
285 "</td>
286 </tr>";
288 $form .= "
289 <tr>
290 <td></td>
291 <td class='mw-submit'>" .
292 Xml::submitButton(
293 wfMessage( 'filedelete-submit' )->text(),
295 'name' => 'mw-filedelete-submit',
296 'id' => 'mw-filedelete-submit',
297 'tabindex' => '4'
300 "</td>
301 </tr>" .
302 Xml::closeElement( 'table' ) .
303 Xml::closeElement( 'fieldset' ) .
304 Xml::closeElement( 'form' );
306 if ( $wgUser->isAllowed( 'editinterface' ) ) {
307 $title = wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle();
308 $link = Linker::linkKnown(
309 $title,
310 wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
312 [ 'action' => 'edit' ]
314 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
317 $wgOut->addHTML( $form );
321 * Show deletion log fragments pertaining to the current file
323 private function showLogEntries() {
324 global $wgOut;
325 $deleteLogPage = new LogPage( 'delete' );
326 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
327 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
331 * Prepare a message referring to the file being deleted,
332 * showing an appropriate message depending upon whether
333 * it's a current file or an old version
335 * @param string $message Message base
336 * @return string
338 private function prepareMessage( $message ) {
339 global $wgLang;
340 if ( $this->oldimage ) {
341 # Message keys used:
342 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
343 return wfMessage(
344 "{$message}-old",
345 wfEscapeWikiText( $this->title->getText() ),
346 $wgLang->date( $this->getTimestamp(), true ),
347 $wgLang->time( $this->getTimestamp(), true ),
348 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
349 } else {
350 return wfMessage(
351 $message,
352 wfEscapeWikiText( $this->title->getText() )
353 )->parseAsBlock();
358 * Set headers, titles and other bits
360 private function setHeaders() {
361 global $wgOut;
362 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
363 $wgOut->setRobotPolicy( 'noindex,nofollow' );
364 $wgOut->addBacklinkSubtitle( $this->title );
368 * Is the provided `oldimage` value valid?
370 * @param string $oldimage
371 * @return bool
373 public static function isValidOldSpec( $oldimage ) {
374 return strlen( $oldimage ) >= 16
375 && strpos( $oldimage, '/' ) === false
376 && strpos( $oldimage, '\\' ) === false;
380 * Could we delete the file specified? If an `oldimage`
381 * value was provided, does it correspond to an
382 * existing, local, old version of this file?
384 * @param File $file
385 * @param File $oldfile
386 * @param File $oldimage
387 * @return bool
389 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
390 return $oldimage
391 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
392 : $file && $file->exists() && $file->isLocal();
396 * Prepare the form action
398 * @return string
400 private function getAction() {
401 $q = [];
402 $q['action'] = 'delete';
404 if ( $this->oldimage ) {
405 $q['oldimage'] = $this->oldimage;
408 return $this->title->getLocalURL( $q );
412 * Extract the timestamp of the old version
414 * @return string
416 private function getTimestamp() {
417 return $this->oldfile->getTimestamp();