Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / api / ApiFileRevert.php
bloba49397dc81f7d509b7c3e449b83a533ffe52a8d1
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->useTransactionalTimeLimit();
43 $this->params = $this->extractRequestParams();
44 // Extract the file and archiveName from the request parameters
45 $this->validateParameters();
47 // Check whether we're allowed to revert this file
48 $this->checkPermissions( $this->getUser() );
50 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
51 $status = $this->file->upload(
52 $sourceUrl,
53 $this->params['comment'],
54 $this->params['comment'],
56 false,
57 false,
58 $this->getUser()
61 if ( $status->isGood() ) {
62 $result = array( 'result' => 'Success' );
63 } else {
64 $result = array(
65 'result' => 'Failure',
66 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
70 $this->getResult()->addValue( null, $this->getModuleName(), $result );
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.
78 protected function checkPermissions( $user ) {
79 $title = $this->file->getTitle();
80 $permissionErrors = array_merge(
81 $title->getUserPermissionsErrors( 'edit', $user ),
82 $title->getUserPermissionsErrors( 'upload', $user )
85 if ( $permissionErrors ) {
86 $this->dieUsageMsg( $permissionErrors[0] );
90 /**
91 * Validate the user parameters and set $this->archiveName and $this->file.
92 * Throws an error if validation fails
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( array( 'invalidtitle', $this->params['filename'] ) );
100 $localRepo = RepoGroup::singleton()->getLocalRepo();
102 // Check if the file really exists
103 $this->file = $localRepo->newFile( $title );
104 if ( !$this->file->exists() ) {
105 $this->dieUsageMsg( 'notanarticle' );
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' );
116 public function mustBePosted() {
117 return true;
120 public function isWriteMode() {
121 return true;
124 public function getAllowedParams() {
125 return array(
126 'filename' => array(
127 ApiBase::PARAM_TYPE => 'string',
128 ApiBase::PARAM_REQUIRED => true,
130 'comment' => array(
131 ApiBase::PARAM_DFLT => '',
133 'archivename' => array(
134 ApiBase::PARAM_TYPE => 'string',
135 ApiBase::PARAM_REQUIRED => true,
140 public function needsToken() {
141 return 'csrf';
144 protected function getExamplesMessages() {
145 return array(
146 'action=filerevert&filename=Wiki.png&comment=Revert&' .
147 'archivename=20110305152740!Wiki.png&token=123ABC'
148 => 'apihelp-filerevert-example-revert',