]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiFileRevert.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / api / ApiFileRevert.php
1 <?php
2 /**
3  *
4  *
5  * Created on March 5, 2011
6  *
7  * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 /**
28  * @ingroup API
29  */
30 class ApiFileRevert extends ApiBase {
31         /** @var LocalFile */
32         protected $file;
33
34         /** @var string */
35         protected $archiveName;
36
37         /** @var array */
38         protected $params;
39
40         public function execute() {
41                 $this->useTransactionalTimeLimit();
42
43                 $this->params = $this->extractRequestParams();
44                 // Extract the file and archiveName from the request parameters
45                 $this->validateParameters();
46
47                 // Check whether we're allowed to revert this file
48                 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
49
50                 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
51                 $status = $this->file->upload(
52                         $sourceUrl,
53                         $this->params['comment'],
54                         $this->params['comment'],
55                         0,
56                         false,
57                         false,
58                         $this->getUser()
59                 );
60
61                 if ( $status->isGood() ) {
62                         $result = [ 'result' => 'Success' ];
63                 } else {
64                         $result = [
65                                 'result' => 'Failure',
66                                 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
67                         ];
68                 }
69
70                 $this->getResult()->addValue( null, $this->getModuleName(), $result );
71         }
72
73         /**
74          * Validate the user parameters and set $this->archiveName and $this->file.
75          * Throws an error if validation fails
76          */
77         protected function validateParameters() {
78                 // Validate the input title
79                 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
80                 if ( is_null( $title ) ) {
81                         $this->dieWithError(
82                                 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
83                         );
84                 }
85                 $localRepo = RepoGroup::singleton()->getLocalRepo();
86
87                 // Check if the file really exists
88                 $this->file = $localRepo->newFile( $title );
89                 if ( !$this->file->exists() ) {
90                         $this->dieWithError( 'apierror-missingtitle' );
91                 }
92
93                 // Check if the archivename is valid for this file
94                 $this->archiveName = $this->params['archivename'];
95                 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
96                 if ( !$oldFile->exists() ) {
97                         $this->dieWithError( 'filerevert-badversion' );
98                 }
99         }
100
101         public function mustBePosted() {
102                 return true;
103         }
104
105         public function isWriteMode() {
106                 return true;
107         }
108
109         public function getAllowedParams() {
110                 return [
111                         'filename' => [
112                                 ApiBase::PARAM_TYPE => 'string',
113                                 ApiBase::PARAM_REQUIRED => true,
114                         ],
115                         'comment' => [
116                                 ApiBase::PARAM_DFLT => '',
117                         ],
118                         'archivename' => [
119                                 ApiBase::PARAM_TYPE => 'string',
120                                 ApiBase::PARAM_REQUIRED => true,
121                         ],
122                 ];
123         }
124
125         public function needsToken() {
126                 return 'csrf';
127         }
128
129         protected function getExamplesMessages() {
130                 return [
131                         'action=filerevert&filename=Wiki.png&comment=Revert&' .
132                                 'archivename=20110305152740!Wiki.png&token=123ABC'
133                                 => 'apihelp-filerevert-example-revert',
134                 ];
135         }
136 }