* 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
}
$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 ) {
// 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 ) );
+ }
}