Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
Oracle | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
shouldUseParsoid | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
isParsoidDefaultFor | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\ParserMigration; |
4 | |
5 | use MediaWiki\Config\Config; |
6 | use MediaWiki\Request\WebRequest; |
7 | use MediaWiki\Title\Title; |
8 | use MediaWiki\User\Options\UserOptionsManager; |
9 | use MediaWiki\User\User; |
10 | use MobileContext; |
11 | |
12 | class Oracle { |
13 | |
14 | private Config $mainConfig; |
15 | private UserOptionsManager $userOptionsManager; |
16 | private ?MobileContext $mobileContext; |
17 | |
18 | public const USERPREF_ALWAYS = 1; |
19 | public const USERPREF_DEFAULT = 0; |
20 | public const USERPREF_NEVER = 2; |
21 | |
22 | public function __construct( |
23 | Config $mainConfig, |
24 | UserOptionsManager $userOptionsManager, |
25 | ?MobileContext $mobileContext |
26 | ) { |
27 | $this->mainConfig = $mainConfig; |
28 | $this->userOptionsManager = $userOptionsManager; |
29 | $this->mobileContext = $mobileContext; |
30 | } |
31 | |
32 | /** |
33 | * Determine whether to use Parsoid for read views on this request, |
34 | * request, based on the user's preferences and the URL query string. |
35 | * |
36 | * @param User $user |
37 | * @param WebRequest $request |
38 | * @param Title $title |
39 | * @return bool True if Parsoid should be used for this request |
40 | */ |
41 | public function shouldUseParsoid( User $user, WebRequest $request, Title $title ): bool { |
42 | // Find out if the user has opted in to Parsoid Read Views by default |
43 | $userPref = intval( $this->userOptionsManager->getOption( |
44 | $user, |
45 | 'parsermigration-parsoid-readviews' |
46 | ) ); |
47 | $userOptIn = $this->isParsoidDefaultFor( $title ); |
48 | |
49 | // Override the default if a preference is set |
50 | if ( $userPref === self::USERPREF_ALWAYS ) { |
51 | $userOptIn = true; |
52 | } |
53 | if ( $userPref === self::USERPREF_NEVER ) { |
54 | $userOptIn = false; |
55 | } |
56 | |
57 | // Allow disabling query string handling via config change to manage |
58 | // parser cache usage. |
59 | $queryStringEnabled = $this->mainConfig->get( |
60 | 'ParserMigrationEnableQueryString' |
61 | ); |
62 | if ( !$queryStringEnabled ) { |
63 | // Ignore query string and use Parsoid read views if and only |
64 | // if the user has opted in. |
65 | return $userOptIn; |
66 | } |
67 | |
68 | // Otherwise, use the user's opt-in status to set the default for |
69 | // query string processing. |
70 | return $request->getFuzzyBool( 'useparsoid', $userOptIn ); |
71 | } |
72 | |
73 | /** |
74 | * Determine whether Parsoid should be used by default on this page, |
75 | * based on per-wiki configuration. User preferences and query |
76 | * string parameters are not consulted. |
77 | * @param Title $title |
78 | * @return bool |
79 | */ |
80 | public function isParsoidDefaultFor( Title $title ): bool { |
81 | $articlePagesEnabled = $this->mainConfig->get( |
82 | 'ParserMigrationEnableParsoidArticlePages' |
83 | ); |
84 | // This enables Parsoid on all talk pages, which isn't *exactly* |
85 | // the same as "the set of pages where DiscussionTools is enabled", |
86 | // but it will do for now. |
87 | $talkPagesEnabled = $this->mainConfig->get( |
88 | 'ParserMigrationEnableParsoidDiscussionTools' |
89 | ); |
90 | |
91 | $isEnabled = $title->isTalkPage() ? $talkPagesEnabled : $articlePagesEnabled; |
92 | |
93 | // Exclude mobile domains by default, regardless of the namespace settings |
94 | // above, if the config isn't on |
95 | $disableOnMobile = |
96 | !$this->mainConfig->get( 'ParserMigrationEnableParsoidMobileFrontend' ); |
97 | if ( |
98 | $title->isTalkPage() && |
99 | !$this->mainConfig->get( 'ParserMigrationEnableParsoidMobileFrontendTalkPages' ) |
100 | ) { |
101 | $disableOnMobile = true; |
102 | } |
103 | if ( |
104 | $disableOnMobile && |
105 | $this->mobileContext && |
106 | $this->mobileContext->usingMobileDomain() |
107 | ) { |
108 | $isEnabled = false; |
109 | } |
110 | |
111 | return $isEnabled; |
112 | } |
113 | } |