MediaWiki  master
FileDeleteAction.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Actions;
22 
23 use DeleteAction;
24 use ErrorPageError;
25 use File;
26 use FileDeleteForm;
27 use IContextSource;
28 use LocalFile;
31 use OldLocalFile;
32 use Page;
34 use Title;
35 
43  private $file;
45  private $oldImage;
47  private $oldFile;
48 
52  public function __construct( Page $page, IContextSource $context = null ) {
53  parent::__construct( $page, $context );
54  $services = MediaWikiServices::getInstance();
55  $this->file = $this->getArticle()->getFile();
56  $this->oldImage = $this->getRequest()->getText( 'oldimage', '' );
57  if ( $this->oldImage !== '' ) {
58  $this->oldFile = $services->getRepoGroup()->getLocalRepo()->newFromArchiveName(
59  $this->getTitle(),
60  $this->oldImage
61  );
62  }
63  }
64 
65  protected function tempDelete() {
66  $file = $this->file;'@phan-var LocalFile $file';
68  $this->tempExecute( $file );
69  }
70 
71  private function tempExecute( LocalFile $file ): void {
72  $context = $this->getContext();
73  $title = $this->getTitle();
74 
75  $this->runExecuteChecks();
76 
77  $outputPage = $context->getOutput();
78  $this->prepareOutput( $context->msg( 'filedelete', $title->getText() ) );
79 
80  $request = $context->getRequest();
81 
82  $checkFile = $this->oldFile ?: $file;
83  if ( !$checkFile->exists() || !$checkFile->isLocal() ) {
84  $outputPage->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
85  $outputPage->addReturnTo( $title );
86  return;
87  }
88 
89  // Perform the deletion if appropriate
90  $token = $request->getVal( 'wpEditToken' );
91  if (
92  !$request->wasPosted() ||
93  !$context->getUser()->matchEditToken( $token, [ 'delete', $title->getPrefixedText() ] )
94  ) {
95  $this->showConfirm();
96  return;
97  }
98 
99  $permissionStatus = PermissionStatus::newEmpty();
100  if ( !$context->getAuthority()->authorizeWrite(
101  'delete', $title, $permissionStatus
102  ) ) {
103  throw new PermissionsError( 'delete', $permissionStatus );
104  }
105 
106  $reason = $this->getDeleteReason();
107 
108  # Flag to hide all contents of the archived revisions
109  $suppress = $request->getCheck( 'wpSuppress' ) &&
110  $context->getAuthority()->isAllowed( 'suppressrevision' );
111 
112  $status = FileDeleteForm::doDelete(
113  $title,
114  $file,
115  $this->oldImage,
116  $reason,
117  $suppress,
118  $context->getUser()
119  );
120 
121  if ( !$status->isGood() ) {
122  $outputPage->wrapWikiTextAsInterface(
123  'error',
124  $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' )
125  );
126  }
127  if ( $status->isOK() ) {
128  $outputPage->setPageTitle( $context->msg( 'actioncomplete' ) );
129  $outputPage->addHTML( $this->prepareMessage( 'filedelete-success' ) );
130  // Return to the main page if we just deleted all versions of the
131  // file, otherwise go back to the description page
132  $outputPage->addReturnTo( $this->oldImage ? $title : Title::newMainPage() );
133 
134  $this->watchlistManager->setWatch(
135  $request->getCheck( 'wpWatch' ),
137  $title
138  );
139  }
140  }
141 
142  protected function showFormWarnings(): void {
143  $this->getOutput()->addHTML( $this->prepareMessage( 'filedelete-intro' ) );
144  }
145 
149  private function showConfirm() {
150  $this->prepareOutputForForm();
151  $this->showFormWarnings();
152  $this->showForm( $this->getDefaultReason() );
153  $this->showEditReasonsLinks();
154  $this->showLogEntries();
155  }
156 
165  private function prepareMessage( string $message ) {
166  if ( $this->oldFile ) {
167  $lang = $this->getContext()->getLanguage();
168  # Message keys used:
169  # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
170  return $this->getContext()->msg(
171  "{$message}-old",
172  wfEscapeWikiText( $this->getTitle()->getText() ),
173  $lang->date( $this->oldFile->getTimestamp(), true ),
174  $lang->time( $this->oldFile->getTimestamp(), true ),
175  wfExpandUrl( $this->file->getArchiveUrl( $this->oldImage ), PROTO_CURRENT )
176  )->parseAsBlock();
177  } else {
178  return $this->getContext()->msg(
179  $message,
180  wfEscapeWikiText( $this->getTitle()->getText() )
181  )->parseAsBlock();
182  }
183  }
184 
188  protected function getFormAction(): string {
189  $q = [];
190  $q['action'] = 'delete';
191 
192  if ( $this->oldImage ) {
193  $q['oldimage'] = $this->oldImage;
194  }
195 
196  return $this->getTitle()->getLocalURL( $q );
197  }
198 
202  protected function runExecuteChecks(): void {
203  parent::runExecuteChecks();
204 
205  if ( $this->getContext()->getConfig()->get( 'UploadMaintenance' ) ) {
206  throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
207  }
208  }
209 
214  protected function getFormMessages(): array {
215  return [
216  self::MSG_REASON_DROPDOWN => 'filedelete-reason-dropdown',
217  self::MSG_REASON_DROPDOWN_SUPPRESS => 'filedelete-reason-dropdown-suppress',
218  self::MSG_REASON_DROPDOWN_OTHER => 'filedelete-reason-otherlist',
219  self::MSG_COMMENT => 'filedelete-comment',
220  self::MSG_REASON_OTHER => 'filedelete-otherreason',
221  self::MSG_SUBMIT => 'filedelete-submit',
222  self::MSG_LEGEND => 'filedelete-legend',
223  self::MSG_EDIT_REASONS => 'filedelete-edit-reasonlist',
224  self::MSG_EDIT_REASONS_SUPPRESS => 'filedelete-edit-reasonlist-suppress',
225  ];
226  }
227 
231  protected function getDefaultReason(): string {
232  // TODO Should we autogenerate something for files?
233  return $this->getRequest()->getText( 'wpReason' );
234  }
235 }
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
MediaWiki\Actions\FileDeleteAction
Handle file deletion.
Definition: FileDeleteAction.php:41
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:202
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:146
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
MediaWiki\Actions\FileDeleteAction\prepareMessage
prepareMessage(string $message)
Prepare a message referring to the file being deleted, showing an appropriate message depending upon ...
Definition: FileDeleteAction.php:165
MediaWiki\Permissions\PermissionStatus\newEmpty
static newEmpty()
Definition: PermissionStatus.php:67
MediaWiki\Actions
Definition: ActionFactory.php:20
MediaWiki\Actions\FileDeleteAction\getFormMessages
getFormMessages()
TODO Do we need all these messages to be different?
Definition: FileDeleteAction.php:214
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:32
Title\newMainPage
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:728
IContextSource\getAuthority
getAuthority()
MediaWiki\Actions\FileDeleteAction\showConfirm
showConfirm()
Show the confirmation form.
Definition: FileDeleteAction.php:149
MediaWiki\Actions\FileDeleteAction\__construct
__construct(Page $page, IContextSource $context=null)
Only public since 1.21.Stability: stableto callCalling with anything other then Article is deprecated...
Definition: FileDeleteAction.php:52
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:263
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:132
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:67
MediaWiki\Actions\FileDeleteAction\getFormAction
getFormAction()
Definition: FileDeleteAction.php:188
DeleteAction\showLogEntries
showLogEntries()
Show deletion log fragments pertaining to the current page.
Definition: DeleteAction.php:459
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
Action\getArticle
getArticle()
Get a Article object.
Definition: Action.php:206
DeleteAction\prepareOutput
prepareOutput(Message $pageTitle)
Definition: DeleteAction.php:469
FileDeleteForm
File deletion user interface.
Definition: FileDeleteForm.php:33
$title
$title
Definition: testCompression.php:38
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:62
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:195
FileDeleteForm\doDelete
static doDelete(Title $title, LocalFile $file, ?string $oldimage, $reason, $suppress, UserIdentity $user, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:48
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
DeleteAction\showEditReasonsLinks
showEditReasonsLinks()
Definition: DeleteAction.php:260
MediaWiki\Actions\FileDeleteAction\getDefaultReason
getDefaultReason()
Default reason to be used for the deletion form.string
Definition: FileDeleteAction.php:231
DeleteAction\prepareOutputForForm
prepareOutputForForm()
Definition: DeleteAction.php:476
MediaWiki\Actions\FileDeleteAction\tempDelete
tempDelete()
Definition: FileDeleteAction.php:65
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1456
MediaWiki\Actions\FileDeleteAction\$file
File $file
Definition: FileDeleteAction.php:43
DeleteAction\showForm
showForm(string $reason)
Definition: DeleteAction.php:292
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:216
IContextSource\getUser
getUser()
DeleteAction\getDeleteReason
getDeleteReason()
Definition: DeleteAction.php:441
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
MediaWiki\Permissions\PermissionStatus
A StatusValue for permission errors.
Definition: PermissionStatus.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:47
MediaWiki\Actions\FileDeleteAction\runExecuteChecks
runExecuteChecks()
Should use Action::checkCanExecute instead
Definition: FileDeleteAction.php:202
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
MediaWiki\Actions\FileDeleteAction\$oldImage
string $oldImage
Descriptor for the old version of the image, if applicable.
Definition: FileDeleteAction.php:45
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:156
DeleteAction
Handle page deletion.
Definition: DeleteAction.php:35
IContextSource\getRequest
getRequest()
MediaWiki\Actions\FileDeleteAction\showFormWarnings
showFormWarnings()
Definition: FileDeleteAction.php:142
MediaWiki\Actions\FileDeleteAction\tempExecute
tempExecute(LocalFile $file)
Definition: FileDeleteAction.php:71
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
IContextSource\getOutput
getOutput()
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:35
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:474
MediaWiki\Actions\FileDeleteAction\$oldFile
OldLocalFile null $oldFile
Corresponding to oldImage, if applicable.
Definition: FileDeleteAction.php:47