Mattflaschen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/349977 )

Change subject: WIP: Transform user ID from repo to client in change prop
......................................................................

WIP: Transform user ID from repo to client in change prop

Plus minor typo fix in ChangeHandler

Bug: T51315
Change-Id: Ie7b9c482cf6a0dd7215b34841efd86fb51be651a
---
M client/includes/Changes/ChangeHandler.php
M client/includes/RecentChanges/ExternalChangeFactory.php
M client/includes/RecentChanges/RecentChangeFactory.php
M client/includes/RecentChanges/RevisionData.php
M client/includes/WikibaseClient.php
M docs/change-propagation.wiki
M lib/includes/Changes/EntityChange.php
7 files changed, 146 insertions(+), 19 deletions(-)


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

diff --git a/client/includes/Changes/ChangeHandler.php 
b/client/includes/Changes/ChangeHandler.php
index 20910e9..172cc35 100644
--- a/client/includes/Changes/ChangeHandler.php
+++ b/client/includes/Changes/ChangeHandler.php
@@ -26,28 +26,28 @@
 class ChangeHandler {
 
        /**
-        * The change requites any rendered version of the page to be purged 
from the parser cache.
+        * The change requires any rendered version of the page to be purged 
from the parser cache.
         */
        const PARSER_PURGE_ACTION = 'parser';
 
        /**
-        * The change requites a LinksUpdate job to be scheduled to update any 
links
+        * The change requires a LinksUpdate job to be scheduled to update any 
links
         * associated with the page.
         */
        const LINKS_UPDATE_ACTION = 'links';
 
        /**
-        * The change requites any HTML output generated from the page to be 
purged from web cached.
+        * The change requires any HTML output generated from the page to be 
purged from web cached.
         */
        const WEB_PURGE_ACTION = 'web';
 
        /**
-        * The change requites an entry to be injected into the recentchanges 
table.
+        * The change requires an entry to be injected into the recentchanges 
table.
         */
        const RC_ENTRY_ACTION = 'rc';
 
        /**
-        * The change requites an entry to be injected into the revision table.
+        * The change requires an entry to be injected into the revision table.
         */
        const HISTORY_ENTRY_ACTION = 'history';
 
diff --git a/client/includes/RecentChanges/ExternalChangeFactory.php 
b/client/includes/RecentChanges/ExternalChangeFactory.php
index 77e2efa..b452537 100644
--- a/client/includes/RecentChanges/ExternalChangeFactory.php
+++ b/client/includes/RecentChanges/ExternalChangeFactory.php
@@ -111,6 +111,10 @@
                        throw new UnexpectedValueException( 'Invalid Wikibase 
change' );
                }
 
+               // TODO: Add central_user_id after
+               // Ie7b9c482cf6a0dd7215b34841efd86fb51be651a has been deployed 
long
+               // enough that there are no rows without this.
+               // Bug: T51315
                $keys = array( 'type', 'page_id', 'rev_id', 'parent_id', 
'object_id' );
 
                foreach ( $keys as $key ) {
diff --git a/client/includes/RecentChanges/RecentChangeFactory.php 
b/client/includes/RecentChanges/RecentChangeFactory.php
index b9c3e6a..c288183 100644
--- a/client/includes/RecentChanges/RecentChangeFactory.php
+++ b/client/includes/RecentChanges/RecentChangeFactory.php
@@ -35,12 +35,18 @@
        private $siteLinkCommentCreator;
 
        /**
+        * @var CentralIdLookup
+        */
+       private $centralIdLookup;
+
+       /**
         * @param Language $language
         * @param SiteLinkCommentCreator $siteLinkCommentCreator
         */
-       public function __construct( Language $language, SiteLinkCommentCreator 
$siteLinkCommentCreator ) {
+       public function __construct( Language $language, SiteLinkCommentCreator 
$siteLinkCommentCreator, CentralIdLookup $centralIdLookup ) {
                $this->language = $language;
                $this->siteLinkCommentCreator = $siteLinkCommentCreator;
+               $this->centralIdLookup = $centralIdLookup;
        }
 
        /**
@@ -115,8 +121,43 @@
                        'wikibase-repo-change' => $metadata,
                );
 
+               $repoUserId = $fields['user_id'];
+
+               if ( $repoUserId === 0 ) {
+                       // Logged out on repo just copied to client
+                       $clientUserId = 0;
+               } else {
+                       // Use -1 as client RC user ID, if we know they're 
logged in, but
+                       // we can't determine their client user ID.  That will 
at least
+                       // properly filter them as logged in.
+
+                       // Temporary compatibility until 
Ie7b9c482cf6a0dd7215b34841efd86fb51be651a
+                       // has been deployed long enough that all rows have it.
+                       if ( array_key_exists( 'central_user_id', $metadata ) ) 
{
+                               // See change-propagation.wiki for why it can 
be 0 other than pre-deploy
+                               // rows.
+                               $centralUserId = $metadata['central_user_id'];
+                       } else {
+                               $centralUserId = 0;
+                       }
+
+                       if ( $centralUserId === 0 ) {
+                               $clientUserId = -1;
+                       } else {
+                               $clientUser = 
$this->centralIdLookup->localUserFromCentralId(
+                                       $centralUserId
+                               );
+
+                               if ( $clientUser === null ) {
+                                       $clientUserId = -1;
+                               } else {
+                                       $clientUserId = $clientUser->getId();
+                               }
+                       }
+               }
+
                return array(
-                       'rc_user' => 0,
+                       'rc_user' => $clientUserId,
                        'rc_user_text' => $userText,
                        'rc_comment' => $comment,
                        'rc_type' => RC_EXTERNAL,
diff --git a/client/includes/RecentChanges/RevisionData.php 
b/client/includes/RecentChanges/RevisionData.php
index 37a291d..48ab89d 100644
--- a/client/includes/RecentChanges/RevisionData.php
+++ b/client/includes/RecentChanges/RevisionData.php
@@ -70,6 +70,17 @@
        }
 
        /**
+        * Gets the central user ID.  This should be from CentralIdLookup,
+        * with the repo wiki and client wiki being part of the same central
+        * system.
+        *
+        * @return int
+        */
+       public function getCentralUserId() {
+               return $this->changeParams['central_user_id'];
+       }
+
+       /**
         * @return int
         */
        public function getPageId() {
diff --git a/client/includes/WikibaseClient.php 
b/client/includes/WikibaseClient.php
index 2666f5a..40371be 100644
--- a/client/includes/WikibaseClient.php
+++ b/client/includes/WikibaseClient.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Client;
 
+use CentralIdLookup;
 use DataTypes\DataTypeFactory;
 use DataValues\Deserializers\DataValueDeserializer;
 use DataValues\Geo\Values\GlobeCoordinateValue;
@@ -1192,7 +1193,8 @@
                                $this->getContentLanguage(),
                                $this->siteLookup,
                                $this->settings->getSetting( 'siteGlobalID' )
-                       )
+                       ),
+                       CentralIdLookup::factory()
                );
        }
 
diff --git a/docs/change-propagation.wiki b/docs/change-propagation.wiki
index 0f7031a..a5ef1d6 100644
--- a/docs/change-propagation.wiki
+++ b/docs/change-propagation.wiki
@@ -38,10 +38,11 @@
 * change_time, a varbinary(14) the time at which the edit was made
 * change_object_id, a varbinary(14) containing the entity ID
 * change_revision_id, a int(10) containing the revision ID
-* change_user_id, a int(10) containing the user id
+* change_user_id, a int(10) containing the original (repository) user id, or 0 
for logged out users.
 * change_info, a mediumblob containing a JSON structure with additional 
information about the change. Well known top level fields are:
 ** "diff": a serialized diff, as produced by EntityDiffer
 ** "metadata", a JSON object representing essential revision meta data, using 
the following fields:
+*** "central_user_id": the central user ID (int).  0 if the repo is not 
connected to a central store, the action was by a logged out user, the 
particular user is not attached on the repo, or the user is restricted (uses 
AUDIENCE_PUBLIC)
 *** "user_text": the user name (string)
 *** "page_id": the id of the wiki page containing the entity on the repo (int)
 *** "rev_id": the id of the revision created by this change on the repo (int)
diff --git a/lib/includes/Changes/EntityChange.php 
b/lib/includes/Changes/EntityChange.php
index bf6dbe1..dbb1d2a 100644
--- a/lib/includes/Changes/EntityChange.php
+++ b/lib/includes/Changes/EntityChange.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase;
 
+use CentralIdLookup;
 use Deserializers\Deserializer;
 use Diff\DiffOp\DiffOp;
 use Diff\DiffOpFactory;
@@ -37,6 +38,22 @@
         * @var EntityId|null
         */
        private $entityId = null;
+
+       protected function getCentralIdLookup() {
+               $centralIdLookup = CentralIdLookup::factory();
+
+               if ( $centralIdLookup !== null &&
+
+                       // LocalIdLookup is the default for standalone wikis.  
However,
+                       // it will map to the wrong user unless repo and client
+                       // are both using it, and using the same shared user 
tables.
+                       !( $centralIdLookup instanceof LocalIdLookup )
+               ) {
+                       return $centralIdLookup;
+               }
+
+               return null;
+       }
 
        /**
         * @return string
@@ -104,6 +121,7 @@
                        'bot',
                        'rev_id',
                        'parent_id',
+                       'central_user_id',
                        'user_text',
                        'comment'
                );
@@ -149,29 +167,78 @@
        public function setMetadataFromRC( RecentChange $rc ) {
                $this->setFields( array(
                        'revision_id' => $rc->getAttribute( 'rc_this_oldid' ),
-                       'user_id' => $rc->getAttribute( 'rc_user' ),
                        'time' => $rc->getAttribute( 'rc_timestamp' ),
                ) );
 
                $this->setMetadata( array(
-                       'user_text' => $rc->getAttribute( 'rc_user_text' ),
                        'bot' => $rc->getAttribute( 'rc_bot' ),
                        'page_id' => $rc->getAttribute( 'rc_cur_id' ),
                        'rev_id' => $rc->getAttribute( 'rc_this_oldid' ),
                        'parent_id' => $rc->getAttribute( 'rc_last_oldid' ),
                        'comment' => $rc->getAttribute( 'rc_comment' ),
                ) );
+
+               $this->addUserMetadata(
+                       $rc->getAttribute( 'rc_user' ),
+                       $rc->getAttribute( 'rc_user_text' )
+               );
        }
 
        /**
-        * @param User $user
+        * Add fields and metadata related to the user.
         *
+        * This does not touch other fields or metadata.
+        *
+        * @param int $repoUserId User ID on wiki where change was made, or 0 
for anon
+        * @param string $repoUserText User text on wiki where change was made, 
for either
+        *   logged in user or anon
+        */
+       protected function addUserMetadata( $repoUserId, $repoUserText ) {
+               static $centralIdLookup = null;
+
+               // CentralIdLookup::factory can already return null, plus we 
then
+               // filter it.  So this distinguishes between "uninitalized" and
+               // "initialized to something unusable".
+               static $isCentralIdLookupInitialized = false;
+
+               $this->setFields( array(
+                       'user_id' => $repoUserId,
+               ) );
+
+               $metadata = [
+                       'user_text' => $repoUserText,
+               ];
+
+               $centralUserId = 0;
+
+               if ( $repoUserId !== 0 ) {
+                       $repoUser = User::newFromId( $repoUserId );
+
+                       if ( !$isCentralIdLookupInitialized ) {
+                               $centralIdLookup = $this->getCentralIdLookup();
+
+                               $isCentralIdLookupInitialized = true;
+                       }
+
+                       if ( $centralIdLookup !== null ) {
+                               $centralUserId = 
$centralIdLookup->centralIdFromLocalUser(
+                                       $repoUser
+                               );
+                       }
+               }
+               $metadata['central_user_id'] = $centralUserId;
+
+               $this->setMetadata( $metadata );
+       }
+
+       /**
         * @todo rename to setUserInfo
         */
        public function setMetadataFromUser( User $user ) {
-               $this->setFields( array(
-                       'user_id' => $user->getId(),
-               ) );
+               $this->addUserMetadata(
+                       $user->getId(),
+                       $user->getName()
+               );
 
                // TODO: init page_id etc in getMetadata, not here!
                $metadata = array_merge( array(
@@ -182,8 +249,6 @@
                        $this->getMetadata()
                );
 
-               $metadata['user_text'] = $user->getName();
-
                $this->setMetadata( $metadata );
        }
 
@@ -193,7 +258,6 @@
        public function setRevisionInfo( Revision $revision ) {
                $this->setFields( array(
                        'revision_id' => $revision->getId(),
-                       'user_id' => $revision->getUser(),
                        'time' => $revision->getTimestamp(),
                ) );
 
@@ -208,12 +272,16 @@
                }
 
                $this->setMetadata( array(
-                       'user_text' => $revision->getUserText(),
                        'page_id' => $revision->getPage(),
                        'parent_id' => $revision->getParentId(),
                        'comment' => $revision->getComment(),
                        'rev_id' => $revision->getId(),
                ) );
+
+               $this->addUserMetadata(
+                       $revision->getUser(),
+                       $revision->getUserText()
+               );
        }
 
        /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie7b9c482cf6a0dd7215b34841efd86fb51be651a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>

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

Reply via email to