allow selecting the preferred CDN and add event

We now have two CDNs available. code.jquery.com which is the more
popular one and CDNjs which is the faster one. Plugin authors can use a
plugin hook to easily implement their own preferred CDN. Authors might
even use this event to conditionally load additional JavaScript files.
This commit is contained in:
Andreas Gohr 2016-12-21 16:14:48 +01:00
parent 7a5ae06dac
commit fa07866393
4 changed files with 41 additions and 24 deletions

View File

@ -120,21 +120,48 @@ function getInterwiki() {
}
/**
* Returns the jquery script versions defined in lib/scripts/jquery/versions
* Returns the jquery script URLs for the versions defined in lib/scripts/jquery/versions
*
* @trigger CONFUTIL_CDN_SELECT
* @return array
*/
function getJqueryVersions() {
function getCdnUrls() {
global $conf;
// load version info
$versions = array();
$lines = file(DOKU_INC . 'lib/scripts/jquery/versions');
foreach($lines as $line ) {
foreach($lines as $line) {
$line = preg_replace('/#.*$/', '', $line);
list($key, $val) = explode('=', $line, 2);
$key = trim($key);
$val = trim($val);
$versions[$key] = $val;
}
return $versions;
$src = array();
$data = array(
'versions' => $versions,
'src' => &$src
);
$event = new Doku_Event('CONFUTIL_CDN_SELECT', $data);
if($event->advise_before()) {
if(!$conf['jquerycdn']) {
$jqmod = md5(join('-', $versions));
$src[] = DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod;
} elseif($conf['jquerycdn'] == 'jquery') {
$src[] = sprintf('https://code.jquery.com/jquery-%s.min.js', $versions['JQ_VERSION']);
$src[] = sprintf('https://code.jquery.com/jquery-migrate-%s.min.js', $versions['JQM_VERSION']);
$src[] = sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
} elseif($conf['jquerycdn'] == 'cdnjs') {
$src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery/%s/jquery.min.js', $versions['JQ_VERSION']);
$src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jquery-migrate/%s/jquery-migrate.min.js', $versions['JQM_VERSION']);
$src[] = sprintf('https://cdnjs.cloudflare.com/ajax/libs/jqueryui/%s/jquery-ui.min.js', $versions['JQUI_VERSION']);
}
}
$event->advise_after();
return $src;
}
/**

View File

@ -410,25 +410,10 @@ function tpl_metaheaders($alt = true) {
$head['script'][] = array('type'=> 'text/javascript', '_data'=> $script);
// load jquery
$jqver = getJqueryVersions();
if($conf['jquerycdn']) {
$jquery = getCdnUrls();
foreach($jquery as $src) {
$head['script'][] = array(
'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '',
'src' => sprintf('https://code.jquery.com/jquery-%s.min.js', $jqver['JQ_VERSION'])
);
$head['script'][] = array(
'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '',
'src' => sprintf('https://code.jquery.com/jquery-migrate-%s.min.js', $jqver['JQM_VERSION'])
);
$head['script'][] = array(
'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '',
'src' => sprintf('https://code.jquery.com/ui/%s/jquery-ui.min.js', $jqver['JQUI_VERSION'])
);
} else {
$jqmod = md5(join('-', $jqver));
$head['script'][] = array(
'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '',
'src' => DOKU_BASE . 'lib/exe/jquery.php' . '?tseed=' . $jqmod
'type' => 'text/javascript', 'charset' => 'utf-8', '_data' => '', 'src' => $src
);
}

View File

@ -175,7 +175,12 @@ $lang['renderer__plugin'] = '%s (plugin)';
/* Network Options */
$lang['dnslookups'] = 'DokuWiki will lookup hostnames for remote IP addresses of users editing pages. If you have a slow or non working DNS server or don\'t want this feature, disable this option';
$lang['jquerycdn'] = 'Should the jQuery and jQuery UI script files be loaded from the code.jquery.com CDN? This adds additional HTTP requests, but users may already have the files cached.';
$lang['jquerycdn'] = 'Should the jQuery and jQuery UI script files be loaded from a CDN? This adds additional HTTP requests, but files may load faster and users may have them cached already.';
/* jQuery CDN options */
$lang['jquerycdn_o_0'] = 'No CDN, local delivery only';
$lang['jquerycdn_o_jquery'] = 'CDN at code.jquery.com';
$lang['jquerycdn_o_cdnjs'] = 'CDN at cdnjs.com';
/* Proxy Options */
$lang['proxy____host'] = 'Proxy servername';

View File

@ -221,7 +221,7 @@ $meta['readdircache'] = array('numeric');
$meta['_network'] = array('fieldset');
$meta['dnslookups'] = array('onoff');
$meta['jquerycdn'] = array('onoff');
$meta['jquerycdn'] = array('multichoice', '_choices' => array(0,'jquery', 'cdnjs'));
$meta['proxy____host'] = array('string','_pattern' => '#^(|[a-z0-9\-\.+]+)$#i');
$meta['proxy____port'] = array('numericopt');
$meta['proxy____user'] = array('string');