From: Stephane Bisson Date: Wed, 6 Mar 2019 16:28:51 +0000 (-0500) Subject: Introducing User::getLatestEditTimestamp() X-Git-Tag: 1.34.0-rc.0~2618 X-Git-Url: http://git.cyclocoop.org/data/%24self?a=commitdiff_plain;h=33a06450c9ba5a5d238f6c8863f9966f96ce7cbd;p=lhc%2Fweb%2Fwiklou.git Introducing User::getLatestEditTimestamp() 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 --- diff --git a/includes/user/User.php b/includes/user/User.php index 1e3ecf2136..f84a6fffee 100644 --- a/includes/user/User.php +++ b/includes/user/User.php @@ -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 ) { diff --git a/tests/phpunit/includes/user/UserTest.php b/tests/phpunit/includes/user/UserTest.php index 64bdf31d63..dad7bf29e1 100644 --- a/tests/phpunit/includes/user/UserTest.php +++ b/tests/phpunit/includes/user/UserTest.php @@ -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 ) ); + } }