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

87 lines
2.4 KiB
PHP
Raw Normal View History

<?php
/**
* ResourceLoader module for user preference customizations.
*
* 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
*/
/**
* Module for user preference customizations
*/
class ResourceLoaderUserCSSPrefsModule extends ResourceLoaderModule {
protected $origin = self::ORIGIN_CORE_INDIVIDUAL;
/**
resourceloader: Enable module content version for data modules This greatly simplifies logic required to compute module versions. It also makes it significantly less error-prone. Since f37cee996e, we support hashes as versions (instead of timestamps). This means we can build a hash of the content directly, instead of compiling a large array with all values that may influence the module content somehow. Benefits: * Remove all methods and logic related to querying database and disk for timestamps, revision numbers, definition summaries, cache epochs, and more. * No longer needlessly invalidate cache as a result of no-op changes to implementation datails. Due to inclusion of absolute file paths in the definition summary, cache was always invalidated when moving wikis to newer MediaWiki branches; even if the module observed no actual changes. * When changes are reverted within a certain period of time, old caches can now be re-used. The module would produce the same version hash as before. Previously when a change was deployed and then reverted, all web clients (even those that never saw the bad version) would have re-fetch modules because the version increased. Updated unit tests to account for the change in version. New default version of empty test modules is: "mvgTPvXh". For the record, this comes from the base64 encoding of the SHA1 digest of the JSON serialised form of the module content: > $str = '{"scripts":"","styles":{"css":[]},"messagesBlob":"{}"}'; > echo base64_encode(sha1($str, true)); > FEb3+VuiUm/fOMfod1bjw/te+AQ= Enabled content versioning for the data modules in MediaWiki core: * EditToolbarModule * JqueryMsgModule * LanguageDataModule * LanguageNamesModule * SpecialCharacterDataModule * UserCSSPrefsModule * UserDefaultsModule * UserOptionsModule The FileModule and base class explicitly disable it for now and keep their current behaviour of using the definition summary. We may remove it later, but that requires more performance testing first. Explicitly disable it in the WikiModule class to avoid breakage when the default changes. Ref T98087. Change-Id: I782df43c50dfcfb7d7592f744e13a3a0430b0dc6
2015-06-02 17:27:23 +00:00
* @return bool
*/
resourceloader: Enable module content version for data modules This greatly simplifies logic required to compute module versions. It also makes it significantly less error-prone. Since f37cee996e, we support hashes as versions (instead of timestamps). This means we can build a hash of the content directly, instead of compiling a large array with all values that may influence the module content somehow. Benefits: * Remove all methods and logic related to querying database and disk for timestamps, revision numbers, definition summaries, cache epochs, and more. * No longer needlessly invalidate cache as a result of no-op changes to implementation datails. Due to inclusion of absolute file paths in the definition summary, cache was always invalidated when moving wikis to newer MediaWiki branches; even if the module observed no actual changes. * When changes are reverted within a certain period of time, old caches can now be re-used. The module would produce the same version hash as before. Previously when a change was deployed and then reverted, all web clients (even those that never saw the bad version) would have re-fetch modules because the version increased. Updated unit tests to account for the change in version. New default version of empty test modules is: "mvgTPvXh". For the record, this comes from the base64 encoding of the SHA1 digest of the JSON serialised form of the module content: > $str = '{"scripts":"","styles":{"css":[]},"messagesBlob":"{}"}'; > echo base64_encode(sha1($str, true)); > FEb3+VuiUm/fOMfod1bjw/te+AQ= Enabled content versioning for the data modules in MediaWiki core: * EditToolbarModule * JqueryMsgModule * LanguageDataModule * LanguageNamesModule * SpecialCharacterDataModule * UserCSSPrefsModule * UserDefaultsModule * UserOptionsModule The FileModule and base class explicitly disable it for now and keep their current behaviour of using the definition summary. We may remove it later, but that requires more performance testing first. Explicitly disable it in the WikiModule class to avoid breakage when the default changes. Ref T98087. Change-Id: I782df43c50dfcfb7d7592f744e13a3a0430b0dc6
2015-06-02 17:27:23 +00:00
public function enableModuleContentVersion() {
return true;
}
/**
* @param ResourceLoaderContext $context
* @return array
*/
public function getStyles( ResourceLoaderContext $context ) {
if ( !$this->getConfig()->get( 'AllowUserCssPrefs' ) ) {
return [];
}
$options = $context->getUserObj()->getOptions();
// Build CSS rules
$rules = [];
UserCSSPrefsModule: Remove redundant inline text-decoration:none css rules By default user option 'underline' is '2' which means no skin override. Once a upon a time, none of the popular MediaWiki skins specified whether links should have underlines, and most browsers default to having an underline on links (when not hovering). As such, there was an exception added for languages where underlines cause text to be unreadable. Nowadays however all modern skins explicitly specify the intended design for links. Neither the default skin (Vector) nor other popular skins I checked have underlines by default. As such, this rule is redundant. If a skin comes along that does specify underlines by default, then these rules should be placed in that skins' stylesheet. Not in the HTML of all pages. Incidentally, this was the last code branch in ResourceLoaderUserCSSPrefsModule that is true by default. Which means from now on this <style> tag is automatically omitted from the HTML for logged-out users and users that haven't changed the 'underline' or 'editfont' preference. Implement the missing isKnownEmpty() method so that OutputPage does the automatic omission. To test the new text-decoration rule in mediawiki.skinning: * Change user preference language to "ar" and observe anchor links in the interface not having underlines on-hover. * Change $wgLanguageCode to "ar" and observe anchor links in the content not having underlines on-hover. History: * 2011 (043f98a3; r101445) Move overrides from shared.css to ResourceLoaderUserOptionsModule Also remove "!important" from underline css * 2011 (33293c9b; r96261) Apply "!important" to underline user option css * 2011 (81446abc; r91432; T31712) Move overrides from MessageXX.php to skins/common/shared.css * 2006 (43b2fb56; r15823) Move overrides from LanguageXX.php to MessageX.php * 2004 (f9f3e915) Add override to LanguageAr.php Bug: T105313 Change-Id: I94dda30fb80b30e9c12a74a8b09065da55681929
2015-07-08 18:56:31 +00:00
// Underline: 2 = skin default, 1 = always, 0 = never
if ( $options['underline'] < 2 ) {
$rules[] = "a { text-decoration: " .
( $options['underline'] ? 'underline' : 'none' ) . "; }";
}
if ( $options['editfont'] !== 'default' ) {
// Double-check that $options['editfont'] consists of safe characters only
if ( preg_match( '/^[a-zA-Z0-9_, -]+$/', $options['editfont'] ) ) {
$rules[] = "textarea { font-family: {$options['editfont']}; }\n";
}
}
$style = implode( "\n", $rules );
if ( $this->getFlip( $context ) ) {
$style = CSSJanus::transform( $style, true, false );
}
return [ 'all' => $style ];
}
UserCSSPrefsModule: Remove redundant inline text-decoration:none css rules By default user option 'underline' is '2' which means no skin override. Once a upon a time, none of the popular MediaWiki skins specified whether links should have underlines, and most browsers default to having an underline on links (when not hovering). As such, there was an exception added for languages where underlines cause text to be unreadable. Nowadays however all modern skins explicitly specify the intended design for links. Neither the default skin (Vector) nor other popular skins I checked have underlines by default. As such, this rule is redundant. If a skin comes along that does specify underlines by default, then these rules should be placed in that skins' stylesheet. Not in the HTML of all pages. Incidentally, this was the last code branch in ResourceLoaderUserCSSPrefsModule that is true by default. Which means from now on this <style> tag is automatically omitted from the HTML for logged-out users and users that haven't changed the 'underline' or 'editfont' preference. Implement the missing isKnownEmpty() method so that OutputPage does the automatic omission. To test the new text-decoration rule in mediawiki.skinning: * Change user preference language to "ar" and observe anchor links in the interface not having underlines on-hover. * Change $wgLanguageCode to "ar" and observe anchor links in the content not having underlines on-hover. History: * 2011 (043f98a3; r101445) Move overrides from shared.css to ResourceLoaderUserOptionsModule Also remove "!important" from underline css * 2011 (33293c9b; r96261) Apply "!important" to underline user option css * 2011 (81446abc; r91432; T31712) Move overrides from MessageXX.php to skins/common/shared.css * 2006 (43b2fb56; r15823) Move overrides from LanguageXX.php to MessageX.php * 2004 (f9f3e915) Add override to LanguageAr.php Bug: T105313 Change-Id: I94dda30fb80b30e9c12a74a8b09065da55681929
2015-07-08 18:56:31 +00:00
/**
* @param ResourceLoaderContext $context
* @return bool
*/
public function isKnownEmpty( ResourceLoaderContext $context ) {
$styles = $this->getStyles( $context );
return isset( $styles['all'] ) && $styles['all'] === '';
}
/**
* @return string
*/
public function getGroup() {
return 'private';
}
}