Legoktm has uploaded a new change for review.

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

Change subject: [WIP] Store user id in revert messages
......................................................................

[WIP] Store user id in revert messages

The resulting message looks super ugly.

Bug: 18526
Change-Id: I9a6d8782b48134314faf3b7045bf639852d6b490
---
M includes/Linker.php
M includes/cache/UserCache.php
M languages/i18n/en.json
M tests/phpunit/includes/LinkerTest.php
4 files changed, 107 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/79/153979/1

diff --git a/includes/Linker.php b/includes/Linker.php
index 9f578a9..853563a 100644
--- a/includes/Linker.php
+++ b/includes/Linker.php
@@ -1311,6 +1311,7 @@
 
                # Render autocomments and make links:
                $comment = self::formatAutocomments( $comment, $title, $local );
+               $comment = self::formatUsernames( $comment );
                $comment = self::formatLinksInComment( $comment, $title, $local 
);
 
                wfProfileOut( __METHOD__ );
@@ -1318,6 +1319,34 @@
        }
 
        /**
+        * Converts a {{USERNAME:$userid}} to a username if the user
+        * isn't hidden. See bug 18526 for background.
+        *
+        * @param string $comment
+        * @return string
+        */
+       private static function formatUsernames( $comment ) {
+               return preg_replace_callback(
+                       '/\{\{USERNAME:(\d*?)\}\}/',
+                       function ( $match ) {
+                               $int = intval( $match[1] );
+                               if ( $int === 0 ) {
+                                       // Invalid user id.
+                                       return $match[0];
+                               }
+
+                               $userCache = UserCache::singleton();
+                               if ( $userCache->isHidden( $int ) ) {
+                                       return wfMessage( 'rev-deleted-user' 
)->text();
+                               } else {
+                                       return $userCache->getUserName( $int, 
'' );
+                               }
+                       },
+                       $comment
+               );
+       }
+
+       /**
         * Converts autogenerated comments in edit summaries into section links.
         * The pattern for autogen comments is / * foo * /, which makes for
         * some nasty regex.
diff --git a/includes/cache/UserCache.php b/includes/cache/UserCache.php
index 7f36f5a..411ffbb 100644
--- a/includes/cache/UserCache.php
+++ b/includes/cache/UserCache.php
@@ -29,15 +29,27 @@
        protected $typesCached = array(); // (uid => cache type => 1)
 
        /**
+        * @var UserCache
+        */
+       private static $instance;
+
+       /**
         * @return UserCache
         */
        public static function singleton() {
-               static $instance = null;
-               if ( $instance === null ) {
-                       $instance = new self();
+               if ( self::$instance === null ) {
+                       self::$instance = new self();
                }
 
-               return $instance;
+               return self::$instance;
+       }
+
+       public static function destroySingleton() {
+               if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+                       throw new MWException( __METHOD__ . ' should not be 
called outside of unit tests' );
+               }
+
+               self::$instance = null;
        }
 
        protected function __construct() {
@@ -74,6 +86,15 @@
        }
 
        /**
+        * @param int $userId
+        * @return bool
+        * @since 1.24
+        */
+       public function isHidden( $userId ) {
+               return $userId > 0 ? $this->getProp( $userId, 'hidden' ): false;
+       }
+
+       /**
         * Preloads user names for given list of users.
         * @param array $userIds List of user IDs
         * @param array $options Option flags; include 'userpage' and 'usertalk'
@@ -102,21 +123,23 @@
                // Lookup basic info for users not yet loaded...
                if ( count( $usersToQuery ) ) {
                        $dbr = wfGetDB( DB_SLAVE );
-                       $table = array( 'user' );
+                       $table = array( 'user', 'ipblocks' );
                        $conds = array( 'user_id' => $usersToQuery );
-                       $fields = array( 'user_name', 'user_real_name', 
'user_registration', 'user_id' );
+                       $fields = array( 'user_name', 'user_real_name', 
'user_registration', 'user_id', 'ipb_deleted' );
+                       $join_conds = array( 'ipblocks' => array( 'LEFT JOIN', 
'ipb_user=user_id' ) );
 
                        $comment = __METHOD__;
                        if ( strval( $caller ) !== '' ) {
                                $comment .= "/$caller";
                        }
 
-                       $res = $dbr->select( $table, $fields, $conds, $comment 
);
+                       $res = $dbr->select( $table, $fields, $conds, $comment, 
array(), $join_conds );
                        foreach ( $res as $row ) { // load each user into cache
                                $userId = (int)$row->user_id;
                                $this->cache[$userId]['name'] = $row->user_name;
                                $this->cache[$userId]['real_name'] = 
$row->user_real_name;
                                $this->cache[$userId]['registration'] = 
$row->user_registration;
+                               $this->cache[$userId]['hidden'] = 
$row->ipb_deleted === '1';
                                $usersToCheck[$userId] = $row->user_name;
                        }
                }
diff --git a/languages/i18n/en.json b/languages/i18n/en.json
index a43a742..941944a 100644
--- a/languages/i18n/en.json
+++ b/languages/i18n/en.json
@@ -1887,7 +1887,7 @@
        "cantrollback": "Cannot revert edit;\nlast contributor is only author 
of this page.",
        "alreadyrolled": "Cannot rollback last edit of [[:$1]] by 
[[User:$2|$2]] ([[User 
talk:$2|talk]]{{int:pipe-separator}}[[Special:Contributions/$2|{{int:contribslink}}]]);\nsomeone
 else has edited or rolled back the page already.\n\nThe last edit to the page 
was by [[User:$3|$3]] ([[User 
talk:$3|talk]]{{int:pipe-separator}}[[Special:Contributions/$3|{{int:contribslink}}]]).",
        "editcomment": "The edit summary was: \"''$1''\".",
-       "revertpage": "Reverted edits by [[Special:Contributions/$2|$2]] 
([[User talk:$2|talk]]) to last revision by [[User:$1|$1]]",
+       "revertpage": "Reverted edits by 
[[Special:Contributions/{{USERNAME:$2}}|{{USERNAME:$2}}]] ([[User 
talk:{{USERNAME:$2}}|talk]]) to last revision by 
[[User:{{USERNAME:$1}}|{{USERNAME:$1}}]]",
        "revertpage-nouser": "Reverted edits by a hidden user to last revision 
by {{GENDER:$1|[[User:$1|$1]]}}",
        "rollback-success": "Reverted edits by $1;\nchanged back to last 
revision by $2.",
        "sessionfailure-title": "Session failure",
diff --git a/tests/phpunit/includes/LinkerTest.php 
b/tests/phpunit/includes/LinkerTest.php
index 72114e9..fb79f95 100644
--- a/tests/phpunit/includes/LinkerTest.php
+++ b/tests/phpunit/includes/LinkerTest.php
@@ -1,5 +1,8 @@
 <?php
 
+/**
+ * @group Database
+ */
 class LinkerTest extends MediaWikiLangTestCase {
 
        /**
@@ -185,4 +188,48 @@
                        ),
                );
        }
+
+       private function hideUser( $userid ) {
+               $block = new Block();
+               $block->setTarget( User::newFromId( $userid ) );
+               $block->mHideName = true;
+               $block->mExpiry = 'infinite';
+               $this->assertNotFalse( $block->insert() );
+       }
+
+       /**
+        * @dataProvider provideFormatUsername
+        * @covers Linker::formatUsernames
+        */
+       public function testFormatUsername( $text, $expected, $userToHide ) {
+               if ( $userToHide ) {
+                       $this->hideUser( $userToHide );
+               }
+
+               $expected = is_callable( $expected ) ? call_user_func( 
$expected ) : $expected;
+
+               UserCache::destroySingleton();
+               $comment = Linker::formatComment( $text );
+               $this->assertEquals( $expected, $comment );
+       }
+
+       public static function provideFormatUsername() {
+               return array(
+                       array(
+                               '{{USERNAME:1}}',
+                               function() {return User::newFromId( '1' 
)->getName(); },
+                               false,
+                       ),
+                       array(
+                               '{{USERNAME:1}}',
+                               '(username removed)',
+                               true,
+                       ),
+                       array(
+                               '{{USERNAME:INVALID}}',
+                               '{{USERNAME:INVALID}}',
+                               false,
+                       )
+               );
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9a6d8782b48134314faf3b7045bf639852d6b490
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to