From eb9cf4b00ce9098de6ff977e16fcb11a0b7724ef Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Wed, 16 Feb 2011 19:54:02 +0000 Subject: [PATCH] Create a user.groups module in ResourceLoader, which bundles a CSS and JS page for each usergroup the user is a member of (MediaWiki:Sysop.js, MediaWiki:Autoconfirmed.css, etc). Groups '*' and 'user' are not included. --- RELEASE-NOTES | 2 + includes/AutoLoader.php | 1 + includes/OutputPage.php | 19 +++--- includes/Skin.php | 5 +- .../ResourceLoaderUserGroupsModule.php | 61 +++++++++++++++++++ resources/Resources.php | 1 + 6 files changed, 79 insertions(+), 10 deletions(-) create mode 100644 includes/resourceloader/ResourceLoaderUserGroupsModule.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 1e5d4746fa..a33bffa398 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -72,6 +72,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN (maintenance/fixDoubleRedirects.php) * (bug 23315) New body classes to allow easier styling of special pages * (bug 27159) Make email confirmation code expiration time configurable +* CSS/JS for each user group is imported from MediaWiki:Sysop.js, + MediaWiki:Autoconfirmed.css, etc. === Bug fixes in 1.18 === * (bug 23119) WikiError class and subclasses are now marked as deprecated diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 198f279af6..9a420998c4 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -211,6 +211,7 @@ $wgAutoloadLocalClasses = array( 'ResourceLoaderFileModule' => 'includes/resourceloader/ResourceLoaderFileModule.php', 'ResourceLoaderSiteModule' => 'includes/resourceloader/ResourceLoaderSiteModule.php', 'ResourceLoaderUserModule' => 'includes/resourceloader/ResourceLoaderUserModule.php', + 'ResourceLoaderUserGroupsModule' => 'includes/resourceloader/ResourceLoaderUserGroupsModule.php', 'ResourceLoaderUserOptionsModule' => 'includes/resourceloader/ResourceLoaderUserOptionsModule.php', 'ResourceLoaderStartUpModule' => 'includes/resourceloader/ResourceLoaderStartUpModule.php', 'ReverseChronologicalPager' => 'includes/Pager.php', diff --git a/includes/OutputPage.php b/includes/OutputPage.php index ca2acbf88c..f03e0e96d0 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2555,28 +2555,29 @@ class OutputPage { // Legacy Scripts $scripts .= "\n" . $this->mScripts; + $userScripts = array( 'user.options' ); + // Add site JS if enabled if ( $wgUseSiteJs ) { $scripts .= $this->makeResourceLoaderLink( $sk, 'site', ResourceLoaderModule::TYPE_SCRIPTS ); + if( $wgUser->isLoggedIn() ){ + $userScripts[] = 'user.groups'; + } } - // Add user JS if enabled - trying to load user.options as a bundle if possible - $userOptionsAdded = false; + // Add user JS if enabled if ( $wgAllowUserJs && $wgUser->isLoggedIn() ) { $action = $wgRequest->getVal( 'action', 'view' ); if( $this->mTitle && $this->mTitle->isJsSubpage() && $sk->userCanPreview( $action ) ) { # XXX: additional security check/prompt? $scripts .= Html::inlineScript( "\n" . $wgRequest->getText( 'wpTextbox1' ) . "\n" ) . "\n"; } else { - $scripts .= $this->makeResourceLoaderLink( - $sk, array( 'user', 'user.options' ), ResourceLoaderModule::TYPE_SCRIPTS - ); - $userOptionsAdded = true; + # FIXME: this means that User:Me/Common.js doesn't load when previewing + # User:Me/Vector.js, and vice versa (bug26283) + $userScripts[] = 'user'; } } - if ( !$userOptionsAdded ) { - $scripts .= $this->makeResourceLoaderLink( $sk, 'user.options', ResourceLoaderModule::TYPE_SCRIPTS ); - } + $scripts .= $this->makeResourceLoaderLink( $sk, $userScripts, ResourceLoaderModule::TYPE_SCRIPTS ); return $scripts; } diff --git a/includes/Skin.php b/includes/Skin.php index 489bda8be5..57e535f685 100644 --- a/includes/Skin.php +++ b/includes/Skin.php @@ -546,7 +546,7 @@ abstract class Skin extends Linker { * @private */ function setupUserCss( OutputPage $out ) { - global $wgRequest; + global $wgRequest, $wgUser; global $wgUseSiteCss, $wgAllowUserCss, $wgAllowUserCssPrefs; wfProfileIn( __METHOD__ ); @@ -560,6 +560,9 @@ abstract class Skin extends Linker { // Per-site custom styles if ( $wgUseSiteCss ) { $out->addModuleStyles( 'site' ); + if( $wgUser->isLoggedIn() ){ + $out->addModuleStyles( 'user.groups' ); + } } // Per-user custom styles diff --git a/includes/resourceloader/ResourceLoaderUserGroupsModule.php b/includes/resourceloader/ResourceLoaderUserGroupsModule.php new file mode 100644 index 0000000000..c81a999b2b --- /dev/null +++ b/includes/resourceloader/ResourceLoaderUserGroupsModule.php @@ -0,0 +1,61 @@ +getUser() ) { + $user = User::newFromName( $context->getUser() ); + if( $user instanceof User ){ + $pages = array(); + foreach( $user->getEffectiveGroups() as $group ){ + if( in_array( $group, array( '*', 'user' ) ) ){ + continue; + } + $g = ucfirst( $group ); + $pages["MediaWiki:$g.js"] = array( 'type' => 'script' ); + $pages["MediaWiki:$g.css"] = array( 'type' => 'style' ); + } + return $pages; + } + } + return array(); + } + + /* Methods */ + + public function getGroup() { + return 'user'; + } + + public function getFlip( $context ) { + global $wgContLang; + + return $wgContLang->getDir() !== $context->getDirection(); + } +} diff --git a/resources/Resources.php b/resources/Resources.php index 553c8ae08e..085462cbd3 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -8,6 +8,7 @@ return array( 'startup' => array( 'class' => 'ResourceLoaderStartUpModule' ), 'user' => array( 'class' => 'ResourceLoaderUserModule' ), 'user.options' => array( 'class' => 'ResourceLoaderUserOptionsModule' ), + 'user.groups' => array( 'class' => 'ResourceLoaderUserGroupsModule' ), /* Skins */ -- 2.20.1