jenkins-bot has submitted this change and it was merged.

Change subject: Update CentralAuthUser::chooseHomeWiki() per Keegan
......................................................................


Update CentralAuthUser::chooseHomeWiki() per Keegan

1. User groups in order of: checkuser, oversight, bureaucrat, sysop
2. Edit count
3. Earliest registration date
4. Randomness

https://meta.wikimedia.org/wiki/Single_User_Login_finalisation_announcement/Schema_announcement
(not published yet).

Bug: T91703
Change-Id: I69a91cdde2a881f0fb46ebffca700c07acbf3de1
(cherry picked from commit 4fb5392afda7f0f93f4124b36ea55749b163cefe)
---
M includes/CentralAuthUser.php
M tests/CentralAuthUserTest.php
2 files changed, 106 insertions(+), 18 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/CentralAuthUser.php b/includes/CentralAuthUser.php
index 1c8a9b4..2e2d6dc 100644
--- a/includes/CentralAuthUser.php
+++ b/includes/CentralAuthUser.php
@@ -736,34 +736,46 @@
                }
 
                // Sysops get priority
-               $priorityGroups = array( 'sysop', 'bureaucrat', 'steward' );
-               $workingSet = array();
-               foreach ( $migrationSet as $wiki => $local ) {
-                       if ( array_intersect( $priorityGroups, $local['groups'] 
) ) {
-                               if ( $local['editCount'] ) {
-                                       // Ignore unused sysop accounts
-                                       $workingSet[$wiki] = $local;
+               $found = array();
+               $priorityGroups = array( 'checkuser', 'oversight', 
'bureaucrat', 'sysop' );
+               foreach ( $priorityGroups as $group ) {
+                       foreach ( $migrationSet as $wiki => $local ) {
+                               if ( in_array( $group, $local['groups'] ) ) {
+                                       $found[] = $wiki;
                                }
+                       }
+                       if ( count( $found ) === 1 ) {
+                               // Easy!
+                               return $found[0];
+                       } elseif ( $found ) {
+                               // We'll check edit counts now...
+                               break;
                        }
                }
 
-               if ( !$workingSet ) {
+               if ( !$found ) {
                        // No privileged accounts; look among the plebes...
-                       $workingSet = $migrationSet;
+                       $found = array_keys( $migrationSet );
                }
 
                $maxEdits = -1;
                $homeWiki = null;
-               foreach ( $workingSet as $wiki => $local ) {
-                       if ( $local['editCount'] > $maxEdits ) {
+               foreach ( $found as $wiki ) {
+                       $count = $migrationSet[$wiki]['editCount'];
+                       if ( $count > $maxEdits ) {
                                $homeWiki = $wiki;
-                               $maxEdits = $local['editCount'];
+                               $maxEdits = $count;
+                       } elseif ( $count === $maxEdits ) {
+                               // Tie, check earlier registration
+                               // Note that registration might be "null", 
which means they're a super old account.
+                               if ( $migrationSet[$wiki]['registration'] < 
$migrationSet[$homeWiki]['registration'] ) {
+                                       $homeWiki = $wiki;
+                               } elseif ( $migrationSet[$wiki]['registration'] 
=== $migrationSet[$homeWiki]['registration'] ) {
+                                       // Another tie? Screw it, pick one 
randomly.
+                                       $wikis = array( $wiki, $homeWiki );
+                                       $homeWiki = $wikis[mt_rand( 0, 1 )];
+                               }
                        }
-               }
-
-               if ( !isset( $homeWiki ) ) {
-                       throw new Exception( "Logic error in migration: " .
-                               "Unable to determine primary account for 
$this->mName" );
                }
 
                return $homeWiki;
@@ -2082,7 +2094,9 @@
                                'user_email',
                                'user_email_authenticated',
                                'user_password',
-                               'user_editcount' );
+                               'user_editcount',
+                               'user_registration',
+                       );
                $conds = array( 'user_name' => $this->mName );
                $row = $db->selectRow( 'user', $fields, $conds, __METHOD__ );
                if ( !$row ) {
@@ -2104,6 +2118,8 @@
                        'email' => $row->user_email,
                        'emailAuthenticated' =>
                                wfTimestampOrNull( TS_MW, 
$row->user_email_authenticated ),
+                       'registration' =>
+                               wfTimestampOrNull( TS_MW, 
$row->user_registration ),
                        'password' => $row->user_password,
                        'editCount' => $row->user_editcount,
                        'groups' => array(),
diff --git a/tests/CentralAuthUserTest.php b/tests/CentralAuthUserTest.php
index beea7b0..f706c09 100644
--- a/tests/CentralAuthUserTest.php
+++ b/tests/CentralAuthUserTest.php
@@ -75,6 +75,78 @@
        }
 
        /**
+        * @covers CentralAuthUser::chooseHomeWiki
+        * @dataProvider provideChooseHomeWiki
+        */
+       public function testChooseHomeWiki( $expected, $attached ) {
+               $ca = new CentralAuthUser( 'FooBar' );
+               $this->assertEquals( $expected, $ca->chooseHomeWiki( $attached 
) );
+       }
+
+       public static function provideChooseHomeWiki() {
+               return array(
+                       // Groups win
+                       array( 'barwiki', array(
+                               'foowiki' => array(
+                                       'groups' => array( 'sysop' ),
+                               ),
+                               'barwiki' => array(
+                                       'groups' => array( 'checkuser' ),
+                               ),
+                       ) ),
+                       // Groups tie, editcount wins
+                       array( 'barwiki', array(
+                               'foowiki' => array(
+                                       'groups' => array( 'sysop', 'checkuser' 
),
+                                       'editCount' => '100',
+                               ),
+                               'barwiki' => array(
+                                       'groups' => array( 'checkuser' ),
+                                       'editCount' => '100000000',
+                               ),
+                       ) ),
+                       // No groups, Editcount wins
+                       array( 'barwiki', array(
+                               'foowiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '100'
+                               ),
+                               'barwiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '1000'
+                               ),
+                       ) ),
+                       // Edit count ties, super old registration (null) wins
+                       array( 'foowiki', array(
+                               'foowiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '5',
+                                       'registration' => null
+                               ),
+                               'barwiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '5',
+                                       'registration' => '20150305220251',
+                               ),
+                       ) ),
+                       // Edit count ties, registration wins
+                       array( 'foowiki', array(
+                               'foowiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '5',
+                                       'registration' => '20100305220251'
+                               ),
+                               'barwiki' => array(
+                                       'groups' => array(),
+                                       'editCount' => '5',
+                                       'registration' => '20150305220251',
+                               ),
+                       ) )
+
+               );
+       }
+
+       /**
         * @covers CentralAuthUser::getPasswordFromString
         * @dataProvider provideGetPasswordFromString
         */

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I69a91cdde2a881f0fb46ebffca700c07acbf3de1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralAuth
Gerrit-Branch: wmf/1.25wmf20
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to