Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
SiteMapper | |
0.00% |
0 / 31 |
|
0.00% |
0 / 6 |
156 | |
0.00% |
0 / 1 |
getDomainCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCurrentLanguageCode | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
getCXServerURL | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
getRestApiURL | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getApiURL | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getPageURL | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ContentTranslation; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | |
7 | class SiteMapper { |
8 | /** |
9 | * Get the domain code matching language |
10 | * |
11 | * @param string $language Language code (MediaWiki internal format) |
12 | * @return string |
13 | */ |
14 | public static function getDomainCode( $language ) { |
15 | global $wgContentTranslationDomainCodeMapping; |
16 | |
17 | return $wgContentTranslationDomainCodeMapping[$language] ?? $language; |
18 | } |
19 | |
20 | /** |
21 | * Get the target language code for the current wiki. |
22 | * |
23 | * This can be different from $wgLanguageCode (aka content language). simple.wikipedia.org |
24 | * has it as `en`. It can also be different from the subdomain name. For no.wikipedia.org |
25 | * it is `nb`. |
26 | * |
27 | * @return string Language code in a format used internally by CX. |
28 | */ |
29 | public static function getCurrentLanguageCode() { |
30 | global $wgConf, $wgDBname, $wgContentTranslationDomainCodeMapping; |
31 | |
32 | [ , $domain ] = $wgConf->siteFromDB( $wgDBname ); |
33 | |
34 | // Fallback for non-wmf-style farms. $domain can be null or empty string in that case. |
35 | if ( ( $domain ?? '' ) === '' ) { |
36 | return MediaWikiServices::getInstance()->getContentLanguage()->getCode(); |
37 | } |
38 | |
39 | // Do a reverse lookup in our code map, falling back to the domain as key if not |
40 | // present in the array. |
41 | foreach ( $wgContentTranslationDomainCodeMapping as $key => $value ) { |
42 | if ( $domain === $value ) { |
43 | return $key; |
44 | } |
45 | } |
46 | |
47 | return $domain; |
48 | } |
49 | |
50 | /** |
51 | * @param string $module |
52 | * @param array $params |
53 | * @return string |
54 | */ |
55 | public static function getCXServerURL( string $module, array $params = [] ): string { |
56 | global $wgContentTranslationSiteTemplates; |
57 | global $wgContentTranslationVersion; |
58 | |
59 | $cxserverURL = $wgContentTranslationSiteTemplates['cx'] . $module; |
60 | if ( (int)$wgContentTranslationVersion === 2 ) { |
61 | $cxserverURL = str_replace( 'v1', 'v2', $cxserverURL ); |
62 | } |
63 | $parsedUrl = parse_url( $cxserverURL ); |
64 | if ( !isset( $parsedUrl['scheme'] ) ) { |
65 | // $wgContentTranslationSiteTemplates['cx'] is protocol relative path |
66 | $cxserverURL = 'https:' . $cxserverURL; |
67 | } |
68 | |
69 | return wfAppendQuery( $cxserverURL, $params ); |
70 | } |
71 | |
72 | /** |
73 | * @param string $language |
74 | * @param string $module |
75 | * @param array $params |
76 | * @return string |
77 | */ |
78 | public static function getRestApiURL( string $language, string $module, array $params = [] ): string { |
79 | global $wgContentTranslationSiteTemplates; |
80 | |
81 | $domain = self::getDomainCode( $language ); |
82 | $restbaseUrl = $wgContentTranslationSiteTemplates['restbase'] . $module; |
83 | $parsedUrl = parse_url( $restbaseUrl ); |
84 | |
85 | if ( !isset( $parsedUrl['scheme'] ) ) { |
86 | // $wgContentTranslationSiteTemplates['restbase'] is protocol relative path |
87 | $restbaseUrl = 'https:' . $restbaseUrl; |
88 | } |
89 | |
90 | $url = str_replace( '$1', $domain, $restbaseUrl ); |
91 | return wfAppendQuery( $url, $params ); |
92 | } |
93 | |
94 | /** |
95 | * Get the API URL constructed from the domain template of sites |
96 | * @param string $language |
97 | * @param array|null $params |
98 | * @return string |
99 | */ |
100 | public static function getApiURL( $language, $params = null ) { |
101 | global $wgContentTranslationSiteTemplates; |
102 | |
103 | $domain = self::getDomainCode( $language ); |
104 | // $wgContentTranslationSiteTemplates['api'] is protocol relative path |
105 | $url = 'https:' . str_replace( '$1', $domain, $wgContentTranslationSiteTemplates['api'] ); |
106 | return wfAppendQuery( $url, $params ); |
107 | } |
108 | |
109 | /** |
110 | * Get the page URL constructed from the domain template of sites |
111 | * @param string $language |
112 | * @param string $title |
113 | * @return string |
114 | */ |
115 | public static function getPageURL( $language, $title ) { |
116 | global $wgContentTranslationSiteTemplates; |
117 | |
118 | $domain = self::getDomainCode( $language ); |
119 | |
120 | return str_replace( |
121 | [ '$1', '$2' ], |
122 | [ $domain, $title ], |
123 | $wgContentTranslationSiteTemplates['view'] |
124 | ); |
125 | } |
126 | } |