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