Skip to content

Commit 318baa9

Browse files
committed
Tidy InterlanguageLinkParserFunction
1 parent f97cc0b commit 318baa9

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

src/InterlanguageLinkParserFunction.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,11 @@ private function checkIfIMultipleParserCallsOccurred( $languageCode, $linkRefere
117117

118118
private function createSiteLanguageLinks( InterlanguageLink $interlanguageLink ) {
119119

120-
$this->siteLanguageLinksGenerator->addLanguageTargetLinksToOutput(
120+
$knownTargetLink = $this->siteLanguageLinksGenerator->tryAddLanguageTargetLinksToOutput(
121121
$interlanguageLink,
122122
$this->title
123123
);
124124

125-
$knownTargetLink = $this->siteLanguageLinksGenerator->checkIfTargetIsKnownForCurrentLanguage(
126-
$this->title
127-
);
128-
129125
// If target is known we stop processing and output an error
130126
if ( $knownTargetLink ) {
131127
return $this->createErrorMessageFor(
@@ -159,15 +155,21 @@ private function isSupportedLanguage( $languageCode ) {
159155
}
160156

161157
private function createErrorMessageFor( $messageKey, $arg1 = '', $arg2 = '', $arg3 = '',$arg4 = '' ) {
162-
return '<span class="error">' . wfMessage( $messageKey, $arg1, $arg2, $arg3, $arg4 )->inContentLanguage()->text() . '</span>';
158+
return '<div class="smw-callout smw-callout-error">' . wfMessage(
159+
$messageKey,
160+
$arg1,
161+
$arg2,
162+
$arg3,
163+
$arg4
164+
)->inContentLanguage()->text() . '</div>';
163165
}
164166

165167
private function getInMemoryParserTracker() {
166168

167169
// Use the FixedInMemoryCache to ensure that during a job run the array is not hit by any
168170
// memory leak and limited to a fixed size
169171
if ( self::$inMemoryParserTracker === null ) {
170-
self::$inMemoryParserTracker = CacheFactory::getInstance()->newFixedInMemoryCache( 50 );
172+
self::$inMemoryParserTracker = CacheFactory::getInstance()->newFixedInMemoryLruCache( 50 );
171173
}
172174

173175
return self::$inMemoryParserTracker;

src/SiteLanguageLinksGenerator.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ class SiteLanguageLinksGenerator {
2323
*/
2424
private $interlanguageLinksLookup;
2525

26-
/**
27-
* @var boolean|string|Title
28-
*/
29-
private $selectedTargetLinkForCurrentLanguage = false;
30-
3126
/**
3227
* @since 1.0
3328
*
@@ -44,8 +39,13 @@ public function __construct( ParserOutput $parserOutput, InterlanguageLinksLooku
4439
*
4540
* @param InterlanguageLink $interlanguageLink
4641
* @param Title|null $target
42+
*
43+
* @return string
4744
*/
48-
public function addLanguageTargetLinksToOutput( InterlanguageLink $interlanguageLink, Title $target = null ) {
45+
public function tryAddLanguageTargetLinksToOutput( InterlanguageLink $interlanguageLink, Title $target = null ) {
46+
47+
$knownTargetLink = '';
48+
$selectedTargetLinkForCurrentLanguage = false;
4949

5050
$languageTargetLinks = $this->interlanguageLinksLookup->queryLanguageTargetLinks(
5151
$interlanguageLink,
@@ -59,34 +59,41 @@ public function addLanguageTargetLinksToOutput( InterlanguageLink $interlanguage
5959
$interlanguageLink->getLanguageCode()
6060
);
6161

62-
$this->addLanguageLinksToOutput(
63-
$interlanguageLink,
64-
$languageTargetLinks
62+
if ( isset( $languageTargetLinks[ $interlanguageLink->getLanguageCode() ] ) ) {
63+
$selectedTargetLinkForCurrentLanguage = $languageTargetLinks[ $interlanguageLink->getLanguageCode() ];
64+
}
65+
66+
$knownTargetLink = $this->compareTargetToCurrentLanguage(
67+
$target,
68+
$selectedTargetLinkForCurrentLanguage
6569
);
6670

71+
if ( !$knownTargetLink ) {
72+
$this->addLanguageLinksToOutput(
73+
$interlanguageLink,
74+
$languageTargetLinks
75+
);
76+
}
77+
6778
$this->doPurgeParserCache( $languageTargetLinks );
79+
80+
return $knownTargetLink;
6881
}
6982

7083
/**
7184
* Identify whether a double assignment did occur by comparing the target
7285
* for the requested language and the current article as target that invoked
7386
* INTERLANGUAGELINK parser.
7487
*
75-
* @since 1.0
76-
*
77-
* @param Title $target
78-
*
7988
* @return boolean|string
8089
*/
81-
public function checkIfTargetIsKnownForCurrentLanguage( Title $target ) {
82-
83-
$selectedTargetLinkForCurrentLanguage = $this->selectedTargetLinkForCurrentLanguage;
90+
private function compareTargetToCurrentLanguage( Title $target = null, $selectedTargetLinkForCurrentLanguage ) {
8491

8592
if ( $selectedTargetLinkForCurrentLanguage instanceof Title ) {
8693
$selectedTargetLinkForCurrentLanguage = $selectedTargetLinkForCurrentLanguage->getPrefixedText();
8794
}
8895

89-
if ( $selectedTargetLinkForCurrentLanguage !== $target->getPrefixedText() ) {
96+
if ( $target !== null && $selectedTargetLinkForCurrentLanguage !== $target->getPrefixedText() ) {
9097
return $selectedTargetLinkForCurrentLanguage;
9198
}
9299

@@ -116,10 +123,6 @@ private function addLanguageLinksToOutput( InterlanguageLink $interlanguageLink,
116123

117124
private function sanitizeLanguageTargetLinks( InterlanguageLink $interlanguageLink, array $languageTargetLinks ) {
118125

119-
if ( isset( $languageTargetLinks[ $interlanguageLink->getLanguageCode() ] ) ) {
120-
$this->selectedTargetLinkForCurrentLanguage = $languageTargetLinks[ $interlanguageLink->getLanguageCode() ];
121-
}
122-
123126
unset( $languageTargetLinks[ $interlanguageLink->getLanguageCode() ] );
124127
ksort( $languageTargetLinks );
125128

tests/phpunit/Unit/InterlanguageLinkParserFunctionTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public function testTryParseThatCausesErrorMessage() {
7575
);
7676

7777
$this->assertContains(
78-
'span class="error"',
78+
'-error',
7979
$instance->parse( '', 'Foo' )
8080
);
8181

8282
$this->assertContains(
83-
'span class="error"',
83+
'-error',
8484
$instance->parse( 'en', '{[[:Template:Foo]]' )
8585
);
8686
}
@@ -101,8 +101,10 @@ public function testParseToCreateErrorMessageForKnownTarget() {
101101
->getMock();
102102

103103
$siteLanguageLinksGenerator->expects( $this->once() )
104-
->method( 'checkIfTargetIsKnownForCurrentLanguage' )
105-
->with( $this->equalTo( $title ) )
104+
->method( 'tryAddLanguageTargetLinksToOutput' )
105+
->with(
106+
$this->anything(),
107+
$this->equalTo( $title ) )
106108
->will( $this->returnValue( 'Foo' ) );
107109

108110
$instance = new InterlanguageLinkParserFunction(
@@ -114,7 +116,7 @@ public function testParseToCreateErrorMessageForKnownTarget() {
114116
$instance->setInterlanguageLinksState( false );
115117

116118
$this->assertContains(
117-
'span class="error"',
119+
'-error',
118120
$instance->parse( 'en', 'Foo' )
119121
);
120122
}
@@ -146,12 +148,12 @@ public function testReturnMessagesForMultipleParseCalls() {
146148
);
147149

148150
$this->assertContains(
149-
'span class="error"',
151+
'-error',
150152
$instance->parse( 'vi', 'Foo' )
151153
);
152154

153155
$this->assertContains(
154-
'span class="error"',
156+
'-error',
155157
$instance->parse( 'en', 'Bar' )
156158
);
157159
}

tests/phpunit/Unit/SiteLanguageLinksGeneratorTest.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testAddLanguageTargetLinksToOutput() {
6464
$interlanguageLinksLookup
6565
);
6666

67-
$instance->addLanguageTargetLinksToOutput( $interlanguageLink );
67+
$instance->tryAddLanguageTargetLinksToOutput( $interlanguageLink );
6868
}
6969

7070
public function testCompareLanguageTargetLinksForExistingLanguageEntry() {
@@ -92,15 +92,12 @@ public function testCompareLanguageTargetLinksForExistingLanguageEntry() {
9292
$interlanguageLinksLookup
9393
);
9494

95-
$instance->addLanguageTargetLinksToOutput( $interlanguageLink );
96-
97-
$this->assertEquals(
98-
'Foo',
99-
$instance->checkIfTargetIsKnownForCurrentLanguage( \Title::newFromText( 'Bar' ) )
95+
$knownTarget = $instance->tryAddLanguageTargetLinksToOutput(
96+
$interlanguageLink
10097
);
10198

10299
$this->assertFalse(
103-
$instance->checkIfTargetIsKnownForCurrentLanguage( \Title::newFromText( 'Foo' ) )
100+
$knownTarget
104101
);
105102
}
106103

@@ -130,7 +127,7 @@ public function testAddLanguageTargetLinksToOutputFromStore() {
130127
$interlanguageLinksLookup
131128
);
132129

133-
$instance->addLanguageTargetLinksToOutput( $interlanguageLink );
130+
$instance->tryAddLanguageTargetLinksToOutput( $interlanguageLink );
134131
}
135132

136133
}

0 commit comments

Comments
 (0)