Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/49853
Change subject: (bug 45099) Return full error report from API.
......................................................................
(bug 45099) Return full error report from API.
This generalizes entity saving and error handling in the API.
By this, consistency is improved, redundant code is reduced,
and error reporting is greatly improved.
NOTE: requires core patch I8ee9da400 to work.
Change-Id: I4094feee1e6166171fd15ffdf7249b3c0d27ce1c
---
M repo/includes/EditEntity.php
M repo/includes/api/Api.php
M repo/includes/api/ApiGetEntities.php
M repo/includes/api/ApiLinkTitles.php
M repo/includes/api/ApiModifyClaim.php
M repo/includes/api/ApiModifyEntity.php
M repo/includes/api/ApiSetReference.php
M repo/includes/api/RemoveQualifiers.php
M repo/includes/api/RemoveReferences.php
M repo/includes/api/SetClaim.php
M repo/includes/api/SetQualifier.php
M repo/includes/api/SetStatementRank.php
M repo/tests/phpunit/includes/api/SetQualifierTest.php
13 files changed, 276 insertions(+), 230 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/53/49853/1
diff --git a/repo/includes/EditEntity.php b/repo/includes/EditEntity.php
index e8965ff..d47de72 100644
--- a/repo/includes/EditEntity.php
+++ b/repo/includes/EditEntity.php
@@ -867,41 +867,4 @@
wfProfileOut( __METHOD__ );
return true;
}
-
- /**
- * Die with an error corresponding to any errors that occurred during
attemptSave(), if any.
- * Intended for use in API modules.
- *
- * If there is no error but there are warnings, they are added to the
API module's result.
- *
- * @param \ApiBase $api the API module to report the error for.
- * @param String $errorCode string Brief, arbitrary, stable string
to allow easy
- * automated identification of the error,
e.g., 'unknown_action'
- * @param int $httpRespCode int HTTP response code
- * @param array $extradata array Data to add to the "<error>"
element; array in ApiResult format
- */
- public function reportApiErrors( \ApiBase $api, $errorCode,
$httpRespCode = 0, $extradata = null ) {
- wfProfileIn( __METHOD__ );
- if ( $this->status === null ) {
- wfProfileOut( __METHOD__ );
- return;
- }
-
- // report all warnings
- // XXX: also report all errors, in sequence, here, before
failing on the error?
- $errors = $this->status->getErrorsByType( 'warning' );
- if ( is_array($errors) && $errors !== array() ) {
- $path = array( null, 'warnings' );
- $api->getResult()->addValue( null, 'warnings', $errors
);
- $api->getResult()->setIndexedTagName( $path, 'warning'
);
- }
-
- if ( !$this->status->isOK() ) {
- $description = $this->status->getWikiText(
'wikibase-api-cant-edit', 'wikibase-api-cant-edit' );
- wfProfileOut( __METHOD__ );
- $api->dieUsage( $description, $errorCode,
$httpRespCode, $extradata );
- }
-
- wfProfileOut( __METHOD__ );
- }
}
diff --git a/repo/includes/api/Api.php b/repo/includes/api/Api.php
index 8339ae8..007ab12 100644
--- a/repo/includes/api/Api.php
+++ b/repo/includes/api/Api.php
@@ -18,6 +18,20 @@
abstract class Api extends \ApiBase {
/**
+ * No wrapper for single error messages
+ *
+ * @var bool
+ */
+ protected static $shortErrorConextMessage = false;
+
+ /**
+ * Default wrapper for single error messages
+ *
+ * @var bool
+ */
+ protected static $longErrorConextMessage = false;
+
+ /**
* Figure out the instance-specific usekeys-state
*
* @deprecated
@@ -397,4 +411,177 @@
return $content;
}
+
+ /**
+ * Handle a status object. If $status->isOK() returns false, this
method will terminate via
+ * the a call to $this->dieUsage(). Details from the Status object will
be included in the
+ * API call's output.
+ *
+ * @param \Status $status
+ * @param string $errorCode
+ * @param array $extradata
+ * @param int $httpRespCode
+ */
+ protected function handleStatus( Status $status, $errorCode, array
$extradata = array(), $httpRespCode = 0 ) {
+ wfProfileIn( __METHOD__ );
+
+ $res = $this->getResult();
+ $isError = ( !$status->isOK() || $httpRespCode >= 400 );
+
+ // report all warnings and errors
+ if ( $status->isGood() ) {
+ $description = null;
+ } else {
+ $description = $status->getWikiText(
self::$shortErrorConextMessage, self::$longErrorConextMessage );
+ }
+
+ $errors = $status->getErrorsByType( $isError ? 'error' :
'warning' );
+ $messages = $this->compileStatusReport( $errors );
+
+ if ( $messages ) {
+ //NOTE: this doesn't work:
+ //$html = $status->getHTML(
self::$shortErrorConextMessage, self::$longErrorConextMessage );
+ $html = $this->messageToHtml( $description );
+ $res->setContent( $messages, $html, 'html' );
+ }
+
+ if ( $isError ) {
+ $res->setElement( $extradata, 'messages', $messages );
+
+ wfProfileOut( __METHOD__ );
+ $this->dieUsage( $description, $errorCode,
$httpRespCode, $extradata );
+ } elseif ( $messages ) {
+ $res->disableSizeCheck();
+ $res->addValue( array( 'warnings' ), 'messages',
$messages, true );
+ $res->enableSizeCheck();
+
+ wfProfileOut( __METHOD__ );
+ }
+ }
+
+ protected function messageToHtml( $text ) {
+ if ( $text === null || $text === false || $text === '' ) {
+ return $text;
+ }
+
+ $out = \MessageCache::singleton()->parse( $text,
$this->getContext()->getTitle(), /*linestart*/true,
+ /*interface*/true, $this->getContext()->getLanguage() );
+
+ return $out->getText();
+ }
+
+ /**
+ * Utility method for adding a list of messages to the result object.
+ * Useful for error reporting.
+ *
+ * @param $errors array a list of errors, as returned by
Status::getErrorsByType()
+ * @param $messages array an result structure to add to (optional)
+ *
+ * @return array a result structure containing the given messages.
+ */
+ protected function compileStatusReport( $errors, $messages = array() ) {
+ if ( !is_array($errors) || $errors === array() ) {
+ return $messages;
+ }
+
+ $res = $this->getResult();
+
+ foreach ( $errors as $m ) {
+ $type = null;
+ $name = null;
+ $params = null;
+
+ if ( is_string( $m ) ) {
+ // it's a plain string containing a message key
+ $name = $m;
+ } elseif ( is_array( $m ) ) {
+ if ( isset( $m[0]) ) {
+ // it's an indexed array, the first
entriy is the message key, the rest are paramters
+ $name = $m[0];
+ $params = array_slice( $m, 1 );
+ } else{
+ // it's an assoc array, find message
key and params in fields.
+ $type = isset( $m['type'] ) ?
$m['type'] : null;
+ $params = isset( $m['params'] ) ?
$m['params'] : null;
+
+ if( isset( $m['message'] ) ) {
+ if ( $m['message'] instanceof
\Message ) {
+ // message object,
handle below
+ $m = $m['message']; //
NOTE: this triggers the "$m is an object" case below!
+ } else {
+ // plain key and param
list
+ $name = strval(
$m['message'] );
+ }
+ }
+ }
+ }
+
+ if ( $m instanceof \Message ) { //NOTE: no elsif, since
$m can be manipulated
+ // a message object
+
+ $name = $m->getKey();
+ $params = $m->getParams();
+ }
+
+ if ( $name !== null ) {
+ // got at least a name
+
+ $row = array();
+
+ $res->setElement( $row, 'name', $name );
+
+ if ( $type !== null ) {
+ $res->setElement( $row, 'type', $type );
+ }
+
+ if ( $params !== null && !empty( $params ) ) {
+ $res->setElement( $row, 'parameters',
$params );
+ $res->setIndexedTagName(
$row['parameters'], 'parameter' );
+ }
+
+ $messages[] = $row;
+ }
+ }
+
+ $res->setIndexedTagName( $messages, 'message' );
+ return $messages;
+ }
+
+ protected function attemptSaveEntity( EntityContent $content, $summary,
$flags = 0 ) {
+ $params = $this->extractRequestParams();
+ $user = $this->getUser();
+
+ $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
+
+ $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
+ $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
+
+ $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+
+ if ( !$this->needsToken() ) {
+ // false disabled the token check
+ $token = false;
+ } else {
+ // null fails the token check
+ $token = isset( $params['token'] ) ? $params['token'] :
null;
+ }
+
+ $status = $editEntity->attemptSave(
+ $summary,
+ $flags,
+ $token
+ );
+
+ if ( $editEntity->hasError( EditEntity::TOKEN_ERROR ) ) {
+ $this->handleStatus( $status, 'session-failure' );
+ }
+ elseif ( $editEntity->hasError( EditEntity::EDIT_CONFLICT_ERROR
) ) {
+ $this->handleStatus( $status, 'edit-conflict' );
+ }
+ else {
+ $this->handleStatus( $status, 'save-failed' );
+ }
+
+ return $editEntity;
+ }
}
diff --git a/repo/includes/api/ApiGetEntities.php
b/repo/includes/api/ApiGetEntities.php
index 535a38a..5ab6acc 100644
--- a/repo/includes/api/ApiGetEntities.php
+++ b/repo/includes/api/ApiGetEntities.php
@@ -27,6 +27,25 @@
$params = $this->extractRequestParams();
+ $status = \Status::newGood();
+
+ $status->warning( 'wikibase-api-create-failed', 'abcde' );
+ $status->warning( wfMessage( 'wikibase-api-save-failed'
)->params( "q1234" ) );
+
+ $t = isset( $params['fail'] ) ? $params['fail'] : '';
+
+ switch ( $t ) {
+ case "w":
+ $this->getResult()->setWarning( "foo" );
+ $this->handleStatus( $status, 'testing' );
+ return;
+ case "e":
+ $this->getResult()->setWarning( "foo" );
+ $status->fatal( wfMessage(
'wikibase-api-illegal-field' )->params( "xxx" ) );
+ $this->handleStatus( $status, 'testing' );
+ return;
+ }
+
if ( !( isset( $params['ids'] ) XOR ( isset( $params['sites'] )
&& isset( $params['titles'] ) ) ) ) {
wfProfileOut( __METHOD__ );
$this->dieUsage( $this->msg(
'wikibase-api-ids-xor-wikititles' )->text(), 'id-xor-wikititle' );
@@ -201,6 +220,9 @@
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_ISMULTI => true,
),
+ 'fail' => array(
+ ApiBase::PARAM_TYPE => 'string',
+ ),
'sites' => array(
ApiBase::PARAM_TYPE =>
$this->getSiteLinkTargetSites()->getGlobalIdentifiers(),
ApiBase::PARAM_ISMULTI => true,
diff --git a/repo/includes/api/ApiLinkTitles.php
b/repo/includes/api/ApiLinkTitles.php
index 88f2eee..9e3c40e 100644
--- a/repo/includes/api/ApiLinkTitles.php
+++ b/repo/includes/api/ApiLinkTitles.php
@@ -136,30 +136,16 @@
$this->addSiteLinksToResult( $return, 'entity' );
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
-
if ( $itemContent === null ) {
// to not have an ItemContent isn't really bad at this
point
$status = Status::newGood( true );
}
else {
// Do the actual save, or if it don't exist yet create
it.
- $editEntity = new EditEntity( $itemContent, $user,
false, $this->getContext() );
- $status = $editEntity->attemptSave(
- Autocomment::buildApiSummary( $this, $params,
$itemContent ),
- $flags,
- ( $this->needsToken() ? $params['token'] : '' )
- );
-
- if ( $editEntity->hasError( EditEntity::TOKEN_ERROR ) )
{
- $editEntity->reportApiErrors( $this,
'session-failure' );
- }
- elseif ( $editEntity->hasError(
EditEntity::EDIT_CONFLICT_ERROR ) ) {
- $editEntity->reportApiErrors( $this,
'edit-conflict' );
- }
- elseif ( $editEntity->hasError() ) {
- $editEntity->reportApiErrors( $this,
'save-failed' );
- }
+ $summary = Autocomment::buildApiSummary( $this,
$params, $itemContent );
+ $editEntity = $this->attemptSaveEntity( $itemContent,
+ $summary,
+ $flags );
$revision = $editEntity->getNewRevision();
if ( $revision ) {
@@ -168,6 +154,8 @@
'lastrevid', intval( $revision->getId()
)
);
}
+
+ $status = $editEntity->getStatus();
}
if ( $itemContent !== null ) {
diff --git a/repo/includes/api/ApiModifyClaim.php
b/repo/includes/api/ApiModifyClaim.php
index 1ed6fd5..54bad46 100644
--- a/repo/includes/api/ApiModifyClaim.php
+++ b/repo/includes/api/ApiModifyClaim.php
@@ -48,32 +48,17 @@
$summary = Autocomment::buildApiSummary( $this,
$params, $content );
}
- $user = $this->getUser();
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
-
- $flags = EDIT_UPDATE;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
-
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
-
- $status = $editEntity->attemptSave(
+ $editEntity = $this->attemptSaveEntity( $content,
$summary,
- $flags,
- isset( $params['token'] ) ? $params['token'] : ''
- );
+ EDIT_UPDATE );
- if ( !$status->isOK() ) {
- $this->dieUsage( $status->getHTML(
'wikibase-api-save-failed' ), 'save-failed' );
- }
+ $revision = $editEntity->getNewRevision();
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/ApiModifyEntity.php
b/repo/includes/api/ApiModifyEntity.php
index c76d0dc..f773217 100644
--- a/repo/includes/api/ApiModifyEntity.php
+++ b/repo/includes/api/ApiModifyEntity.php
@@ -171,9 +171,6 @@
$this->dieUsage( $this->msg(
'wikibase-api-modify-failed' )->text(), 'modify-failed' );
}
- // This is similar to ApiEditPage.php and what it uses at line
314
- $this->flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] )
? EDIT_FORCE_BOT : 0;
-
// if the entity is not up for creation, set the EDIT_UPDATE
flags
if ( !$entityContent->isNew() && ( $this->flags & EDIT_NEW )
=== 0 ) {
$this->flags |= EDIT_UPDATE;
@@ -183,27 +180,10 @@
// not added to $this->flags explicitly, the save will
fail.
// collect information and create an EditEntity
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : null;
- $editEntity = new EditEntity( $entityContent, $user,
$baseRevisionId, $this->getContext() );
-
- // Do the actual save, or if it don't exist yet create it.
- // There will be exceptions but we just leak them out ;)
- $editEntity->attemptSave(
- Autocomment::buildApiSummary( $this, $params,
$entityContent ),
- $this->flags,
- ( $this->needsToken() ? $params['token'] : '' )
- );
-
- if ( $editEntity->hasError( EditEntity::TOKEN_ERROR ) ) {
- $editEntity->reportApiErrors( $this, 'session-failure'
);
- }
- elseif ( $editEntity->hasError( EditEntity::EDIT_CONFLICT_ERROR
) ) {
- $editEntity->reportApiErrors( $this, 'edit-conflict' );
- }
- elseif ( $editEntity->hasError() ) {
- $editEntity->reportApiErrors( $this, 'save-failed' );
- }
+ $summary = Autocomment::buildApiSummary( $this, $params,
$entityContent );
+ $editEntity = $this->attemptSaveEntity( $entityContent,
+ $summary,
+ $this->flags );
$this->getResult()->addValue(
'entity',
diff --git a/repo/includes/api/ApiSetReference.php
b/repo/includes/api/ApiSetReference.php
index 90760e8..deede8c 100644
--- a/repo/includes/api/ApiSetReference.php
+++ b/repo/includes/api/ApiSetReference.php
@@ -167,32 +167,18 @@
* @param EntityContent $content
*/
protected function saveChanges( EntityContent $content ) {
- $params = $this->extractRequestParams();
- $user = $this->getUser();
- $flags = 0;
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
- $flags |= EDIT_UPDATE;
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+ $summary = '/* wbsetreference */'; // TODO: automcomment
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- $flags,
- isset( $params['token'] ) ? $params['token'] : ''
- );
+ $revision = $editEntity->getNewRevision();
- if ( !$status->isGood() ) {
- $this->dieUsage( 'Failed to save the change',
'setreference-save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/RemoveQualifiers.php
b/repo/includes/api/RemoveQualifiers.php
index 2c6bad0..42b8f71 100644
--- a/repo/includes/api/RemoveQualifiers.php
+++ b/repo/includes/api/RemoveQualifiers.php
@@ -9,7 +9,6 @@
use Wikibase\EntityId;
use Wikibase\Entity;
use Wikibase\EntityContentFactory;
-use Wikibase\EditEntity;
use Wikibase\Claim;
use Wikibase\Settings;
@@ -134,33 +133,19 @@
* @param EntityContent $content
*/
protected function saveChanges( EntityContent $content ) {
- $params = $this->extractRequestParams();
+ // collect information and create an EditEntity
+ $summary = '/* wbremovequalifiers */'; //TODO: autosummary!
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $user = $this->getUser();
- $flags = 0;
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
- $flags |= EDIT_UPDATE;
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+ $revision = $editEntity->getNewRevision();
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- $flags,
- isset( $params['token'] ) ? $params['token'] : false
- );
-
- if ( !$status->isOk() ) {
- $this->dieUsage( 'Failed to save the change',
'save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/RemoveReferences.php
b/repo/includes/api/RemoveReferences.php
index c9984fb..547d820 100644
--- a/repo/includes/api/RemoveReferences.php
+++ b/repo/includes/api/RemoveReferences.php
@@ -9,7 +9,6 @@
use Wikibase\EntityId;
use Wikibase\Entity;
use Wikibase\EntityContentFactory;
-use Wikibase\EditEntity;
use Wikibase\Statement;
use Wikibase\References;
use Wikibase\Settings;
@@ -146,33 +145,19 @@
* @param EntityContent $content
*/
protected function saveChanges( EntityContent $content ) {
- $params = $this->extractRequestParams();
+ // collect information and create an EditEntity
+ $summary = '/* wbremovereferences */'; //TODO: autosummary
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $user = $this->getUser();
- $flags = 0;
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
- $flags |= EDIT_UPDATE;
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+ $revision = $editEntity->getNewRevision();
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- $flags,
- isset( $params['token'] ) ? $params['token'] : ''
- );
-
- if ( !$status->isGood() ) {
- $this->dieUsage( 'Failed to save the change',
'save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php
index 0bf5853..f220d02 100644
--- a/repo/includes/api/SetClaim.php
+++ b/repo/includes/api/SetClaim.php
@@ -8,7 +8,6 @@
use Wikibase\Entity;
use Wikibase\EntityContent;
use Wikibase\Claim;
-use Wikibase\EditEntity;
use Wikibase\EntityId;
use Wikibase\EntityContentFactory;
@@ -152,27 +151,19 @@
protected function saveChanges( EntityContent $content ) {
$params = $this->extractRequestParams();
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $editEntity = new EditEntity( $content, $this->getUser(),
$baseRevisionId, $this->getContext() );
+ // collect information and create an EditEntity
+ $summary = '/* wbsetclaim */'; // TODO: automcomment;
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- EDIT_UPDATE,
- isset( $params['token'] ) ? $params['token'] : ''
- );
+ $revision = $editEntity->getNewRevision();
- if ( !$status->isGood() ) {
- $this->dieUsage( $status->getMessage(),
'setclaim-save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/SetQualifier.php
b/repo/includes/api/SetQualifier.php
index 014da9b..d8e735d 100644
--- a/repo/includes/api/SetQualifier.php
+++ b/repo/includes/api/SetQualifier.php
@@ -9,7 +9,6 @@
use Wikibase\EntityId;
use Wikibase\Entity;
use Wikibase\EntityContentFactory;
-use Wikibase\EditEntity;
use Wikibase\Claim;
use Wikibase\Snaks;
use Wikibase\SnakFactory;
@@ -255,33 +254,19 @@
* @param EntityContent $content
*/
protected function saveChanges( EntityContent $content ) {
- $params = $this->extractRequestParams();
+ // collect information and create an EditEntity
+ $summary = '/* wbsetqualifier */'; // TODO: automcomment
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $user = $this->getUser();
- $flags = 0;
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
- $flags |= EDIT_UPDATE;
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+ $revision = $editEntity->getNewRevision();
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- $flags,
- isset( $params['token'] ) ? $params['token'] : false
- );
-
- if ( !$status->isOk() ) {
- $this->dieUsage( 'Failed to save the change',
'save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/includes/api/SetStatementRank.php
b/repo/includes/api/SetStatementRank.php
index 2d35c01..be9cf9c 100644
--- a/repo/includes/api/SetStatementRank.php
+++ b/repo/includes/api/SetStatementRank.php
@@ -9,7 +9,6 @@
use Wikibase\EntityId;
use Wikibase\Entity;
use Wikibase\EntityContentFactory;
-use Wikibase\EditEntity;
use Wikibase\Statement;
use Wikibase\Settings;
@@ -136,33 +135,19 @@
* @param EntityContent $content
*/
protected function saveChanges( EntityContent $content ) {
- $params = $this->extractRequestParams();
+ // collect information and create an EditEntity
+ $summary = '/* wbsetstatementrank */'; // TODO: automcomment
+ $editEntity = $this->attemptSaveEntity( $content,
+ $summary,
+ EDIT_UPDATE );
- $user = $this->getUser();
- $flags = 0;
- $baseRevisionId = isset( $params['baserevid'] ) ? intval(
$params['baserevid'] ) : null;
- $baseRevisionId = $baseRevisionId > 0 ? $baseRevisionId : false;
- $flags |= ( $user->isAllowed( 'bot' ) && $params['bot'] ) ?
EDIT_FORCE_BOT : 0;
- $flags |= EDIT_UPDATE;
- $editEntity = new EditEntity( $content, $user, $baseRevisionId,
$this->getContext() );
+ $revision = $editEntity->getNewRevision();
- $status = $editEntity->attemptSave(
- '', // TODO: automcomment
- $flags,
- isset( $params['token'] ) ? $params['token'] : ''
- );
-
- if ( !$status->isGood() ) {
- $this->dieUsage( 'Failed to save the change',
'save-failed' );
- }
-
- $statusValue = $status->getValue();
-
- if ( isset( $statusValue['revision'] ) ) {
+ if ( $revision ) {
$this->getResult()->addValue(
'pageinfo',
'lastrevid',
- (int)$statusValue['revision']->getId()
+ $revision->getId()
);
}
}
diff --git a/repo/tests/phpunit/includes/api/SetQualifierTest.php
b/repo/tests/phpunit/includes/api/SetQualifierTest.php
index 4386508..ef086db 100644
--- a/repo/tests/phpunit/includes/api/SetQualifierTest.php
+++ b/repo/tests/phpunit/includes/api/SetQualifierTest.php
@@ -6,6 +6,7 @@
use Wikibase\Statement;
use Wikibase\Claim;
use Wikibase\EntityId;
+use Wikibase\Test\ApiModifyItemBase;
/**
* Unit tests for the Wikibase\Repo\Api\SetQualifier class.
@@ -42,7 +43,7 @@
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
*/
-class SetQualifierTest extends \ApiTestCase {
+class SetQualifierTest extends ApiModifyItemBase {
/**
* @return Snak[]
@@ -132,11 +133,14 @@
}
protected function makeAddRequest( $statementGuid, Snak $qualifier,
EntityId $entityId ) {
+ $token = $this->getItemToken();
+
$params = array(
'action' => 'wbsetqualifier',
'claim' => $statementGuid,
'snaktype' => $qualifier->getType(),
'property' =>
$qualifier->getPropertyId()->getPrefixedId(),
+ 'token' => $token
);
if ( $qualifier instanceof \Wikibase\PropertyValueSnak ) {
--
To view, visit https://gerrit.wikimedia.org/r/49853
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4094feee1e6166171fd15ffdf7249b3c0d27ce1c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits