Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ConfigHelper.php
1<?php
2declare( strict_types = 1 );
3
5
7use MediaWiki\Permissions\Authority;
8
22 public function getValidationExclusionFile() {
23 global $wgTranslateValidationExclusionFile;
24 return $wgTranslateValidationExclusionFile;
25 }
26
27 public function getTranslateAuthorExclusionList(): array {
28 global $wgTranslateAuthorExclusionList;
29 return $wgTranslateAuthorExclusionList;
30 }
31
32 public function getDisabledTargetLanguages(): array {
33 global $wgTranslateDisabledTargetLanguages;
34 return $wgTranslateDisabledTargetLanguages;
35 }
36
44 public function isTargetLanguageDisabled(
45 MessageGroup $group,
46 string $languageCode,
47 ?string &$reason = null
48 ): bool {
49 $globalDisabledReason = null;
50 $groupId = $group->getId();
51 $checks = [
52 $groupId,
53 strtok( $groupId, '-' ),
54 '*'
55 ];
56
57 $disabledLanguages = $this->getDisabledTargetLanguages();
58 foreach ( $checks as $check ) {
59 if ( isset( $disabledLanguages[$check][$languageCode] ) ) {
60 $globalDisabledReason = $disabledLanguages[$check][$languageCode];
61 break;
62 }
63 }
64
65 $groupLanguages = $group->getTranslatableLanguages();
66
67 $isLanguageDisabled = $groupLanguages === MessageGroup::DEFAULT_LANGUAGES
68 ? $globalDisabledReason !== null
69 : !array_key_exists( $languageCode, $groupLanguages );
70
71 // Only set the reason if the language is actually disabled
72 if ( $isLanguageDisabled ) {
73 $reason = $globalDisabledReason;
74 }
75
76 return $isLanguageDisabled;
77 }
78
90 Authority $authority,
91 int $translated,
92 int $fuzzy
93 ): bool {
94 if ( $translated === 0 && $fuzzy === 0 ) {
95 return false;
96 }
97 return $authority->isAllowed( 'pagetranslation' );
98 }
99
100 public function isAuthorExcluded( MessageGroup|string $group, string $languageCode, string $username ): bool {
101 $groupId = $group instanceof MessageGroup ? $group->getId() : $group;
102 $hash = "$groupId;$languageCode;$username";
103 $authorExclusionList = $this->getTranslateAuthorExclusionList();
104 $excluded = false;
105
106 foreach ( $authorExclusionList as [ $type, $regex ] ) {
107 if ( preg_match( $regex, $hash ) ) {
108 if ( $type === 'include' ) {
109 return false;
110 } else {
111 $excluded = true;
112 }
113 }
114 }
115
116 return $excluded;
117 }
118}
A helper class added to work with configuration values of the Translate Extension.
isTargetLanguageDisabled(MessageGroup $group, string $languageCode, ?string &$reason=null)
Helper to validate MessageGroup::getTranslatableLanguage against site configuration.
canSeeExcludedLanguageStats(Authority $authority, int $translated, int $fuzzy)
Whether an excluded language group with existing translations should be visible to the given user.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
getTranslatableLanguages()
Get all the translatable languages for a group, considering the inclusion and exclusion list.
getId()
Returns the unique identifier for this group.