Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SiteMapper | |
0.00% |
0 / 33 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| getDomainCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentLanguageCode | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getLanguageCodeFromDomain | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getCXUrl | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getApiURL | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 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; |
| 31 | $mwInstance = MediaWikiServices::getInstance(); |
| 32 | |
| 33 | [ , $domain ] = $wgConf->siteFromDB( $wgDBname ); |
| 34 | |
| 35 | // Fallback for non-wmf-style farms. $domain can be null or empty string in that case. |
| 36 | if ( ( $domain ?? '' ) === '' ) { |
| 37 | return $mwInstance->getContentLanguageCode()->toString(); |
| 38 | } |
| 39 | |
| 40 | $languageCode = self::getLanguageCodeFromDomain( $domain ); |
| 41 | // If an invalid language code is fetched, return the wiki's content language code |
| 42 | if ( !$mwInstance->getLanguageNameUtils()->isKnownLanguageTag( $languageCode ) ) { |
| 43 | $languageCode = $mwInstance->getContentLanguageCode()->toString(); |
| 44 | } |
| 45 | |
| 46 | return $languageCode; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the language code for the given wiki domain. |
| 51 | * |
| 52 | * This can be different from $wgLanguageCode (aka content language). simple.wikipedia.org |
| 53 | * has it as `en`. It can also be different from the subdomain name. For no.wikipedia.org |
| 54 | * it is `nb`. |
| 55 | * |
| 56 | * @return string Language code in a format used internally by CX. |
| 57 | */ |
| 58 | public static function getLanguageCodeFromDomain( string $domain ): string { |
| 59 | global $wgContentTranslationDomainCodeMapping; |
| 60 | |
| 61 | // Do a reverse lookup in our code map, falling back to the domain as key if not |
| 62 | // present in the array. |
| 63 | foreach ( $wgContentTranslationDomainCodeMapping as $key => $value ) { |
| 64 | if ( $domain === $value ) { |
| 65 | return $key; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return $domain; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the URL for Special:CX on the needed wiki |
| 74 | * according to given source and target title and the target language. |
| 75 | * |
| 76 | * @param string $sourceLanguage |
| 77 | * @param string $targetLanguage |
| 78 | * @param string|null $sourceTitle |
| 79 | * @param string|null $targetTitle |
| 80 | * @param array $extra Additional query parameters |
| 81 | * @return string URL |
| 82 | */ |
| 83 | public static function getCXUrl( |
| 84 | string $sourceLanguage, |
| 85 | string $targetLanguage, |
| 86 | ?string $sourceTitle, |
| 87 | ?string $targetTitle, |
| 88 | array $extra = [] |
| 89 | ): string { |
| 90 | // Initialize query parameters |
| 91 | $queryParams = array_merge( [ 'from' => $sourceLanguage, 'to' => $targetLanguage ], $extra ); |
| 92 | |
| 93 | // Add source title if provided |
| 94 | if ( $sourceTitle ) { |
| 95 | $queryParams['page'] = $sourceTitle; |
| 96 | } |
| 97 | |
| 98 | // Add target title if provided |
| 99 | if ( $targetTitle ) { |
| 100 | $queryParams['targettitle'] = $targetTitle; |
| 101 | } |
| 102 | |
| 103 | $cxPage = 'Special:ContentTranslation'; |
| 104 | |
| 105 | $targetCXUrl = self::getPageURL( $targetLanguage, $cxPage ); |
| 106 | return wfAppendQuery( $targetCXUrl, $queryParams ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the API URL constructed from the domain template of sites |
| 111 | */ |
| 112 | public static function getApiURL( string $language, array $params = [] ): string { |
| 113 | global $wgContentTranslationSiteTemplates; |
| 114 | |
| 115 | $domain = self::getDomainCode( $language ); |
| 116 | $url = str_replace( '$1', $domain, $wgContentTranslationSiteTemplates['api'] ); |
| 117 | $parsedUrl = parse_url( $url ); |
| 118 | if ( !isset( $parsedUrl['scheme'] ) ) { |
| 119 | $url = 'https:' . $url; |
| 120 | } |
| 121 | return wfAppendQuery( $url, $params ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the page URL constructed from the domain template of sites |
| 126 | */ |
| 127 | public static function getPageURL( string $language, string $title ): string { |
| 128 | global $wgContentTranslationSiteTemplates; |
| 129 | |
| 130 | $domain = self::getDomainCode( $language ); |
| 131 | |
| 132 | return str_replace( |
| 133 | [ '$1', '$2' ], |
| 134 | [ $domain, $title ], |
| 135 | $wgContentTranslationSiteTemplates['view'] |
| 136 | ); |
| 137 | } |
| 138 | } |