61966e54873d28a2a14b26a8be9320b73660d02a
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
30 class ApiFileRevert
extends ApiBase
{
35 protected $archiveName;
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(
51 $this->params
['comment'],
52 $this->params
['comment'],
59 if ( $status->isGood() ) {
60 $result = array( 'result' => 'Success' );
63 'result' => 'Failure',
64 'errors' => $this->getResult()->convertStatusToArray( $status ),
68 $this->getResult()->addValue( null, $this->getModuleName(), $result );
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] );
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() {
118 public function isWriteMode() {
122 public function getAllowedParams() {
125 ApiBase
::PARAM_TYPE
=> 'string',
126 ApiBase
::PARAM_REQUIRED
=> true,
129 ApiBase
::PARAM_DFLT
=> '',
131 'archivename' => array(
132 ApiBase
::PARAM_TYPE
=> 'string',
133 ApiBase
::PARAM_REQUIRED
=> true,
138 public function needsToken() {
142 protected function getExamplesMessages() {
144 'action=filerevert&filename=Wiki.png&comment=Revert&' .
145 'archivename=20110305152740!Wiki.png&token=123ABC'
146 => 'apihelp-filerevert-example-revert',