Extension:GlobalCssJs

From MediaWiki.org

Jump to: navigation, search

               

Manual on MediaWiki Extensions
List of MediaWiki Extensions
Crystal Clear action run.png
Global CSS/JS

Release status: stable

Implementation  Skin, MyWiki
Description Allows global CSS and JS on a "central" wiki to be loaded for all wikis in the farm
Author(s)  Ryan Schmidt (SkizzerzTalk)
Last Version  2.0
MediaWiki  1.8+
License None (Public Domain)
Download Code
Usage
Example  Jack Phoenix.com

check usage (experimental)

Contents

[edit] Usage

This extension allows loading of global css and js from a central wiki (good for use on wiki farms). Both overall CSS/JS can be added (via MediaWiki:Global.css and MediaWiki:Global.js on the central wiki), as well as user CSS/JS. In order for the global site CSS/JS to work, the local site CSS/JS must be enabled for each respective function to work. In order for the global user CSS/JS to work, the user must have the same username on the local wiki and the central wiki, they must have enabled the "Enable Global User CSS/JS" in their Special:Preferences, and the user CSS and/or JS must be enabled on the local wiki for each respective function to work as well.

Warning Warning: You can set the preference for user css/js to be automatically enabled, but it is not recommended to do so on wikis without a shared user database, as it opens up a potential XSS vector, which allows other users to hijack the user's account, among other things.

Note Note: Currently, the global CSS and JS is loaded after the local CSS and JS, so it overrides all the other styles. This should hopefully be fixed sometime soon.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extension/GlobalCssJs/GlobalCssJs.php and $IP/extension/GlobalCssJs/GlobalCssJs.i18n.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php (obviously replace "path_to_wiki" with the path to your wiki's index.php file, such as http://www.mediawiki.org/w/index.php

$wgGlobalCssJsUrl = 'http://path_to_wiki/index.php';
require_once("$IP/extensions/GlobalCssJs/GlobalCssJs.php");

[edit] Configuration parameters

[edit] $wgGlobalCssJsUrl

Default value: null

This is the URL (including the http://) to the wiki where you are loading the global css/js from. The url must be the path to that wiki's index.php.

Note Note: If you do not set this, the extension will not run.

[edit] Code

[edit] GlobalCssJs.php

<?php
/**
* Global CSS/JS loader by Ryan Schmidt
* Put global site CSS/JS in the "central" wiki's MediaWiki:Global.css and MediaWiki:Global.js respectively
* Put global user CSS/JS in the "central" wiki's User:Yourname/global.css and User:Yourname/global.js respectively
* See http://www.mediawiki.org/wiki/Extension:GlobalCssJs
*/
 
if(!defined('MEDIAWIKI')) {
    echo("This is an extension to the MediaWiki software and cannot be used standalone");
    die(1);
}
 
$wgExtensionCredits['other'][] = array(
'name' => 'Global CSS/JS',
'author' => 'Ryan Schmidt',
'version' => '2.0.1',
'url' => 'http://www.mediawiki.org/wiki/Extension:GlobalCssJs',
'description' => 'Allows global CSS and JS on a "central" wiki to be loaded for all wikis in the farm',
);
 
$wgExtensionFunctions[] = 'efGlobalCssJs';
 
$wgHooks['UserToggles'][] = 'wfGlobalCssJsAddPrefToggle';
$wgHooks['BeforePageDisplay'][] = 'wfGlobalCssJs';
 
function wfGlobalCssJsAddPrefToggle(&$extraToggles) {
    $extraToggles[] = 'enableglobalcssjs';
    return true;
}
 
function wfGlobalCssJs(&$out) {
    global $wgGlobalCssJsUrl, $wgUser, $wgAllowUserCss, $wgAllowUserJs, $wgJsMimeType, $wgUseSiteCss, $wgUseSiteJs;
    if( !isset($wgGlobalCssJsUrl) || !$wgUser->isLoggedIn() )
            return true;
    $name = urlencode($wgUser->getName());
    $url = $wgGlobalCssJsUrl; // just makes the lines shorter, nothing more.
    $toggle = $wgUser->getBoolOption('enableglobalcssjs');
    if($wgUseSiteCss)
        $out->addScript('<style type="text/css">/*<![CDATA[*/ @import "' . $url . '?title=MediaWiki:Global.css&amp;action=raw&amp;ctype=text/css";/*]]>*/</style>' . "\n");
    if($wgUseSiteJs)
        $out->addScript('<script type="' . $wgJsMimeType . '" src="' . $url . '?title=MediaWiki:Global.js&amp;action=raw&amp;ctype=' . $wgJsMimeType . '&amp;dontcountme=s"></script>' . "\n");
    if($wgAllowUserCss && $toggle)
        $out->addScript('<style type="text/css">/*<![CDATA[*/ @import "' . $url . '?title=User:' . $name . '/global.css&amp;action=raw&amp;ctype=text/css"; /*]]>*/</style>' . "\n");
    if($wgAllowUserJs && $toggle)
        $out->addScript('<script type="' . $wgJsMimeType . '" src="' . $url . '?title=User:' . $name . '/global.js&amp;action=raw&amp;ctype=' . $wgJsMimeType . '&amp;dontcountme=s"></script>' . "\n");
    return true;
}
 
function efGlobalCssJs() {
    global $wgMessageCache;
    require_once( dirname( __FILE__ ) . '/GlobalCssJs.i18n.php' );
    foreach( efGlobalCssJsMessages() as $lang => $messages )
        $wgMessageCache->addMessages( $messages, $lang );
}

[edit] GlobalCssJs.i18n.php

<?php
 
/**
* Internationalisation file for the Global CSS/JS extension
* If you are good at translating messages, please contact me :)
*/
 
function efGlobalCssJsMessages() {
	$messages = array(
	/* English (Ryan Schmidt) */
		'en' => array(
			'tog-enableglobalcssjs' => 'Enable Global User CSS and JS',
		),
	/* Finnish (Jack Phoenix) */
                'fi' => array(
                        'tog-enableglobalcssjs' => 'Ota käyttöön käyttäjäkohtainen globaali CSS ja JS',
                ),
	);
 
	return $messages;
}