Skip to content

Commit

Permalink
(refactor) Rename TimestampFormatter service to TimestampTools
Browse files Browse the repository at this point in the history
The plan is to move the method ModerationEntry::canReapproveRejected()
into this service.
  • Loading branch information
edwardspec committed Dec 31, 2024
1 parent a65d167 commit 391fa36
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
"MediaWiki\\Moderation\\RollbackResistantQuery": "util/RollbackResistantQuery.php",
"MediaWiki\\Moderation\\SendNotificationEmailConsequence": "lib/consequence/SendNotificationEmailConsequence.php",
"MediaWiki\\Moderation\\TagRevisionAsMergedConsequence": "lib/consequence/TagRevisionAsMergedConsequence.php",
"MediaWiki\\Moderation\\TimestampFormatter": "util/TimestampFormatter.php",
"MediaWiki\\Moderation\\TimestampTools": "util/TimestampTools.php",
"MediaWiki\\Moderation\\UnblockUserConsequence": "lib/consequence/UnblockUserConsequence.php",
"MediaWiki\\Moderation\\WatchOrUnwatchConsequence": "lib/consequence/WatchOrUnwatchConsequence.php",
"ModerationAction": "action/ModerationAction.php",
Expand Down
8 changes: 4 additions & 4 deletions hooks/ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use MediaWiki\Moderation\Hook\HookRunner;
use MediaWiki\Moderation\NewChangeFactory;
use MediaWiki\Moderation\RollbackResistantQuery;
use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;

// @codeCoverageIgnoreStart
// (PHPUnit doesn't support @covers for out-of-class code)
Expand Down Expand Up @@ -84,7 +84,7 @@
return new EntryFactory(
$services->getLinkRenderer(),
$services->getService( 'Moderation.ActionLinkRenderer' ),
$services->getService( 'Moderation.TimestampFormatter' ),
$services->getService( 'Moderation.TimestampTools' ),
$services->getService( 'Moderation.ConsequenceManager' ),
$services->getService( 'Moderation.CanSkip' ),
$services->getService( 'Moderation.ApproveHook' ),
Expand Down Expand Up @@ -124,8 +124,8 @@ static function ( MediaWikiServices $services ): RollbackResistantQuery {
$services->getDBLoadBalancer()
);
},
'Moderation.TimestampFormatter' => static function (): TimestampFormatter {
return new TimestampFormatter();
'Moderation.TimestampTools' => static function (): TimestampTools {
return new TimestampTools();
},
'Moderation.VersionCheck' => static function ( MediaWikiServices $services ): ModerationVersionCheck {
return new ModerationVersionCheck(
Expand Down
12 changes: 6 additions & 6 deletions lib/entry/EntryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class EntryFactory {
/** @var ActionLinkRenderer */
protected $actionLinkRenderer;

/** @var TimestampFormatter */
protected $timestampFormatter;
/** @var TimestampTools */
protected $timestampTools;

/** @var IConsequenceManager */
protected $consequenceManager;
Expand All @@ -67,7 +67,7 @@ class EntryFactory {
/**
* @param LinkRenderer $linkRenderer
* @param ActionLinkRenderer $actionLinkRenderer
* @param TimestampFormatter $timestampFormatter
* @param TimestampTools $timestampTools
* @param IConsequenceManager $consequenceManager
* @param ModerationCanSkip $canSkip
* @param ModerationApproveHook $approveHook
Expand All @@ -76,7 +76,7 @@ class EntryFactory {
*/
public function __construct( LinkRenderer $linkRenderer,
ActionLinkRenderer $actionLinkRenderer,
TimestampFormatter $timestampFormatter,
TimestampTools $timestampTools,
IConsequenceManager $consequenceManager,
ModerationCanSkip $canSkip,
ModerationApproveHook $approveHook,
Expand All @@ -85,7 +85,7 @@ public function __construct( LinkRenderer $linkRenderer,
) {
$this->linkRenderer = $linkRenderer;
$this->actionLinkRenderer = $actionLinkRenderer;
$this->timestampFormatter = $timestampFormatter;
$this->timestampTools = $timestampTools;
$this->consequenceManager = $consequenceManager;
$this->canSkip = $canSkip;
$this->approveHook = $approveHook;
Expand All @@ -105,7 +105,7 @@ public function makeFormatter( $row, IContextSource $context ) {
$context,
$this->linkRenderer,
$this->actionLinkRenderer,
$this->timestampFormatter,
$this->timestampTools,
$this->canSkip
);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/entry/ModerationEntryFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Moderation\ActionLinkRenderer;
use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;
use Wikimedia\IPUtils;

class ModerationEntryFormatter extends ModerationEntry {
Expand All @@ -35,8 +35,8 @@ class ModerationEntryFormatter extends ModerationEntry {
/** @var ActionLinkRenderer */
protected $actionLinkRenderer;

/** @var TimestampFormatter */
protected $timestampFormatter;
/** @var TimestampTools */
protected $timestampTools;

/** @var ModerationCanSkip */
protected $canSkip;
Expand All @@ -46,19 +46,19 @@ class ModerationEntryFormatter extends ModerationEntry {
* @param IContextSource $context
* @param LinkRenderer $linkRenderer
* @param ActionLinkRenderer $actionLinkRenderer
* @param TimestampFormatter $timestampFormatter
* @param TimestampTools $timestampTools
* @param ModerationCanSkip $canSkip
*/
public function __construct( $row, IContextSource $context, LinkRenderer $linkRenderer,
ActionLinkRenderer $actionLinkRenderer, TimestampFormatter $timestampFormatter,
ActionLinkRenderer $actionLinkRenderer, TimestampTools $timestampTools,
ModerationCanSkip $canSkip
) {
parent::__construct( $row );

$this->context = $context;
$this->linkRenderer = $linkRenderer;
$this->actionLinkRenderer = $actionLinkRenderer;
$this->timestampFormatter = $timestampFormatter;
$this->timestampTools = $timestampTools;
$this->canSkip = $canSkip;
}

Expand Down Expand Up @@ -219,7 +219,7 @@ public function getHTML() {

$line .= ' ';

$line .= $this->timestampFormatter->format( $row->timestamp, $this->context );
$line .= $this->timestampTools->format( $row->timestamp, $this->context );

$line .= ' . . ';
$line .= ChangesList::showCharacterDifference(
Expand Down
12 changes: 6 additions & 6 deletions tests/benchmarks/formatTimestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

/**
* @file
* Benchmark: how fast is TimestampFormatter)?
* Benchmark: how fast is TimestampTools)?
*
* Usage:
* php maintenance/runScript.php extensions/Moderation/tests/benchmarks/formatTimestamp.php
*/

require_once __DIR__ . '/ModerationBenchmark.php';

use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;

class BenchmarkFormatTimestamp extends ModerationBenchmark {

Expand All @@ -35,9 +35,9 @@ class BenchmarkFormatTimestamp extends ModerationBenchmark {
protected $context;

/**
* @var TimestampFormatter
* @var TimestampTools
*/
protected $timestampFormatter;
protected $timestampTools;

/**
* Default number of loops.
Expand All @@ -52,14 +52,14 @@ public function getDefaultLoops() {
*/
public function beforeBenchmark( $numberOfUsers ) {
$this->context = RequestContext::getMain();
$this->timestampFormatter = new TimestampFormatter();
$this->timestampTools = new TimestampTools();
}

/**
* @param int $i
*/
public function doActualWork( $i ) {
$this->timestampFormatter->format( '20180101000000', $this->context );
$this->timestampTools->format( '20180101000000', $this->context );
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/consequence/EntryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use MediaWiki\Moderation\EntryFactory;
use MediaWiki\Moderation\IConsequenceManager;
use MediaWiki\Moderation\PendingEdit;
use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;
use MediaWiki\Revision\RevisionLookup;

require_once __DIR__ . "/autoload.php";
Expand Down Expand Up @@ -344,7 +344,7 @@ public function dataProviderFindAllApprovableEntries() {
private function makeFactory() {
$linkRenderer = $this->createMock( LinkRenderer::class );
$actionLinkRenderer = $this->createMock( ActionLinkRenderer::class );
$timestampFormatter = $this->createMock( TimestampFormatter::class );
$timestampTools = $this->createMock( TimestampTools::class );
$consequenceManager = $this->createMock( IConsequenceManager::class );
$canSkip = $this->createMock( ModerationCanSkip::class );
$approveHook = $this->createMock( ModerationApproveHook::class );
Expand All @@ -353,14 +353,14 @@ private function makeFactory() {

'@phan-var LinkRenderer $linkRenderer';
'@phan-var ActionLinkRenderer $actionLinkRenderer';
'@phan-var TimestampFormatter $timestampFormatter';
'@phan-var TimestampTools $timestampTools';
'@phan-var IConsequenceManager $consequenceManager';
'@phan-var ModerationCanSkip $canSkip';
'@phan-var ModerationApproveHook $approveHook';
'@phan-var IContentHandlerFactory $contentHandlerFactory';
'@phan-var RevisionLookup $revisionLookup';

return new EntryFactory( $linkRenderer, $actionLinkRenderer, $timestampFormatter,
return new EntryFactory( $linkRenderer, $actionLinkRenderer, $timestampTools,
$consequenceManager, $canSkip, $approveHook, $contentHandlerFactory, $revisionLookup );
}
}
12 changes: 6 additions & 6 deletions tests/phpunit/consequence/ModerationEntryFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Moderation\ActionLinkRenderer;
use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;

require_once __DIR__ . "/autoload.php";

Expand All @@ -43,7 +43,7 @@ class ModerationEntryFormatterTest extends ModerationUnitTestCase {
/**
* @var mixed
*/
private $timestampFormatter;
private $timestampTools;

/**
* @var mixed
Expand Down Expand Up @@ -186,7 +186,7 @@ public function testGetHTML( array $options ) {
$this->context->expects( $this->any() )->method( 'getConfig' )
->willReturn( RequestContext::getMain()->getConfig() );

// Mock all calls to msg(), makeLink() and TimestampFormatter:format().
// Mock all calls to msg(), makeLink() and TimestampTools:format().
$this->actionLinkRenderer->expects( $this->any() )->method( 'makeLink' )
->willReturnCallback( function ( $action, $id ) use ( $row ) {
$this->assertEquals( $row->id, $id );
Expand All @@ -213,7 +213,7 @@ static function ( Title $title, $text, $classes, array $extraAttribs, array $que
->willReturnCallback( static function ( $key, ...$args ) use ( $lang ) {
return wfMessage( $key, ...$args )->inLanguage( $lang );
} );
$this->timestampFormatter->expects( $this->once() )->method( 'format' )->with(
$this->timestampTools->expects( $this->once() )->method( 'format' )->with(
$this->identicalTo( $row->timestamp )
)->willReturn( '{FormattedTime}' );

Expand Down Expand Up @@ -403,7 +403,7 @@ public function dataProviderGetHTML() {
private function makeTestFormatter( $row = null ) {
return new ModerationEntryFormatter( $row ?? new stdClass, $this->context,
$this->linkRenderer, $this->actionLinkRenderer,
$this->timestampFormatter, $this->canSkip );
$this->timestampTools, $this->canSkip );
}

/**
Expand All @@ -414,7 +414,7 @@ public function setUp(): void {

$this->linkRenderer = $this->createMock( LinkRenderer::class );
$this->actionLinkRenderer = $this->createMock( ActionLinkRenderer::class );
$this->timestampFormatter = $this->createMock( TimestampFormatter::class );
$this->timestampTools = $this->createMock( TimestampTools::class );
$this->context = $this->createMock( IContextSource::class );
$this->canSkip = $this->createMock( ModerationCanSkip::class );
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/consequence/ServiceWiringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use MediaWiki\Moderation\Hook\HookRunner;
use MediaWiki\Moderation\NewChangeFactory;
use MediaWiki\Moderation\RollbackResistantQuery;
use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;

require_once __DIR__ . "/autoload.php";

Expand Down Expand Up @@ -65,7 +65,7 @@ public function dataProviderGetService() {
[ 'Moderation.NotifyModerator', ModerationNotifyModerator::class ],
[ 'Moderation.Preload', ModerationPreload::class ],
[ 'Moderation.RollbackResistantQuery', RollbackResistantQuery::class ],
[ 'Moderation.TimestampFormatter', TimestampFormatter::class ],
[ 'Moderation.TimestampTools', TimestampTools::class ],
[ 'Moderation.VersionCheck', ModerationVersionCheck::class ],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@

/**
* @file
* Unit test of TimestampFormatter.
* Unit test of TimestampTools.
*/

use MediaWiki\Moderation\TimestampFormatter;
use MediaWiki\Moderation\TimestampTools;
use Wikimedia\TestingAccessWrapper;

require_once __DIR__ . "/autoload.php";

class TimestampFormatterTest extends ModerationUnitTestCase {
class TimestampToolsTest extends ModerationUnitTestCase {
/**
* Test the results of TimestampFormatter::format().
* Test the results of TimestampTools::format().
* @dataProvider dataProviderFormat
* @param string $timestamp First parameter passed to format().
* @param string $mockedAdjustedTimestamp Mocked value of "userAdjust($timestamp)".
* @param string $mockedAdjustedToday Mocked value of "userAdjust(wfTimestampNow())".
* @param bool $expectTimeOnly Whether format() should return date+time (false) or time (true).
*
* @covers MediaWiki\Moderation\TimestampFormatter
* @covers MediaWiki\Moderation\TimestampTools
*/
public function testFormat( $timestamp, $mockedAdjustedTimestamp,
$mockedAdjustedToday, $expectTimeOnly
Expand Down Expand Up @@ -88,11 +88,11 @@ public function testFormat( $timestamp, $mockedAdjustedTimestamp,
'@phan-var IContextSource $context';
'@phan-var Language $lang';

$formatter = new TimestampFormatter();
$formatter = new TimestampTools();
$result = $formatter->format( $timestamp, $context );
$this->assertEquals( $mockedResult, $result );

// Additionally test the internal cache of TimestampFormatter ($skippedToday).
// Additionally test the internal cache of TimestampTools ($skippedToday).
$skippedToday = TestingAccessWrapper::newFromObject( $formatter )->skippedToday;
if ( $expectTimeOnly ) {
$this->assertFalse( $skippedToday,
Expand Down
2 changes: 1 addition & 1 deletion util/TimestampFormatter.php → util/TimestampTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use IContextSource;
use Language;

class TimestampFormatter {
class TimestampTools {
/**
* @var bool
* If true, return value of format() will always be "time+date" pair.
Expand Down

0 comments on commit 391fa36

Please sign in to comment.