Index: trunk/phase3/includes/Status.php |
— | — | @@ -287,7 +287,25 @@ |
288 | 288 | } |
289 | 289 | return $result; |
290 | 290 | } |
| 291 | + |
291 | 292 | /** |
| 293 | + * Returns a list of status messages of the given type, with message and |
| 294 | + * params left untouched, like a sane version of getStatusArray |
| 295 | + * |
| 296 | + * @param $type String |
| 297 | + * |
| 298 | + * @return Array |
| 299 | + */ |
| 300 | + public function getErrorsByType( $type ) { |
| 301 | + $result = array(); |
| 302 | + foreach ( $this->errors as $error ) { |
| 303 | + if ( $error['type'] === $type ) { |
| 304 | + $result[] = $error; |
| 305 | + } |
| 306 | + } |
| 307 | + return $result; |
| 308 | + } |
| 309 | + /** |
292 | 310 | * Returns true if the specified message is present as a warning or error |
293 | 311 | * |
294 | 312 | * @param $msg String: message name |
Index: trunk/phase3/includes/api/ApiFileRevert.php |
— | — | @@ -0,0 +1,192 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * |
| 5 | + * |
| 6 | + * Created on March 5, 2011 |
| 7 | + * |
| 8 | + * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com> |
| 9 | + * |
| 10 | + * This program is free software; you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation; either version 2 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License along |
| 21 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 23 | + * http://www.gnu.org/copyleft/gpl.html |
| 24 | + * |
| 25 | + * @file |
| 26 | + */ |
| 27 | + |
| 28 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 29 | + // Eclipse helper - will be ignored in production |
| 30 | + require_once( "ApiBase.php" ); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @ingroup API |
| 35 | + */ |
| 36 | +class ApiFileRevert extends ApiBase { |
| 37 | + |
| 38 | + /** |
| 39 | + * @var File |
| 40 | + */ |
| 41 | + protected $file; |
| 42 | + protected $archiveName; |
| 43 | + |
| 44 | + protected $params; |
| 45 | + |
| 46 | + public function __construct( $main, $action ) { |
| 47 | + parent::__construct( $main, $action ); |
| 48 | + } |
| 49 | + |
| 50 | + public function execute() { |
| 51 | + global $wgUser; |
| 52 | + |
| 53 | + // First check permission to upload/revert |
| 54 | + $this->checkPermissions( $wgUser ); |
| 55 | + |
| 56 | + $this->params = $this->extractRequestParams(); |
| 57 | + $this->validateParameters(); |
| 58 | + |
| 59 | + |
| 60 | + $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName ); |
| 61 | + $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'] ); |
| 62 | + |
| 63 | + if ( $status->isGood() ) { |
| 64 | + $result = array( 'result' => 'Success' ); |
| 65 | + } else { |
| 66 | + $result = array( |
| 67 | + 'result' => 'Failure', |
| 68 | + 'errors' => $this->getResult()->convertStatusToArray( $status ), |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks that the user has permissions to perform this revert. |
| 78 | + * Dies with usage message on inadequate permissions. |
| 79 | + * @param $user User The user to check. |
| 80 | + */ |
| 81 | + protected function checkPermissions( $user ) { |
| 82 | + $permission = $user->isAllowed( 'edit' ) && $user->isAllowed( 'upload' ); |
| 83 | + |
| 84 | + if ( $permission !== true ) { |
| 85 | + if ( !$user->isLoggedIn() ) { |
| 86 | + $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) ); |
| 87 | + } else { |
| 88 | + $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Validate the user parameters and set $this->archiveName and $this->file. |
| 95 | + * Throws an error if validation fails |
| 96 | + */ |
| 97 | + protected function validateParameters() { |
| 98 | + // Validate the input title |
| 99 | + $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] ); |
| 100 | + if ( is_null( $title ) ) { |
| 101 | + $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) ); |
| 102 | + } |
| 103 | + // Check if the file really exists |
| 104 | + $this->file = wfLocalFile( $title ); |
| 105 | + if ( !$this->file->exists() ) { |
| 106 | + $this->dieUsageMsg( array( 'notanarticle' ) ); |
| 107 | + } |
| 108 | + |
| 109 | + // Check if the archivename is valid for this file |
| 110 | + $this->archiveName = $this->params['archivename']; |
| 111 | + $oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName ); |
| 112 | + if ( !$oldFile->exists() ) { |
| 113 | + $this->dieUsageMsg( array( 'filerevert-badversion' ) ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + public function mustBePosted() { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + public function isWriteMode() { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + public function getAllowedParams() { |
| 128 | + return array( |
| 129 | + 'filename' => array( |
| 130 | + ApiBase::PARAM_TYPE => 'string', |
| 131 | + ApiBase::PARAM_REQUIRED => true, |
| 132 | + ), |
| 133 | + 'comment' => array( |
| 134 | + ApiBase::PARAM_DFLT => '', |
| 135 | + ), |
| 136 | + 'archivename' => array( |
| 137 | + ApiBase::PARAM_TYPE => 'string', |
| 138 | + ApiBase::PARAM_REQUIRED => true, |
| 139 | + ), |
| 140 | + 'token' => null, |
| 141 | + ); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + public function getParamDescription() { |
| 146 | + $params = array( |
| 147 | + 'filename' => 'Target filename', |
| 148 | + 'token' => 'Edit token. You can get one of these through prop=info', |
| 149 | + 'comment' => 'Upload comment', |
| 150 | + 'archivename' => 'Archive name of the revision to revert to', |
| 151 | + ); |
| 152 | + |
| 153 | + return $params; |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + public function getDescription() { |
| 158 | + return array( |
| 159 | + 'Revert a file to an old version' |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + public function getPossibleErrors() { |
| 164 | + return array_merge( parent::getPossibleErrors(), |
| 165 | + array( |
| 166 | + array( 'mustbeloggedin', 'upload' ), |
| 167 | + array( 'badaccess-groups' ), |
| 168 | + array( 'invalidtitle', 'title' ), |
| 169 | + array( 'notanarticle' ), |
| 170 | + array( 'filerevert-badversion' ), |
| 171 | + ) |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + public function needsToken() { |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + public function getTokenSalt() { |
| 180 | + return ''; |
| 181 | + } |
| 182 | + |
| 183 | + protected function getExamples() { |
| 184 | + return array( |
| 185 | + 'Revert Wiki.png to the version of 20110305152740:', |
| 186 | + ' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\', |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + public function getVersion() { |
| 191 | + return __CLASS__ . ': $Id$'; |
| 192 | + } |
| 193 | +} |
Property changes on: trunk/phase3/includes/api/ApiFileRevert.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 194 | + native |
Added: svn:keywords |
2 | 195 | + Id |
Index: trunk/phase3/includes/api/ApiResult.php |
— | — | @@ -338,7 +338,28 @@ |
339 | 339 | global $wgContLang; |
340 | 340 | $s = $wgContLang->normalize( $s ); |
341 | 341 | } |
| 342 | + |
342 | 343 | |
| 344 | + /** |
| 345 | + * Converts a Status object to an array suitable for addValue |
| 346 | + * @param Status $status |
| 347 | + * @param string $errorType |
| 348 | + * @return array |
| 349 | + */ |
| 350 | + public function convertStatusToArray( $status, $errorType = 'error' ) { |
| 351 | + if ( $status->isGood() ) { |
| 352 | + return array(); |
| 353 | + } |
| 354 | + |
| 355 | + $result = array(); |
| 356 | + foreach ( $status->getErrorsByType( $errorType ) as $error ) { |
| 357 | + $this->setIndexedTagName( $error['params'], 'param' ); |
| 358 | + $result[] = $error; |
| 359 | + } |
| 360 | + $this->setIndexedTagName( $result, $errorType ); |
| 361 | + return $result; |
| 362 | + } |
| 363 | + |
343 | 364 | public function execute() { |
344 | 365 | ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' ); |
345 | 366 | } |
Index: trunk/phase3/includes/api/ApiMain.php |
— | — | @@ -76,6 +76,7 @@ |
77 | 77 | 'move' => 'ApiMove', |
78 | 78 | 'edit' => 'ApiEditPage', |
79 | 79 | 'upload' => 'ApiUpload', |
| 80 | + 'filerevert' => 'ApiFileRevert', |
80 | 81 | 'emailuser' => 'ApiEmailUser', |
81 | 82 | 'watch' => 'ApiWatch', |
82 | 83 | 'patrol' => 'ApiPatrol', |
Index: trunk/phase3/includes/AutoLoader.php |
— | — | @@ -275,6 +275,7 @@ |
276 | 276 | 'ApiEmailUser' => 'includes/api/ApiEmailUser.php', |
277 | 277 | 'ApiExpandTemplates' => 'includes/api/ApiExpandTemplates.php', |
278 | 278 | 'ApiFeedWatchlist' => 'includes/api/ApiFeedWatchlist.php', |
| 279 | + 'ApiFileRevert' => 'includes/api/ApiFileRevert.php', |
279 | 280 | 'ApiFormatBase' => 'includes/api/ApiFormatBase.php', |
280 | 281 | 'ApiFormatDbg' => 'includes/api/ApiFormatDbg.php', |
281 | 282 | 'ApiFormatDump' => 'includes/api/ApiFormatDump.php', |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -208,6 +208,7 @@ |
209 | 209 | * (bug 27862) Useremail module didn't properly return success on success. |
210 | 210 | * (bug 27590) prop=imageinfo now allows querying the media type |
211 | 211 | * (bug 27587) list=filearchive now outputs full title info |
| 212 | +(bug 27018) Added action=filerevert to revert files to an old version |
212 | 213 | |
213 | 214 | === Languages updated in 1.18 === |
214 | 215 | |