MediaWiki REL1_29
FileDeleteForm.php
Go to the documentation of this file.
1<?php
25
32
36 private $title = null;
37
41 private $file = null;
42
46 private $oldfile = null;
47 private $oldimage = '';
48
54 public function __construct( $file ) {
55 $this->title = $file->getTitle();
56 $this->file = $file;
57 }
58
63 public function execute() {
65
66 $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
67 if ( count( $permissionErrors ) ) {
68 throw new PermissionsError( 'delete', $permissionErrors );
69 }
70
71 if ( wfReadOnly() ) {
72 throw new ReadOnlyError;
73 }
74
76 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
77 }
78
79 $this->setHeaders();
80
81 $this->oldimage = $wgRequest->getText( 'oldimage', false );
82 $token = $wgRequest->getText( 'wpEditToken' );
83 # Flag to hide all contents of the archived revisions
84 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
85
86 if ( $this->oldimage ) {
87 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
88 $this->title,
89 $this->oldimage
90 );
91 }
92
93 if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
94 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
95 $wgOut->addReturnTo( $this->title );
96 return;
97 }
98
99 // Perform the deletion if appropriate
100 if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
101 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
102 $deleteReason = $wgRequest->getText( 'wpReason' );
103
104 if ( $deleteReasonList == 'other' ) {
105 $reason = $deleteReason;
106 } elseif ( $deleteReason != '' ) {
107 // Entry from drop down menu + additional comment
108 $reason = $deleteReasonList . wfMessage( 'colon-separator' )
109 ->inContentLanguage()->text() . $deleteReason;
110 } else {
111 $reason = $deleteReasonList;
112 }
113
115 $this->title,
116 $this->file,
117 $this->oldimage,
118 $reason,
119 $suppress,
120 $wgUser
121 );
122
123 if ( !$status->isGood() ) {
124 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
125 $wgOut->addWikiText( '<div class="error">' .
126 $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' )
127 . '</div>' );
128 }
129 if ( $status->isOK() ) {
130 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
131 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
132 // Return to the main page if we just deleted all versions of the
133 // file, otherwise go back to the description page
134 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
135
136 WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title, $wgUser );
137 }
138 return;
139 }
140
141 $this->showForm();
142 $this->showLogEntries();
143 }
144
158 public static function doDelete( &$title, &$file, &$oldimage, $reason,
159 $suppress, User $user = null, $tags = []
160 ) {
161 if ( $user === null ) {
163 $user = $wgUser;
164 }
165
166 if ( $oldimage ) {
167 $page = null;
168 $status = $file->deleteOld( $oldimage, $reason, $suppress, $user );
169 if ( $status->ok ) {
170 // Need to do a log item
171 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
172 if ( trim( $reason ) != '' ) {
173 $logComment .= wfMessage( 'colon-separator' )
174 ->inContentLanguage()->text() . $reason;
175 }
176
177 $logtype = $suppress ? 'suppress' : 'delete';
178
179 $logEntry = new ManualLogEntry( $logtype, 'delete' );
180 $logEntry->setPerformer( $user );
181 $logEntry->setTarget( $title );
182 $logEntry->setComment( $logComment );
183 $logEntry->setTags( $tags );
184 $logid = $logEntry->insert();
185 $logEntry->publish( $logid );
186
187 $status->value = $logid;
188 }
189 } else {
190 $status = Status::newFatal( 'cannotdelete',
192 );
194 $dbw = wfGetDB( DB_MASTER );
195 $dbw->startAtomic( __METHOD__ );
196 // delete the associated article first
197 $error = '';
198 $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error,
199 $user, $tags );
200 // doDeleteArticleReal() returns a non-fatal error status if the page
201 // or revision is missing, so check for isOK() rather than isGood()
202 if ( $deleteStatus->isOK() ) {
203 $status = $file->delete( $reason, $suppress, $user );
204 if ( $status->isOK() ) {
205 $status->value = $deleteStatus->value; // log id
206 $dbw->endAtomic( __METHOD__ );
207 } else {
208 // Page deleted but file still there? rollback page delete
209 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
210 $lbFactory->rollbackMasterChanges( __METHOD__ );
211 }
212 } else {
213 // Done; nothing changed
214 $dbw->endAtomic( __METHOD__ );
215 }
216 }
217
218 if ( $status->isOK() ) {
219 Hooks::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
220 }
221
222 return $status;
223 }
224
228 private function showForm() {
230
231 if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
232 $suppress = "<tr id=\"wpDeleteSuppressRow\">
233 <td></td>
234 <td class='mw-input'><strong>" .
235 Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
236 'wpSuppress', 'wpSuppress', false, [ 'tabindex' => '3' ] ) .
237 "</strong></td>
238 </tr>";
239 } else {
240 $suppress = '';
241 }
242
243 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
244 $form = Xml::openElement( 'form', [ 'method' => 'post', 'action' => $this->getAction(),
245 'id' => 'mw-img-deleteconfirm' ] ) .
246 Xml::openElement( 'fieldset' ) .
247 Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
248 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
249 $this->prepareMessage( 'filedelete-intro' ) .
250 Xml::openElement( 'table', [ 'id' => 'mw-img-deleteconfirm-table' ] ) .
251 "<tr>
252 <td class='mw-label'>" .
253 Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
254 "</td>
255 <td class='mw-input'>" .
256 Xml::listDropDown(
257 'wpDeleteReasonList',
258 wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
259 wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
260 '',
261 'wpReasonDropDown',
262 1
263 ) .
264 "</td>
265 </tr>
266 <tr>
267 <td class='mw-label'>" .
268 Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
269 "</td>
270 <td class='mw-input'>" .
271 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
272 [ 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ] ) .
273 "</td>
274 </tr>
275 {$suppress}";
276 if ( $wgUser->isLoggedIn() ) {
277 $form .= "
278 <tr>
279 <td></td>
280 <td class='mw-input'>" .
281 Xml::checkLabel( wfMessage( 'watchthis' )->text(),
282 'wpWatch', 'wpWatch', $checkWatch, [ 'tabindex' => '3' ] ) .
283 "</td>
284 </tr>";
285 }
286 $form .= "
287 <tr>
288 <td></td>
289 <td class='mw-submit'>" .
290 Xml::submitButton(
291 wfMessage( 'filedelete-submit' )->text(),
292 [
293 'name' => 'mw-filedelete-submit',
294 'id' => 'mw-filedelete-submit',
295 'tabindex' => '4'
296 ]
297 ) .
298 "</td>
299 </tr>" .
300 Xml::closeElement( 'table' ) .
301 Xml::closeElement( 'fieldset' ) .
302 Xml::closeElement( 'form' );
303
304 if ( $wgUser->isAllowed( 'editinterface' ) ) {
305 $title = wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle();
306 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
307 $link = $linkRenderer->makeKnownLink(
308 $title,
309 wfMessage( 'filedelete-edit-reasonlist' )->text(),
310 [],
311 [ 'action' => 'edit' ]
312 );
313 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
314 }
315
316 $wgOut->addHTML( $form );
317 }
318
322 private function showLogEntries() {
324 $deleteLogPage = new LogPage( 'delete' );
325 $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
326 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
327 }
328
337 private function prepareMessage( $message ) {
339 if ( $this->oldimage ) {
340 # Message keys used:
341 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
342 return wfMessage(
343 "{$message}-old",
344 wfEscapeWikiText( $this->title->getText() ),
345 $wgLang->date( $this->getTimestamp(), true ),
346 $wgLang->time( $this->getTimestamp(), true ),
347 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
348 } else {
349 return wfMessage(
350 $message,
351 wfEscapeWikiText( $this->title->getText() )
352 )->parseAsBlock();
353 }
354 }
355
359 private function setHeaders() {
361 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
362 $wgOut->setRobotPolicy( 'noindex,nofollow' );
363 $wgOut->addBacklinkSubtitle( $this->title );
364 }
365
372 public static function isValidOldSpec( $oldimage ) {
373 return strlen( $oldimage ) >= 16
374 && strpos( $oldimage, '/' ) === false
375 && strpos( $oldimage, '\\' ) === false;
376 }
377
388 public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
389 return $oldimage
391 : $file && $file->exists() && $file->isLocal();
392 }
393
399 private function getAction() {
400 $q = [];
401 $q['action'] = 'delete';
402
403 if ( $this->oldimage ) {
404 $q['oldimage'] = $this->oldimage;
405 }
406
407 return $this->title->getLocalURL( $q );
408 }
409
415 private function getTimestamp() {
416 return $this->oldfile->getTimestamp();
417 }
418}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgUploadMaintenance
To disable file delete/restore temporarily.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
$wgUser
Definition Setup.php:781
$wgOut
Definition Setup.php:791
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:639
An error page which can definitely be safely rendered using the OutputPage.
File deletion user interface.
getAction()
Prepare the form action.
static haveDeletableFile(&$file, &$oldfile, $oldimage)
Could we delete the file specified? If an oldimage value was provided, does it correspond to an exist...
prepareMessage( $message)
Prepare a message referring to the file being deleted, showing an appropriate message depending upon ...
getTimestamp()
Extract the timestamp of the old version.
showForm()
Show the confirmation form.
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
execute()
Fulfil the request; shows the form or deletes the file, pending authentication, confirmation,...
showLogEntries()
Show deletion log fragments pertaining to the current file.
setHeaders()
Set headers, titles and other bits.
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
__construct( $file)
Constructor.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:51
isLocal()
Returns true if the file comes from the local file repository.
Definition File.php:1836
exists()
Returns true if file exists in the repository.
Definition File.php:877
getTitle()
Return the associated title object.
Definition File.php:326
delete( $reason, $suppress=false, $user=null)
Delete all versions of the file.
Definition File.php:1930
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Class to simplify the use of log pages.
Definition LogPage.php:31
Class for creating log entries manually, to inject them into the database.
Definition LogEntry.php:396
MediaWikiServices is the service locator for the application scope of MediaWiki.
Show an error when a user tries to do something they do not have the necessary permissions for.
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
Represents a title within MediaWiki.
Definition Title.php:39
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1451
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:50
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:120
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition design.txt:56
$lbFactory
const PROTO_CURRENT
Definition Defines.php:220
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:249
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2578
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:2937
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition hooks.txt:108
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition hooks.txt:1049
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition hooks.txt:2007
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
title
const DB_MASTER
Definition defines.php:26