Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / api / ApiFileRevert.php
blobf518e172e3427fb24f5b722dee68fb5f12a6c98a
1 <?php
2 /**
5 * Created on March 5, 2011
7 * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
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.
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.
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiFileRevert extends ApiBase {
31 /** @var LocalFile */
32 protected $file;
34 /** @var string */
35 protected $archiveName;
37 /** @var array */
38 protected $params;
40 public function execute() {
41 $this->params = $this->extractRequestParams();
42 // Extract the file and archiveName from the request parameters
43 $this->validateParameters();
45 // Check whether we're allowed to revert this file
46 $this->checkPermissions( $this->getUser() );
48 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
49 $status = $this->file->upload(
50 $sourceUrl,
51 $this->params['comment'],
52 $this->params['comment'],
54 false,
55 false,
56 $this->getUser()
59 if ( $status->isGood() ) {
60 $result = array( 'result' => 'Success' );
61 } else {
62 $result = array(
63 'result' => 'Failure',
64 'errors' => $this->getResult()->convertStatusToArray( $status ),
68 $this->getResult()->addValue( null, $this->getModuleName(), $result );
71 /**
72 * Checks that the user has permissions to perform this revert.
73 * Dies with usage message on inadequate permissions.
74 * @param User $user The user to check.
76 protected function checkPermissions( $user ) {
77 $title = $this->file->getTitle();
78 $permissionErrors = array_merge(
79 $title->getUserPermissionsErrors( 'edit', $user ),
80 $title->getUserPermissionsErrors( 'upload', $user )
83 if ( $permissionErrors ) {
84 $this->dieUsageMsg( $permissionErrors[0] );
88 /**
89 * Validate the user parameters and set $this->archiveName and $this->file.
90 * Throws an error if validation fails
92 protected function validateParameters() {
93 // Validate the input title
94 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
95 if ( is_null( $title ) ) {
96 $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
98 $localRepo = RepoGroup::singleton()->getLocalRepo();
100 // Check if the file really exists
101 $this->file = $localRepo->newFile( $title );
102 if ( !$this->file->exists() ) {
103 $this->dieUsageMsg( 'notanarticle' );
106 // Check if the archivename is valid for this file
107 $this->archiveName = $this->params['archivename'];
108 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
109 if ( !$oldFile->exists() ) {
110 $this->dieUsageMsg( 'filerevert-badversion' );
114 public function mustBePosted() {
115 return true;
118 public function isWriteMode() {
119 return true;
122 public function getAllowedParams() {
123 return array(
124 'filename' => array(
125 ApiBase::PARAM_TYPE => 'string',
126 ApiBase::PARAM_REQUIRED => true,
128 'comment' => array(
129 ApiBase::PARAM_DFLT => '',
131 'archivename' => array(
132 ApiBase::PARAM_TYPE => 'string',
133 ApiBase::PARAM_REQUIRED => true,
138 public function getParamDescription() {
139 return array(
140 'filename' => 'Target filename without the File: prefix',
141 'comment' => 'Upload comment',
142 'archivename' => 'Archive name of the revision to revert to',
146 public function getDescription() {
147 return array(
148 'Revert a file to an old version.'
152 public function needsToken() {
153 return 'csrf';
156 public function getExamples() {
157 return array(
158 'api.php?action=filerevert&filename=Wiki.png&comment=Revert&' .
159 'archivename=20110305152740!Wiki.png&token=123ABC'
160 => 'Revert Wiki.png to the version of 20110305152740',