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