r83302 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83301‎ | r83302 | r83303 >
Date:17:23, 5 March 2011
Author:btongminh
Status:resolved (Comments)
Tags:
Comment:
(bug 27018) Added action=filerevert to revert files to an old version. Copied procedure from FileRevertForm, as most of the verification procedure is simple enough to not warrant the effort of writing a dedicated backend.

Further changes:
* Added Status::getErrorsByType() which returns the internal error array untouched
* Added ApiResult::convertStatusToArray() which converts a Status object to something useful for the Api
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/Status.php (modified) (history)
  • /trunk/phase3/includes/api/ApiFileRevert.php (added) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiResult.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Status.php
@@ -287,7 +287,25 @@
288288 }
289289 return $result;
290290 }
 291+
291292 /**
 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+ /**
292310 * Returns true if the specified message is present as a warning or error
293311 *
294312 * @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
1194 + native
Added: svn:keywords
2195 + Id
Index: trunk/phase3/includes/api/ApiResult.php
@@ -338,7 +338,28 @@
339339 global $wgContLang;
340340 $s = $wgContLang->normalize( $s );
341341 }
 342+
342343
 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+
343364 public function execute() {
344365 ApiBase::dieDebug( __METHOD__, 'execute() is not supported on Result object' );
345366 }
Index: trunk/phase3/includes/api/ApiMain.php
@@ -76,6 +76,7 @@
7777 'move' => 'ApiMove',
7878 'edit' => 'ApiEditPage',
7979 'upload' => 'ApiUpload',
 80+ 'filerevert' => 'ApiFileRevert',
8081 'emailuser' => 'ApiEmailUser',
8182 'watch' => 'ApiWatch',
8283 'patrol' => 'ApiPatrol',
Index: trunk/phase3/includes/AutoLoader.php
@@ -275,6 +275,7 @@
276276 'ApiEmailUser' => 'includes/api/ApiEmailUser.php',
277277 'ApiExpandTemplates' => 'includes/api/ApiExpandTemplates.php',
278278 'ApiFeedWatchlist' => 'includes/api/ApiFeedWatchlist.php',
 279+ 'ApiFileRevert' => 'includes/api/ApiFileRevert.php',
279280 'ApiFormatBase' => 'includes/api/ApiFormatBase.php',
280281 'ApiFormatDbg' => 'includes/api/ApiFormatDbg.php',
281282 'ApiFormatDump' => 'includes/api/ApiFormatDump.php',
Index: trunk/phase3/RELEASE-NOTES
@@ -208,6 +208,7 @@
209209 * (bug 27862) Useremail module didn't properly return success on success.
210210 * (bug 27590) prop=imageinfo now allows querying the media type
211211 * (bug 27587) list=filearchive now outputs full title info
 212+(bug 27018) Added action=filerevert to revert files to an old version
212213
213214 === Languages updated in 1.18 ===
214215

Sign-offs

UserFlagDate
Jarry1250tested20:58, 6 March 2011
Reedyinspected23:06, 20 March 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r86086Follow-up r83302: Check permissionsbtongminh21:09, 14 April 2011

Comments

#Comment by Reedy (talk | contribs)   23:05, 20 March 2011

Looks good. Bit of whitespace needs tidying up...

Just need to test it

#Comment by Reedy (talk | contribs)   23:30, 20 March 2011

I'm struggling to get the archive name.... Granted, the aiprop=archivename is bogus (per BZ)

api.php?action=query&titles=File:DSC902459.jpg&prop=imageinfo&iiprop=archivename doesn't give me anything, even when there are deleted images. So I'm slightly confused. Not actually an API bug

Same on enwiki... http://en.wikipedia.org/w/api.php?action=query&titles=File:United_States_(orthographic_projection).svg&prop=imageinfo&iiprop=archivename

And list=filearchive doesn't give the archivename.....

I'm confused about this... And can't actually test it...

' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',

Seems it's the uploaddate!Imagename.ext or something


bug 27610 requests it on filearchive

Am I missing something?

#Comment by Bryan (talk | contribs)   08:19, 21 March 2011

The terminology is confusing, but archivename refers to an oldimage, not to an archivedfile. I.e. http://commons.wikimedia.org/w/api.php?action=query&titles=File:Nick_Reilly_2010.JPG&prop=imageinfo&iiprop=archivename&iilimit=10

#Comment by Bryan (talk | contribs)   08:21, 21 March 2011

Marking fixme: this does not check permissions on the file itself, i.e. Title::userCan()

Status & tagging log