1
0

merge_lang.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCoreScripts Scripts
  7. * @{
  8. */
  9. $aPathInfo = pathinfo(__FILE__);
  10. require_once ($aPathInfo['dirname'] . '/../inc/header.inc.php');
  11. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  12. bx_import('BxDolXmlParser');
  13. /**
  14. * Command line interface for checking for unused language strings
  15. */
  16. class BxDolLangsMergeCmd
  17. {
  18. protected $_bQuiet = false;
  19. protected $_sInputFileOld = '';
  20. protected $_sInputFileNew = '';
  21. protected $_bSortOutput = false;
  22. protected $_sModule = false;
  23. protected $_iLimit = 0;
  24. public function __construct()
  25. {
  26. }
  27. public function main()
  28. {
  29. $a = getopt('hso:n:');
  30. if (isset($a['h']) || !$a)
  31. $this->finish(0, $this->getHelp());
  32. if (isset($a['s']))
  33. $this->_bSortOutput = true;
  34. if (isset($a['o']))
  35. $this->_sInputFileOld = '/' == $a['o'][1] ? $a['o'] : BX_DIRECTORY_PATH_ROOT . $a['o'];
  36. if (isset($a['n']))
  37. $this->_sInputFileNew = '/' == $a['n'][1] ? $a['n'] : BX_DIRECTORY_PATH_ROOT . $a['n'];
  38. if (!$this->_sInputFileOld || !file_exists($this->_sInputFileOld))
  39. $this->finish(2, 'Input old file not exist - ' . $this->_sInputFileOld);
  40. if (!$this->_sInputFileNew || !file_exists($this->_sInputFileNew))
  41. $this->finish(2, 'Input new file not exist - ' . $this->_sInputFileNew);
  42. $oXmlParser = BxDolXmlParser::getInstance();
  43. $sXmlContentOld = file_get_contents($this->_sInputFileOld);
  44. $sXmlContentNew = file_get_contents($this->_sInputFileNew);
  45. $aLangOld = $oXmlParser->getValues($sXmlContentOld, 'string');
  46. $aLangNew = $oXmlParser->getValues($sXmlContentNew, 'string');
  47. $this->fixEmptyValues($aLangOld);
  48. $this->fixEmptyValues($aLangNew);
  49. $aLangAdd = $this->findAddedKeys($aLangNew, $aLangOld);
  50. $aLangChange = $this->findChangedKeys($aLangNew, $aLangOld);
  51. $aLangDel = $this->findDeletedKeys($aLangNew, $aLangOld);
  52. if (!$aLangAdd && !$aLangChange && !$aLangDel) {
  53. $this->output("No any changes");
  54. $this->finish(0);
  55. }
  56. if ($this->_bSortOutput) {
  57. ksort($aLangAdd);
  58. ksort($aLangChange);
  59. ksort($aLangDel);
  60. }
  61. if ($aLangAdd)
  62. $this->output($this->xmlExport($aLangAdd, 'string_add', ''), false);
  63. if ($aLangChange)
  64. $this->output($this->xmlExport($aLangChange, 'string_upd', ''), false);
  65. if ($aLangDel)
  66. $this->output($this->xmlExport($aLangDel, 'string_del', ''), false);
  67. $this->finish(1);
  68. }
  69. function fixEmptyValues (&$a)
  70. {
  71. foreach ($a as $k => $v)
  72. if (NULL === $v)
  73. $a[$k] = '';
  74. }
  75. function findDeletedKeys ($aLangNew, $aLangOld)
  76. {
  77. $a = array ();
  78. foreach ($aLangOld as $k => $v)
  79. if (!isset($aLangNew[$k]))
  80. $a[$k] = $v;
  81. return $a;
  82. }
  83. function findAddedKeys ($aLangNew, $aLangOld)
  84. {
  85. $a = array ();
  86. foreach ($aLangNew as $k => $v)
  87. if (!isset($aLangOld[$k]))
  88. $a[$k] = $v;
  89. return $a;
  90. }
  91. function findChangedKeys ($aLangNew, $aLangOld)
  92. {
  93. $a = array ();
  94. foreach ($aLangNew as $k => $v)
  95. if (isset($aLangOld[$k]) && $aLangOld[$k] != $aLangNew[$k])
  96. $a[$k] = $v;
  97. return $a;
  98. }
  99. protected function getHelp()
  100. {
  101. $n = 21;
  102. $s = "Usage: php merge_lang.php [options]\n";
  103. $s .= str_pad("\t -h", $n) . "Print this help\n";
  104. $s .= str_pad("\t -s", $n) . "Sort output\n";
  105. $s .= str_pad("\t -o <input_file>", $n) . "Input old XML language file\n";
  106. $s .= str_pad("\t -n <input_file>", $n) . "Input new XML language file\n";
  107. $s .= "\n";
  108. $s .= "Return codes:\n";
  109. $s .= str_pad("\t 0", 5) . "Success - no any difference\n";
  110. $s .= str_pad("\t 1", 5) . "Changes were found\n";
  111. $s .= str_pad("\t 2", 5) . "Error occured\n";
  112. return $s;
  113. }
  114. protected function xmlExport($a, $sTag = 'string', $sRoot = 'resources', $bCData = true)
  115. {
  116. if (!$sTag)
  117. $sTag = 'string';
  118. $s = $sRoot ? "<" . $sRoot . ">\n" : "\n";
  119. foreach ($a as $sKey => $sString) {
  120. if ($bCData)
  121. $sString = '<![CDATA[' . $sString . ']]>';
  122. $s .= "\t".'<' . $sTag . ' name="' . $sKey . '">' . $sString . "</$sTag>\n";
  123. }
  124. $s .= $sRoot ? "</" . $sRoot . ">\n" : '';
  125. return $s;
  126. }
  127. protected function finish ($iCode, $sMsg = null, $bAddLn = true)
  128. {
  129. if (!$this->_bQuiet && null !== $sMsg)
  130. echo $sMsg . ($bAddLn ? "\n" : '');
  131. exit($iCode);
  132. }
  133. protected function output ($sMsg, $bAddLn = true)
  134. {
  135. if (!$this->_bQuiet)
  136. echo $sMsg . ($bAddLn ? "\n" : '');
  137. }
  138. }
  139. $o = new BxDolLangsMergeCmd();
  140. $o->main();
  141. /** @} */