wiki.techinc.nl/includes/resourceloader/ResourceLoaderUserOptionsModule.php

87 lines
2.5 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserOptionsLookup;
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Trevor Parscal
* @author Roan Kattouw
*/
/**
resourceloader: Merge 'user.tokens' module into 'user.options' For back-compat, keep 'user.tokens' as deprecated alias to 'user.options' for one release cycle (to be removed in MW 1.36). == user.options == As before, 'user.options' arrives immediately on every page view, embedded in the HTML. It has an async dependency on 'user.defaults', which is not downloaded until there is a known demand on 'user.options'. Once that arrives, the implementation closure of 'user.options' will execute, and the module becomes 'ready'. == user.options "empty" == Before this change, UserOptionsModule used isKnownEmpty to consider the module "empty" for logged-out users (as well as for logged-in users that haven't yet set any preferences). This was a mistake. It is invalid in ResourceLoader to mark a module as "empty" if that module has dependencies (see also T191596 and c3f200849). This broke the state machine. The impact was minimal given that it is unlikely for features to read keys from mw.user.options for logged-out users, which if attempted would have simply returned null for all keys. == New HTML == The user.options module is always embedded (never empty), and always has a dependency on user.defaults. == Cached HTML == The cached HTML for anons sets user.options's state to ready without waiting for any dependency. Per the above, this was already causing subtle bugs with mw.user.options.get() likely returning null for anons, which was fairly innocent. For tokens a bottom value of null would be problematic as the default for tokens must be "+\" instead. To make sure that is available for cached page views, set this directly in mediawiki.base.js. The cached HTML does contain an implement call for 'user.tokens' that contains the same defaults, but new code will not be asking for or waiting for user.tokens, so that is unused. Bug: T235457 Change-Id: I51e01d6fa604578cd2906337bde5a4760633c027
2020-03-13 22:46:14 +00:00
* Module for per-user private data that is transmitted on all HTML web responses.
*
* It is send to the browser from the HTML <head>. See OutputPage.
*
* @ingroup ResourceLoader
* @internal
*/
class ResourceLoaderUserOptionsModule extends ResourceLoaderModule {
protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
protected $targets = [ 'desktop', 'mobile' ];
/**
* @param ResourceLoaderContext|null $context
resourceloader: Merge 'user.tokens' module into 'user.options' For back-compat, keep 'user.tokens' as deprecated alias to 'user.options' for one release cycle (to be removed in MW 1.36). == user.options == As before, 'user.options' arrives immediately on every page view, embedded in the HTML. It has an async dependency on 'user.defaults', which is not downloaded until there is a known demand on 'user.options'. Once that arrives, the implementation closure of 'user.options' will execute, and the module becomes 'ready'. == user.options "empty" == Before this change, UserOptionsModule used isKnownEmpty to consider the module "empty" for logged-out users (as well as for logged-in users that haven't yet set any preferences). This was a mistake. It is invalid in ResourceLoader to mark a module as "empty" if that module has dependencies (see also T191596 and c3f200849). This broke the state machine. The impact was minimal given that it is unlikely for features to read keys from mw.user.options for logged-out users, which if attempted would have simply returned null for all keys. == New HTML == The user.options module is always embedded (never empty), and always has a dependency on user.defaults. == Cached HTML == The cached HTML for anons sets user.options's state to ready without waiting for any dependency. Per the above, this was already causing subtle bugs with mw.user.options.get() likely returning null for anons, which was fairly innocent. For tokens a bottom value of null would be problematic as the default for tokens must be "+\" instead. To make sure that is available for cached page views, set this directly in mediawiki.base.js. The cached HTML does contain an implement call for 'user.tokens' that contains the same defaults, but new code will not be asking for or waiting for user.tokens, so that is unused. Bug: T235457 Change-Id: I51e01d6fa604578cd2906337bde5a4760633c027
2020-03-13 22:46:14 +00:00
* @return string[] List of module names
*/
resourceloader: Add context param to ResourceLoaderModule::getDependencies By providing context as a parameter in getDependencies, we allow modules to dyanamically determine dependencies based on context. Note: To ease rollout, the parameter is optional in this patch. It is expected that it will be made non-optional in the near future. The use case is for CentralNotice campaigns to be able to add special modules ahead of deciding which banner to show a user. The dynamically chosen RL modules would replace ad-hoc JS currently sent with some banners. A list of possible campaigns and banners is already sent as a PHP- implemented RL module; that's the module that will dynamically choose other modules as dependencies when appropriate. This approach will save a round trip as compared to dynamically loading the modules client-side. For compatibility, extensions that override ResourceLoaderModule::getDependencies() should be updated with the new method signature. Here are changes for extensions currently deployed on Wikimedia wikis: * CentralNotice: I816bffa3815e2eab7e88cb04d1b345070e6aa15f * Gadgets: I0a10fb0cbf17d095ece493e744296caf13dcee02 * EventLogging: I67e957f74d6ca48cfb9a41fb5144bcc78f885e50 * PageTriage: Ica3ba32aa2fc76d11a44f391b6edfc871e7fbe0d * UniversalLanguageSelector: Ic63e617f51702c27104e123d4bed91983a726b7f * VisualEditor: I0ac775ca286e64825e31a9213b94648e41a5bc30 For more on the CentralNotice use case, please see I9f80edcbcacca2. Bug: T98924 Change-Id: Iee61e5b527321d01287baa03ad9b4d4f526ff3ef
2015-04-08 21:34:08 +00:00
public function getDependencies( ResourceLoaderContext $context = null ) {
return [ 'user.defaults' ];
}
/**
* @param ResourceLoaderContext $context
* @return string JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
resourceloader: Merge 'user.tokens' module into 'user.options' For back-compat, keep 'user.tokens' as deprecated alias to 'user.options' for one release cycle (to be removed in MW 1.36). == user.options == As before, 'user.options' arrives immediately on every page view, embedded in the HTML. It has an async dependency on 'user.defaults', which is not downloaded until there is a known demand on 'user.options'. Once that arrives, the implementation closure of 'user.options' will execute, and the module becomes 'ready'. == user.options "empty" == Before this change, UserOptionsModule used isKnownEmpty to consider the module "empty" for logged-out users (as well as for logged-in users that haven't yet set any preferences). This was a mistake. It is invalid in ResourceLoader to mark a module as "empty" if that module has dependencies (see also T191596 and c3f200849). This broke the state machine. The impact was minimal given that it is unlikely for features to read keys from mw.user.options for logged-out users, which if attempted would have simply returned null for all keys. == New HTML == The user.options module is always embedded (never empty), and always has a dependency on user.defaults. == Cached HTML == The cached HTML for anons sets user.options's state to ready without waiting for any dependency. Per the above, this was already causing subtle bugs with mw.user.options.get() likely returning null for anons, which was fairly innocent. For tokens a bottom value of null would be problematic as the default for tokens must be "+\" instead. To make sure that is available for cached page views, set this directly in mediawiki.base.js. The cached HTML does contain an implement call for 'user.tokens' that contains the same defaults, but new code will not be asking for or waiting for user.tokens, so that is unused. Bug: T235457 Change-Id: I51e01d6fa604578cd2906337bde5a4760633c027
2020-03-13 22:46:14 +00:00
$user = $context->getUserObj();
$tokens = [
'patrolToken' => $user->getEditToken( 'patrol' ),
'watchToken' => $user->getEditToken( 'watch' ),
'csrfToken' => $user->getEditToken(),
];
$script = 'mw.user.tokens.set(' . $context->encodeJson( $tokens ) . ');';
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
$options = $userOptionsLookup->getOptions( $user, UserOptionsLookup::EXCLUDE_DEFAULTS );
resourceloader: Merge 'user.tokens' module into 'user.options' For back-compat, keep 'user.tokens' as deprecated alias to 'user.options' for one release cycle (to be removed in MW 1.36). == user.options == As before, 'user.options' arrives immediately on every page view, embedded in the HTML. It has an async dependency on 'user.defaults', which is not downloaded until there is a known demand on 'user.options'. Once that arrives, the implementation closure of 'user.options' will execute, and the module becomes 'ready'. == user.options "empty" == Before this change, UserOptionsModule used isKnownEmpty to consider the module "empty" for logged-out users (as well as for logged-in users that haven't yet set any preferences). This was a mistake. It is invalid in ResourceLoader to mark a module as "empty" if that module has dependencies (see also T191596 and c3f200849). This broke the state machine. The impact was minimal given that it is unlikely for features to read keys from mw.user.options for logged-out users, which if attempted would have simply returned null for all keys. == New HTML == The user.options module is always embedded (never empty), and always has a dependency on user.defaults. == Cached HTML == The cached HTML for anons sets user.options's state to ready without waiting for any dependency. Per the above, this was already causing subtle bugs with mw.user.options.get() likely returning null for anons, which was fairly innocent. For tokens a bottom value of null would be problematic as the default for tokens must be "+\" instead. To make sure that is available for cached page views, set this directly in mediawiki.base.js. The cached HTML does contain an implement call for 'user.tokens' that contains the same defaults, but new code will not be asking for or waiting for user.tokens, so that is unused. Bug: T235457 Change-Id: I51e01d6fa604578cd2906337bde5a4760633c027
2020-03-13 22:46:14 +00:00
// Optimisation: Only output this function call if the user has non-default settings.
if ( $options ) {
$script .= 'mw.user.options.set(' . $context->encodeJson( $options ) . ');';
}
// Use FILTER_NOMIN annotation to prevent needless minification and caching (T84960).
resourceloader: Merge 'user.tokens' module into 'user.options' For back-compat, keep 'user.tokens' as deprecated alias to 'user.options' for one release cycle (to be removed in MW 1.36). == user.options == As before, 'user.options' arrives immediately on every page view, embedded in the HTML. It has an async dependency on 'user.defaults', which is not downloaded until there is a known demand on 'user.options'. Once that arrives, the implementation closure of 'user.options' will execute, and the module becomes 'ready'. == user.options "empty" == Before this change, UserOptionsModule used isKnownEmpty to consider the module "empty" for logged-out users (as well as for logged-in users that haven't yet set any preferences). This was a mistake. It is invalid in ResourceLoader to mark a module as "empty" if that module has dependencies (see also T191596 and c3f200849). This broke the state machine. The impact was minimal given that it is unlikely for features to read keys from mw.user.options for logged-out users, which if attempted would have simply returned null for all keys. == New HTML == The user.options module is always embedded (never empty), and always has a dependency on user.defaults. == Cached HTML == The cached HTML for anons sets user.options's state to ready without waiting for any dependency. Per the above, this was already causing subtle bugs with mw.user.options.get() likely returning null for anons, which was fairly innocent. For tokens a bottom value of null would be problematic as the default for tokens must be "+\" instead. To make sure that is available for cached page views, set this directly in mediawiki.base.js. The cached HTML does contain an implement call for 'user.tokens' that contains the same defaults, but new code will not be asking for or waiting for user.tokens, so that is unused. Bug: T235457 Change-Id: I51e01d6fa604578cd2906337bde5a4760633c027
2020-03-13 22:46:14 +00:00
return ResourceLoader::FILTER_NOMIN . $script;
}
/**
* @return bool
*/
public function supportsURLLoading() {
return false;
}
/**
* @return string
*/
public function getGroup() {
return 'private';
}
}