Completed
Push — master ( 1597db...37b3e5 )
by mw
16:02
created

AnnotatedLanguageParserFunction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 76
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B parse() 0 26 4
A createTemplateInclusionCode() 0 14 1
A modifyTargetLink() 0 8 3
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
}
90