Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
PageDisplayHandlers | |
0.00% |
0 / 25 |
|
0.00% |
0 / 6 |
462 | |
0.00% |
0 / 1 |
shouldShowStoriesOnSkin | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
shouldShowStoriesForAction | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
shouldShowStoriesForUser | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
56 | |||
shouldShowStoriesOnPage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
shouldShowStories | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 | |||
onBeforePageDisplay | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Wikistories\Hooks; |
4 | |
5 | use MediaWiki\Config\Config; |
6 | use MediaWiki\Context\IContextSource; |
7 | use MediaWiki\Extension\BetaFeatures\BetaFeatures; |
8 | use MediaWiki\Extension\Wikistories\Hooks; |
9 | use MediaWiki\MediaWikiServices; |
10 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
11 | use MediaWiki\Output\OutputPage; |
12 | use MediaWiki\Registration\ExtensionRegistry; |
13 | use MediaWiki\Title\Title; |
14 | use MediaWiki\User\Options\UserOptionsLookup; |
15 | use MediaWiki\User\User; |
16 | use Skin; |
17 | |
18 | class PageDisplayHandlers implements BeforePageDisplayHook { |
19 | |
20 | /** |
21 | * @param Skin $skin |
22 | * @return bool |
23 | */ |
24 | private static function shouldShowStoriesOnSkin( Skin $skin ) { |
25 | $isMobileView = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) && |
26 | MediaWikiServices::getInstance()->getService( 'MobileFrontend.Context' )->shouldDisplayMobileView(); |
27 | return $skin->getSkinName() === 'minerva' && $isMobileView; |
28 | } |
29 | |
30 | /** |
31 | * @param IContextSource $context |
32 | * @return bool |
33 | */ |
34 | private static function shouldShowStoriesForAction( IContextSource $context ) { |
35 | return $context->getActionName() === 'view' && |
36 | $context->getRequest()->getText( 'diff' ) === ''; |
37 | } |
38 | |
39 | /** |
40 | * @param User $user |
41 | * @param Config $config |
42 | * @return bool |
43 | */ |
44 | private static function shouldShowStoriesForUser( User $user, Config $config ): bool { |
45 | if ( Hooks::isBetaDiscoveryMode( $config ) ) { |
46 | return $user->isNamed() |
47 | && ExtensionRegistry::getInstance()->isLoaded( 'BetaFeatures' ) |
48 | && BetaFeatures::isFeatureEnabled( $user, BetaFeaturesHandlers::WIKISTORIES_BETA_FEATURE ); |
49 | } elseif ( Hooks::isPublicDiscoveryMode( $config ) ) { |
50 | /** @var UserOptionsLookup $userOptionsLookup */ |
51 | $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
52 | return ( $user->isAnon() || $user->isTemp() ) |
53 | || (bool)$userOptionsLookup->getOption( $user, Hooks::WIKISTORIES_PREF_SHOW_DISCOVERY, true ); |
54 | } else { |
55 | // unknown discovery mode |
56 | return false; |
57 | } |
58 | } |
59 | |
60 | /** |
61 | * @param Title $title |
62 | * @return bool |
63 | */ |
64 | private static function shouldShowStoriesOnPage( Title $title ): bool { |
65 | return !$title->isMainPage() |
66 | && $title->inNamespace( NS_MAIN ) |
67 | && $title->exists(); |
68 | } |
69 | |
70 | /** |
71 | * @param User $user |
72 | * @param Title $title |
73 | * @param Skin $skin |
74 | * @param IContextSource $context |
75 | * @return bool |
76 | */ |
77 | private static function shouldShowStories( User $user, Title $title, Skin $skin, IContextSource $context ): bool { |
78 | return self::shouldShowStoriesForUser( $user, $context->getConfig() ) |
79 | && self::shouldShowStoriesOnSkin( $skin ) |
80 | && self::shouldShowStoriesOnPage( $title ) |
81 | && self::shouldShowStoriesForAction( $context ); |
82 | } |
83 | |
84 | /** |
85 | * @param OutputPage $out |
86 | * @param Skin $skin |
87 | */ |
88 | public function onBeforePageDisplay( $out, $skin ): void { |
89 | $title = $out->getTitle(); |
90 | if ( self::shouldShowStories( $out->getUser(), $title, $out->getSkin(), $out->getContext() ) ) { |
91 | $out->addModules( [ 'ext.wikistories.discover' ] ); |
92 | $out->addModuleStyles( 'ext.wikistories.discover.styles' ); |
93 | } |
94 | } |
95 | } |