Debian Codegraph @

skins/Vector/includes/ConfigHelper.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Skins\Vector;
4
5 use MediaWiki\MediaWikiServices;
6 use MediaWiki\Request\WebRequest;
7 use 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 */
13 class 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 ? $title->getRootTitle() : null;
37
38 $exclusions = $options[ 'exclude' ] ?? [];
39 $inclusions = $options[ 'include' ] ?? [];
40
41 $excludeQueryString = $exclusions[ 'querystring' ] ?? [];
42 foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
43 $paramValue = $request->getRawVal( $param );
44 if ( $paramValue !== null ) {
45 if ( $excludedParamPattern === '*' ) {
46 // Backwards compatibility for the '*' wildcard.
47 $excludedParamPattern = '.+';
48 }
49 return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
50 }
51 }
52
53 if ( $title && $title->isMainPage() ) {
54 // only one check to make
55 return $exclusions[ 'mainpage' ] ?? false;
56 }
57 if ( $canonicalTitle && $canonicalTitle->isSpecialPage() ) {
58 $spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
59 [ $canonicalName, $par ] = $spFactory->resolveAlias( $canonicalTitle->getDBKey() );
60 if ( $canonicalName ) {
61 $canonicalTitle = Title::makeTitle( NS_SPECIAL, $canonicalName );
62 }
63 }
64
65 //
66 // Check the inclusions based on the canonical title
67 // The inclusions are checked first as these trump any exclusions.
68 //
69 // Now we have the canonical title and the inclusions link we look for any matches.
70 foreach ( $inclusions as $titleText ) {
71 $includedTitle = Title::newFromText( $titleText );
72
73 if ( $canonicalTitle->equals( $includedTitle ) ) {
74 return false;
75 }
76 }
77
78 //
79 // Check the excluded page titles based on the canonical title
80 //
81 // Now we have the canonical title and the exclusions link we look for any matches.
82 $pageTitles = $exclusions[ 'pagetitles' ] ?? [];
83 foreach ( $pageTitles as $titleText ) {
84 // use strtolower to make sure the config passed for special pages
85 // is case insensitive, so it does not generate a wrong special page title
86 $excludedTitle = Title::newFromText( $titleText );
87
88 if ( $canonicalTitle && $canonicalTitle->equals( $excludedTitle ) ) {
89 return true;
90 }
91 }
92
93 //
94 // Check the exclusions
95 // If nothing matches the exclusions to determine what should happen
96 //
97 $excludeNamespaces = $exclusions[ 'namespaces' ] ?? [];
98 return $title && $title->inNamespaces( $excludeNamespaces );
99 }
100 }