Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ActionPage | |
0.00% |
0 / 28 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
initLanguage | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
getUserLang | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
setUserOptionsLookup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setLanguageFallback | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
msg | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll\Pages; |
4 | |
5 | use MediaWiki\Extension\SecurePoll\Context; |
6 | use MediaWiki\Extension\SecurePoll\Entities\Election; |
7 | use MediaWiki\Extension\SecurePoll\SpecialSecurePoll; |
8 | use MediaWiki\Extension\SecurePoll\User\Auth; |
9 | use MediaWiki\Extension\SecurePoll\User\Voter; |
10 | use MediaWiki\Language\Language; |
11 | use MediaWiki\Languages\LanguageFallback; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\Message\Message; |
14 | use MediaWiki\User\Options\UserOptionsLookup; |
15 | use MediaWiki\User\User; |
16 | use MessageLocalizer; |
17 | use Wikimedia\Message\MessageSpecifier; |
18 | |
19 | /** |
20 | * Parent class for Special:SecurePoll subpages. |
21 | */ |
22 | abstract class ActionPage implements MessageLocalizer { |
23 | public const LOG_TYPE_ADDADMIN = 0; |
24 | public const LOG_TYPE_REMOVEADMIN = 1; |
25 | public const LOG_TYPE_VIEWVOTES = 2; |
26 | |
27 | /** @var SpecialSecurePoll */ |
28 | public $specialPage; |
29 | /** @var Election */ |
30 | public $election; |
31 | /** @var Auth */ |
32 | public $auth; |
33 | /** @var Context */ |
34 | public $context; |
35 | /** @var UserOptionsLookup */ |
36 | protected $userOptionsLookup; |
37 | /** @var LanguageFallback */ |
38 | protected $languageFallback; |
39 | /** @var Language */ |
40 | public $userLang; |
41 | |
42 | /** |
43 | * Constructor. |
44 | * @param SpecialSecurePoll $specialPage |
45 | */ |
46 | public function __construct( $specialPage ) { |
47 | $this->specialPage = $specialPage; |
48 | $this->context = $specialPage->sp_context; |
49 | } |
50 | |
51 | /** |
52 | * Execute the subpage. |
53 | * @param array $params Array of subpage parameters. |
54 | */ |
55 | abstract public function execute( $params ); |
56 | |
57 | /** |
58 | * Internal utility function for initializing the global entity language |
59 | * fallback sequence. |
60 | * @param Voter|User $user |
61 | * @param Election $election |
62 | */ |
63 | public function initLanguage( $user, $election ) { |
64 | $uselang = $this->specialPage->getRequest()->getVal( 'uselang' ); |
65 | $services = MediaWikiServices::getInstance(); |
66 | $userOptionsLookup = $services->getUserOptionsLookup(); |
67 | $languageFallback = $services->getLanguageFallback(); |
68 | $languageFactory = $services->getLanguageFactory(); |
69 | if ( $uselang !== null ) { |
70 | $userLang = $uselang; |
71 | } elseif ( $user instanceof Voter ) { |
72 | $userLang = $user->getLanguage(); |
73 | } else { |
74 | $userLang = $userOptionsLookup->getOption( $user, 'language' ); |
75 | } |
76 | |
77 | $languages = array_merge( |
78 | [ $userLang ], |
79 | $languageFallback->getAll( $userLang ) |
80 | ); |
81 | |
82 | if ( !in_array( $election->getLanguage(), $languages ) ) { |
83 | $languages[] = $election->getLanguage(); |
84 | } |
85 | if ( !in_array( 'en', $languages ) ) { |
86 | $languages[] = 'en'; |
87 | } |
88 | $this->context->setLanguages( $languages ); |
89 | $this->userLang = $languageFactory->getLanguage( $userLang ); |
90 | } |
91 | |
92 | /** |
93 | * Get the current language. Call this after initLanguage() to get the |
94 | * voter language on the vote subpage. |
95 | * |
96 | * @return Language |
97 | */ |
98 | public function getUserLang() { |
99 | if ( $this->userLang ) { |
100 | return $this->userLang; |
101 | } else { |
102 | return $this->specialPage->getLanguage(); |
103 | } |
104 | } |
105 | |
106 | public function setUserOptionsLookup( $userOptionsLookup ) { |
107 | $this->userOptionsLookup = $userOptionsLookup; |
108 | } |
109 | |
110 | public function setLanguageFallback( $languageFallback ) { |
111 | $this->languageFallback = $languageFallback; |
112 | } |
113 | |
114 | /** |
115 | * Relay for SpecialPage::msg |
116 | * @param string|string[]|MessageSpecifier $key Message key, or array of keys, |
117 | * or a MessageSpecifier. |
118 | * @param mixed ...$params Normal message parameters |
119 | * @return Message |
120 | */ |
121 | public function msg( $key, ...$params ) { |
122 | return $this->specialPage->msg( $key, ...$params ); |
123 | } |
124 | } |