Merge "Add SPARQL client to core"
[lhc/web/wiklou.git] / includes / api / ApiFileRevert.php
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 /**
24 * @ingroup API
25 */
26 class ApiFileRevert extends ApiBase {
27 /** @var LocalFile */
28 protected $file;
29
30 /** @var string */
31 protected $archiveName;
32
33 /** @var array */
34 protected $params;
35
36 public function execute() {
37 $this->useTransactionalTimeLimit();
38
39 $this->params = $this->extractRequestParams();
40 // Extract the file and archiveName from the request parameters
41 $this->validateParameters();
42
43 // Check whether we're allowed to revert this file
44 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
45
46 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
47 $status = $this->file->upload(
48 $sourceUrl,
49 $this->params['comment'],
50 $this->params['comment'],
51 0,
52 false,
53 false,
54 $this->getUser()
55 );
56
57 if ( $status->isGood() ) {
58 $result = [ 'result' => 'Success' ];
59 } else {
60 $result = [
61 'result' => 'Failure',
62 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
63 ];
64 }
65
66 $this->getResult()->addValue( null, $this->getModuleName(), $result );
67 }
68
69 /**
70 * Validate the user parameters and set $this->archiveName and $this->file.
71 * Throws an error if validation fails
72 */
73 protected function validateParameters() {
74 // Validate the input title
75 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
76 if ( is_null( $title ) ) {
77 $this->dieWithError(
78 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
79 );
80 }
81 $localRepo = RepoGroup::singleton()->getLocalRepo();
82
83 // Check if the file really exists
84 $this->file = $localRepo->newFile( $title );
85 if ( !$this->file->exists() ) {
86 $this->dieWithError( 'apierror-missingtitle' );
87 }
88
89 // Check if the archivename is valid for this file
90 $this->archiveName = $this->params['archivename'];
91 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
92 if ( !$oldFile->exists() ) {
93 $this->dieWithError( 'filerevert-badversion' );
94 }
95 }
96
97 public function mustBePosted() {
98 return true;
99 }
100
101 public function isWriteMode() {
102 return true;
103 }
104
105 public function getAllowedParams() {
106 return [
107 'filename' => [
108 ApiBase::PARAM_TYPE => 'string',
109 ApiBase::PARAM_REQUIRED => true,
110 ],
111 'comment' => [
112 ApiBase::PARAM_DFLT => '',
113 ],
114 'archivename' => [
115 ApiBase::PARAM_TYPE => 'string',
116 ApiBase::PARAM_REQUIRED => true,
117 ],
118 ];
119 }
120
121 public function needsToken() {
122 return 'csrf';
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=filerevert&filename=Wiki.png&comment=Revert&' .
128 'archivename=20110305152740!Wiki.png&token=123ABC'
129 => 'apihelp-filerevert-example-revert',
130 ];
131 }
132 }