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