Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ConfigHelper
92.86% covered (success)
92.86%
26 / 28
0.00% covered (danger)
0.00%
0 / 2
17.11
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldDisable
96.30% covered (success)
96.30%
26 / 27
0.00% covered (danger)
0.00%
0 / 1
16
1<?php
2
3namespace MediaWiki\Skins\Vector;
4
5use MediaWiki\Request\WebRequest;
6use MediaWiki\SpecialPage\SpecialPageFactory;
7use MediaWiki\Title\Title;
8
9/**
10 * @stable for use inside Minerva as a soft dependency temporarily until T360452 is resolved.
11 * @see doc/adr/0004-code-sharing-between-vector-and-minerva.md
12 */
13class ConfigHelper {
14    public function __construct(
15        private readonly SpecialPageFactory $specialPageFactory,
16    ) {
17    }
18
19    /**
20     * Determine whether the configuration should be disabled on the page.
21     *
22     * @param array $options read from MediaWiki configuration.
23     *   $params = [
24     *      'exclude' => [
25     *            'mainpage' => (bool) should it be disabled on the main page?
26     *            'namespaces' => int[] namespaces it should be excluded on.
27     *            'querystring' => array of strings mapping to regex for patterns
28     *                     the query strings it should be excluded on
29     *                     e.g. [ 'action' => '*' ] disable on all actions
30     *            'pagetitles' => string[] of pages it should be excluded on.
31     *                     For special pages, use canonical English name.
32     *      ]
33     *   ]
34     * @param WebRequest $request
35     * @param Title|null $title
36     *
37     * @return bool
38     */
39    public function shouldDisable( array $options, WebRequest $request, ?Title $title = null ) {
40        $canonicalTitle = $title ? $title->getRootTitle() : null;
41
42        $exclusions = $options[ 'exclude' ] ?? [];
43        $inclusions = $options[ 'include' ] ?? [];
44
45        $excludeQueryString = $exclusions[ 'querystring' ] ?? [];
46        foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
47            $paramValue = $request->getRawVal( $param );
48            if ( $paramValue !== null ) {
49                if ( $excludedParamPattern === '*' ) {
50                    // Backwards compatibility for the '*' wildcard.
51                    $excludedParamPattern = '.+';
52                }
53                return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
54            }
55        }
56
57        if ( $title && $title->isMainPage() ) {
58            // only one check to make
59            return $exclusions[ 'mainpage' ] ?? false;
60        }
61        if ( $canonicalTitle && $canonicalTitle->isSpecialPage() ) {
62            [ $canonicalName, $par ] = $this->specialPageFactory->resolveAlias( $canonicalTitle->getDBKey() );
63            if ( $canonicalName ) {
64                $canonicalTitle = Title::makeTitle( NS_SPECIAL, $canonicalName );
65            }
66        }
67
68        //
69        // Check the inclusions based on the canonical title
70        // The inclusions are checked first as these trump any exclusions.
71        //
72        // Now we have the canonical title and the inclusions link we look for any matches.
73        foreach ( $inclusions as $titleText ) {
74            $includedTitle = Title::newFromText( $titleText );
75
76            if ( $canonicalTitle->equals( $includedTitle ) ) {
77                return false;
78            }
79        }
80
81        //
82        // Check the excluded page titles based on the canonical title
83        //
84        // Now we have the canonical title and the exclusions link we look for any matches.
85        $pageTitles = $exclusions[ 'pagetitles' ] ?? [];
86        foreach ( $pageTitles as $titleText ) {
87            // use strtolower to make sure the config passed for special pages
88            // is case insensitive, so it does not generate a wrong special page title
89            $excludedTitle = Title::newFromText( $titleText );
90
91            if ( $canonicalTitle && $canonicalTitle->equals( $excludedTitle ) ) {
92                return true;
93            }
94        }
95
96        //
97        // Check the exclusions
98        // If nothing matches the exclusions to determine what should happen
99        //
100        $excludeNamespaces = $exclusions[ 'namespaces' ] ?? [];
101        return $title && $title->inNamespaces( $excludeNamespaces );
102    }
103}