Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
ApiFileRevert | |
0.00% |
0 / 58 |
|
0.00% |
0 / 9 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
validateParameters | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
mustBePosted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isWriteMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
2 | |||
needsToken | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com> |
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 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 |
19 | * |
20 | * @file |
21 | */ |
22 | |
23 | use MediaWiki\Title\Title; |
24 | use Wikimedia\ParamValidator\ParamValidator; |
25 | |
26 | /** |
27 | * @ingroup API |
28 | */ |
29 | class ApiFileRevert extends ApiBase { |
30 | /** @var LocalFile */ |
31 | protected $file; |
32 | |
33 | /** @var string */ |
34 | protected $archiveName; |
35 | |
36 | /** @var array */ |
37 | protected $params; |
38 | |
39 | /** @var RepoGroup */ |
40 | private $repoGroup; |
41 | |
42 | /** |
43 | * @param ApiMain $main |
44 | * @param string $action |
45 | * @param RepoGroup $repoGroup |
46 | */ |
47 | public function __construct( |
48 | ApiMain $main, |
49 | $action, |
50 | RepoGroup $repoGroup |
51 | ) { |
52 | parent::__construct( $main, $action ); |
53 | $this->repoGroup = $repoGroup; |
54 | } |
55 | |
56 | public function execute() { |
57 | $this->useTransactionalTimeLimit(); |
58 | |
59 | $this->params = $this->extractRequestParams(); |
60 | // Extract the file and archiveName from the request parameters |
61 | $this->validateParameters(); |
62 | |
63 | // Check whether we're allowed to revert this file |
64 | $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] ); |
65 | |
66 | $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName ); |
67 | $status = $this->file->upload( |
68 | $sourceUrl, |
69 | $this->params['comment'], |
70 | $this->params['comment'], |
71 | 0, |
72 | false, |
73 | false, |
74 | $this->getAuthority() |
75 | ); |
76 | |
77 | if ( $status->isGood() ) { |
78 | $result = [ 'result' => 'Success' ]; |
79 | } else { |
80 | $result = [ |
81 | 'result' => 'Failure', |
82 | 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ), |
83 | ]; |
84 | } |
85 | |
86 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
87 | } |
88 | |
89 | /** |
90 | * Validate the user parameters and set $this->archiveName and $this->file. |
91 | * Throws an error if validation fails |
92 | */ |
93 | protected function validateParameters() { |
94 | // Validate the input title |
95 | $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] ); |
96 | if ( $title === null ) { |
97 | $this->dieWithError( |
98 | [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ] |
99 | ); |
100 | } |
101 | $localRepo = $this->repoGroup->getLocalRepo(); |
102 | |
103 | // Check if the file really exists |
104 | $this->file = $localRepo->newFile( $title ); |
105 | if ( !$this->file->exists() ) { |
106 | $this->dieWithError( 'apierror-missingtitle' ); |
107 | } |
108 | |
109 | // Check if the archivename is valid for this file |
110 | $this->archiveName = $this->params['archivename']; |
111 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141 |
112 | $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName ); |
113 | if ( !$oldFile->exists() ) { |
114 | $this->dieWithError( 'filerevert-badversion' ); |
115 | } |
116 | } |
117 | |
118 | public function mustBePosted() { |
119 | return true; |
120 | } |
121 | |
122 | public function isWriteMode() { |
123 | return true; |
124 | } |
125 | |
126 | public function getAllowedParams() { |
127 | return [ |
128 | 'filename' => [ |
129 | ParamValidator::PARAM_TYPE => 'string', |
130 | ParamValidator::PARAM_REQUIRED => true, |
131 | ], |
132 | 'comment' => [ |
133 | ParamValidator::PARAM_DEFAULT => '', |
134 | ], |
135 | 'archivename' => [ |
136 | ParamValidator::PARAM_TYPE => 'string', |
137 | ParamValidator::PARAM_REQUIRED => true, |
138 | ], |
139 | ]; |
140 | } |
141 | |
142 | public function needsToken() { |
143 | return 'csrf'; |
144 | } |
145 | |
146 | protected function getExamplesMessages() { |
147 | return [ |
148 | 'action=filerevert&filename=Wiki.png&comment=Revert&' . |
149 | 'archivename=20110305152740!Wiki.png&token=123ABC' |
150 | => 'apihelp-filerevert-example-revert', |
151 | ]; |
152 | } |
153 | |
154 | public function getHelpUrls() { |
155 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert'; |
156 | } |
157 | } |