Introducing User::getLatestEditTimestamp()
authorStephane Bisson <sbisson@wikimedia.org>
Wed, 6 Mar 2019 16:28:51 +0000 (11:28 -0500)
committerSbisson <sbisson@wikimedia.org>
Thu, 7 Mar 2019 20:17:03 +0000 (20:17 +0000)
This will be used by the GrowthExperiments
Mentorship module to show when a user made their
latest contribution.

* Introduce a private function to reduce duplication with
  getFirstEditTimestamp()
* Add unit tests for both

Bug: T216631
Change-Id: Ica3e6e7165496bdc9b8f12972cf93847ecfffa50

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

index 1e3ecf2..f84a6ff 100644 (file)
@@ -4974,6 +4974,28 @@ class User implements IDBAccessObject, UserIdentity {
         *  non-existent/anonymous user accounts.
         */
        public function getFirstEditTimestamp() {
+               return $this->getEditTimestamp( true );
+       }
+
+       /**
+        * Get the timestamp of the latest edit
+        *
+        * @since 1.33
+        * @return string|bool Timestamp of first edit, or false for
+        *  non-existent/anonymous user accounts.
+        */
+       public function getLatestEditTimestamp() {
+               return $this->getEditTimestamp( false );
+       }
+
+       /**
+        * Get the timestamp of the first or latest edit
+        *
+        * @param bool $first True for the first edit, false for the latest one
+        * @return string|bool Timestamp of first or latest edit, or false for
+        *  non-existent/anonymous user accounts.
+        */
+       private function getEditTimestamp( $first ) {
                if ( $this->getId() == 0 ) {
                        return false; // anons
                }
@@ -4981,12 +5003,13 @@ class User implements IDBAccessObject, UserIdentity {
                $actorWhere = ActorMigration::newMigration()->getWhere( $dbr, 'rev_user', $this );
                $tsField = isset( $actorWhere['tables']['temp_rev_user'] )
                        ? 'revactor_timestamp' : 'rev_timestamp';
+               $sortOrder = $first ? 'ASC' : 'DESC';
                $time = $dbr->selectField(
                        [ 'revision' ] + $actorWhere['tables'],
                        $tsField,
                        [ $actorWhere['conds'] ],
                        __METHOD__,
-                       [ 'ORDER BY' => "$tsField ASC" ],
+                       [ 'ORDER BY' => "$tsField $sortOrder" ],
                        $actorWhere['joins']
                );
                if ( !$time ) {
index 64bdf31..dad7bf2 100644 (file)
@@ -1462,4 +1462,38 @@ class UserTest extends MediaWikiTestCase {
                // clean up
                $block->delete();
        }
+
+       /**
+        * @covers User::getFirstEditTimestamp
+        * @covers User::getLatestEditTimestamp
+        */
+       public function testGetFirstLatestEditTimestamp() {
+               $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' );
+               MWTimestamp::setFakeTime( function () use ( &$clock ) {
+                       return $clock += 1000;
+               } );
+               $user = $this->getTestUser()->getUser();
+               $firstRevision = self::makeEdit( $user, 'Help:UserTest_GetEditTimestamp', 'one', 'test' );
+               $secondRevision = self::makeEdit( $user, 'Help:UserTest_GetEditTimestamp', 'two', 'test' );
+               // Sanity check: revisions timestamp are different
+               $this->assertNotEquals( $firstRevision->getTimestamp(), $secondRevision->getTimestamp() );
+
+               $this->assertEquals( $firstRevision->getTimestamp(), $user->getFirstEditTimestamp() );
+               $this->assertEquals( $secondRevision->getTimestamp(), $user->getLatestEditTimestamp() );
+       }
+
+       /**
+        * @param User $user
+        * @param string $title
+        * @param string $content
+        * @param string $comment
+        * @return \MediaWiki\Revision\RevisionRecord|null
+        */
+       private static function makeEdit( User $user, $title, $content, $comment ) {
+               $page = WikiPage::factory( Title::newFromText( $title ) );
+               $content = ContentHandler::makeContent( $content, $page->getTitle() );
+               $updater = $page->newPageUpdater( $user );
+               $updater->setContent( 'main', $content );
+               return $updater->saveRevision( CommentStoreComment::newUnsavedComment( $comment ) );
+       }
 }