Tobias Gritschacher has uploaded a new change for review. https://gerrit.wikimedia.org/r/58880
Change subject: Autosummary for setClaim (WIP,DNM) ...................................................................... Autosummary for setClaim (WIP,DNM) still work in progress! Bug: 45840 Bug: 45100 Change-Id: I6e90d80fc3d8d579f218a5d77906674e968d126c --- M repo/Wikibase.i18n.php M repo/includes/ClaimSaver.php M repo/includes/api/CreateClaim.php M repo/includes/api/ModifyClaim.php M repo/includes/api/RemoveClaims.php M repo/includes/api/SetClaim.php 6 files changed, 139 insertions(+), 80 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/80/58880/1 diff --git a/repo/Wikibase.i18n.php b/repo/Wikibase.i18n.php index b298350..cf4e29e 100644 --- a/repo/Wikibase.i18n.php +++ b/repo/Wikibase.i18n.php @@ -293,6 +293,9 @@ 'wikibase-item-summary-special-create-item' => 'Created an [$2] item with {{PLURAL:$1|value|values}}', 'wikibase-item-summary-wbcreateclaim-create' => 'Created claim', 'wikibase-item-summary-wbremoveclaims-remove' => 'Removed {{PLURAL:$3|claim|claims}}', + 'wikibase-item-summary-wbsetclaim-update' => 'Changed {{PLURAL:$3|claim|claims}}', + 'wikibase-item-summary-wbsetclaim-create' => 'Created {{PLURAL:$3|claim|claims}}', + 'wikibase-item-summary-wbsetqualifier-update' => 'Changed $1 {{PLURAL:$3|qualifier|qualifiers}}', // property - summary and autocomment, see docs/summaries.txt 'wikibase-property-summary-wbeditentity-create' => 'Created a new property', diff --git a/repo/includes/ClaimSaver.php b/repo/includes/ClaimSaver.php index 6f54708..fb84657 100644 --- a/repo/includes/ClaimSaver.php +++ b/repo/includes/ClaimSaver.php @@ -39,6 +39,22 @@ class ClaimSaver { /** + * @var Summary + */ + protected $summary; + + /** + * Constructs a new ClaimSaver + * + * @since 0.4 + * + * @param Summary|null $summary + */ + public function __construct( $summary = null ) { + $this->summary = $summary; + } + + /** * @see ApiBase::execute * * @since 0.4 @@ -57,13 +73,13 @@ * * This status object can be used with ApiWikibase::handleSaveStatus(). */ - public function saveClaim( Claim $claim, $baseRevId, $token, User $user ) { + public function saveClaim( Claim $claim, ClaimDiffer $claimDiffer, $baseRevId, $token, User $user ) { try { $entityId = $this->getEntityIdForClaim( $claim ); $content = $this->getEntityContent( $entityId, $baseRevId ); - $this->updateClaim( $content->getEntity(), $claim ); + $this->updateClaim( $content->getEntity(), $claim, $claimDiffer ); $status = $this->saveChanges( $content, $baseRevId, $token, $user ); } catch ( ExceptionWithCode $ex ) { @@ -116,17 +132,77 @@ * * @param Entity $entity * @param Claim $claim + * @param ClaimDiffer $claimDiffer */ - protected function updateClaim( Entity $entity, Claim $claim ) { + protected function updateClaim( Entity $entity, Claim $claim, ClaimDiffer $claimDiffer ) { $claims = new \Wikibase\Claims( $entity->getClaims() ); + $this->summary->addAutoCommentArgs( 1 ); // we're always having singular here if ( $claims->hasClaimWithGuid( $claim->getGuid() ) ) { + //claim is changed + $oldClaim = $claims->getClaimWithGuid( $claim->getGuid() ); + $claimDifference = $claimDiffer->diffClaims( $oldClaim, $claim ); + + $this->summary->setAction( 'update' ); + + if ( $claimDifference->getMainSnakChange() !== null ) { + $summaryArgs = $this->buildSummaryArgs( new \Wikibase\Claims( array( $claim ) ), array( $claim->getGuid() ) ); + $this->summary->addAutoSummaryArgs( $summaryArgs ); + } + + if ( $claimDifference->getQualifierChanges()->isEmpty() === false ) { + $pair = array(); + $pair[$claim->getMainSnak()->getPropertyId()->getPrefixedId()][] = '/* wikibase-item-summary-wbsetqualifier-update */'; #"Modified qualifiers"; + $summaryArgs = array( $pair ); + $this->summary->addAutoSummaryArgs( $summaryArgs ); + } + $claims->removeClaimWithGuid( $claim->getGuid() ); + } else { + //new claim is added + $summaryArgs = $this->buildSummaryArgs( new \Wikibase\Claims( array( $claim ) ), array( $claim->getGuid() ) ); + $this->summary->addAutoSummaryArgs( $summaryArgs ); + $this->summary->setAction( 'create' ); } $claims->addClaim( $claim ); $entity->setClaims( $claims ); + } + + /** + * Build key (property) => value pairs for summary arguments + * + * @todo see if this can be more generic and put elsewhere... + * + * @param Claims $claims + * @param string[] $guids + * + * @return mixed[] // propertyId (prefixed) => array of values + */ + protected function buildSummaryArgs( Claims $claims, array $guids ) { + $pairs = array(); + + foreach( $guids as $guid ) { + if ( $claims->hasClaimWithGuid( $guid ) ) { + $snak = $claims->getClaimWithGuid( $guid )->getMainSnak(); + $key = $snak->getPropertyId()->getPrefixedId(); + + if ( !array_key_exists( $key, $pairs ) ) { + $pairs[$key] = array(); + } + + if ( $snak instanceof PropertyValueSnak ) { + $value = $snak->getDataValue(); + } else { + $value = '-'; // todo handle no values in general way (needed elsewhere) + } + + $pairs[$key][] = $value; + } + } + + return array( $pairs ); } /** @@ -175,7 +251,7 @@ $editEntity = new \Wikibase\EditEntity( $content, $user, $baseRevisionId ); $status = $editEntity->attemptSave( - '', // TODO: automcomment + $this->summary ? $this->summary->toString() : '', EDIT_UPDATE, $token ); diff --git a/repo/includes/api/CreateClaim.php b/repo/includes/api/CreateClaim.php index 80da322..78cc90f 100644 --- a/repo/includes/api/CreateClaim.php +++ b/repo/includes/api/CreateClaim.php @@ -71,7 +71,8 @@ } $claim = $this->addClaim( $entityContent->getEntity(), $snak ); - $summary = $this->createSummary( $snak, 'create' ); + $summary = $this->createSummary( 'create' ); + $summary->addAutoSummaryArgs( $snak->getPropertyId(), $snak->getDataValue() ); $this->saveChanges( $entityContent, $summary ); diff --git a/repo/includes/api/ModifyClaim.php b/repo/includes/api/ModifyClaim.php index a7892a8..56b474b 100644 --- a/repo/includes/api/ModifyClaim.php +++ b/repo/includes/api/ModifyClaim.php @@ -2,6 +2,8 @@ namespace Wikibase\Api; use ApiBase, MWException; +use Wikibase\Claims; +use Wikibase\PropertyValueSnak; use Wikibase\Snak; use Wikibase\Summary; @@ -41,21 +43,54 @@ * * @since 0.4 * - * @param Snak $snak * @param string $action * * @return Summary */ - protected function createSummary( Snak $snak, $action ) { + protected function createSummary( $action ) { if ( !is_string( $action ) ) { throw new \MWException( 'action is invalid or unknown type.' ); } $summary = new Summary( $this->getModuleName() ); $summary->setAction( $action ); - $summary->addAutoSummaryArgs( $snak->getPropertyId(), $snak->getDataValue() ); return $summary; } + /** + * Build key (property) => value pairs for summary arguments + * + * @todo see if this can be more generic and put elsewhere... + * + * @param Claims $claims + * @param string[] $guids + * + * @return mixed[] // propertyId (prefixed) => array of values + */ + protected function buildSummaryArgs( Claims $claims, array $guids ) { + $pairs = array(); + + foreach( $guids as $guid ) { + if ( $claims->hasClaimWithGuid( $guid ) ) { + $snak = $claims->getClaimWithGuid( $guid )->getMainSnak(); + $key = $snak->getPropertyId()->getPrefixedId(); + + if ( !array_key_exists( $key, $pairs ) ) { + $pairs[$key] = array(); + } + + if ( $snak instanceof PropertyValueSnak ) { + $value = $snak->getDataValue(); + } else { + $value = '-'; // todo handle no values in general way (needed elsewhere) + } + + $pairs[$key][] = $value; + } + } + + return array( $pairs ); + } + } diff --git a/repo/includes/api/RemoveClaims.php b/repo/includes/api/RemoveClaims.php index 814454c..6e05bde 100644 --- a/repo/includes/api/RemoveClaims.php +++ b/repo/includes/api/RemoveClaims.php @@ -12,7 +12,6 @@ use Wikibase\EntityContentFactory; use Wikibase\Claims; use Wikibase\Summary; -use Wikibase\PropertyValueSnak; /** * API module for removing claims. @@ -40,7 +39,7 @@ * @licence GNU GPL v2+ * @author Jeroen De Dauw < jeroended...@gmail.com > */ -class RemoveClaims extends ApiWikibase { +class RemoveClaims extends ModifyClaim { // TODO: example // TODO: rights @@ -64,73 +63,6 @@ $this->outputResult( $removedClaimKeys ); wfProfileOut( __METHOD__ ); - } - - /** - * Create a summary - * - * @since 0.4 - * - * @param Claims $claims - * @param string[] $guids - * @param string $action - * - * @return Summary - */ - protected function createSummary( Claims $claims, array $guids, $action ) { - if ( !is_string( $action ) ) { - throw new \MWException( 'action is invalid or unknown type.' ); - } - - $summary = new Summary( $this->getModuleName() ); - $summary->setAction( $action ); - - $count = count( $guids ); - - $summary->addAutoCommentArgs( $count ); - - $summaryArgs = $this->buildSummaryArgs( $claims, $guids ); - - if ( $summaryArgs !== array() ) { - $summary->addAutoSummaryArgs( $summaryArgs ); - } - - return $summary; - } - - /** - * Build key (property) => value pairs for summary arguments - * - * @todo see if this can be more generic and put elsewhere... - * - * @param Claims $claims - * @param string[] $guids - * - * @return mixed[] // propertyId (prefixed) => array of values - */ - protected function buildSummaryArgs( Claims $claims, array $guids ) { - $pairs = array(); - - foreach( $guids as $guid ) { - if ( $claims->hasClaimWithGuid( $guid ) ) { - $snak = $claims->getClaimWithGuid( $guid )->getMainSnak(); - $key = $snak->getPropertyId()->getPrefixedId(); - - if ( !array_key_exists( $key, $pairs ) ) { - $pairs[$key] = array(); - } - - if ( $snak instanceof PropertyValueSnak ) { - $value = $snak->getDataValue(); - } else { - $value = '-'; // todo handle no values in general way (needed elsewhere) - } - - $pairs[$key][] = $value; - } - } - - return array( $pairs ); } /** @@ -186,7 +118,14 @@ $entity->setClaims( $claims ); - $summary = $this->createSummary( $removedClaims, $guids[$entity->getPrefixedId()], 'remove' ); + $summary = $this->createSummary( 'remove' ); + + $count = count( $guids[$entity->getPrefixedId()] ); + $summary->addAutoCommentArgs( $count ); + $summaryArgs = $this->buildSummaryArgs( $removedClaims, $guids[$entity->getPrefixedId()] ); + if ( $summaryArgs !== array() ) { + $summary->addAutoSummaryArgs( $summaryArgs ); + } $this->saveChanges( $entityContent, $summary ); } diff --git a/repo/includes/api/SetClaim.php b/repo/includes/api/SetClaim.php index e191703..fcfb2c4 100644 --- a/repo/includes/api/SetClaim.php +++ b/repo/includes/api/SetClaim.php @@ -5,11 +5,15 @@ use MWException; use ApiBase; +use Diff\ListDiffer; + use Wikibase\EntityContent; use Wikibase\Claim; use Wikibase\EntityContentFactory; +use Wikibase\ClaimDiffer; use Wikibase\ClaimSaver; use Wikibase\ExceptionWithCode; +use Wikibase\Summary; /** * API module for creating or updating an entire Claim. @@ -49,7 +53,8 @@ public function execute() { $claim = $this->getClaimFromRequest(); - $claimSetter = new ClaimSaver(); + $claimSaver = new ClaimSaver( new Summary( $this->getModuleName() ) ); + $claimDiffer = new ClaimDiffer( new ListDiffer() ); $params = $this->extractRequestParams(); $baseRevisionId = isset( $params['baserevid'] ) ? intval( $params['baserevid'] ) : null; @@ -57,7 +62,7 @@ $newRevisionId = null; - $status = $claimSetter->saveClaim( $claim, $baseRevisionId, $token, $this->getUser() ); + $status = $claimSaver->saveClaim( $claim, $claimDiffer, $baseRevisionId, $token, $this->getUser() ); $this->handleSaveStatus( $status ); // die on error, report warnings, etc $statusValue = $status->getValue(); -- To view, visit https://gerrit.wikimedia.org/r/58880 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I6e90d80fc3d8d579f218a5d77906674e968d126c Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits