Allow more control over the max-width rules

Bug: T260091
Change-Id: Ie534b0c34e240c588a4cc330898531f1d12df1f0
This commit is contained in:
jdlrobson 2021-01-06 13:50:19 -08:00 committed by Jdlrobson
parent b0c30102fb
commit 05dc15954d
4 changed files with 249 additions and 5 deletions

View File

@ -12,6 +12,7 @@ use ResourceLoaderContext;
use Skin;
use SkinTemplate;
use SkinVector;
use Title;
use User;
use Vector\HTMLForm\Fields\HTMLLegacySkinVersionField;
@ -278,9 +279,93 @@ class Hooks {
return;
}
if ( !$out->getConfig()->get( 'VectorUseCoreSearch' ) ) {
$config = $sk->getConfig();
if ( !$config->get( 'VectorUseCoreSearch' ) ) {
$bodyAttrs['class'] .= ' skin-vector-search-vue';
}
if ( $sk->getTitle() && self::shouldDisableMaxWidth(
$config->get( 'VectorMaxWidthOptions' ),
$sk->getTitle(),
$out->getRequest()->getValues()
) ) {
$bodyAttrs['class'] .= ' skin-vector-disable-max-width';
}
}
/**
* Per the $options configuration (for use with $wgVectorMaxWidthOptions)
* determine whether max-width should be disabled on the page.
* For the main page: Check the value of $options['exclude']['mainpage']
* For all other pages, the following will happen:
* - the array $options['include'] of canonical page names will be checked
* against the current page. If a page has been listed there, function will return false
* (max-width will not be disabled)
* Max width is disabled if:
* 1) The current namespace is listed in array $options['exclude']['namespaces']
* OR
* 2) The query string matches one of the name and value pairs $exclusions['querystring'].
* Note the wildcard "*" for a value, will match all query string values for the given
* query string parameter.
*
* @internal only for use inside tests.
* @param array $options
* @param Title $title
* @param array $requestValues
* @return bool
*/
public static function shouldDisableMaxWidth( array $options, Title $title, array $requestValues ) {
$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 => $excludedParamValue ) {
$paramValue = $requestValues[$param] ?? false;
if ( $paramValue ) {
if ( $excludedParamValue === '*' ) {
// check wildcard
return true;
} elseif ( $paramValue === $excludedParamValue ) {
// Check if the excluded param value matches
return true;
}
}
}
return false;
}
/**

View File

@ -200,8 +200,7 @@ body {
margin-right: auto;
// For container logic specific to special pages and history pages.
.action-history &,
.ns-special & {
.skin-vector-disable-max-width & {
// Allow the max-width of content on history/special pages to be wider than
// the max-width of content on article pages.
max-width: none;
@ -240,8 +239,7 @@ body {
// Adjusts the content when sidebar is open regardless of the viewport width.
.mw-checkbox-hack-checkbox:checked ~ .mw-workspace-container .mw-content-container {
// For container logic specific to special pages and history pages.
.action-history &,
.ns-special & {
.skin-vector-disable-max-width & {
margin-left: @margin-start-content;
}
}

View File

@ -181,6 +181,25 @@
"VectorUseIconWatch": {
"value": true
},
"VectorMaxWidthOptions": {
"value": {
"exclude": {
"mainpage": false,
"querystring": {
"action": "history",
"diff": "*"
},
"namespaces": [
-1,
14
]
},
"include": [
"Special:Preferences"
]
},
"description": "options for configuring where where max-width should and should not apply. More details can be found in Hooks::shouldDisableMaxWidth PHP documentation."
},
"VectorResponsive": {
"value": false
},

View File

@ -18,6 +18,148 @@ const SKIN_PREFS_SECTION = 'rendering/skin/skin-prefs';
* @coversDefaultClass \Vector\Hooks
*/
class VectorHooksTest extends \MediaWikiTestCase {
/**
* helper
* @param bool $excludeMainPage
* @param array $excludeNamespaces
* @param array $include
* @param array $querystring
* @return array
*/
private static function makeMaxWidthConfig(
$excludeMainPage,
$excludeNamespaces = [],
$include = [],
$querystring = []
) {
return [
'exclude' => [
'mainpage' => $excludeMainPage,
'namespaces' => $excludeNamespaces,
'querystring' => $querystring,
],
'include' => $include
];
}
/**
* @covers ::shouldDisableMaxWidth
*/
public function providerShouldDisableMaxWidth() {
$excludeTalkFooConfig = self::makeMaxWidthConfig(
false,
[ NS_TALK ],
[ 'Talk:Foo' ],
[]
);
return [
[
'No options, nothing disables max width',
[],
Title::makeTitle( NS_MAIN, 'Foo' ),
[],
false
],
[
'Main page disables max width if exclude.mainpage set',
self::makeMaxWidthConfig( true ),
Title::newMainPage(),
[],
true
],
[
'Namespaces can be excluded',
self::makeMaxWidthConfig( false, [ NS_CATEGORY ] ),
Title::makeTitle( NS_CATEGORY, 'Category' ),
[],
true
],
[
'Namespaces are included if not excluded',
self::makeMaxWidthConfig( false, [ NS_CATEGORY ] ),
Title::makeTitle( NS_SPECIAL, 'SpecialPages' ),
[],
false
],
[
'More than one namespace can be included',
self::makeMaxWidthConfig( false, [ NS_CATEGORY, NS_SPECIAL ] ),
Title::makeTitle( NS_SPECIAL, 'Specialpages' ),
[],
true
],
[
'Can be disabled on history page',
self::makeMaxWidthConfig(
false,
[
/* no namespaces excluded */
],
[
/* no includes */
],
[ 'action' => 'history' ]
),
Title::makeTitle( NS_MAIN, 'History page' ),
[ 'action' => 'history' ],
true
],
[
'Include can override exclusions',
self::makeMaxWidthConfig(
false,
[ NS_CATEGORY, NS_SPECIAL ],
[ 'Special:Specialpages' ],
[ 'action' => 'history' ]
),
Title::makeTitle( NS_SPECIAL, 'Specialpages' ),
[ 'action' => 'history' ],
false
],
[
'Max width can be disabled on talk pages',
$excludeTalkFooConfig,
Title::makeTitle( NS_TALK, 'A talk page' ),
[],
true
],
[
'includes can be used to override any page in a disabled namespace',
$excludeTalkFooConfig,
Title::makeTitle( NS_TALK, 'Foo' ),
[],
false
],
[
'Excludes/includes are based on root title so should apply to subpages',
$excludeTalkFooConfig,
Title::makeTitle( NS_TALK, 'Foo/subpage' ),
[],
false
]
];
}
/**
* @covers ::shouldDisableMaxWidth
* @dataProvider providerShouldDisableMaxWidth
*/
public function testShouldDisableMaxWidth(
$msg,
$options,
$title,
$requestValues,
$shouldDisableMaxWidth
) {
$this->assertSame(
Hooks::shouldDisableMaxWidth( $options, $title, $requestValues ),
$shouldDisableMaxWidth,
$msg
);
}
/**
* @covers ::onGetPreferences
*/