Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
PreferenceHelper | |
0.00% |
0 / 15 |
|
0.00% |
0 / 2 |
30 | |
0.00% |
0 / 1 |
setGlobalPreference | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getGlobalPreference | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * User preference helper methods adapted from ContentTranslation extension. |
4 | * |
5 | * @copyright See https://raw.githubusercontent.com/wikimedia/mediawiki-extensions-ContentTranslation/736585619e98883f0907e7eb208a06d456f04c77/AUTHORS.txt |
6 | * @license GPL-2.0-or-later |
7 | */ |
8 | |
9 | namespace MediaWiki\Extension\TheWikipediaLibrary; |
10 | |
11 | use GlobalPreferences\GlobalPreferencesFactory; |
12 | use GlobalPreferences\Storage; |
13 | use MediaWiki\Context\RequestContext; |
14 | use MediaWiki\MediaWikiServices; |
15 | use MediaWiki\User\User; |
16 | |
17 | class PreferenceHelper { |
18 | |
19 | /** |
20 | * Set a global preference for the user. |
21 | * @param User $user |
22 | * @param string $preference |
23 | * @param string $value |
24 | * @return bool |
25 | */ |
26 | public static function setGlobalPreference( User $user, string $preference, string $value ) { |
27 | /** @var GlobalPreferencesFactory $globalPref */ |
28 | $globalPref = MediaWikiServices::getInstance()->getPreferencesFactory(); |
29 | // Need GlobalPreferences extension. |
30 | if ( !$globalPref instanceof GlobalPreferencesFactory ) { |
31 | return false; |
32 | } |
33 | '@phan-var GlobalPreferencesFactory $globalPref'; |
34 | $prefs = $globalPref->getGlobalPreferencesValues( $user, Storage::SKIP_CACHE ); |
35 | $prefs[$preference] = $value; |
36 | $user = $user->getInstanceForUpdate(); |
37 | // Set up the context and check if WikiPage is available from it |
38 | // Once preference definitions don't require the context, this can be removed |
39 | $context = RequestContext::getMain(); |
40 | if ( $context->canUseWikiPage() ) { |
41 | return $globalPref->setGlobalPreferences( $user, $prefs, $context ); |
42 | } |
43 | |
44 | return false; |
45 | } |
46 | |
47 | /** |
48 | * Get a global preference for the user. |
49 | * @param User $user |
50 | * @param string $preference |
51 | * @return string|null Preference value |
52 | */ |
53 | public static function getGlobalPreference( User $user, string $preference ) { |
54 | /** @var GlobalPreferencesFactory $globalPref */ |
55 | $globalPref = MediaWikiServices::getInstance()->getPreferencesFactory(); |
56 | // Need GlobalPreferences extension. |
57 | if ( !$globalPref instanceof GlobalPreferencesFactory ) { |
58 | return null; |
59 | } |
60 | '@phan-var GlobalPreferencesFactory $globalPref'; |
61 | $prefs = $globalPref->getGlobalPreferencesValues( $user, Storage::SKIP_CACHE ); |
62 | return $prefs[$preference] ?? null; |
63 | } |
64 | } |