Daniel Kinzler has uploaded a new change for review.

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

Change subject: Generate edit summary in ExternalChangeFactory.
......................................................................

Generate edit summary in ExternalChangeFactory.

This is part of the refactoring of the edit summary handling code that will
allow us to re-use the repo side summary generation code on the client.

This change implements points A, B, and C of part 4 of T101836#1414639.

Bug: T111039
Change-Id: I0fed0a5280450af6caeff9328e07f9f3409ee2b7
---
M client/WikibaseClient.hooks.php
M client/includes/recentchanges/ChangeLineFormatter.php
M client/includes/recentchanges/ExternalChangeFactory.php
M client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
M client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
5 files changed, 85 insertions(+), 81 deletions(-)


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

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 0725b88..0256fa0 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -143,7 +143,8 @@
                if ( $type == RC_EXTERNAL ) {
                        $wikibaseClient = WikibaseClient::getDefaultInstance();
                        $changeFactory = new ExternalChangeFactory(
-                               $wikibaseClient->getSettings()->getSetting( 
'repoSiteId' )
+                               $wikibaseClient->getSettings()->getSetting( 
'repoSiteId' ),
+                               $wikibaseClient->getContentLanguage()
                        );
 
                        try {
diff --git a/client/includes/recentchanges/ChangeLineFormatter.php 
b/client/includes/recentchanges/ChangeLineFormatter.php
index 41a5310..219bcb9 100644
--- a/client/includes/recentchanges/ChangeLineFormatter.php
+++ b/client/includes/recentchanges/ChangeLineFormatter.php
@@ -4,6 +4,7 @@
 
 use Language;
 use Linker;
+use Sanitizer;
 use Title;
 use User;
 use Wikibase\Client\RepoLinker;
@@ -52,7 +53,7 @@
         * @param int $count
         * @param string $flag - flag formatted by 
ChangesList::recentChangesFlags()
         *
-        * @return string
+        * @return string HTML
         */
        public function format( ExternalChange $externalChange, Title $title, 
$count, $flag ) {
                $changeType = $externalChange->getChangeType();
@@ -73,13 +74,13 @@
 
                $line .= $this->formatTimestamp( $rev->getTimestamp() );
                $line .= $this->formatUserLinks( $rev->getUserName() );
-               $line .= $this->formatComment( $rev->getComment() );
+               $line .= Sanitizer::removeHTMLtags( $rev->getComment() );
 
                return $line;
        }
 
        /**
-        * @return string
+        * @return string HTML
         */
        private function changeSeparator() {
                return ' <span class="mw-changeslist-separator">. .</span> ';
@@ -88,7 +89,7 @@
        /**
         * @param string $timestamp
         *
-        * @return string
+        * @return string HTML
         */
        private function formatTimestamp( $timestamp ) {
                return wfMessage( 'semicolon-separator' )->text()
@@ -100,7 +101,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function formatUserLinks( $userName ) {
                $links = $this->buildUserLinks( $userName );
@@ -117,7 +118,7 @@
         *
         * @param string[] $links
         *
-        * @return string
+        * @return string HTML
         */
        private function formatIpUserLinks( array $links ) {
                $userlinks = $links['contribs'];
@@ -135,7 +136,7 @@
         *
         * @param string[] $links
         *
-        * @return string
+        * @return string HTML
         */
        private function formatRegisteredUserLinks( array $links ) {
                $userlinks = $links['user'];
@@ -158,7 +159,7 @@
        /**
         * @param EntityId $entityId
         *
-        * @return string
+        * @return string HTML
         */
        private function formatEntityLink( EntityId $entityId ) {
                $entityLink = $this->repoLinker->buildEntityLink( $entityId );
@@ -168,7 +169,7 @@
        }
 
        /**
-        * @return string
+        * @return string HTML
         */
        private function formatDeletionLogLink() {
                $logLink = $this->repoLinker->formatLink(
@@ -184,7 +185,7 @@
         * @param RevisionData $rev
         * @param int $count
         *
-        * @return string
+        * @return string HTML
         */
        private function formatDiffHist( EntityId $entityId, RevisionData $rev, 
$count ) {
                $diffLink = $this->buildDiffLink( $entityId, $rev, $count );
@@ -200,7 +201,7 @@
         * @param RevisionData $rev
         * @param int $count
         *
-        * @return string
+        * @return string HTML
         */
        private function buildDiffLink( EntityId $entityId, RevisionData $rev, 
$count ) {
                $params = array(
@@ -226,7 +227,7 @@
         * @param EntityId $entityId
         * @param RevisionData $rev
         *
-        * @return string
+        * @return string HTML
         */
        private function buildHistoryLink( EntityId $entityId, RevisionData 
$rev ) {
                $titleText = $this->repoLinker->getEntityTitle( $entityId );
@@ -251,7 +252,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserLink( $userName ) {
                return $this->repoLinker->formatLink(
@@ -268,7 +269,7 @@
         * @param string $userName
         * @param string $text
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserContribsLink( $userName, $text = null ) {
                // @todo: know how the repo is localised. it's english now
@@ -285,7 +286,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserTalkLink( $userName ) {
                // @todo: localize this once we can localize namespaces on the 
repo
@@ -298,7 +299,7 @@
        /**
         * @param string $userName
         *
-        * @return string[]
+        * @return string[] List of HTML links
         */
        private function buildUserLinks( $userName ) {
                $links = array();
@@ -313,24 +314,6 @@
                }
 
                return $links;
-       }
-
-       /**
-        * @param array $comment
-        *
-        * @return string
-        */
-       private function formatComment( array $comment ) {
-               $commentMsg = wfMessage( $comment['key'] );
-
-               if ( isset( $comment['numparams'] ) ) {
-                       $commentMsg->numParams( $comment['numparams'] );
-               }
-
-               // fixme: find a way to inject or not use Linker
-               return Linker::commentBlock(
-                       $commentMsg->inLanguage( $this->lang )->text()
-               );
        }
 
 }
diff --git a/client/includes/recentchanges/ExternalChangeFactory.php 
b/client/includes/recentchanges/ExternalChangeFactory.php
index cddd1f9..eac4a8a 100644
--- a/client/includes/recentchanges/ExternalChangeFactory.php
+++ b/client/includes/recentchanges/ExternalChangeFactory.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Client\RecentChanges;
 
 use InvalidArgumentException;
+use Language;
+use Linker;
 use RecentChange;
 use UnexpectedValueException;
 use Wikibase\DataModel\Entity\ItemId;
@@ -12,6 +14,7 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < aude.w...@gmail.com >
+ * @author Daniel Kinzler
  */
 class ExternalChangeFactory {
 
@@ -21,10 +24,17 @@
        private $repoSiteId;
 
        /**
-        * @param string $repoSiteId
+        * @var Language
         */
-       public function __construct( $repoSiteId ) {
+       private $summaryLanguage;
+
+       /**
+        * @param string $repoSiteId
+        * @param Language $summaryLanguage Language to use when generating 
edit summaries
+        */
+       public function __construct( $repoSiteId, Language $summaryLanguage ) {
                $this->repoSiteId = $repoSiteId;
+               $this->summaryLanguage = $summaryLanguage;
        }
 
        /**
@@ -55,13 +65,15 @@
                $repoId = isset( $changeParams['site_id'] )
                        ? $changeParams['site_id'] : $this->repoSiteId;
 
+               $commentOverride = $this->extractCommentOverride( $changeParams 
);
+
                return new RevisionData(
                        $recentChange->getAttribute( 'rc_user_text' ),
                        $changeParams['page_id'],
                        $changeParams['rev_id'],
                        $changeParams['parent_id'],
                        $recentChange->getAttribute( 'rc_timestamp' ),
-                       $this->extractComment( $changeParams ),
+                       $commentOverride ?: $recentChange->getAttribute( 
'rc_comment' ),
                        $repoId
                );
        }
@@ -168,7 +180,7 @@
         *
         * @return string
         */
-       private function parseComment( $comment, $type ) {
+       private function parseAutoComment( $comment, $type ) {
                $newComment = array(
                        'key' => 'wikibase-comment-update'
                );
@@ -192,38 +204,54 @@
        }
 
        /**
-        * @fixme refactor comments handling!
+        * @param array $comment
         *
+        * @return string
+        */
+       private function formatComment( array $comment ) {
+               $commentMsg = wfMessage( $comment['key'] )->inLanguage( 
$this->summaryLanguage );
+
+               if ( isset( $comment['numparams'] ) ) {
+                       $commentMsg->numParams( $comment['numparams'] );
+               }
+
+               // fixme: find a way to inject or not use Linker
+               return Linker::commentBlock(
+                       $commentMsg->text()
+               );
+       }
+
+       /**
         * @param array $changeParams
         *
         * @return string
         */
-       private function extractComment( $changeParams ) {
-               $comment = array(
-                       'key' => 'wikibase-comment-update'
-               );
-
-               //TODO: If $changeParams['changes'] is set, this is a coalesced 
change.
-               //        Combine all the comments! Up to some max length?
+       private function extractCommentOverride( array $changeParams ) {
+               // NOTE: We want to get rid of the comment and 
composite-comment fields in $changeParams
+               // in the future, see 
https://phabricator.wikimedia.org/T101836#1414639 part 3.
                if ( array_key_exists( 'composite-comment', $changeParams ) ) {
                        $comment['key'] = 'wikibase-comment-multi';
                        $comment['numparams'] = $this->countCompositeComments( 
$changeParams['composite-comment'] );
-               } elseif ( array_key_exists( 'comment', $changeParams ) ) {
-                       $comment = $this->parseComment( 
$changeParams['comment'], $changeParams['type'] );
-               }
 
-               return $comment;
+                       return $this->formatComment( $comment );
+               } elseif ( array_key_exists( 'comment', $changeParams ) ) {
+                       $comment = $this->parseAutoComment( 
$changeParams['comment'], $changeParams['type'] );
+
+                       return $this->formatComment( $comment );
+               } else {
+                       // no override
+                       return false;
+               }
        }
 
        /**
         * normalizes for extra empty comment in rc_params (see bug T47812)
-        * @fixme: can remove at some point in the future
         *
         * @param array $comments
         *
         * @return int
         */
-       private function countCompositeComments( $comments ) {
+       private function countCompositeComments( array $comments ) {
                $compositeComments = array_filter( $comments );
 
                return count( $compositeComments );
diff --git 
a/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php 
b/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
index 084abe3..6de8f4f 100644
--- a/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
+++ b/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
@@ -57,7 +57,7 @@
                $context = $this->getTestContext();
 
                $changesList = ChangesList::newFromContext( $context );
-               $changeFactory = new ExternalChangeFactory( 'testrepo' );
+               $changeFactory = new ExternalChangeFactory( 'testrepo', 
Language::factory( 'en' ) );
                $externalChange = $changeFactory->newFromRecentChange( 
$recentChange );
 
                $formatter = new ChangeLineFormatter(
@@ -74,7 +74,7 @@
                );
 
                foreach ( $expectedTags as $key => $tag ) {
-                       $this->assertTag( $tag, $formattedLine, $key );
+                       $this->assertTag( $tag, $formattedLine, $key . "\n\t" . 
$formattedLine );
                }
 
                foreach ( $patterns as $pattern ) {
diff --git 
a/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php 
b/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
index 3b4957d..5f9d063 100644
--- a/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
+++ b/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Client\Tests\RecentChanges;
 
+use Language;
 use RecentChange;
 use Wikibase\Client\RecentChanges\ExternalChange;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
@@ -21,12 +22,16 @@
  */
 class ExternalChangeFactoryTest extends \MediaWikiTestCase {
 
+       private function getExternalChangeFactory() {
+               return new ExternalChangeFactory( 'testrepo', 
Language::factory( 'qqx' ) );
+       }
+
        public function testNewFromRecentChange_itemUpdated() {
                $commentData = 'wikibase-comment-update';
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
@@ -46,7 +51,7 @@
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
@@ -61,7 +66,7 @@
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~add', false );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-linked', 'add' ),
@@ -74,7 +79,7 @@
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
@@ -99,14 +104,11 @@
 
                $expected = new ExternalChange(
                        new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => 'wikibase-comment-multi',
-                               'numparams' => 2
-                       ) ),
+                       $this->makeRevisionData( ' <span 
class="comment">((wikibase-comment-multi: 2))</span>' ),
                        'update'
                );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $expected,
@@ -119,15 +121,7 @@
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', true );
 
-               $expected = new ExternalChange(
-                       new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => 'wikibase-comment-update'
-                       ) ),
-                       'update'
-               );
-
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
@@ -140,7 +134,7 @@
 
                $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
@@ -149,29 +143,27 @@
        }
 
        /**
-        * @param string $expectedComment
+        * @param string $expectedCommentKey
         * @param string $expectedType
         *
         * @return ExternalChange
         */
-       private function makeExpectedExternalChange( $expectedComment, 
$expectedType ) {
+       private function makeExpectedExternalChange( $expectedCommentKey, 
$expectedType ) {
                return new ExternalChange(
                        new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => $expectedComment
-                       ) ),
+                       $this->makeRevisionData( ' <span class="comment">((' . 
$expectedCommentKey . '))</span>' ),
                        $expectedType
                );
        }
 
-       private function makeRevisionData( array $comment ) {
+       private function makeRevisionData( $comment ) {
                return new RevisionData(
                        'Cat',
                        5,
                        92,
                        90,
                        '20130819111741',
-                       $comment,
+                       strval( $comment ),
                        'testrepo'
                );
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0fed0a5280450af6caeff9328e07f9f3409ee2b7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to