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