Skip to content

Commit 37b3e55

Browse files
authored
Add annotatedlanguage parser function (#50)
1 parent 1597dbb commit 37b3e55

8 files changed

+261
-0
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This file contains the RELEASE-NOTES of the Semantic Interlanguage Links (a.k.a.
44

55
Released on July 29, 2017.
66

7+
* #50 Added the `annotatedlanguage` parser function
78
* Minimum requirement for
89
* PHP changed to version 5.5 and later
910
* Semantic MediaWiki changed to version 2.4 and later

docs/01-parser-function.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ category).
4040
The parser function `{{interlanguagelist: interlanguage reference | template }}` can
4141
generate a customizable language target link list for the provided `interlanguage reference`
4242
and being formatted using a template with parameters:
43+
4344
- `target-link` being the target link
4445
- `lang-code` the language code
4546
- `lang-name` representing the localized language name for the language code
@@ -60,5 +61,15 @@ Page:FooBar
6061
6162
```
6263

64+
## annotatedlanguage
65+
66+
The parser function is called either as `{{annotatedlanguage: }}` where it just returns the language code
67+
for the current page (if one is annotated using SIL) or with `{{annotatedlanguage: template }}` to return content
68+
as formatted using a template with parameters defined as:
69+
70+
- `target-link` being the target link
71+
- `lang-code` the language code
72+
- `lang-name` representing the localized language name for the language code
73+
6374
[iwlm]: https://www.mediawiki.org/wiki/Manual:$wgInterwikiMagic
6475
[iwlp]: https://www.mediawiki.org/wiki/Manual:$wgExtraInterlanguageLinkPrefixes

i18n/SemanticInterlanguageLinks.magic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
$magicWords['en'] = array(
1212
'interlanguagelink' => array( 0, 'INTERLANGUAGELINK:', 'SIL:' ),
1313
'interlanguagelist' => array( 0, 'INTERLANGUAGELIST:', 'SILIST:' ),
14+
'annotatedlanguage' => array( 0, 'ANNOTATEDLANGUAGE:' ),
1415
);
1516

1617
/**
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace SIL;
4+
5+
use Title;
6+
use Language;
7+
8+
/**
9+
* @license GNU GPL v2+
10+
* @since 1.4
11+
*
12+
* @author mwjames
13+
*/
14+
class AnnotatedLanguageParserFunction {
15+
16+
/**
17+
* @var InterlanguageLinksLookup
18+
*/
19+
private $interlanguageLinksLookup;
20+
21+
/**
22+
* @since 1.0
23+
*
24+
* @param InterlanguageLinksLookup $interlanguageLinksLookup
25+
*/
26+
public function __construct( InterlanguageLinksLookup $interlanguageLinksLookup ) {
27+
$this->interlanguageLinksLookup = $interlanguageLinksLookup;
28+
}
29+
30+
/**
31+
* @since 1.0
32+
*
33+
* @param Title $source
34+
* @param string $template
35+
*
36+
* @return string
37+
*/
38+
public function parse( Title $source, $template ) {
39+
40+
$source = $this->interlanguageLinksLookup->getRedirectTargetFor( $source );
41+
42+
if ( $source === null ) {
43+
return '';
44+
}
45+
46+
$languageCode = $this->interlanguageLinksLookup->findPageLanguageForTarget( $source );
47+
48+
if ( $languageCode === '' ) {
49+
return '';
50+
}
51+
52+
if ( $template === '' ) {
53+
return $languageCode;
54+
}
55+
56+
$templateText = $this->createTemplateInclusionCode(
57+
$source,
58+
$languageCode,
59+
$template
60+
);
61+
62+
return array( $templateText, 'noparse' => $templateText === '', 'isHTML' => false );
63+
}
64+
65+
private function createTemplateInclusionCode( $source, $languageCode, $template ) {
66+
67+
$result = '';
68+
$templateText = '';
69+
$wikitext = '';
70+
71+
$wikitext .= "|target-link=" . $this->modifyTargetLink( $source );
72+
$wikitext .= "|lang-code=" . wfBCP47( $languageCode );
73+
$wikitext .= "|lang-name=" . Language::fetchLanguageName( $languageCode );
74+
75+
$templateText .= '{{' . $template . $wikitext . '}}';
76+
77+
return $templateText;
78+
}
79+
80+
private function modifyTargetLink( $targetLink ) {
81+
82+
if ( !$targetLink instanceOf Title ) {
83+
$targetLink = Title::newFromText( $targetLink );
84+
}
85+
86+
return ( $targetLink->getNamespace() === NS_CATEGORY ? ':' : '' ) . $targetLink->getPrefixedText();
87+
}
88+
89+
}

src/HookRegistry.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
154154

155155
$parser->setFunctionHook( $name, $definition, $flag );
156156

157+
list( $name, $definition, $flag ) = $parserFunctionFactory->newAnnotatedLanguageParserFunctionDefinition(
158+
$interlanguageLinksLookup
159+
);
160+
161+
$parser->setFunctionHook( $name, $definition, $flag );
162+
157163
return true;
158164
};
159165

src/ParserFunctionFactory.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,25 @@ public function newInterlanguageListParserFunctionDefinition( InterlanguageLinks
8989
return array( 'interlanguagelist', $interlanguageListParserFunctionDefinition, Parser::SFH_NO_HASH );
9090
}
9191

92+
/**
93+
* @since 1.4
94+
*
95+
* @param InterlanguageLinksLookup $interlanguageLinksLookup
96+
*
97+
* @return array
98+
*/
99+
public function newAnnotatedLanguageParserFunctionDefinition( InterlanguageLinksLookup $interlanguageLinksLookup ) {
100+
101+
$annotatedLanguageParserFunctionDefinition = function( $parser, $template = '' ) use ( $interlanguageLinksLookup ) {
102+
103+
$annotatedLanguageParserFunction = new AnnotatedLanguageParserFunction(
104+
$interlanguageLinksLookup
105+
);
106+
107+
return $annotatedLanguageParserFunction->parse( $parser->getTitle(), $template );
108+
};
109+
110+
return array( 'annotatedlanguage', $annotatedLanguageParserFunctionDefinition, Parser::SFH_NO_HASH );
111+
}
112+
92113
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace SIL\Tests;
4+
5+
use SIL\AnnotatedLanguageParserFunction;
6+
7+
/**
8+
* @covers \SIL\AnnotatedLanguageParserFunction
9+
* @group semantic-interlanguage-links
10+
*
11+
* @license GNU GPL v2+
12+
* @since 1.0
13+
*
14+
* @author mwjames
15+
*/
16+
class AnnotatedLanguageParserFunctionTest extends \PHPUnit_Framework_TestCase {
17+
18+
public function testCanConstruct() {
19+
20+
$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
21+
->disableOriginalConstructor()
22+
->getMock();
23+
24+
$this->assertInstanceOf(
25+
AnnotatedLanguageParserFunction::class,
26+
new AnnotatedLanguageParserFunction( $interlanguageLinksLookup )
27+
);
28+
}
29+
30+
public function testParseOnMissingAnnotatedLanguage() {
31+
32+
$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
33+
->disableOriginalConstructor()
34+
->getMock();
35+
36+
$interlanguageLinksLookup->expects( $this->once() )
37+
->method( 'findPageLanguageForTarget' )
38+
->will( $this->returnValue( '' ) );
39+
40+
$interlanguageLinksLookup->expects( $this->once() )
41+
->method( 'getRedirectTargetFor' )
42+
->will( $this->returnValue( \Title::newFromText( 'Foo' ) ) );
43+
44+
$instance = new AnnotatedLanguageParserFunction(
45+
$interlanguageLinksLookup
46+
);
47+
48+
$this->assertEquals(
49+
'',
50+
$instance->parse( \Title::newFromText( 'Foo' ), 'FakeTemplate' )
51+
);
52+
}
53+
54+
public function testParseOnAnnotatedLanguageWithoutTemplate() {
55+
56+
$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
57+
->disableOriginalConstructor()
58+
->getMock();
59+
60+
$interlanguageLinksLookup->expects( $this->once() )
61+
->method( 'findPageLanguageForTarget' )
62+
->will( $this->returnValue( 'en' ) );
63+
64+
$interlanguageLinksLookup->expects( $this->once() )
65+
->method( 'getRedirectTargetFor' )
66+
->will( $this->returnValue( \Title::newFromText( 'Foo' ) ) );
67+
68+
$instance = new AnnotatedLanguageParserFunction(
69+
$interlanguageLinksLookup
70+
);
71+
72+
$expected = 'en';
73+
74+
$this->assertEquals(
75+
$expected,
76+
$instance->parse( \Title::newFromText( 'Foo' ), '' )
77+
);
78+
}
79+
80+
public function testParseOnAnnotatedLanguageWithTemplate() {
81+
82+
$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
83+
->disableOriginalConstructor()
84+
->getMock();
85+
86+
$interlanguageLinksLookup->expects( $this->once() )
87+
->method( 'findPageLanguageForTarget' )
88+
->will( $this->returnValue( 'en' ) );
89+
90+
$interlanguageLinksLookup->expects( $this->once() )
91+
->method( 'getRedirectTargetFor' )
92+
->will( $this->returnValue( \Title::newFromText( 'Foo' ) ) );
93+
94+
$instance = new AnnotatedLanguageParserFunction(
95+
$interlanguageLinksLookup
96+
);
97+
98+
$expected = '{{FakeTemplate|target-link=Foo|lang-code=en|lang-name=English}}';
99+
100+
$this->assertEquals(
101+
array( $expected, "noparse" => false, "isHTML" => false ),
102+
$instance->parse( \Title::newFromText( 'Foo' ), 'FakeTemplate' )
103+
);
104+
}
105+
106+
}

tests/phpunit/Unit/ParserFunctionFactoryTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,30 @@ public function testNewInterlanguageListParserFunctionDefinition() {
103103
);
104104
}
105105

106+
public function testNewAnnotatedLanguageParserFunctionDefinition() {
107+
108+
$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
109+
->disableOriginalConstructor()
110+
->getMock();
111+
112+
$this->parser->setTitle( Title::newFromText( __METHOD__ ) );
113+
114+
$instance = new ParserFunctionFactory();
115+
116+
list( $name, $definition, $flag ) = $instance->newAnnotatedLanguageParserFunctionDefinition(
117+
$interlanguageLinksLookup
118+
);
119+
120+
$this->assertEquals(
121+
'annotatedlanguage',
122+
$name
123+
);
124+
125+
$text = '';
126+
127+
$this->assertEmpty(
128+
call_user_func_array( $definition, array( $this->parser, $text ) )
129+
);
130+
}
131+
106132
}

0 commit comments

Comments
 (0)