Merge "ResourceLoader: Document the clientPrefs system and make Skin option"

This commit is contained in:
jenkins-bot 2023-08-18 20:49:17 +00:00 committed by Gerrit Code Review
commit 38ae6adbc5
11 changed files with 64 additions and 52 deletions

View file

@ -3215,16 +3215,6 @@ config-schema:
via the `useskin` query parameter. To uninstall a skin, remove its inclusion
from LocalSettings.php.
@see \SkinFactory::getAllowedSkins
ResourceLoaderClientPreferences:
default: false
description: |-
Enable client-side preferences for unregistered users.
This is only supported for unregistered users. For registered users, skins
and extensions must use user preferences (e.g. hidden or API-only options)
and swap class names server-side through the Skin interface.
@warning EXPERIMENTAL!
@since 1.40
@see \MediaWiki\ResourceLoader\ClientHtml
DisableOutputCompression:
default: false
description: 'Disable output compression (enabled by default if zlib is available)'

View file

@ -1975,12 +1975,6 @@ $wgFallbackSkin = null;
*/
$wgSkipSkins = null;
/**
* Config variable stub for the ResourceLoaderClientPreferences setting, for use by phpdoc and IDEs.
* @see MediaWiki\MainConfigSchema::ResourceLoaderClientPreferences
*/
$wgResourceLoaderClientPreferences = null;
/**
* Config variable stub for the DisableOutputCompression setting, for use by phpdoc and IDEs.
* @see MediaWiki\MainConfigSchema::DisableOutputCompression

View file

@ -1990,12 +1990,6 @@ class MainConfigNames {
*/
public const SkipSkins = 'SkipSkins';
/**
* Name constant for the ResourceLoaderClientPreferences setting, for use with Config::get()
* @see MainConfigSchema::ResourceLoaderClientPreferences
*/
public const ResourceLoaderClientPreferences = 'ResourceLoaderClientPreferences';
/**
* Name constant for the DisableOutputCompression setting, for use with Config::get()
* @see MainConfigSchema::DisableOutputCompression

View file

@ -5163,21 +5163,6 @@ class MainConfigSchema {
'type' => 'map',
];
/**
* Enable client-side preferences for unregistered users.
*
* This is only supported for unregistered users. For registered users, skins
* and extensions must use user preferences (e.g. hidden or API-only options)
* and swap class names server-side through the Skin interface.
*
* @warning EXPERIMENTAL!
* @since 1.40
* @see \MediaWiki\ResourceLoader\ClientHtml
*/
public const ResourceLoaderClientPreferences = [
'default' => false,
];
/**
* Disable output compression (enabled by default if zlib is available)
*/

View file

@ -3393,7 +3393,7 @@ class OutputPage extends ContextSource {
$config = $this->getConfig();
$clientPrefEnabled = (
$config->get( MainConfigNames::ResourceLoaderClientPreferences ) &&
$this->getSkin()->getOptions()['clientPrefEnabled'] &&
!$this->getUser()->isRegistered()
);
$clientPrefCookiePrefix = $config->get( MainConfigNames::CookiePrefix );

View file

@ -60,8 +60,8 @@ class ClientHtml {
* @param array $options [optional] Array of options
* - 'target': Parameter for modules=startup request, see StartUpModule.
* - 'safemode': Parameter for modules=startup request, see StartUpModule.
* - 'clientPrefEnabled': See $wgResourceLoaderClientPreferences.
* - 'clientPrefCookiePrefix': See $wgResourceLoaderClientPreferences.
* - 'clientPrefEnabled': See Skin options.
* - 'clientPrefCookiePrefix': See $wgCookiePrefix.
*/
public function __construct( Context $context, array $options = [] ) {
$this->context = $context;

View file

@ -640,7 +640,6 @@ return [
'FallbackSkin' => 'fallback',
'SkipSkins' => [
],
'ResourceLoaderClientPreferences' => false,
'DisableOutputCompression' => false,
'FragmentMode' => [
0 => 'html5',

View file

@ -257,8 +257,35 @@ abstract class Skin extends ContextSource {
* When an array is passed:
* - `name`: Internal skin name, generally in lowercase to comply with conventions
* for interface message keys and CSS class names which embed this value.
* - `scripts`: An array of ResourceLoader script modules.
* - `styles`: An array of ResourceLoader style modules to load on all pages.
*
* - `styles`: ResourceLoader style modules to load on all pages. Default: `[]`
*
* - `scripts`: ResourceLoader script modules to load on all pages. Default: `[]`
*
* - `toc`: Whether a table of contents is included in the main article content
* area. If your skin has place a table of contents elsewhere (for example, the sidebar),
* set this to `false`.
*
* See ParserOutput::getText() for the implementation logic.
*
* Default: `true`
*
* - `bodyClasses`: An array of extra class names to add to the HTML `<body>` element.
* Default: `[]`
*
* - `bodyOnly`: Whether the skin is takes control of generating the `<html>`, `<head>` and
* `<body>` elements. This is for SkinTemplate subclasses only. For SkinMustache, this is
* always true and ignored.
*
* Default: `false`
*
* - `clientPrefEnabled`: Enable support for mw.user.clientPrefs.
* This instructs OutputPage and ResourceLoader\ClientHtml to include an inline script
* in web responses for unregistered users to switch HTML classes as needed.
*
* Since: MW 1.41
* Default: `false`
*
* - `responsive`: Whether the skin supports responsive behaviour and wants a viewport meta
* tag to be added to the HTML head. Note, users can disable this feature via a user
* preference.
@ -2343,8 +2370,9 @@ abstract class Skin extends ContextSource {
// the <html>, <head> and <body> tags. For SkinMustache this is always true and
// ignored.
'bodyOnly' => false,
// Does the skin support the temporary user banner?
// If it does the temporary user banner is displayed at the top of the page.
'clientPrefEnabled' => false,
'responsive' => false,
'link' => [],
'tempUserBanner' => false,
'menus' => [
// Legacy keys that are enabled by default for backwards compatibility

View file

@ -28,6 +28,7 @@
"mw.storage",
"mw.storage.session",
"mw.user",
"mw.user.clientPrefs",
"mw.util",
"mw.plugin.*",
"mw.cookie",

View file

@ -24,6 +24,7 @@
/**
* Save the feature value to the client preferences cookie.
*
* @private
* @param {string} feature
* @param {string} value
*/
@ -45,9 +46,11 @@
}
/**
* Checks if the feature name is composed of valid characters.
* Check if the feature name is composed of valid characters.
*
* A valid feature name may contain letters, numbers, and "-" characters.
*
* @private
* @param {string} value
* @return {boolean}
*/
@ -56,8 +59,9 @@
}
/**
* Checks if the value is composed of valid characters.
* Check if the value is composed of valid characters.
*
* @private
* @param {string} value
* @return {boolean}
*/
@ -311,7 +315,27 @@
},
/**
* Client preferences store's management
* Manage client preferences
*
* For skins that enable the `clientPrefEnabled` option (see Skin class in PHP),
* this feature allows you to store preferences in the browser session that will
* switch one or more the classes on the HTML document.
*
* This is only supported for unregistered users. For registered users, skins
* and extensions must use user preferences (e.g. hidden or API-only options)
* and swap class names server-side through the Skin interface.
*
* This feature is limited to page views by unregistered users. For logged-in requests,
* store preferences in the database instead, via UserOptionsManager or mw.Api#saveOption
* (may be hidden or API-only to exclude from Special:Preferences), and then include the
* desired classes directly in Skin::getHtmlElementAttributes.
*
* Classes toggled by this feature must be named as `<feature>-clientpref-<value>`,
* where `value` contains only alphanumerical characters (a-zA-Z0-9), and `feature`
* can also include hyphens.
*
* @class mw.user.clientPrefs
* @singleton
*/
clientPrefs: {
/**

View file

@ -3,7 +3,6 @@
namespace MediaWiki\Tests\ResourceLoader;
use HashConfig;
use MediaWiki\MainConfigNames;
use MediaWiki\Request\FauxRequest;
use MediaWiki\ResourceLoader\ClientHtml;
use MediaWiki\ResourceLoader\Context;
@ -347,9 +346,7 @@ class ClientHtmlTest extends \PHPUnit\Framework\TestCase {
}
private static function makeContext( $extraQuery = [] ) {
$conf = new HashConfig( [
MainConfigNames::ResourceLoaderClientPreferences => false
] );
$conf = new HashConfig( [] );
return new Context(
new ResourceLoader( $conf, null, null, [
'loadScript' => '/w/load.php',