Skip to content

Commit eb6f063

Browse files
committed
Fix PHP 7.3 deprecations in lib/ directory.
Original commit: dokuwiki/dokuwiki@5312cb0
1 parent 68f6057 commit eb6f063

File tree

1 file changed

+83
-45
lines changed

1 file changed

+83
-45
lines changed

lib/exe/js.php

Lines changed: 83 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
* @author Andreas Gohr <[email protected]>
77
*/
88

9-
if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
9+
use dokuwiki\Cache\Cache;
10+
use dokuwiki\Extension\Event;
11+
12+
if(!defined('DOKU_INC')) define('DOKU_INC', __DIR__ .'/../../');
1013
if(!defined('NOSESSION')) define('NOSESSION',true); // we do not use a session or authentication here (better caching)
1114
if(!defined('NL')) define('NL',"\n");
1215
if(!defined('DOKU_DISABLE_GZIP_OUTPUT')) define('DOKU_DISABLE_GZIP_OUTPUT',1); // we gzip ourself here
@@ -36,19 +39,9 @@ function js_out(){
3639
$tpl = trim(preg_replace('/[^\w-]+/','',$INPUT->str('t')));
3740
if(!$tpl) $tpl = $conf['template'];
3841

39-
// The generated script depends on some dynamic options
40-
$cache = new cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].DOKU_BASE.$tpl,'.js');
41-
$cache->_event = 'JS_CACHE_USE';
42-
43-
// load minified version for some files
44-
$min = $conf['compress'] ? '.min' : '';
45-
4642
// array of core files
4743
$files = array(
48-
DOKU_INC."lib/scripts/jquery/jquery$min.js",
4944
DOKU_INC.'lib/scripts/jquery/jquery.cookie.js',
50-
DOKU_INC."lib/scripts/jquery/jquery-ui$min.js",
51-
DOKU_INC."lib/scripts/jquery/jquery-migrate$min.js",
5245
DOKU_INC.'inc/lang/'.$conf['lang'].'/jquery.ui.datepicker.js',
5346
DOKU_INC."lib/scripts/fileuploader.js",
5447
DOKU_INC."lib/scripts/fileuploaderextended.js",
@@ -57,6 +50,7 @@ function js_out(){
5750
DOKU_INC.'lib/scripts/cookie.js',
5851
DOKU_INC.'lib/scripts/script.js',
5952
DOKU_INC.'lib/scripts/qsearch.js',
53+
DOKU_INC.'lib/scripts/search.js',
6054
DOKU_INC.'lib/scripts/tree.js',
6155
DOKU_INC.'lib/scripts/index.js',
6256
DOKU_INC.'lib/scripts/textselection.js',
@@ -75,12 +69,19 @@ function js_out(){
7569

7670
// add possible plugin scripts and userscript
7771
$files = array_merge($files,js_pluginscripts());
78-
if(!empty($config_cascade['userscript']['default'])) {
72+
if(is_array($config_cascade['userscript']['default'])) {
7973
foreach($config_cascade['userscript']['default'] as $userscript) {
8074
$files[] = $userscript;
8175
}
8276
}
8377

78+
// Let plugins decide to either put more scripts here or to remove some
79+
Event::createAndTrigger('JS_SCRIPT_LIST', $files);
80+
81+
// The generated script depends on some dynamic options
82+
$cache = new Cache('scripts'.$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'].md5(serialize($files)),'.js');
83+
$cache->setEvent('JS_CACHE_USE');
84+
8485
$cache_files = array_merge($files, getConfigFiles('main'));
8586
$cache_files[] = __FILE__;
8687

@@ -92,26 +93,29 @@ function js_out(){
9293
// start output buffering and build the script
9394
ob_start();
9495

95-
$json = new JSON();
9696
// add some global variables
9797
print "var DOKU_BASE = '".DOKU_BASE."';";
9898
print "var DOKU_TPL = '".tpl_basedir($tpl)."';";
99-
print "var DOKU_COOKIE_PARAM = " . $json->encode(
99+
print "var DOKU_COOKIE_PARAM = " . json_encode(
100100
array(
101101
'path' => empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'],
102102
'secure' => $conf['securecookie'] && is_ssl()
103103
)).";";
104104
// FIXME: Move those to JSINFO
105-
print "var DOKU_UHN = ".((int) useHeading('navigation')).";";
106-
print "var DOKU_UHC = ".((int) useHeading('content')).";";
105+
print "Object.defineProperty(window, 'DOKU_UHN', { get: function() {".
106+
"console.warn('Using DOKU_UHN is deprecated. Please use JSINFO.useHeadingNavigation instead');".
107+
"return JSINFO.useHeadingNavigation; } });";
108+
print "Object.defineProperty(window, 'DOKU_UHC', { get: function() {".
109+
"console.warn('Using DOKU_UHC is deprecated. Please use JSINFO.useHeadingContent instead');".
110+
"return JSINFO.useHeadingContent; } });";
107111

108112
// load JS specific translations
109113
$lang['js']['plugins'] = js_pluginstrings();
110114
$templatestrings = js_templatestrings($tpl);
111115
if(!empty($templatestrings)) {
112116
$lang['js']['template'] = $templatestrings;
113117
}
114-
echo 'LANG = '.$json->encode($lang['js']).";\n";
118+
echo 'LANG = '.json_encode($lang['js']).";\n";
115119

116120
// load toolbar
117121
toolbar_JSdefines('toolbar');
@@ -172,15 +176,15 @@ function js_load($file){
172176

173177
// is it a include_once?
174178
if($match[1]){
175-
$base = utf8_basename($ifile);
179+
$base = \dokuwiki\Utf8\PhpString::basename($ifile);
176180
if(array_key_exists($base, $loaded) && $loaded[$base] === true){
177181
$data = str_replace($match[0], '' ,$data);
178182
continue;
179183
}
180184
$loaded[$base] = true;
181185
}
182186

183-
if($ifile{0} != '/') $ifile = dirname($file).'/'.$ifile;
187+
if($ifile[0] != '/') $ifile = dirname($file).'/'.$ifile;
184188

185189
if(file_exists($ifile)){
186190
$idata = io_readFile($ifile);
@@ -219,18 +223,33 @@ function js_pluginscripts(){
219223
* @return array
220224
*/
221225
function js_pluginstrings() {
222-
global $conf;
226+
global $conf, $config_cascade;
223227
$pluginstrings = array();
224228
$plugins = plugin_list();
225-
foreach ($plugins as $p){
226-
if (isset($lang)) unset($lang);
227-
if (file_exists(DOKU_PLUGIN."$p/lang/en/lang.php")) {
228-
include DOKU_PLUGIN."$p/lang/en/lang.php";
229+
foreach($plugins as $p) {
230+
$path = DOKU_PLUGIN . $p . '/lang/';
231+
232+
if(isset($lang)) unset($lang);
233+
if(file_exists($path . "en/lang.php")) {
234+
include $path . "en/lang.php";
229235
}
230-
if (isset($conf['lang']) && $conf['lang']!='en' && file_exists(DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php")) {
231-
include DOKU_PLUGIN."$p/lang/".$conf['lang']."/lang.php";
236+
foreach($config_cascade['lang']['plugin'] as $config_file) {
237+
if(file_exists($config_file . $p . '/en/lang.php')) {
238+
include($config_file . $p . '/en/lang.php');
239+
}
240+
}
241+
if(isset($conf['lang']) && $conf['lang'] != 'en') {
242+
if(file_exists($path . $conf['lang'] . "/lang.php")) {
243+
include($path . $conf['lang'] . '/lang.php');
244+
}
245+
foreach($config_cascade['lang']['plugin'] as $config_file) {
246+
if(file_exists($config_file . $p . '/' . $conf['lang'] . '/lang.php')) {
247+
include($config_file . $p . '/' . $conf['lang'] . '/lang.php');
248+
}
249+
}
232250
}
233-
if (isset($lang['js'])) {
251+
252+
if(isset($lang['js'])) {
234253
$pluginstrings[$p] = $lang['js'];
235254
}
236255
}
@@ -247,15 +266,34 @@ function js_pluginstrings() {
247266
* @return array
248267
*/
249268
function js_templatestrings($tpl) {
250-
global $conf;
269+
global $conf, $config_cascade;
270+
271+
$path = tpl_incdir() . 'lang/';
272+
251273
$templatestrings = array();
252-
if (file_exists(tpl_incdir($tpl)."lang/en/lang.php")) {
253-
include tpl_incdir($tpl)."lang/en/lang.php";
274+
if(file_exists($path . "en/lang.php")) {
275+
include $path . "en/lang.php";
254276
}
255-
if (isset($conf['lang']) && $conf['lang']!='en' && file_exists(tpl_incdir($tpl)."lang/".$conf['lang']."/lang.php")) {
256-
include tpl_incdir($tpl)."lang/".$conf['lang']."/lang.php";
277+
foreach($config_cascade['lang']['template'] as $config_file) {
278+
if(file_exists($config_file . $conf['template'] . '/en/lang.php')) {
279+
include($config_file . $conf['template'] . '/en/lang.php');
280+
}
257281
}
258-
if (isset($lang['js'])) {
282+
if(isset($conf['lang']) && $conf['lang'] != 'en' && file_exists($path . $conf['lang'] . "/lang.php")) {
283+
include $path . $conf['lang'] . "/lang.php";
284+
}
285+
if(isset($conf['lang']) && $conf['lang'] != 'en') {
286+
if(file_exists($path . $conf['lang'] . "/lang.php")) {
287+
include $path . $conf['lang'] . "/lang.php";
288+
}
289+
foreach($config_cascade['lang']['template'] as $config_file) {
290+
if(file_exists($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php')) {
291+
include($config_file . $conf['template'] . '/' . $conf['lang'] . '/lang.php');
292+
}
293+
}
294+
}
295+
296+
if(isset($lang['js'])) {
259297
$templatestrings[$tpl] = $lang['js'];
260298
}
261299
return $templatestrings;
@@ -324,13 +362,13 @@ function js_compress($s){
324362
// reserved word (e.g. "for", "else", "if") or a
325363
// variable/object/method (e.g. "foo.color")
326364
while ($i < $slen && (strpos($chars,$s[$i]) === false) ){
327-
$result .= $s{$i};
365+
$result .= $s[$i];
328366
$i = $i + 1;
329367
}
330368

331-
$ch = $s{$i};
369+
$ch = $s[$i];
332370
// multiline comments (keeping IE conditionals)
333-
if($ch == '/' && $s{$i+1} == '*' && $s{$i+2} != '@'){
371+
if($ch == '/' && $s[$i+1] == '*' && $s[$i+2] != '@'){
334372
$endC = strpos($s,'*/',$i+2);
335373
if($endC === false) trigger_error('Found invalid /*..*/ comment', E_USER_ERROR);
336374

@@ -349,7 +387,7 @@ function js_compress($s){
349387
}
350388

351389
// singleline
352-
if($ch == '/' && $s{$i+1} == '/'){
390+
if($ch == '/' && $s[$i+1] == '/'){
353391
$endC = strpos($s,"\n",$i+2);
354392
if($endC === false) trigger_error('Invalid comment', E_USER_ERROR);
355393
$i = $endC;
@@ -360,15 +398,15 @@ function js_compress($s){
360398
if($ch == '/'){
361399
// rewind, skip white space
362400
$j = 1;
363-
while(in_array($s{$i-$j}, $whitespaces_chars)){
401+
while(in_array($s[$i-$j], $whitespaces_chars)){
364402
$j = $j + 1;
365403
}
366-
if( in_array($s{$i-$j}, $regex_starters) ){
404+
if( in_array($s[$i-$j], $regex_starters) ){
367405
// yes, this is an re
368406
// now move forward and find the end of it
369407
$j = 1;
370-
while($s{$i+$j} != '/'){
371-
if($s{$i+$j} == '\\') $j = $j + 2;
408+
while($s[$i+$j] != '/'){
409+
if($s[$i+$j] == '\\') $j = $j + 2;
372410
else $j++;
373411
}
374412
$result .= substr($s,$i,$j+1);
@@ -380,8 +418,8 @@ function js_compress($s){
380418
// double quote strings
381419
if($ch == '"'){
382420
$j = 1;
383-
while( $s{$i+$j} != '"' && ($i+$j < $slen)){
384-
if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == '"' || $s{$i+$j+1} == '\\') ){
421+
while( $s[$i+$j] != '"' && ($i+$j < $slen)){
422+
if( $s[$i+$j] == '\\' && ($s[$i+$j+1] == '"' || $s[$i+$j+1] == '\\') ){
385423
$j += 2;
386424
}else{
387425
$j += 1;
@@ -398,8 +436,8 @@ function js_compress($s){
398436
// single quote strings
399437
if($ch == "'"){
400438
$j = 1;
401-
while( $s{$i+$j} != "'" && ($i+$j < $slen)){
402-
if( $s{$i+$j} == '\\' && ($s{$i+$j+1} == "'" || $s{$i+$j+1} == '\\') ){
439+
while( $s[$i+$j] != "'" && ($i+$j < $slen)){
440+
if( $s[$i+$j] == '\\' && ($s[$i+$j+1] == "'" || $s[$i+$j+1] == '\\') ){
403441
$j += 2;
404442
}else{
405443
$j += 1;

0 commit comments

Comments
 (0)