Daniel Kinzler has submitted this change and it was merged.

Change subject: Implement autosummaries for add and remove claims
......................................................................


Implement autosummaries for add and remove claims

- Create claim
- Remove claims

It is still basic and needs follow-up work.

Change-Id: I7aa7b5d2921306d549d62be31a7d0c2ff4e1ac3b
---
M DataModel/DataModel/Claim/Claims.php
M repo/Wikibase.i18n.php
M repo/Wikibase.php
M repo/includes/Summary.php
M repo/includes/api/CreateClaim.php
A repo/includes/api/ModifyClaim.php
M repo/includes/api/RemoveClaims.php
7 files changed, 271 insertions(+), 108 deletions(-)

Approvals:
  Daniel Kinzler: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/DataModel/DataModel/Claim/Claims.php 
b/DataModel/DataModel/Claim/Claims.php
index b3cd940..8ee664b 100644
--- a/DataModel/DataModel/Claim/Claims.php
+++ b/DataModel/DataModel/Claim/Claims.php
@@ -149,6 +149,17 @@
        }
 
        /**
+        * Get array of Claim guids
+        *
+        * @since 0.4
+        *
+        * @return string[]
+        */
+       public function getGuids() {
+               return array_keys( $this->guidIndex );
+       }
+
+       /**
         * @see GenericArrayObject::preSetElement
         *
         * @since 0.3
diff --git a/repo/Wikibase.i18n.php b/repo/Wikibase.i18n.php
index 752b334..abb8c18 100644
--- a/repo/Wikibase.i18n.php
+++ b/repo/Wikibase.i18n.php
@@ -287,6 +287,8 @@
        'wikibase-item-summary-wbsetclaimvalue' => 'Set {{PLURAL:$1|a claim 
value|claim values}}',
        'wikibase-item-summary-wbremoveclaims' => 'Removed {{PLURAL:$1|a 
claim|claims}}',
        '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}}',
 
        // property - summary and autocomment, see docs/summaries.txt
        'wikibase-property-summary-wbeditentity-create' => 'Created a new 
property',
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index b0178af..c2313c1 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -138,6 +138,7 @@
 $wgAutoloadClasses['Wikibase\Api\EditEntity']                          = $dir 
. 'includes/api/EditEntity.php';
 $wgAutoloadClasses['Wikibase\Api\GetEntities']                                 
= $dir . 'includes/api/GetEntities.php';
 $wgAutoloadClasses['Wikibase\Api\LinkTitles']                          = $dir 
. 'includes/api/LinkTitles.php';
+$wgAutoloadClasses['Wikibase\Api\ModifyClaim']                         = $dir 
. 'includes/api/ModifyClaim.php';
 $wgAutoloadClasses['Wikibase\Api\ModifyEntity']                        = $dir 
. 'includes/api/ModifyEntity.php';
 $wgAutoloadClasses['Wikibase\Api\ModifyLangAttribute']                 = $dir 
. 'includes/api/ModifyLangAttribute.php';
 $wgAutoloadClasses['Wikibase\Api\SearchEntities']                      = $dir 
. 'includes/api/SearchEntities.php';
diff --git a/repo/includes/Summary.php b/repo/includes/Summary.php
index 48b74cd..82cf4f3 100644
--- a/repo/includes/Summary.php
+++ b/repo/includes/Summary.php
@@ -3,6 +3,7 @@
 namespace Wikibase;
 
 use Language;
+use DataValues\StringValue;
 
 /**
  * File defining the handler for autocomments and additional utility functions
@@ -67,8 +68,6 @@
         * @param array|bool $summaryArgs the arguments to the autosummary
         */
        public function __construct( $moduleName = null, $actionName = null, 
$language = null, $commentArgs = array(), $summaryArgs = false ) {
-               //global $wgContLang;
-
                $this->moduleName = $moduleName;
                $this->actionName = $actionName;
                $this->language = $language === null ? null : (string)$language;
@@ -243,6 +242,7 @@
                                return '';
                        }
                        else {
+                               // @todo have some sort of key value formatter
                                return $wgContLang->commaList( $parts );
                        }
                }
@@ -263,35 +263,91 @@
                $strings = array();
 
                foreach ( $args as $arg ) {
-                       // if we find that any arg is an object we shall not 
display them
-                       switch ( true ) {
-                       case is_string( $arg ):
-                               $strings[] = $arg;
-                               break;
-                       case is_object( $arg ) && ($arg instanceof EntityId):
-                               $title = 
\Wikibase\EntityContentFactory::singleton()->getTitleForId( $arg );
-                               $strings[] = '[[' . $title->getFullText() . 
']]';
-                               break;
-                       case is_object( $arg ) && method_exists( $arg, 
'__toString' ):
-                               $strings[] = (string)$arg;
-                               break;
-                       case is_object( $arg ) && !method_exists( $arg, 
'__toString' ):
-                               $strings[] = '';
-                               $this->removeFormat( self::USE_SUMMARY );
-                               break;
-                       case $arg === false || $arg === null:
-                               $strings[] = '';
-                               break;
-                       default:
-                               $strings[] = '';
-                               $this->removeFormat( self::USE_SUMMARY );
+                       $strings[] = $this->formatArg( $arg );
+               }
+
+        $this->summaryArgs = array_merge(
+            $this->summaryArgs === false ? array() : $this->summaryArgs,
+            array_filter( $strings, function ( $str ) { return 0 < strlen( 
$str ); } )
+        );
+       }
+
+       /**
+        * Format an autosummary argument
+        *
+        * @since 0.4
+        *
+        * @param mixed $arg
+        *
+        * @return string
+        */
+       protected function formatArg( $arg ) {
+               global $wgContLang;
+
+               $string = '';
+
+               if ( is_string( $arg ) ) {
+                       $entityId = EntityId::newFromPrefixedId( $arg );
+                       if ( $entityId instanceof EntityId ) {
+                               $arg = $entityId;
                        }
                }
 
-               $this->summaryArgs = array_merge(
-                       $this->summaryArgs === false ? array() : 
$this->summaryArgs,
-                       array_filter( $strings, function ( $str ) { return 0 < 
strlen( $str ); } )
-               );
+               // if we find that any arg is an object we shall not display 
them
+               switch ( true ) {
+                       case is_array( $arg ):
+                               $string = '';
+
+                               $key = key( $arg );
+                               $value = $arg[$key];
+
+                               if ( !is_int( $key ) ) {
+                                       // @todo i18n for colon in onFormat
+                                       $string .= $this->formatArg( $key ) . 
': ';
+                               }
+
+                               // @todo this is crufty!
+                               $dataValueStrings = array();
+
+                               if ( is_array( $value ) ) {
+                                       foreach( $value as $i ) {
+                                               $dataValueStrings[] = 
$this->formatArg( $i );
+                                       }
+
+                                       if ( $dataValueStrings !== array() ) {
+                                               $string .= 
$wgContLang->commaList( $dataValueStrings );
+                                       }
+                               } else if ( is_string( $value ) ) {
+                                       $string .= $this->formatArg( $value );
+
+                               }
+                               break;
+                       case is_string( $arg ):
+                               $string = $arg;
+                               break;
+                       case is_object( $arg ) && ($arg instanceof EntityId):
+                               $title = 
\Wikibase\EntityContentFactory::singleton()->getTitleForId( $arg );
+                               $string = '[[' . $title->getFullText() . ']]';
+                               break;
+                       case is_object( $arg ) && ( $arg instanceof StringValue 
):
+                               $string = htmlspecialchars( $arg->getValue() );
+                               break;
+                       case is_object( $arg ) && method_exists( $arg, 
'__toString' ):
+                               $string = (string)$arg;
+                               break;
+                       case is_object( $arg ) && !method_exists( $arg, 
'__toString' ):
+                               $string = '';
+                               break;
+                       case is_int( $arg ):
+                               $string = (string) $arg;
+                               break;
+                       case $arg === false || $arg === null:
+                               break;
+                       default:
+                               $string = '';
+               }
+
+               return $string;
        }
 
        /**
@@ -357,7 +413,10 @@
 
                $comment = ( $format & self::USE_COMMENT) ? Utils::trimToNFC( 
$comment ) : '';
                $summary = ( $format & self::USE_SUMMARY) ? Utils::trimToNFC( 
$summary ) : '';
-               return self::formatTotalSummary( $comment, $summary, $length );
+
+               $totalSummary = self::formatTotalSummary( $comment, $summary, 
$length );
+
+               return $totalSummary;
        }
 
 }
diff --git a/repo/includes/api/CreateClaim.php 
b/repo/includes/api/CreateClaim.php
index bf0386f..6f59e9c 100644
--- a/repo/includes/api/CreateClaim.php
+++ b/repo/includes/api/CreateClaim.php
@@ -13,6 +13,8 @@
 use Wikibase\Claim;
 use Wikibase\Autocomment;
 use Wikibase\Settings;
+use Wikibase\Summary;
+use Wikibase\Snak;
 
 /**
  * API module for creating claims.
@@ -40,9 +42,8 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-class CreateClaim extends ApiWikibase implements IAutocomment {
+class CreateClaim extends ModifyClaim {
 
-       // TODO: automcomment
        // TODO: rights
 
        /**
@@ -57,9 +58,21 @@
 
                $entityContent = $this->getEntityContent();
 
-               $claim = $this->addClaim( $entityContent->getEntity() );
+               // It is possible we get an exception from this method because 
of specifying
+               // a non existing-property, specifying an entity id for an 
entity with wrong
+               // entity type or providing an invalid DataValue.
+               try {
+                       $snak = $this->getSnakInstance();
+               }
+               catch ( \Exception $ex ) {
+                       wfProfileOut( __METHOD__ );
+                       $this->dieUsageMsg( $ex->getMessage() );
+               }
 
-               $this->saveChanges( $entityContent );
+               $claim = $this->addClaim( $entityContent->getEntity(), $snak );
+               $summary = $this->createSummary( $snak, 'create' );
+
+               $this->saveChanges( $entityContent, $summary );
 
                $this->outputClaim( $claim );
 
@@ -75,23 +88,12 @@
         * @since 0.2
         *
         * @param \Wikibase\Entity $entity
+        * @param \Wikibase\Snak $snak
         *
         * @return Claim
         */
-       protected function addClaim( Entity $entity ) {
+       protected function addClaim( Entity $entity, Snak $snak ) {
                wfProfileIn( __METHOD__ );
-
-               // It is possible we get an exception from this method because 
of specifying
-               // a non existing-property, specifying an entity id for an 
entity with wrong
-               // entity type or providing an invalid DataValue.
-               try {
-                       $snak = $this->getSnakInstance();
-               }
-               catch ( \Exception $ex ) {
-                       wfProfileOut( __METHOD__ );
-                       $this->dieUsageMsg( $ex->getMessage() );
-               }
-
                $claim = $entity->newClaim( $snak );
 
                $entity->addClaim( $claim );
@@ -104,11 +106,12 @@
         * @since 0.3
         *
         * @param \Wikibase\EntityContent $content
+        * @param \Wikibase\Summary $summary
         */
-       protected function saveChanges( EntityContent $content ) {
+       protected function saveChanges( EntityContent $content, Summary 
$summary ) {
                $status = $this->attemptSaveEntity(
                        $content,
-                       '', // TODO: automcomment
+                       $summary->toString(),
                        EDIT_UPDATE
                );
 
@@ -285,26 +288,4 @@
                        // 'ex' => 'desc'
                );
        }
-
-       /**
-        * @see  \Wikibase\Api\IAutocomment::getTextForComment()
-        */
-       public function getTextForComment( array $params, $plural = 1 ) {
-               return Autocomment::formatAutoComment(
-                       $this->getModuleName(),
-                       array(
-                               /*plural */ 1
-                       )
-               );
-       }
-
-       /**
-        * @see  \Wikibase\Api\IAutocomment::getTextForSummary()
-        */
-       public function getTextForSummary( array $params ) {
-               return Autocomment::formatAutoSummary(
-                       Autocomment::pickValuesFromParams( $params, 'property' )
-               );
-       }
-
 }
diff --git a/repo/includes/api/ModifyClaim.php 
b/repo/includes/api/ModifyClaim.php
new file mode 100644
index 0000000..a7892a8
--- /dev/null
+++ b/repo/includes/api/ModifyClaim.php
@@ -0,0 +1,61 @@
+<?php
+namespace Wikibase\Api;
+
+use ApiBase, MWException;
+use Wikibase\Snak;
+use Wikibase\Summary;
+
+/**
+ * Base class for modifying claims, with common functionality
+ * for creating summaries.
+ *
+ * @todo decide if this is really needed or not
+ *
+ * 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
+ *
+ * @since 0.4
+ *
+ * @ingroup WikibaseRepo
+ * @ingroup API
+ *
+ * @licence GNU GPL v2+
+ * @author Katie Filbert < [email protected] >
+ */
+abstract class ModifyClaim extends ApiWikibase {
+
+       /**
+        * Create a summary
+        *
+        * @since 0.4
+        *
+        * @param Snak $snak
+        * @param string $action
+        *
+        * @return Summary
+        */
+       protected function createSummary( Snak $snak, $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;
+       }
+
+}
diff --git a/repo/includes/api/RemoveClaims.php 
b/repo/includes/api/RemoveClaims.php
index f4a02a4..814454c 100644
--- a/repo/includes/api/RemoveClaims.php
+++ b/repo/includes/api/RemoveClaims.php
@@ -11,6 +11,8 @@
 use Wikibase\EntityContent;
 use Wikibase\EntityContentFactory;
 use Wikibase\Claims;
+use Wikibase\Summary;
+use Wikibase\PropertyValueSnak;
 
 /**
  * API module for removing claims.
@@ -38,7 +40,7 @@
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < [email protected] >
  */
-class RemoveClaims extends ApiWikibase implements IAutocomment {
+class RemoveClaims extends ApiWikibase {
 
        // TODO: example
        // TODO: rights
@@ -62,6 +64,73 @@
                $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 );
        }
 
        /**
@@ -96,31 +165,33 @@
         * the claims that actually got removed.
         *
         * @since 0.3
-        *
+        i*
         * @param \Wikibase\EntityContent[] $entityContents
         * @param array $guids
         *
         * @return string[]
         */
        protected function removeClaims( $entityContents, array $guids ) {
-               $removedClaims = array();
+               $removedClaims = new Claims();
 
                foreach ( $entityContents as $entityContent ) {
                        $entity = $entityContent->getEntity();
 
                        $claims = new Claims( $entity->getClaims() );
 
-                       $removedClaims = array_merge(
-                               $removedClaims,
-                               $this->removeClaimsFromList( $claims, 
$guids[$entity->getPrefixedId()] )
-                       );
+                       $removedBatch = $this->removeClaimsFromList( $claims, 
$guids[$entity->getPrefixedId()] );
+                       foreach( $removedBatch as $claim ) {
+                               $removedClaims->addClaim( $claim );
+                       }
 
                        $entity->setClaims( $claims );
 
-                       $this->saveChanges( $entityContent );
+                       $summary = $this->createSummary( $removedClaims, 
$guids[$entity->getPrefixedId()], 'remove' );
+
+                       $this->saveChanges( $entityContent, $summary );
                }
 
-               return $removedClaims;
+               return $removedClaims->getGuids();
        }
 
        /**
@@ -164,19 +235,19 @@
         * @param Claims $claims
         * @param string[] $guids
         *
-        * @return string[]
+        * @return Claims
         */
        protected function removeClaimsFromList( Claims &$claims, array $guids 
) {
-               $removedGuids = array();
+               $removedClaims = new Claims();
 
                foreach ( $guids as $guid ) {
                        if ( $claims->hasClaimWithGuid( $guid ) ) {
+                               $removedClaims->addClaim( 
$claims->getClaimWithGuid( $guid ) );
                                $claims->removeClaimWithGuid( $guid );
-                               $removedGuids[] = $guid;
                        }
                }
 
-               return $removedGuids;
+               return $removedClaims;
        }
 
        /**
@@ -205,10 +276,10 @@
         *
         * @param EntityContent $content
         */
-       protected function saveChanges( EntityContent $content ) {
+       protected function saveChanges( EntityContent $content, Summary 
$summary ) {
                $status = $this->attemptSaveEntity(
                        $content,
-                       '', // TODO: automcomment
+                       $summary->toString(),
                        EDIT_UPDATE
                );
 
@@ -281,29 +352,6 @@
                return array(
                        // TODO
                        // 'ex' => 'desc'
-               );
-       }
-
-       /**
-        * @see  \Wikibase\Api\IAutocomment::getTextForComment()
-        */
-       public function getTextForComment( array $params, $plural = 1 ) {
-               $guids = $params['claim'];
-
-               return Autocomment::formatAutoComment(
-                       $this->getModuleName(),
-                       array(
-                               /*plural */ count( $guids ),
-                       )
-               );
-       }
-
-       /**
-        * @see  \Wikibase\Api\IAutocomment::getTextForSummary()
-        */
-       public function getTextForSummary( array $params ) {
-               return Autocomment::formatAutoSummary(
-                       Autocomment::pickValuesFromParams( $params, 'claim' )
                );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/55540
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I7aa7b5d2921306d549d62be31a7d0c2ff4e1ac3b
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to