Debian Codegraph @

includes/actions/RevertAction.php

raw symbols SCIP
1 <?php
2 /**
3 * File reversion 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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 * @file
20 * @ingroup Actions
21 * @ingroup Media
22 * @author Alexandre Emsenhuber
23 * @author Rob Church <[email protected]>
24 */
25
26 use MediaWiki\Context\IContextSource;
27 use MediaWiki\HTMLForm\HTMLForm;
28 use MediaWiki\Language\Language;
29 use MediaWiki\MediaWikiServices;
30 use MediaWiki\Output\OutputPage;
31 use MediaWiki\Status\Status;
32 use MediaWiki\User\User;
33 use MediaWiki\Utils\MWTimestamp;
34
35 /**
36 * File reversion user interface
37 * WikiPage must contain getFile method: \WikiFilePage
38 * Article::getFile is only for b/c: \ImagePage
39 *
40 * @ingroup Actions
41 */
42 class RevertAction extends FormAction {
43
44 private Language $contentLanguage;
45 private RepoGroup $repoGroup;
46
47 /**
48 * @param Article $article
49 * @param IContextSource $context
50 * @param Language $contentLanguage
51 * @param RepoGroup $repoGroup
52 */
53 public function __construct(
54 Article $article,
55 IContextSource $context,
56 Language $contentLanguage,
57 RepoGroup $repoGroup
58 ) {
59 parent::__construct( $article, $context );
60 $this->contentLanguage = $contentLanguage;
61 $this->repoGroup = $repoGroup;
62 }
63
64 /**
65 * @var OldLocalFile
66 */
67 protected $oldFile;
68
69 public function getName() {
70 return 'revert';
71 }
72
73 public function getRestriction() {
74 // Required permissions of revert are complicated, will be checked below.
75 return 'upload';
76 }
77
78 protected function checkCanExecute( User $user ) {
79 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
80 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
81 }
82
83 $rights = [ 'reupload' ];
84 if ( $user->equals( $this->getFile()->getUploader() ) ) {
85 // reupload-own is more basic, put it in the front for error messages.
86 array_unshift( $rights, 'reupload-own' );
87 }
88 if ( !$user->isAllowedAny( ...$rights ) ) {
89 throw new PermissionsError( $rights[0] );
90 }
91
92 parent::checkCanExecute( $user );
93
94 $oldimage = $this->getRequest()->getText( 'oldimage' );
95 if ( strlen( $oldimage ) < 16
96 || strpos( $oldimage, '/' ) !== false
97 || strpos( $oldimage, '\\' ) !== false
98 ) {
99 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
100 }
101
102 $this->oldFile = $this->repoGroup->getLocalRepo()
103 ->newFromArchiveName( $this->getTitle(), $oldimage );
104
105 if ( !$this->oldFile->exists() ) {
106 throw new ErrorPageError( '', 'filerevert-badversion' );
107 }
108 }
109
110 protected function usesOOUI() {
111 return true;
112 }
113
114 protected function alterForm( HTMLForm $form ) {
115 $form->setWrapperLegendMsg( 'filerevert-legend' );
116 $form->setSubmitTextMsg( 'filerevert-submit' );
117 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
118 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
119 }
120
121 protected function getFormFields() {
122 $timestamp = $this->oldFile->getTimestamp();
123
124 $user = $this->getUser();
125 $lang = $this->getLanguage();
126 $userDate = $lang->userDate( $timestamp, $user );
127 $userTime = $lang->userTime( $timestamp, $user );
128 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
129 $ts = $siteTs->format( 'YmdHis' );
130 $contLang = $this->contentLanguage;
131 $siteDate = $contLang->date( $ts, false, false );
132 $siteTime = $contLang->time( $ts, false, false );
133 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
134
135 return [
136 'intro' => [
137 'type' => 'info',
138 'raw' => true,
139 'default' => $this->msg( 'filerevert-intro',
140 $this->getTitle()->getText(), $userDate, $userTime,
141 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
142 $this->getFile()
143 ->getArchiveUrl(
144 $this->getRequest()->getText( 'oldimage' )
145 ),
146 PROTO_CURRENT
147 ) )->parseAsBlock()
148 ],
149 'comment' => [
150 'type' => 'text',
151 'label-message' => 'filerevert-comment',
152 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
153 $tzMsg )->inContentLanguage()->text()
154 ]
155 ];
156 }
157
158 public function onSubmit( $data ) {
159 $this->useTransactionalTimeLimit();
160
161 $old = $this->getRequest()->getText( 'oldimage' );
162 /** @var LocalFile $localFile */
163 $localFile = $this->getFile();
164 '@phan-var LocalFile $localFile';
165 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
166
167 $source = $localFile->getArchiveVirtualUrl( $old );
168 $comment = $data['comment'];
169
170 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
171 return Status::newFatal( 'filerevert-identical' );
172 }
173
174 // TODO: Preserve file properties from database instead of reloading from file
175 return $localFile->upload(
176 $source,
177 $comment,
178 $comment,
179 0,
180 false,
181 false,
182 $this->getAuthority(),
183 [],
184 true,
185 true
186 );
187 }
188
189 public function onSuccess() {
190 $timestamp = $this->oldFile->getTimestamp();
191 $user = $this->getUser();
192 $lang = $this->getLanguage();
193 $userDate = $lang->userDate( $timestamp, $user );
194 $userTime = $lang->userTime( $timestamp, $user );
195
196 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
197 $userDate, $userTime,
198 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
199 $this->getFile()
200 ->getArchiveUrl(
201 $this->getRequest()->getText( 'oldimage' )
202 ),
203 PROTO_CURRENT
204 ) );
205 $this->getOutput()->returnToMain( false, $this->getTitle() );
206 }
207
208 protected function getPageTitle() {
209 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
210 }
211
212 protected function getDescription() {
213 return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped();
214 }
215
216 public function doesWrites() {
217 return true;
218 }
219
220 /**
221 * @since 1.35
222 * @return File
223 */
224 private function getFile(): File {
225 /** @var \WikiFilePage $wikiPage */
226 $wikiPage = $this->getWikiPage();
227 // @phan-suppress-next-line PhanUndeclaredMethod
228 return $wikiPage->getFile();
229 }
230 }