Separate generic ConfigHelper class from LimitedWidthContentRequirement

In preparation for a decision around how we share code, the generic code in
LimitedWidthContentrequirement is moved to ConfigHelper class. A fix
recently applied to Minerva (Ied3ed927202dd9356ebeb7e404230f571a1d910d)
is also incorporated into the new class.

The expectation is this code will be either:
1) be replaced by a submodule
OR
2) Used directly by Vector
after a decision tomorrow.

Bug: T359607
in I3967803bd75cf78d27c1a7c08e62730e46678a68 or removed

Change-Id: I04066acc86594e721e071e9096e1b12e1255b54b
This commit is contained in:
Jon Robson 2024-03-19 15:19:10 -07:00 committed by Jdlrobson
parent cccbea5955
commit f28f5850c5
2 changed files with 102 additions and 48 deletions

100
includes/ConfigHelper.php Normal file
View file

@ -0,0 +1,100 @@
<?php
namespace MediaWiki\Skins\Vector;
use MediaWiki\MediaWikiServices;
use MediaWiki\Request\WebRequest;
use MediaWiki\Title\Title;
class ConfigHelper {
/**
* Determine whether the configuration should be disabled on the page.
*
* @param array $options read from MediaWiki configuration.
* $params = [
* 'exclude' => [
* 'mainpage' => (bool) should it be disabled on the main page?
* 'namespaces' => int[] namespaces it should be excluded on.
* 'querystring' => array of strings mapping to regex for patterns
* the query strings it should be excluded on
* e.g. [ 'action' => '*' ] disable on all actions
* 'pagetitles' => string[] of pages it should be excluded on.
* For special pages, use canonical English name.
* ]
* ]
* @param WebRequest $request
* @param Title|null $title
*
* @return bool
*/
public static function shouldDisable( array $options, WebRequest $request, Title $title = null ) {
$canonicalTitle = $title != null ? $title->getRootTitle() : null;
$exclusions = $options[ 'exclude' ] ?? [];
$inclusions = $options['include'] ?? [];
if ( $title != null && $title->isMainPage() ) {
// only one check to make
return $exclusions[ 'mainpage' ] ?? false;
} elseif ( $canonicalTitle != null && $canonicalTitle->isSpecialPage() ) {
$spFactory = MediaWikiServices::getInstance()->getSpecialPageFactory();
[ $canonicalName, $par ] = $spFactory->resolveAlias( $canonicalTitle->getDBKey() );
if ( $canonicalName ) {
$canonicalTitle = Title::makeTitle( NS_SPECIAL, $canonicalName );
}
}
//
// Check the inclusions based on the canonical title
// The inclusions are checked first as these trump any exclusions.
//
// Now we have the canonical title and the inclusions link we look for any matches.
foreach ( $inclusions as $titleText ) {
$includedTitle = Title::newFromText( $titleText );
if ( $canonicalTitle->equals( $includedTitle ) ) {
return false;
}
}
//
// Check the excluded page titles based on the canonical title
//
// Now we have the canonical title and the exclusions link we look for any matches.
$pageTitles = $exclusions[ 'pagetitles' ] ?? [];
foreach ( $pageTitles as $titleText ) {
// use strtolower to make sure the config passed for special pages
// is case insensitive, so it does not generate a wrong special page title
$titleText = $canonicalTitle->isSpecialPage() ? strtolower( $titleText ) : $titleText;
$excludedTitle = Title::newFromText( $titleText );
if ( $canonicalTitle != null && $canonicalTitle->equals( $excludedTitle ) ) {
return true;
}
}
//
// Check the exclusions
// If nothing matches the exclusions to determine what should happen
//
$excludeNamespaces = $exclusions[ 'namespaces' ] ?? [];
if ( $title != null && $title->inNamespaces( $excludeNamespaces ) ) {
return true;
}
$excludeQueryString = $exclusions[ 'querystring' ] ?? [];
foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
$paramValue = $request->getRawVal( $param );
if ( $paramValue !== null ) {
if ( $excludedParamPattern === '*' ) {
// Backwards compatibility for the '*' wildcard.
$excludedParamPattern = '.+';
}
return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
}
}
return false;
}
}

View file

@ -23,6 +23,7 @@ namespace MediaWiki\Skins\Vector\FeatureManagement\Requirements;
use MediaWiki\Config\Config;
use MediaWiki\Request\WebRequest;
use MediaWiki\Skins\Vector\ConfigHelper;
use MediaWiki\Skins\Vector\Constants;
use MediaWiki\Skins\Vector\FeatureManagement\Requirement;
use MediaWiki\Title\Title;
@ -81,54 +82,7 @@ final class LimitedWidthContentRequirement implements Requirement {
* @return bool
*/
private static function shouldDisableMaxWidth( array $options, Title $title, WebRequest $request ): bool {
$canonicalTitle = $title->getRootTitle();
$inclusions = $options['include'] ?? [];
$exclusions = $options['exclude'] ?? [];
if ( $title->isMainPage() ) {
// only one check to make
return $exclusions['mainpage'] ?? false;
} elseif ( $canonicalTitle->isSpecialPage() ) {
$canonicalTitle->fixSpecialName();
}
//
// Check the inclusions based on the canonical title
// The inclusions are checked first as these trump any exclusions.
//
// Now we have the canonical title and the inclusions link we look for any matches.
foreach ( $inclusions as $titleText ) {
$includedTitle = Title::newFromText( $titleText );
if ( $canonicalTitle->equals( $includedTitle ) ) {
return false;
}
}
//
// Check the exclusions
// If nothing matches the exclusions to determine what should happen
//
$excludeNamespaces = $exclusions['namespaces'] ?? [];
// Max width is disabled on certain namespaces
if ( $title->inNamespaces( $excludeNamespaces ) ) {
return true;
}
$excludeQueryString = $exclusions['querystring'] ?? [];
foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
$paramValue = $request->getRawVal( $param );
if ( $paramValue !== null ) {
if ( $excludedParamPattern === '*' ) {
// Backwards compatibility for the '*' wildcard.
$excludedParamPattern = '.+';
}
return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
}
}
return false;
return ConfigHelper::shouldDisable( $options, $request, $title );
}
/**