c4e6bad947baed7debbba612b719cf541b192f8d
[lhc/web/wiklou.git] / js2 / mwEmbed / php / maintenance / mergeJavascriptMsg.php
1 <?
2 //merges in javascript with mwEmbed.i18n.php
3
4 # Abort if called from a web server
5 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
6 print "This script must be run from the command line\n";
7 exit();
8 }
9 define('MEDIAWIKI',true);
10 //get the scriptLoader globals:
11 require_once('../../jsScriptLoader.php');
12
13 $mwSTART_MSG_KEY = '#<JAVASCRIPT EN REPLACE>';
14 $mwEND_MSG_KEY = '#</JAVASCRIPT EN REPLACE>';
15 $mwLangFilePath = '../languages/mwEmbed.i18n.php';
16 //get options (like override JS or override php
17
18
19 //read in mwEmbed.i18n.php
20 $rawLangFile = file_get_contents($mwLangFilePath);
21
22 $startInx = strpos($rawLangFile, $mwSTART_MSG_KEY) + strlen($mwSTART_MSG_KEY);
23 $endInx = strpos($rawLangFile, $mwEND_MSG_KEY);
24 if( $startInx === false || $endInx === false ){
25 print "Could not find $mwSTART_MSG_KEY or $mwEND_MSG_KEY in mwEmbed.i18n.php \n";
26 exit();
27 }
28
29 $preFile = substr( $rawLangFile, 0, $startInx);
30 $msgSet = substr( $rawLangFile, $startInx, $endInx-$startInx);
31 $postFile = substr($rawLangFile, $endInx);
32
33 //build replacement from all javascript in mwEmbed
34
35 $path = realpath('../../');
36
37 $curFileName = '';
38 //@@todo existing msgSet should be parsed (or we just "include" the file first)
39 $msgSet = '$messages[\'en\'] = array(';
40
41 $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
42 foreach($objects as $fname => $object){
43 if(substr($fname, -3) == '.js'){
44 $jsFileText = file_get_contents( $fname );
45 $mwPos = strpos($fname, 'mwEmbed') + 7;
46 $curFileName = substr( $fname, $mwPos );
47 if( preg_match('/loadGM\s*\(\s*{(.*)}\s*\)\s*/siU', //@@todo fix: will break down if someone does }) in their msg text
48 $jsFileText,
49 $matches) ){
50 $msgSet .= doJsonMerge($matches[1]);
51 }
52 }
53 }
54 //close up the msgSet:
55 $msgSet.=");\n";
56
57 function doJsonMerge($json_txt){
58 global $curFileName;
59
60 $out = "\n\t/*
61 \t* js file: {$curFileName}
62 \t*/\n";
63 $jmsg = json_decode( '{' . $json_txt . '}', true );
64 if( count($jmsg) != 0 ){
65 foreach($jmsg as $k=>$v){
66 $out.="\t'{$k}' => '".str_replace('\'', '\\\'', $v) ."',\n";
67 }
68 return $out;
69 }else{
70 print "could not get any json vars\n";
71 return '';
72 }
73 }
74
75 //rebuild and output to file
76 if( file_put_contents($mwLangFilePath, trim($preFile) . "\n" . trim($msgSet) . "\n" . trim($postFile))){
77 print "updated $mwLangFilePath file\n";
78 exit();
79 }
80
81 ?>