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