ResourceLoader: Honor $wgAllow* settings in site/user modules

Bug: 46858
Change-Id: I17b3a2c1df5d52458f92c715242003b34111432c
This commit is contained in:
Timo Tijhof 2013-04-03 22:50:08 +02:00
parent efc1344fea
commit 12a9f0564d
4 changed files with 72 additions and 55 deletions

View file

@ -37,15 +37,19 @@ class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
* @return Array: List of pages
*/
protected function getPages( ResourceLoaderContext $context ) {
global $wgHandheldStyle;
global $wgUseSiteJs, $wgUseSiteCss, $wgHandheldStyle;
$pages = array(
'MediaWiki:Common.js' => array( 'type' => 'script' ),
'MediaWiki:Common.css' => array( 'type' => 'style' ),
'MediaWiki:' . ucfirst( $context->getSkin() ) . '.js' => array( 'type' => 'script' ),
'MediaWiki:' . ucfirst( $context->getSkin() ) . '.css' => array( 'type' => 'style' ),
'MediaWiki:Print.css' => array( 'type' => 'style', 'media' => 'print' ),
);
$pages = array();
if ( $wgUseSiteJs ) {
$pages['MediaWiki:Common.js'] = array( 'type' => 'script' );
$pages['MediaWiki:' . ucfirst( $context->getSkin() ) . '.js'] = array( 'type' => 'script' );
}
if ( $wgUseSiteCss ) {
$pages['MediaWiki:Common.css'] = array( 'type' => 'style' );
$pages['MediaWiki:' . ucfirst( $context->getSkin() ) . '.css'] = array( 'type' => 'style' );
}
$pages['MediaWiki:Print.css'] = array( 'type' => 'style', 'media' => 'print' );
if ( $wgHandheldStyle ) {
$pages['MediaWiki:Handheld.css'] = array(
'type' => 'style',

View file

@ -56,43 +56,44 @@ class ResourceLoaderUserCSSPrefsModule extends ResourceLoaderModule {
public function getStyles( ResourceLoaderContext $context ) {
global $wgAllowUserCssPrefs, $wgUser;
if ( $wgAllowUserCssPrefs ) {
$options = $wgUser->getOptions();
// Build CSS rules
$rules = array();
// Underline: 2 = browser default, 1 = always, 0 = never
if ( $options['underline'] < 2 ) {
$rules[] = "a { text-decoration: " .
( $options['underline'] ? 'underline' : 'none' ) . "; }";
} else {
# The scripts of these languages are very hard to read with underlines
$rules[] = 'a:lang(ar), a:lang(ckb), a:lang(fa),a:lang(kk-arab), ' .
'a:lang(mzn), a:lang(ps), a:lang(ur) { text-decoration: none; }';
}
if ( $options['justify'] ) {
$rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
}
if ( !$options['showtoc'] ) {
$rules[] = "#toc { display: none; }\n";
}
if ( !$options['editsection'] ) {
$rules[] = ".editsection { display: none; }\n";
}
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 array( 'all' => $style );
if ( !$wgAllowUserCssPrefs ) {
return array();
}
return array();
$options = $wgUser->getOptions();
// Build CSS rules
$rules = array();
// Underline: 2 = browser default, 1 = always, 0 = never
if ( $options['underline'] < 2 ) {
$rules[] = "a { text-decoration: " .
( $options['underline'] ? 'underline' : 'none' ) . "; }";
} else {
# The scripts of these languages are very hard to read with underlines
$rules[] = 'a:lang(ar), a:lang(ckb), a:lang(fa),a:lang(kk-arab), ' .
'a:lang(mzn), a:lang(ps), a:lang(ur) { text-decoration: none; }';
}
if ( $options['justify'] ) {
$rules[] = "#article, #bodyContent, #mw_content { text-align: justify; }\n";
}
if ( !$options['showtoc'] ) {
$rules[] = "#toc { display: none; }\n";
}
if ( !$options['editsection'] ) {
$rules[] = ".editsection { display: none; }\n";
}
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 array( 'all' => $style );
}
/**

View file

@ -33,12 +33,15 @@ class ResourceLoaderUserGroupsModule extends ResourceLoaderWikiModule {
* @return array
*/
protected function getPages( ResourceLoaderContext $context ) {
global $wgUser;
global $wgUser, $wgUseSiteJs, $wgUseSiteCss;
$userName = $context->getUser();
if ( $userName === null ) {
return array();
}
if ( !$wgUseSiteJs && !$wgUseSiteCss ) {
return array();
}
// Use $wgUser is possible; allows to skip a lot of code
if ( is_object( $wgUser ) && $wgUser->getName() == $userName ) {
@ -55,8 +58,12 @@ class ResourceLoaderUserGroupsModule extends ResourceLoaderWikiModule {
if ( in_array( $group, array( '*', 'user' ) ) ) {
continue;
}
$pages["MediaWiki:Group-$group.js"] = array( 'type' => 'script' );
$pages["MediaWiki:Group-$group.css"] = array( 'type' => 'style' );
if ( $wgUseSiteJs ) {
$pages["MediaWiki:Group-$group.js"] = array( 'type' => 'script' );
}
if ( $wgUseSiteCss ) {
$pages["MediaWiki:Group-$group.css"] = array( 'type' => 'style' );
}
}
return $pages;
}

View file

@ -35,11 +35,15 @@ class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
* @return array
*/
protected function getPages( ResourceLoaderContext $context ) {
global $wgAllowUserJs, $wgAllowUserCss;
$username = $context->getUser();
if ( $username === null ) {
return array();
}
if ( !$wgAllowUserJs && !$wgAllowUserCss ) {
return array();
}
// Get the normalized title of the user's user page
$userpageTitle = Title::makeTitleSafe( NS_USER, $username );
@ -50,14 +54,15 @@ class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
$userpage = $userpageTitle->getPrefixedDBkey(); // Needed so $excludepages works
$pages = array(
"$userpage/common.js" => array( 'type' => 'script' ),
"$userpage/" . $context->getSkin() . '.js' =>
array( 'type' => 'script' ),
"$userpage/common.css" => array( 'type' => 'style' ),
"$userpage/" . $context->getSkin() . '.css' =>
array( 'type' => 'style' ),
);
$pages = array();
if ( $wgAllowUserJs ) {
$pages["$userpage/common.js"] = array( 'type' => 'script' );
$pages["$userpage/" . $context->getSkin() . '.js'] = array( 'type' => 'script' );
}
if ( $wgAllowUserCss ) {
$pages["$userpage/common.css"] = array( 'type' => 'style' );
$pages["$userpage/" . $context->getSkin() . '.css'] = array( 'type' => 'style' );
}
// Hack for bug 26283: if we're on a preview page for a CSS/JS page,
// we need to exclude that page from this module. In that case, the excludepage