Extension:GlobalCssJs
| This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net. |
|
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 and version matrix | |||
Contents |
Usage [edit]
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: 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: 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.
Download instructions [edit]
Please copy 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.
Installation [edit]
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");
Configuration parameters [edit]
$wgGlobalCssJsUrl [edit]
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:If you do not set this, the extension will not run.
Code [edit]
GlobalCssJs.php [edit]
<?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&action=raw&ctype=text/css";/*]]>*/</style>' . "\n"); if($wgUseSiteJs) $out->addScript('<script type="' . $wgJsMimeType . '" src="' . $url . '?title=MediaWiki:Global.js&action=raw&ctype=' . $wgJsMimeType . 'dontcountme=s"></script>' . "\n"); if($wgAllowUserCss) $out->addScript('<style type="text/css">/*<![CDATA[*/ @import "' . $url . '?title=User:' . $name . '/global.css&action=raw&ctype=text/css"; /*]]>*/</style>' . "\n"); if($wgAllowUserJs) $out->addScript('<script type="' . $wgJsMimeType . '" src="' . $url . '?title=User:' . $name . '/global.js&action=raw&ctype=' . $wgJsMimeType . '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 ); }
GlobalCssJs.i18n.php [edit]
<?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', ), /* German Tim Weyer (SVG) */ 'de' => array( 'tog-enableglobalcssjs' => 'Erlaubt globale Benuter-CSS und -JS', ), /* Finnish (Jack Phoenix) */ 'fi' => array( 'tog-enableglobalcssjs' => 'Ota käyttöön käyttäjäkohtainen globaali CSS ja JS', ), /* Russian (Saint Johann) */ 'ru' => array( 'tog-enableglobalcssjs' => 'Включить глобальные личные CSS и JS', ); return $messages; }