Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.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->checkPermissions( $this->getUser() );
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 * Checks that the user has permissions to perform this revert.
75 * Dies with usage message on inadequate permissions.
76 * @param User $user The user to check.
77 */
78 protected function checkPermissions( $user ) {
79 $title = $this->file->getTitle();
80 $permissionErrors = array_merge(
81 $title->getUserPermissionsErrors( 'edit', $user ),
82 $title->getUserPermissionsErrors( 'upload', $user )
83 );
84
85 if ( $permissionErrors ) {
86 $this->dieUsageMsg( $permissionErrors[0] );
87 }
88 }
89
90 /**
91 * Validate the user parameters and set $this->archiveName and $this->file.
92 * Throws an error if validation fails
93 */
94 protected function validateParameters() {
95 // Validate the input title
96 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
97 if ( is_null( $title ) ) {
98 $this->dieUsageMsg( [ 'invalidtitle', $this->params['filename'] ] );
99 }
100 $localRepo = RepoGroup::singleton()->getLocalRepo();
101
102 // Check if the file really exists
103 $this->file = $localRepo->newFile( $title );
104 if ( !$this->file->exists() ) {
105 $this->dieUsageMsg( 'notanarticle' );
106 }
107
108 // Check if the archivename is valid for this file
109 $this->archiveName = $this->params['archivename'];
110 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
111 if ( !$oldFile->exists() ) {
112 $this->dieUsageMsg( 'filerevert-badversion' );
113 }
114 }
115
116 public function mustBePosted() {
117 return true;
118 }
119
120 public function isWriteMode() {
121 return true;
122 }
123
124 public function getAllowedParams() {
125 return [
126 'filename' => [
127 ApiBase::PARAM_TYPE => 'string',
128 ApiBase::PARAM_REQUIRED => true,
129 ],
130 'comment' => [
131 ApiBase::PARAM_DFLT => '',
132 ],
133 'archivename' => [
134 ApiBase::PARAM_TYPE => 'string',
135 ApiBase::PARAM_REQUIRED => true,
136 ],
137 ];
138 }
139
140 public function needsToken() {
141 return 'csrf';
142 }
143
144 protected function getExamplesMessages() {
145 return [
146 'action=filerevert&filename=Wiki.png&comment=Revert&' .
147 'archivename=20110305152740!Wiki.png&token=123ABC'
148 => 'apihelp-filerevert-example-revert',
149 ];
150 }
151 }