2011-03-05 17:23:35 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Created on March 5, 2011
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @ingroup API
|
|
|
|
|
*/
|
|
|
|
|
class ApiFileRevert extends ApiBase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var File
|
|
|
|
|
*/
|
|
|
|
|
protected $file;
|
|
|
|
|
protected $archiveName;
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
protected $params;
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->params = $this->extractRequestParams();
|
2011-04-14 21:09:16 +00:00
|
|
|
// Extract the file and archiveName from the request parameters
|
2011-03-05 17:23:35 +00:00
|
|
|
$this->validateParameters();
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-04-14 21:09:16 +00:00
|
|
|
// Check whether we're allowed to revert this file
|
2011-10-26 23:27:01 +00:00
|
|
|
$this->checkPermissions( $this->getUser() );
|
2011-07-17 16:38:24 +00:00
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
$sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
|
2012-10-06 09:54:50 +00:00
|
|
|
$status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'], 0, false, false, $this->getUser() );
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
if ( $status->isGood() ) {
|
|
|
|
|
$result = array( 'result' => 'Success' );
|
|
|
|
|
} else {
|
2011-07-17 16:38:24 +00:00
|
|
|
$result = array(
|
|
|
|
|
'result' => 'Failure',
|
2011-03-05 17:23:35 +00:00
|
|
|
'errors' => $this->getResult()->convertStatusToArray( $status ),
|
|
|
|
|
);
|
|
|
|
|
}
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $result );
|
2011-03-05 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks that the user has permissions to perform this revert.
|
|
|
|
|
* Dies with usage message on inadequate permissions.
|
|
|
|
|
* @param $user User The user to check.
|
|
|
|
|
*/
|
|
|
|
|
protected function checkPermissions( $user ) {
|
2012-08-04 13:40:36 +00:00
|
|
|
$title = $this->file->getTitle();
|
2011-04-14 21:09:16 +00:00
|
|
|
$permissionErrors = array_merge(
|
2013-02-03 18:47:42 +00:00
|
|
|
$title->getUserPermissionsErrors( 'edit', $user ),
|
|
|
|
|
$title->getUserPermissionsErrors( 'upload', $user )
|
2011-04-14 21:09:16 +00:00
|
|
|
);
|
2011-03-05 17:23:35 +00:00
|
|
|
|
2011-04-14 21:09:16 +00:00
|
|
|
if ( $permissionErrors ) {
|
|
|
|
|
$this->dieUsageMsg( $permissionErrors[0] );
|
2011-03-05 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
/**
|
|
|
|
|
* Validate the user parameters and set $this->archiveName and $this->file.
|
|
|
|
|
* Throws an error if validation fails
|
|
|
|
|
*/
|
|
|
|
|
protected function validateParameters() {
|
|
|
|
|
// Validate the input title
|
|
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
|
|
|
|
|
if ( is_null( $title ) ) {
|
|
|
|
|
$this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
|
|
|
|
|
}
|
2012-08-04 13:40:36 +00:00
|
|
|
$localRepo = RepoGroup::singleton()->getLocalRepo();
|
|
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
// Check if the file really exists
|
2012-08-04 13:40:36 +00:00
|
|
|
$this->file = $localRepo->newFile( $title );
|
2011-03-05 17:23:35 +00:00
|
|
|
if ( !$this->file->exists() ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'notanarticle' );
|
2011-03-05 17:23:35 +00:00
|
|
|
}
|
2011-03-07 14:59:41 +00:00
|
|
|
|
2011-03-05 17:23:35 +00:00
|
|
|
// Check if the archivename is valid for this file
|
|
|
|
|
$this->archiveName = $this->params['archivename'];
|
2012-08-04 13:40:36 +00:00
|
|
|
$oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
|
2011-03-05 17:23:35 +00:00
|
|
|
if ( !$oldFile->exists() ) {
|
2011-05-19 17:51:16 +00:00
|
|
|
$this->dieUsageMsg( 'filerevert-badversion' );
|
2011-03-05 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
|
return array(
|
|
|
|
|
'filename' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
|
),
|
|
|
|
|
'comment' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => '',
|
|
|
|
|
),
|
|
|
|
|
'archivename' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
2011-07-17 16:38:24 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2011-03-05 17:23:35 +00:00
|
|
|
),
|
2012-07-18 17:24:38 +00:00
|
|
|
'token' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
|
ApiBase::PARAM_REQUIRED => true
|
|
|
|
|
),
|
2011-03-05 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getParamDescription() {
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
return array(
|
2012-05-01 16:54:14 +00:00
|
|
|
'filename' => 'Target filename without the File: prefix',
|
2011-03-05 17:23:35 +00:00
|
|
|
'token' => 'Edit token. You can get one of these through prop=info',
|
|
|
|
|
'comment' => 'Upload comment',
|
|
|
|
|
'archivename' => 'Archive name of the revision to revert to',
|
|
|
|
|
);
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
}
|
2011-03-05 17:23:35 +00:00
|
|
|
|
Added result properties to action=paraminfo
Added information about the properties of the results of API calls
to action=paraminfo, including information about "property groups":
what should the prop parameter be set to to get that property.
Uses the same format for types as parameters already do.
The output format of some modules doesn't fit this, so the result
properties for them weren't added, or only partially.
Partially implemented modules:
* expandtemplates:
parsetree is in its own tag
* protect, allusers, backlinks, deletedrevs, info, imageinfo,
logevents, querypage, recentchanges, revisions, searchinfo,
usercontribs, userinfo, users, watchlist, upload:
response with partially complex structure
Not implemented modules:
* feedcontributions, feedwatchlist, opensearch, rds:
non-standard reponse
* help:
error is normal response; not very useful for automated tools anyway
* paraminfo, parse, pageprops, siteinfo, userrights:
response with complex structure
Change-Id: Iff2a9bef79f994e73eef3062b4dd5461bff968ab
2012-05-02 15:00:30 +00:00
|
|
|
public function getResultProperties() {
|
|
|
|
|
return array(
|
|
|
|
|
'' => array(
|
|
|
|
|
'result' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => array(
|
|
|
|
|
'Success',
|
|
|
|
|
'Failure'
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
'errors' => array(
|
|
|
|
|
ApiBase::PROP_TYPE => 'string',
|
|
|
|
|
ApiBase::PROP_NULLABLE => true
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
2011-03-05 17:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getDescription() {
|
|
|
|
|
return array(
|
|
|
|
|
'Revert a file to an old version'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(),
|
|
|
|
|
array(
|
|
|
|
|
array( 'mustbeloggedin', 'upload' ),
|
|
|
|
|
array( 'badaccess-groups' ),
|
|
|
|
|
array( 'invalidtitle', 'title' ),
|
|
|
|
|
array( 'notanarticle' ),
|
|
|
|
|
array( 'filerevert-badversion' ),
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function needsToken() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTokenSalt() {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2011-03-05 17:23:35 +00:00
|
|
|
return array(
|
2013-03-08 02:09:01 +00:00
|
|
|
'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=123ABC'
|
2011-12-27 16:22:35 +00:00
|
|
|
=> 'Revert Wiki.png to the version of 20110305152740',
|
2011-03-05 17:23:35 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|