Daniel Kinzler has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/236051

Change subject: Expand auto-comments in ChangeHandler
......................................................................

Expand auto-comments in ChangeHandler

This is part 3 of the client side edit comments refactoring.
(Note that part 3 goes in after part 4, the numbers indicate
the information flow).

Bug: T111521
Change-Id: I75738c5b4e46e27237cbd543d4905164f8b0731d
---
M client/includes/Changes/ChangeHandler.php
M client/includes/SiteLinkCommentCreator.php
M client/includes/recentchanges/ExternalRecentChange.php
M client/tests/phpunit/includes/Changes/ChangeHandlerTest.php
M client/tests/phpunit/includes/SiteLinkCommentCreatorTest.php
M lib/includes/formatters/AutoCommentFormatter.php
M lib/tests/phpunit/changes/TestChanges.php
M lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
8 files changed, 297 insertions(+), 160 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/51/236051/1

diff --git a/client/includes/Changes/ChangeHandler.php 
b/client/includes/Changes/ChangeHandler.php
index a5e7d7a..f94825a 100644
--- a/client/includes/Changes/ChangeHandler.php
+++ b/client/includes/Changes/ChangeHandler.php
@@ -6,15 +6,20 @@
 use Hooks;
 use InvalidArgumentException;
 use IORMRow;
+use Language;
+use Message;
 use MWException;
 use Title;
 use Wikibase\Change;
 use Wikibase\Client\Store\TitleFactory;
 use Wikibase\Client\Usage\EntityUsage;
 use Wikibase\Client\Usage\PageEntityUsages;
+use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\EntityChange;
 use Wikibase\ItemChange;
+use Wikibase\Lib\AutoCommentFormatter;
 use Wikibase\SiteLinkCommentCreator;
+use Wikimedia\Assert\Assert;
 
 /**
  * Interface for change handling. Whenever a change is detected,
@@ -74,6 +79,11 @@
        private $changeListTransformer;
 
        /**
+        * @var Language
+        */
+       private $language;
+
+       /**
         * @var string
         */
        private $localSiteId;
@@ -88,16 +98,17 @@
         * @param TitleFactory $titleFactory
         * @param PageUpdater $updater
         * @param ChangeListTransformer $changeListTransformer
+        * @param Language $language
         * @param string $localSiteId
         * @param bool $injectRecentChanges
         *
-        * @throws InvalidArgumentException
         */
        public function __construct(
                AffectedPagesFinder $affectedPagesFinder,
                TitleFactory $titleFactory,
                PageUpdater $updater,
                ChangeListTransformer $changeListTransformer,
+               Language $language,
                $localSiteId,
                $injectRecentChanges = true
        ) {
@@ -113,6 +124,7 @@
                $this->titleFactory = $titleFactory;
                $this->updater = $updater;
                $this->changeListTransformer = $changeListTransformer;
+               $this->language = $language;
                $this->localSiteId = $localSiteId;
                $this->injectRecentChanges = $injectRecentChanges;
        }
@@ -311,70 +323,131 @@
        /**
         * Constructs RC attributes for the given change
         *
+        * @see ExternalRecentChange::buildAttributes
+        *
         * @param EntityChange $change The Change that caused the update
         *
         * @return array[]|bool an array of RC attributes,
-        *         or false if the change does not provide edit meta data
+        *         as understood by ExternalRecentChange::buildAttributes.
         */
        private function getRCAttributes( EntityChange $change ) {
                $rcinfo = $change->getMetadata();
-
-               if ( empty( $rcinfo ) ) {
-                       return false;
-               }
 
                //@todo: add getFields() to the interface, or provide getters!
                $fields = $change->getFields();
                $fields['entity_type'] = 
$change->getEntityId()->getEntityType();
 
-               if ( $change instanceof ItemChange ) {
-                       $rcinfo['comment'] = $this->getEditComment( $change );
-
-                       if ( isset( $fields['info']['changes'] ) ) {
-                               $rcinfo['composite-comment'] = array();
-
-                               foreach ( $fields['info']['changes'] as $part ) 
{
-                                       $rcinfo['composite-comment'][] = 
$this->getEditComment( $part );
-                               }
-                       }
-               }
-
                unset( $fields['info'] );
 
-               $rcinfo = array_merge( $fields, $rcinfo );
+               if ( isset( $fields['info']['changes'] ) ) {
+                       $changesForComment = $fields['info']['changes'];
+               } else {
+                       $changesForComment = array( $change );
+               }
 
+               $comment = $this->getEditCommentMulti( $changesForComment );
+
+               // Use keys known to ExternalRecentChange::buildAttributes.
                return array(
-                       'wikibase-repo-change' => array_merge( $fields, $rcinfo 
)
+                       'wikibase-repo-change' => array_merge( $fields, $rcinfo 
),
+                       'comment' => $comment
                );
        }
 
        /**
-        * Returns the comment as structured array of information, to be
-        * stored in recent change entries and used to display formatted
-        * comments for wikibase changes in recent changes, watchlist, etc.
+        * Returns a human readable comment representing the given changes.
+        *
+        * @param EntityChange[] $changes
+        *
+        * @throws MWException
+        * @return string
+        */
+       private function getEditCommentMulti( array $changes ) {
+               $comments = array();
+
+               foreach ( $changes as $change ) {
+                       $comments[] = $this->getEditComment( $change );
+               }
+
+               //@todo: handle overly long lists nicely!
+               return $this->language->semicolonList( $comments );
+       }
+
+       /**
+        * Returns a human readable comment representing the change.
         *
         * @since 0.4
         *
         * @param EntityChange $change the change to get a comment for
         *
         * @throws MWException
-        * @return array|string
+        * @return string
         */
        public function getEditComment( EntityChange $change ) {
                $siteLinkDiff = $change instanceof ItemChange
                        ? $change->getSiteLinkDiff()
                        : null;
-               $action = $change->getAction();
-               $comment = $change->getComment();
 
-               $commentCreator = new SiteLinkCommentCreator( 
$this->localSiteId );
-               $editComment = $commentCreator->getEditComment( $siteLinkDiff, 
$action, $comment );
+               $editComment = '';
 
-               if ( is_array( $editComment ) && !isset( 
$editComment['message'] ) ) {
-                       throw new MWException( 'getEditComment returned an 
empty comment' );
+               if ( !empty( $siteLinkDiff ) ) {
+                       $action = $change->getAction();
+                       $commentCreator = new SiteLinkCommentCreator( 
$this->language, $this->localSiteId );
+                       $siteLinkComment = $commentCreator->getEditComment( 
$siteLinkDiff, $action );
+                       $editComment = $siteLinkComment === null ? '' : 
$siteLinkComment;
                }
 
+               if ( $editComment == '' ) {
+                       $editComment = $this->expandAutoComments( 
$change->getComment(), $change->getEntityId() );
+               }
+
+               if ( $editComment == '' ) {
+                       // If there is no comment, use something generic. This 
shouldn't happen.
+                       $editComment = $this->msg( 'wikibase-comment-update' );
+               }
+
+               Assert::postcondition( is_string( $editComment ), '$editComment 
must be a string' );
                return $editComment;
        }
 
+       /**
+        * @param string $entityType
+        *
+        * @return string The message key prefix to use for the given entity 
type
+        */
+       private function getMessagePrefix( $entityType ) {
+               return 'wikibase-' . $entityType;
+       }
+
+       /**
+        * @param string $summary An edit summary string, possibly containing a
+        *        localizable auto-comment block.
+        * @param EntityId $entityId The ID of the entity the edit summary 
applies to
+        *
+        * @return string An edit summary string with the localizable 
auto-comment block
+        *         expanded to human readable form.
+        */
+       private function expandAutoComments( $summary, EntityId $entityId ) {
+               $messagePrefix = $this->getMessagePrefix( 
$entityId->getEntityType() );
+
+               $commentFormatter = new AutoCommentFormatter( $this->language, 
$messagePrefix );
+               return $commentFormatter->expandAutoComments( $summary );
+       }
+
+       /**
+        * @param string $key
+        *
+        * @return Message
+        * @throws MWException
+        */
+       private function msg( $key ) {
+               $params = func_get_args();
+               array_shift( $params );
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+
+               return wfMessage( $key, $params )->inLanguage( $this->language 
);
+       }
+
 }
diff --git a/client/includes/SiteLinkCommentCreator.php 
b/client/includes/SiteLinkCommentCreator.php
index 34c5c37..fde6419 100644
--- a/client/includes/SiteLinkCommentCreator.php
+++ b/client/includes/SiteLinkCommentCreator.php
@@ -7,6 +7,9 @@
 use Diff\DiffOp\DiffOpAdd;
 use Diff\DiffOp\DiffOpChange;
 use Diff\DiffOp\DiffOpRemove;
+use Language;
+use Message;
+use MWException;
 
 /**
  * Creates an array structure with comment information for storing
@@ -17,6 +20,7 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
+ * @author Daniel Kinzler
  */
 class SiteLinkCommentCreator {
 
@@ -26,10 +30,17 @@
        private $siteId;
 
        /**
+        * @var Language
+        */
+       private $language;
+
+       /**
+        * @param Language $language
         * @param string $siteId
         */
-       public function __construct( $siteId ) {
+       public function __construct( Language $language, $siteId ) {
                $this->siteId = $siteId;
+               $this->language = $language;
        }
 
        /**
@@ -40,20 +51,20 @@
         *
         * @param Diff|null $siteLinkDiff
         * @param string $action e.g. 'remove', see the constants in 
EntityChange
-        * @param string $comment
         *
-        * @return array|string
+        * @return string|null A human readable edit summary (limited wikitext),
+        *         or null if no summary could be created for the sitelink 
change.
         */
-       public function getEditComment( Diff $siteLinkDiff = null, $action, 
$comment ) {
+       public function getEditComment( Diff $siteLinkDiff = null, $action ) {
                if ( $siteLinkDiff !== null && !$siteLinkDiff->isEmpty() ) {
-                       $siteLinkComment = $this->getSiteLinkComment( $action, 
$siteLinkDiff );
+                       $siteLinkMessage = $this->getSiteLinkMessage( $action, 
$siteLinkDiff );
 
-                       if ( !empty( $siteLinkComment ) ) {
-                               return $siteLinkComment;
+                       if ( !empty( $siteLinkMessage ) ) {
+                               return $this->generateComment( $siteLinkMessage 
);
                        }
                }
 
-               return $comment;
+               return null;
        }
 
        /**
@@ -65,7 +76,7 @@
         *
         * @return array|null
         */
-       private function getSiteLinkComment( $action, Diff $siteLinkDiff ) {
+       private function getSiteLinkMessage( $action, Diff $siteLinkDiff ) {
                if ( $siteLinkDiff->isEmpty() ) {
                        return null;
                }
@@ -86,7 +97,7 @@
                                if ( array_key_exists( 'name', $diffOp ) ) {
                                        $diffOp = $diffOp['name'];
                                } else {
-                                       // change to badges only
+                                       // change to badges only, use original 
message
                                        return null;
                                }
                        }
@@ -97,10 +108,8 @@
                        if ( $diffOpCount === 1 ) {
                                $params = $this->getSiteLinkChangeParams( 
$diffOps );
                        } else {
-                               // @todo report how many changes
-                               $params = array(
-                                       'message' => 'wikibase-comment-update'
-                               );
+                               // multiple changes, use original message
+                               return null;
                        }
                }
 
@@ -205,8 +214,6 @@
                } elseif ( $diffOp instanceof DiffOpChange ) {
                        $params['message'] = 'wikibase-comment-sitelink-change';
 
-                       // FIXME: this code appears to be doing something 
incorrect as "best effort"
-                       // rather than allowing for proper error handling
                        $params['sitelink'] = array(
                                'oldlink' => array(
                                        'site' => $siteId,
@@ -225,4 +232,54 @@
                return $params;
        }
 
+       /**
+        * @param string $siteId
+        * @param string $pageTitle
+        *
+        * @return string wikitext interwiki link
+        */
+       private function getSitelinkWikitext( $siteId, $pageTitle ) {
+               //TODO: make this nicer; The siteId may not be the nav ID!
+               return "[[:$siteId:$pageTitle]]";
+       }
+
+       /**
+        * @param string $key
+        *
+        * @return Message
+        * @throws MWException
+        */
+       private function msg( $key ) {
+               $params = func_get_args();
+               array_shift( $params );
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+
+               return wfMessage( $key, $params )->inLanguage( $this->language 
);
+       }
+
+       /**
+        * @param array $messageSpec
+        *
+        * @return string An edit summary (as limited wikitext).
+        */
+       private function generateComment( array $messageSpec ) {
+               $key = $messageSpec['message'];
+               $args = array();
+
+               if ( isset( $messageSpec['sitelink']['oldlink'] ) ) {
+                       $link = $messageSpec['sitelink']['oldlink'];
+                       $args[] = $this->getSitelinkWikitext( $link['site'], 
$link['page'] );
+               }
+
+               if ( isset( $messageSpec['sitelink']['newlink'] ) ) {
+                       $link = $messageSpec['sitelink']['newlink'];
+                       $args[] = $this->getSitelinkWikitext( $link['site'], 
$link['page'] );
+               }
+
+               $msg = $this->msg( $key, $args );
+               return $msg->text();
+       }
+
 }
diff --git a/client/includes/recentchanges/ExternalRecentChange.php 
b/client/includes/recentchanges/ExternalRecentChange.php
index e98fea2..39274e6 100644
--- a/client/includes/recentchanges/ExternalRecentChange.php
+++ b/client/includes/recentchanges/ExternalRecentChange.php
@@ -9,6 +9,8 @@
 /**
  * @since 0.5
  *
+ * @todo test case!
+ *
  * @licence GNU GPL v2+
  * @author Katie Filbert < [email protected] >
  */
@@ -54,6 +56,7 @@
                }
 
                $time = isset( $metadata['time'] ) ? $metadata['time'] : 
wfTimestamp( TS_MW );
+               $comment = isset( $attribs['comment'] ) ? $attribs['comment'] : 
'';
 
                $this->mAttribs = array(
                        'rc_namespace' => $title->getNamespace(),
@@ -70,7 +73,7 @@
                        'rc_last_oldid' => $title->getLatestRevID(),
                        'rc_params' => serialize( $attribs ),
                        'rc_cur_id' => $title->getArticleID(),
-                       'rc_comment' => '',
+                       'rc_comment' => $comment,
                        'rc_timestamp' => $time,
                        'rc_log_action' => '',
                        'rc_source' => 'wb'
diff --git a/client/tests/phpunit/includes/Changes/ChangeHandlerTest.php 
b/client/tests/phpunit/includes/Changes/ChangeHandlerTest.php
index 9e695b4..d1b1a88 100644
--- a/client/tests/phpunit/includes/Changes/ChangeHandlerTest.php
+++ b/client/tests/phpunit/includes/Changes/ChangeHandlerTest.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Client\Tests\Changes;
 
 use ArrayIterator;
+use Language;
 use MediaWikiTestCase;
 use Title;
 use Wikibase\Change;
@@ -77,6 +78,7 @@
                        $titleFactory,
                        $updater ?: new MockPageUpdater(),
                        $changeListTransformer,
+                       Language::factory( 'qqx' ),
                        'enwiki',
                        true
                );
@@ -249,87 +251,61 @@
                                $changes['item-deletion-linked'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               array( 'message' => 'wikibase-comment-remove' )
+                               '(wikibase-comment-remove)'
                        ),
                        array( // #1
                                $changes['set-de-label'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               'set-de-label:1|'
+                               '/* (wikibase-item-summary-set-de-label: 1, ) 
*/ bla bla'
                        ),
                        array( // #2
                                $changes['add-claim'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               'add-claim:1|'
+                               '/* (wikibase-item-summary-add-claim: 1, ) */ 
bla bla'
                        ),
                        array( // #3
                                $changes['remove-claim'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               'remove-claim:1|'
+                               '/* (wikibase-item-summary-remove-claim: 1, ) 
*/ bla bla'
                        ),
                        array( // #4
                                $changes['set-dewiki-sitelink'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               array(
-                                       'sitelink' => array(
-                                               'newlink' => array( 'site' => 
'dewiki', 'page' => 'Dummy' ),
-                                       ),
-                                       'message' => 
'wikibase-comment-sitelink-add'
-                               )
+                               '(wikibase-comment-sitelink-add: 
[[:dewiki:Dummy]])'
                        ),
                        array( // #5
                                $changes['change-dewiki-sitelink'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               array(
-                                       'sitelink' => array(
-                                               'oldlink' => array( 'site' => 
'dewiki', 'page' => 'Dummy' ),
-                                               'newlink' => array( 'site' => 
'dewiki', 'page' => 'Dummy2' ),
-                                       ),
-                                       'message' => 
'wikibase-comment-sitelink-change'
-                               )
+                               '(wikibase-comment-sitelink-change: 
[[:dewiki:Dummy]], [[:dewiki:Dummy2]])',
                        ),
                        array( // #6
                                $changes['change-enwiki-sitelink'],
                                $dummy,
                                array( 'q100' => array( 'Emmy' ) ),
-                               array(
-                                       'sitelink' => array(
-                                               'oldlink' => array( 'site' => 
'enwiki', 'page' => 'Emmy' ),
-                                               'newlink' => array( 'site' => 
'enwiki', 'page' => 'Emmy2' ),
-                                       ),
-                                       'message' => 
'wikibase-comment-sitelink-change'
-                               )
+                               '(wikibase-comment-sitelink-change: 
[[:enwiki:Emmy]], [[:enwiki:Emmy2]])',
                        ),
                        array( // #7
                                $changes['remove-dewiki-sitelink'],
                                $dummy,
                                array( 'q100' => array( 'Emmy2' ) ),
-                               array(
-                                       'sitelink' => array(
-                                               'oldlink' => array( 'site' => 
'dewiki', 'page' => 'Dummy2' ),
-                                       ),
-                                       'message' => 
'wikibase-comment-sitelink-remove'
-                               )
+                               '(wikibase-comment-sitelink-remove: 
[[:dewiki:Dummy2]])',
                        ),
                        array( // #8
                                $changes['remove-enwiki-sitelink'],
                                $dummy,
                                array( 'q100' => array( 'Emmy2' ) ),
-                               array(
-                                       'message' => 'wikibase-comment-unlink'
-                               )
+                               '(wikibase-comment-unlink)',
                        ),
                        array( // #9
                                $changes['remove-enwiki-sitelink'],
                                $dummy,
                                array( 'q100' => array() ),
-                               array(
-                                       'message' => 'wikibase-comment-unlink'
-                               )
+                               '(wikibase-comment-unlink)',
                        ),
                );
        }
diff --git a/client/tests/phpunit/includes/SiteLinkCommentCreatorTest.php 
b/client/tests/phpunit/includes/SiteLinkCommentCreatorTest.php
index fac2798..25032c8 100644
--- a/client/tests/phpunit/includes/SiteLinkCommentCreatorTest.php
+++ b/client/tests/phpunit/includes/SiteLinkCommentCreatorTest.php
@@ -4,6 +4,7 @@
 
 use Diff\DiffOp\Diff\Diff;
 use Diff\DiffOp\DiffOpChange;
+use Language;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\ItemChange;
@@ -25,9 +26,9 @@
        /**
         * @dataProvider getEditCommentProvider
         */
-       public function testGetEditComment( Diff $siteLinkDiff, $action, 
$comment, $expected ) {
-               $commentCreator = new SiteLinkCommentCreator( 'enwiki' );
-               $comment = $commentCreator->getEditComment( $siteLinkDiff, 
$action, $comment );
+       public function testGetEditComment( Diff $siteLinkDiff, $action, 
$expected ) {
+               $commentCreator = new SiteLinkCommentCreator( 
Language::factory( 'qqx' ), 'enwiki' );
+               $comment = $commentCreator->getEditComment( $siteLinkDiff, 
$action );
 
                $this->assertEquals( $expected, $comment );
        }
@@ -41,7 +42,6 @@
                        $changes[] = array(
                                $update[0],
                                ItemChange::UPDATE,
-                               'wikibase-comment-update',
                                $update[1]
                        );
                }
@@ -49,15 +49,13 @@
                $changes[] = array(
                        $this->getDeleteDiff(),
                        ItemChange::REMOVE,
-                       'wikibase-comment-remove',
-                       array( 'message' => 'wikibase-comment-remove' )
+                       '(wikibase-comment-remove)'
                );
 
                $changes[] = array(
                        $this->getRestoreDiff(),
                        ItemChange::RESTORE,
-                       'wikibase-comment-restore',
-                       array( 'message' => 'wikibase-comment-restore' )
+                       '(wikibase-comment-restore)'
                );
 
                return $changes;
@@ -209,101 +207,47 @@
 
                $updates[] = array(
                        $this->getConnectDiff(),
-                       array( 'message' => 'wikibase-comment-linked' )
+                       '(wikibase-comment-linked)',
                );
 
                $updates[] = array(
                        $this->getUnlinkDiff(),
-                       array( 'message' => 'wikibase-comment-unlink' ),
+                       '(wikibase-comment-unlink)',
                );
 
                $updates[] = array(
                        $this->getLinkChangeDiff(),
-                       array(
-                               'message' => 'wikibase-comment-sitelink-change',
-                               'sitelink' => array(
-                                       'oldlink' => array(
-                                               'site' => 'enwiki',
-                                               'page' => 'Japan'
-                                       ),
-                                       'newlink' => array(
-                                               'site' => 'enwiki',
-                                               'page' => 'Tokyo'
-                                       )
-                               )
-                       )
+                       '(wikibase-comment-sitelink-change: [[:enwiki:Japan]], 
[[:enwiki:Tokyo]])',
                );
 
                $updates[] = array(
                        $this->getOldLinkChangeDiff(),
-                       array(
-                               'message' => 'wikibase-comment-sitelink-change',
-                               'sitelink' => array(
-                                       'oldlink' => array(
-                                               'site' => 'enwiki',
-                                               'page' => 'Japan'
-                                       ),
-                                       'newlink' => array(
-                                               'site' => 'enwiki',
-                                               'page' => 'Tokyo'
-                                       )
-                               )
-                       )
+                       '(wikibase-comment-sitelink-change: [[:enwiki:Japan]], 
[[:enwiki:Tokyo]])',
                );
 
                $updates[] = array(
                        $this->getBadgeChangeDiff(),
-                       'wikibase-comment-update',
+                       null, // changes to badges do not get a special message
                );
 
                $updates[] = array(
                        $this->getAddLinkDiff(),
-                       array(
-                               'message' => 'wikibase-comment-sitelink-add',
-                               'sitelink' => array(
-                                       'newlink' => array(
-                                               'site' => 'dewiki',
-                                               'page' => 'Japan'
-                                       )
-                               )
-                       )
+                       '(wikibase-comment-sitelink-add: [[:dewiki:Japan]])',
                );
 
                $updates[] = array(
                        $this->getAddMultipleLinksDiff(),
-                       array(
-                               'message' => 'wikibase-comment-update'
-                       )
+                       null, // currently multi-link diffs are not supported
                );
 
                $updates[] = array(
                        $this->getRemoveLinkDiff(),
-                       array(
-                               'message' => 'wikibase-comment-sitelink-remove',
-                               'sitelink' => array(
-                                       'oldlink' => array(
-                                               'site' => 'dewiki',
-                                               'page' => 'Japan'
-                                       )
-                               )
-                       )
+                       '(wikibase-comment-sitelink-remove: [[:dewiki:Japan]])',
                );
 
                $updates[] = array(
                        $this->getChangeLinkDiff(),
-                       array(
-                               'message' => 'wikibase-comment-sitelink-change',
-                               'sitelink' => array(
-                                       'oldlink' => array(
-                                               'site' => 'dewiki',
-                                               'page' => 'Japan'
-                                       ),
-                                       'newlink' => array(
-                                               'site' => 'dewiki',
-                                               'page' => 'Tokyo'
-                                       )
-                               )
-                       )
+                       '(wikibase-comment-sitelink-change: [[:dewiki:Japan]], 
[[:dewiki:Tokyo]])',
                );
 
                return $updates;
diff --git a/lib/includes/formatters/AutoCommentFormatter.php 
b/lib/includes/formatters/AutoCommentFormatter.php
index e864f24..d9e2eaa 100644
--- a/lib/includes/formatters/AutoCommentFormatter.php
+++ b/lib/includes/formatters/AutoCommentFormatter.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Lib;
 
 use Language;
+use Message;
+use MWException;
 
 /**
  * Formatter for machine-readable autocomments as generated by 
SummaryFormatter in the repo.
@@ -49,7 +51,7 @@
         *
         * @param string $auto the autocomment unformatted
         *
-        * @return string|null The localized summary, or null
+        * @return string|null The localized summary (HTML), or null
         */
        public function formatAutoComment( $auto ) {
                if ( !preg_match( '/^([a-z\-]+)\s*(:\s*(.*?)\s*)?$/', $auto, 
$matches ) ) {
@@ -60,7 +62,7 @@
                $args = isset( $matches[3] ) ? explode( '|', $matches[3] ) : 
array();
 
                // look up the message
-               $msg = wfMessage( $this->messagePrefix . '-summary-' . 
$matches[1] );
+               $msg = $this->msg( $this->messagePrefix . '-summary-' . 
$matches[1] );
 
                if ( !$msg->exists() || $msg->isDisabled() ) {
                        return null;
@@ -81,16 +83,16 @@
         * @param string $comment the localized comment, as returned by 
formatAutoComment()
         * @param boolean $post True if there is text after the comment, so a 
postfix separator is needed.
         *
-        * @return string
+        * @return string HTML.
         */
        public function wrapAutoComment( $pre, $comment, $post ) {
                if ( $pre ) {
                        # written summary $presep autocomment (summary /* 
section */)
-                       $pre = wfMessage( 'autocomment-prefix' )->inLanguage( 
$this->language )->escaped();
+                       $pre = $this->msg( 'autocomment-prefix' )->escaped();
                }
                if ( $post ) {
                        # autocomment $postsep written summary (/* section */ 
summary)
-                       $comment .= wfMessage( 'colon-separator' )->inLanguage( 
$this->language )->escaped();
+                       $comment .= $this->msg( 'colon-separator' )->escaped();
                }
                $comment = '<span class="autocomment">' . $comment . '</span>';
                $comment = $pre . $this->language->getDirMark()
@@ -100,4 +102,48 @@
                return $comment;
        }
 
+       /**
+        * @param string $summary An edit summary string, possibly containing a
+        *        localizable auto-comment block.
+        *
+        * @return string An edit summary string with the localizable 
auto-comment block
+        *         expanded to human readable form (as limited wikitext).
+        */
+       public function expandAutoComments( $summary ) {
+               $self = $this;
+
+               //NOTE: The matching code below is based on 
Linker::formatAutocomments()
+               //      and should be kept in sync!
+               $expanded = preg_replace_callback(
+                       '!(/\*\s*)(.*?)(\s*\*/)!',
+                       function ( $match ) use ( $self ) {
+                               $body = $match[2];
+
+                               $formatted = $self->formatAutoComment( $body );
+                               $body = $formatted !== null ? $formatted : 
$body;
+
+                               return $match[1] . $body . $match[3];
+                       },
+                       $summary
+               );
+
+               return $expanded;
+       }
+
+       /**
+        * @param string $key
+        *
+        * @return Message
+        * @throws MWException
+        */
+       private function msg( $key ) {
+               $params = func_get_args();
+               array_shift( $params );
+               if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+                       $params = $params[0];
+               }
+
+               return wfMessage( $key, $params )->inLanguage( $this->language 
);
+       }
+
 }
diff --git a/lib/tests/phpunit/changes/TestChanges.php 
b/lib/tests/phpunit/changes/TestChanges.php
index 57bbcdd..796ab51 100644
--- a/lib/tests/phpunit/changes/TestChanges.php
+++ b/lib/tests/phpunit/changes/TestChanges.php
@@ -199,7 +199,7 @@
                                        'rev_id' => $rev,
                                        'parent_id' => $rev -1,
                                        'user_text' => 'Some User',
-                                       'comment' => "$key:1|",
+                                       'comment' => "/* $key:1| */ bla bla",
                                );
 
                                $change->setMetadata( $meta );
diff --git a/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php 
b/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
index 9966f8a..8159c74 100644
--- a/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
+++ b/lib/tests/phpunit/formatters/AutoCommentFormatterTest.php
@@ -112,4 +112,42 @@
                $this->assertEquals( $expected, $value );
        }
 
+       public function provideExpandAutoComments() {
+               return array(
+                       'empty' => array(
+                               '',
+                               ''
+                       ),
+                       'no comment block' => array(
+                               'foo bar',
+                               'foo bar'
+                       ),
+                       'unparsable comment block' => array(
+                               'foo /* who likes kittens */ bar',
+                               'foo /* who likes kittens */ bar'
+                       ),
+                       'simple comment block' => array(
+                               '/* test-message */ bar',
+                               '/* (testing-summary-test-message) */ bar'
+                       ),
+                       'comment block with params' => array(
+                               'foo /* test-message:one|two */',
+                               'foo /* (testing-summary-test-message: one, 
two) */'
+                       ),
+                       'multiple comment blocks' => array(
+                               'foo /* test-message-one */ bar /* 
test-message-two */ zap',
+                               'foo /* (testing-summary-test-message-one) */ 
bar /* (testing-summary-test-message-two) */ zap'
+                       ),
+               );
+       }
+
+       /**
+        * @dataProvider provideExpandAutoComments
+        */
+       public function testExpandAutoComments( $summary, $expected ) {
+               $formatter = new AutoCommentFormatter( $this->language, 
'testing' );
+               $value = $formatter->expandAutoComments( $summary );
+               $this->assertEquals( $expected, $value );
+       }
+
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I75738c5b4e46e27237cbd543d4905164f8b0731d
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

Reply via email to