Two micro-optimizations are done in this patch: 1. We know exactly how these placeholders are built in the makeHolder() method. In »<!--IWLINK'" 1-->« it's guaranteed to be a single number and in »<!--LINK'" 1:2-->« it's two numbers. The most extreme synthetic micro benchmark I did cuts the runtime of these regular expressions down to about 25%. It won't make much of a difference in real-world scenarios but is still worth it, I believe. It also makes the code more specific and less confusing (see below). 2. We don't need to use the full string »<!--LINK'" 1:2-->« as array key when the only thing that matters is the part »1:2«. Note the same is done just a few lines below in the replaceInterwiki() method. This code does have outstanding test coverage via all the parser tests, I believe. Any change here that doesn't make a test fail should be safe. Note the unit tests have been written many years later via I2c12cc7, using "dummy" strings and such instead of the expected numeric namespace and link ids. Most of this is already fixed via previous patches. The last mistake addressed in this patch is that getPrefixedDBkey() is supposed to be a title. It can't contain one of these placeholders. Follow-Up: I2c12cc76a9bf01eb527db3ea038e4adc59446cac Change-Id: Ie994059092df8861ddb97c098acd082698d45c53
112 lines
2.5 KiB
PHP
112 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare( strict_types = 1 );
|
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
/**
|
|
* @covers LinkHolderArray
|
|
*/
|
|
class LinkHolderArrayIntegrationTest extends MediaWikiLangTestCase {
|
|
|
|
/**
|
|
* @dataProvider provideIsBig
|
|
* @covers LinkHolderArray::isBig
|
|
*
|
|
* @param int $size
|
|
* @param int $global
|
|
* @param bool $expected
|
|
*/
|
|
public function testIsBig( int $size, int $global, bool $expected ) {
|
|
$this->overrideConfigValue( MainConfigNames::LinkHolderBatchSize, $global );
|
|
$linkHolderArray = new LinkHolderArray(
|
|
$this->createMock( Parser::class ),
|
|
$this->createMock( ILanguageConverter::class ),
|
|
$this->createHookContainer()
|
|
);
|
|
$linkHolderArray->size = $size;
|
|
|
|
$this->assertSame( $expected, $linkHolderArray->isBig() );
|
|
}
|
|
|
|
public function provideIsBig() {
|
|
yield [ 0, 0, false ];
|
|
yield [ 0, 1, false ];
|
|
yield [ 1, 0, true ];
|
|
yield [ 1, 1, false ];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideMakeHolder_withNsText
|
|
* @covers LinkHolderArray::makeHolder
|
|
*
|
|
* @param bool $isExternal
|
|
* @param string $expected
|
|
*/
|
|
public function testMakeHolder_withNsText(
|
|
bool $isExternal,
|
|
string $expected
|
|
) {
|
|
$link = new LinkHolderArray(
|
|
$this->createMock( Parser::class ),
|
|
$this->createMock( ILanguageConverter::class ),
|
|
$this->createHookContainer()
|
|
);
|
|
$parser = $this->createMock( Parser::class );
|
|
$parser->method( 'nextLinkID' )->willReturn( 9 );
|
|
$link->parent = $parser;
|
|
$title = $this->createMock( Title::class );
|
|
$title->method( 'getPrefixedDBkey' )->willReturn( 'Talk:Dummy' );
|
|
$title->method( 'getNamespace' )->willReturn( 1234 );
|
|
$title->method( 'isExternal' )->willReturn( $isExternal );
|
|
|
|
$this->assertSame( 0, $link->size );
|
|
$result = $link->makeHolder(
|
|
$title,
|
|
'test1 text',
|
|
'test2 trail',
|
|
'test3 prefix'
|
|
);
|
|
$this->assertSame( $expected, $result );
|
|
$this->assertSame( 1, $link->size );
|
|
|
|
if ( $isExternal ) {
|
|
$this->assertArrayEquals(
|
|
[
|
|
9 => [
|
|
'title' => $title,
|
|
'text' => 'test3 prefixtest1 texttest',
|
|
'pdbk' => 'Talk:Dummy',
|
|
],
|
|
],
|
|
$link->interwikis
|
|
);
|
|
$this->assertCount( 0, $link->internals );
|
|
} else {
|
|
$this->assertArrayEquals(
|
|
[
|
|
1234 => [
|
|
9 => [
|
|
'title' => $title,
|
|
'text' => 'test3 prefixtest1 texttest',
|
|
'pdbk' => 'Talk:Dummy',
|
|
],
|
|
],
|
|
],
|
|
$link->internals
|
|
);
|
|
$this->assertCount( 0, $link->interwikis );
|
|
}
|
|
}
|
|
|
|
public function provideMakeHolder_withNsText() {
|
|
yield [
|
|
false,
|
|
'<!--LINK\'" 1234:9-->2 trail',
|
|
];
|
|
yield [
|
|
true,
|
|
'<!--IWLINK\'" 9-->2 trail',
|
|
];
|
|
}
|
|
}
|