-
Notifications
You must be signed in to change notification settings - Fork 1
/
HeaderScriptInjector.php
79 lines (70 loc) · 1.93 KB
/
HeaderScriptInjector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php /* created by Evgeny Fadeev [email protected] https://www.mediawiki.org/wiki/Extension:HeaderScriptInjector */
if (!defined('MEDIAWIKI')){
die();
}
class HeaderScriptInjector {
var $libs = array();
var $scripts = array();
var $styles = array();
var $google_api_key;
var $cdn_list = array('google');
var $google_api_loaded = false;
function loadLib($lib,$cdn,$ver){
if (in_array($lib,$this->libs)){
return;
}
else {
$this->libs[] = $lib;
if ($cdn == 'google'){
$this->loadGoogleApi();
$this->addJSContent('google.load("'.$lib. '","' . $ver . '");');
}
else {
throw new MWException("unsupported content distribution network (CDN): '$cdn'."
." Valid CDN list: " . implode(", ",$this->cdn_list) . ".");
}
}
}
function loadGoogleApi(){
global $wgGoogleApiKey,$wgOut;
if ($this->google_api_loaded == true){
return;
}
if ($wgGoogleApiKey == ''){
throw new MWException("please get Google API Key and set it with "
."\$wgGoogleApiKey variable in LocalSettings.php file");
}
$wgOut->addScript('<script src="http://www.google.com/jsapi?key=' .
$wgGoogleApiKey. '" type="text/javascript"></script>'."\n");
$this->google_api_loaded = true;
}
function addJSContent($content){
global $wgOut;
$wgOut->addScript('<script type="text/javascript">' . $content . '</script>'. "\n");
}
function addScript($url){
global $wgOut;
if (in_array($url,$this->scripts)){
return;
}
else {
$this->scripts[] = $url;
$wgOut->addScript('<script type="text/javascript" src="' . $url . '"></script>'."\n");
}
}
function addCSS($url){
global $wgOut;
if (in_array($url,$this->styles)){
return;
}
else {
$this->styles[] = $url;
$wgOut->addScript('<link rel="stylesheet" type="text/css" href="'.$url.'"></link>'."\n");
}
}
}
global $wgHeader;
$wgHeader = new HeaderScriptInjector();
global $wgGoogleApiKey;
$wgGoogleApiKey = '';
?>