Keep $user->mEditCount up to date
authorStephane Bisson <sbisson@wikimedia.org>
Wed, 3 Aug 2016 15:26:47 +0000 (11:26 -0400)
committerStephane Bisson <sbisson@wikimedia.org>
Fri, 5 Aug 2016 13:06:56 +0000 (09:06 -0400)
Whenever User::incEditCount() is called,
this tries to keep the user object up
to date so hooks can read the edit count
without reloading the user from the db.

Another option would be invalidate the
instance cache and let the read
repopulate it. It would add a db access
on each edit.

Bug: T128249
Change-Id: I79194c41d6b2fd84ad658909a2941d9d3d28d94e

includes/user/User.php
tests/phpunit/includes/user/UserTest.php

index c46836b..8d3fcea 100644 (file)
@@ -5161,12 +5161,20 @@ class User implements IDBAccessObject {
                                // If we actually have a slave server, the count is
                                // at least one behind because the current transaction
                                // has not been committed and replicated.
-                               $this->initEditCount( 1 );
+                               $this->mEditCount = $this->initEditCount( 1 );
                        } else {
                                // But if DB_SLAVE is selecting the master, then the
                                // count we just read includes the revision that was
                                // just added in the working transaction.
-                               $this->initEditCount();
+                               $this->mEditCount = $this->initEditCount();
+                       }
+               } else {
+                       if ( $this->mEditCount === null ) {
+                               $this->getEditCount();
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $this->mEditCount += ( $dbr !== $dbw ) ? 1 : 0;
+                       } else {
+                               $this->mEditCount++;
                        }
                }
                // Edit count in user cache too
index beb5e78..bd076ba 100644 (file)
@@ -212,7 +212,7 @@ class UserTest extends MediaWikiTestCase {
         * @group medium
         * @covers User::getEditCount
         */
-       public function testEditCount() {
+       public function testGetEditCount() {
                $user = $this->getMutableTestUser()->getUser();
 
                // let the user have a few (3) edits
@@ -221,17 +221,15 @@ class UserTest extends MediaWikiTestCase {
                        $page->doEdit( (string)$i, 'test', 0, false, $user );
                }
 
-               $user->clearInstanceCache();
                $this->assertEquals(
                        3,
                        $user->getEditCount(),
                        'After three edits, the user edit count should be 3'
                );
 
-               // increase the edit count and clear the cache
+               // increase the edit count
                $user->incEditCount();
 
-               $user->clearInstanceCache();
                $this->assertEquals(
                        4,
                        $user->getEditCount(),
@@ -239,6 +237,46 @@ class UserTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * Test User::editCount
+        * @group medium
+        * @covers User::getEditCount
+        */
+       public function testGetEditCountForAnons() {
+               $user = User::newFromName( 'Anonymous' );
+
+               $this->assertNull(
+                       $user->getEditCount(),
+                       'Edit count starts null for anonymous users.'
+               );
+
+               $user->incEditCount();
+
+               $this->assertNull(
+                       $user->getEditCount(),
+                       'Edit count remains null for anonymous users despite calls to increase it.'
+               );
+       }
+
+       /**
+        * Test User::editCount
+        * @group medium
+        * @covers User::incEditCount
+        */
+       public function testIncEditCount() {
+               $user = $this->getMutableTestUser()->getUser();
+               $user->incEditCount();
+
+               $reloadedUser = User::newFromId( $user->getId() );
+               $reloadedUser->incEditCount();
+
+               $this->assertEquals(
+                       2,
+                       $reloadedUser->getEditCount(),
+                       'Increasing the edit count after a fresh load leaves the object up to date.'
+               );
+       }
+
        /**
         * Test changing user options.
         * @covers User::setOption