Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.90% covered (warning)
62.90%
39 / 62
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Oracle
62.90% covered (warning)
62.90%
39 / 62
50.00% covered (danger)
50.00%
2 / 4
46.71
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldUseParsoid
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 isParsoidDefaultFor
94.74% covered (success)
94.74%
36 / 38
0.00% covered (danger)
0.00%
0 / 1
15.03
 showingMobileView
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace MediaWiki\Extension\ParserMigration;
4
5use MediaWiki\Config\Config;
6use MediaWiki\HookContainer\HookContainer;
7use MediaWiki\Page\PageProps;
8use MediaWiki\Request\WebRequest;
9use MediaWiki\Title\Title;
10use MediaWiki\User\Options\UserOptionsManager;
11use MediaWiki\User\User;
12use MobileContext;
13
14class Oracle {
15
16    public const USERPREF_ALWAYS = 1;
17    public const USERPREF_DEFAULT = 0;
18    public const USERPREF_NEVER = 2;
19
20    private readonly Hook\HookRunner $hookRunner;
21
22    public function __construct(
23        private readonly Config $mainConfig,
24        private readonly UserOptionsManager $userOptionsManager,
25        HookContainer $hookContainer,
26        private PageProps $pageProps,
27        private readonly ?MobileContext $mobileContext,
28    ) {
29        $this->hookRunner = new Hook\HookRunner( $hookContainer );
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 a hook to opt-in a user (for example, as part of an
58        // experimental intervention).
59        $this->hookRunner->onShouldUseParsoid(
60            $user,
61            $request,
62            $title,
63            $userOptIn
64        );
65
66        // Allow disabling query string handling via config change to manage
67        // parser cache usage.
68        $queryStringEnabled = $this->mainConfig->get(
69            'ParserMigrationEnableQueryString'
70        );
71        if ( !$queryStringEnabled ) {
72            // Ignore query string and use Parsoid read views if and only
73            // if the user has opted in / the hook indicates we should.
74            return $userOptIn;
75        }
76
77        // Otherwise, use the user's opt-in status to set the default for
78        // query string processing.
79        // (Query string needs to be able to override $userOptIn in order to
80        // allow the "switch to legacy"/"switch to Parsoid" options in the
81        // sidebar to work.)
82        return $request->getFuzzyBool( 'useparsoid', $userOptIn );
83    }
84
85    /**
86     * Determine whether Parsoid should be used by default on this page,
87     * based on per-wiki configuration.  User preferences and query
88     * string parameters are not consulted.
89     * @param Title $title
90     * @return bool
91     */
92    public function isParsoidDefaultFor( Title $title ): bool {
93        $articlePagesEnabled = $this->mainConfig->get(
94            $this->showingMobileView() ?
95            'ParserMigrationEnableParsoidMobileArticlePages' :
96            'ParserMigrationEnableParsoidArticlePages'
97        );
98        // This enables Parsoid on all talk pages, which isn't *exactly*
99        // the same as "the set of pages where DiscussionTools is enabled",
100        // but it will do for now.
101        $talkPagesEnabled = $this->mainConfig->get(
102            $this->showingMobileView() ?
103            'ParserMigrationEnableParsoidMobileTalkPages' :
104            'ParserMigrationEnableParsoidTalkPages'
105        );
106
107        $isEnabled = $title->isTalkPage() ? $talkPagesEnabled : $articlePagesEnabled;
108
109        // Incremental deploys (T391881)
110        // (avoid md5 hash unless needed for incremental deploy)
111        $percentage = $this->mainConfig->get( 'ParserMigrationEnableParsoidPercentage' );
112
113        // Let's allow overriding all this confusing configuration with
114        // a single map from namespace to percentage.
115        $unifiedConfig = $this->mainConfig->get(
116            'ParserMigrationEnableParsoid'
117        );
118        // first key: "mobile", "desktop" or "default" (for both)
119        // second key: namespace number, or "talk" or "default" (for all)
120        // value should be a boolean or a numeric percentage.
121        // eg: [
122        //   'mobile' => true,
123        //   'default' => [ NS_MAIN => 5, 'talk' => true, 'default' => 0 ],
124        // ]
125        $unifiedConfig =
126            $unifiedConfig[$this->showingMobileView() ? "mobile" : "desktop"] ??
127            $unifiedConfig['default'] ?? null;
128        if ( is_array( $unifiedConfig ) ) {
129            $unifiedConfig = $unifiedConfig[$title->getNamespace()] ??
130                ( !$title->isTalkPage() ? null : $unifiedConfig['talk'] ?? null ) ??
131                $unifiedConfig['default'] ?? null;
132        }
133        if ( is_bool( $unifiedConfig ) ) {
134            $isEnabled = $unifiedConfig;
135            $percentage = 100;
136        } elseif ( is_int( $unifiedConfig ) ) {
137            $isEnabled = true;
138            $percentage = $unifiedConfig;
139        } else {
140            // Don't override, use the values from above.
141        }
142
143        if ( $isEnabled && ( $percentage < 100 ) ) {
144            $key = $title->getNamespace() . ':' . $title->getDBkey();
145            $hash = hexdec( substr( md5( $key ), 0, 8 ) ) & 0x7fffffff;
146            if ( ( $hash % 100 ) >= $percentage ) {
147                $isEnabled = false;
148            }
149        }
150        $excludeProps = $this->mainConfig->get( 'ParserMigrationExcludePageProps' );
151        if ( $isEnabled && $excludeProps ) {
152            if ( $this->pageProps->getProperties( $title, $excludeProps ) !== [] ) {
153                $isEnabled = false;
154            }
155        }
156
157        return $isEnabled;
158    }
159
160    /** Proxy MobileContext::shouldDisplayMobileView() */
161    public function showingMobileView(): bool {
162        return $this->mobileContext &&
163            $this->mobileContext->shouldDisplayMobileView();
164    }
165}