Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
ULSRelevantLanguagesEntrypointRegistrationHandler | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onBeforePageDisplay | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
getWikipediaList | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace ContentTranslation\HookHandler; |
6 | |
7 | use ContentTranslation\PreferenceHelper; |
8 | use MediaWiki\Extension\SiteMatrix\SiteMatrix; |
9 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
10 | use MediaWiki\Registration\ExtensionRegistry; |
11 | use MediaWiki\ResourceLoader\Context; |
12 | |
13 | class ULSRelevantLanguagesEntrypointRegistrationHandler implements BeforePageDisplayHook { |
14 | |
15 | /** @var PreferenceHelper */ |
16 | private $preferenceHelper; |
17 | |
18 | public function __construct( PreferenceHelper $preferenceHelper ) { |
19 | $this->preferenceHelper = $preferenceHelper; |
20 | } |
21 | |
22 | public function onBeforePageDisplay( $out, $skin ): void { |
23 | $user = $out->getUser(); |
24 | // Enable entrypoint only on Wikipedias where Content Translation is enabled and only for users |
25 | // that have not disabled CX entrypoints |
26 | if ( |
27 | !$this->preferenceHelper->isEnabledForUser( $user ) |
28 | || $this->preferenceHelper->isCXEntrypointDisabled( $user ) |
29 | ) { |
30 | return; |
31 | } |
32 | |
33 | $isContentPage = $out->getTitle()->isContentPage(); |
34 | |
35 | if ( !$isContentPage ) { |
36 | return; |
37 | } |
38 | |
39 | $out->addModules( 'ext.cx.entrypoints.ulsrelevantlanguages' ); |
40 | } |
41 | |
42 | /** |
43 | * Get the list of Wikipedia languages |
44 | * |
45 | * @param Context $context |
46 | * @return string[] Array of language codes |
47 | * @suppress PhanUndeclaredClassMethod |
48 | */ |
49 | public static function getWikipediaList( Context $context ): array { |
50 | // If SiteMatrix extension is loaded, get the list of languages from it. |
51 | // Note that the sitematrix can also be fetched by calling the action=sitematrix api, |
52 | // But it is discouraged to have http requests generated from a ResourceLoader callback. |
53 | if ( ExtensionRegistry::getInstance()->isLoaded( 'SiteMatrix' ) ) { |
54 | $sitematrix = new SiteMatrix(); |
55 | $languages = $sitematrix->getLangList(); |
56 | $filteredLanguages = array_filter( $languages, static function ( $langCode ) use ( $sitematrix ) { |
57 | // Check if the language is a Wikipedia |
58 | // @phan-suppress-next-line PhanUndeclaredClassMethod |
59 | return $sitematrix->exist( $langCode, 'wiki' ); |
60 | } ); |
61 | return array_values( $filteredLanguages ); |
62 | } |
63 | |
64 | // If SiteMatrix extension is not loaded, known wikis are hardcoded here. |
65 | // This is based on the list of wikipedias available on November 2024. |
66 | // This code should be useful only for development and testing purposes. |
67 | // Production wikis will have SiteMatrix extension loaded. |
68 | // There are ways to get the list of wikis by installing sitematrix extension locally. |
69 | // But it requires additional setup and configuration - a text file with known wikis is part of that setup. |
70 | // So it is not much different from hardcoding the list here. |
71 | // Updating this list is not that important as we have enough languages for testing and debugging. |
72 | |
73 | return [ |
74 | 'aa', 'ab', 'ace', 'ady', 'af', 'ak', 'alt', 'am', 'ami', 'an', 'ang', 'ann', 'anp', 'ar', 'arc', 'ary', |
75 | 'arz', 'as', 'ast', 'atj', 'av', 'avk', 'awa', 'ay', 'az', 'azb', 'ba', 'ban', 'bar', 'bbc', 'bcl', 'bdr', |
76 | 'be', 'be-tarask', 'bew', 'bg', 'bh', 'bi', 'bjn', 'blk', 'bm', 'bn', 'bo', 'bpy', 'br', 'bs', 'btm', |
77 | 'bug', 'bxr', 'ca', 'cbk-zam', 'cdo', 'ce', 'ceb', 'ch', 'cho', 'chr', 'chy', 'ckb', 'co', 'cr', 'crh', |
78 | 'cs', 'csb', 'cu', 'cv', 'cy', 'da', 'dag', 'de', 'dga', 'din', 'diq', 'dsb', 'dtp', 'dty', 'dv', 'dz', |
79 | 'ee', 'el', 'eml', 'en', 'eo', 'es', 'et', 'eu', 'ext', 'fa', 'fat', 'ff', 'fi', 'fj', 'fo', 'fon', 'fr', |
80 | 'frp', 'frr', 'fur', 'fy', 'ga', 'gag', 'gan', 'gcr', 'gd', 'gl', 'glk', 'gn', 'gom', 'gor', 'got', 'gpe', |
81 | 'gsw', 'gu', 'guc', 'gur', 'guw', 'gv', 'ha', 'hak', 'haw', 'he', 'hi', 'hif', 'ho', 'hr', 'hsb', 'ht', |
82 | 'hu', 'hy', 'hyw', 'hz', 'ia', 'iba', 'id', 'ie', 'ig', 'igl', 'ii', 'ik', 'ilo', 'inh', 'io', 'is', 'it', |
83 | 'iu', 'ja', 'jam', 'jbo', 'jv', 'ka', 'kaa', 'kab', 'kbd', 'kbp', 'kcg', 'kg', 'kge', 'ki', 'kj', 'kk', |
84 | 'kl', 'km', 'kn', 'ko', 'koi', 'kr', 'krc', 'ks', 'ksh', 'ku', 'kus', 'kv', 'kw', 'ky', 'la', 'lad', 'lb', |
85 | 'lbe', 'lez', 'lfn', 'lg', 'li', 'lij', 'lld', 'lmo', 'ln', 'lo', 'lrc', 'lt', 'ltg', 'lv', 'lzh', 'mad', |
86 | 'mai', 'map-bms', 'mdf', 'mg', 'mh', 'mhr', 'mi', 'min', 'mk', 'ml', 'mn', 'mni', 'mnw', 'mo', 'mos', 'mr', |
87 | 'mrj', 'ms', 'mt', 'mus', 'mwl', 'my', 'myv', 'mzn', 'na', 'nah', 'nan', 'nap', 'nds', 'nds-nl', 'ne', |
88 | 'new', 'ng', 'nia', 'nl', 'nn', 'no', 'nov', 'nqo', 'nr', 'nrm', 'nso', 'nv', 'ny', 'oc', 'olo', 'om', |
89 | 'or', 'os', 'pa', 'pag', 'pam', 'pap', 'pcd', 'pcm', 'pdc', 'pfl', 'pi', 'pih', 'pl', 'pms', 'pnb', 'pnt', |
90 | 'ps', 'pt', 'pwn', 'qu', 'rm', 'rmy', 'rn', 'ro', 'roa-tara', 'rsk', 'ru', 'rue', 'rup', 'rw', 'sa', |
91 | 'sah', 'sat', 'sc', 'scn', 'sco', 'sd', 'se', 'sg', 'sgs', 'sh', 'shi', 'shn', 'shy', 'si', 'simple', |
92 | 'sk', 'skr', 'sl', 'sm', 'smn', 'sn', 'so', 'sq', 'sr', 'srn', 'ss', 'st', 'stq', 'su', 'sv', 'sw', 'szl', |
93 | 'szy', 'ta', 'tay', 'tcy', 'tdd', 'te', 'tet', 'tg', 'th', 'ti', 'tk', 'tl', 'tly', 'tn', 'to', |
94 | 'tpi', 'tr', 'trv', 'ts', 'tt', 'tum', 'tw', 'ty', 'tyv', 'udm', 'ug', 'uk', 'ur', 'uz', 've', 'vec', |
95 | 'vep', 'vi', 'vls', 'vo', 'vro', 'wa', 'war', 'wo', 'wuu', 'xal', 'xh', 'xmf', |
96 | 'yi', 'yo', 'yue', 'za', 'zea', 'zgh', 'zh', 'zu' ]; |
97 | } |
98 | |
99 | } |