check_unused_lang.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 BxDolCheckUnusedLangsCmd
  17. {
  18. protected $_bQuiet = false;
  19. protected $_sInputFile = '';
  20. protected $_sOutputFoundFile = '';
  21. protected $_sOutputUnusedFile = '';
  22. protected $_bSortOutput = false;
  23. protected $_sModule = false;
  24. protected $_iLimit = 0;
  25. protected $_aOptionsSystem;
  26. protected $_aOptionsModule;
  27. protected $_aAlwaysTrue;
  28. public function __construct()
  29. {
  30. $this->_aAlwaysTrue = array (
  31. '/^_are you sure\?$/',
  32. '/^_mma_/',
  33. '/^_ps_group_/',
  34. '/^_adm_txt_modules_/',
  35. '/^_adm_ipbl_Type/',
  36. '/^_week_/',
  37. '/^_adm_txt_module_/',
  38. '/^_adm_block_cpt_/',
  39. '/^_adm_lmi_cpt_/',
  40. '/^_adm_block_cpt_/',
  41. '/^_adm_bp_cpt_type_/',
  42. '/^_adm_form_txt_field_type_/',
  43. '/^_adm_form_txt_field_checker_/',
  44. '/^_sys_uploader_/',
  45. '/^_adm_[A-Za-z0-9_-]+_cnf_/',
  46. '/^_sys_request_[A-Za-z0-9_-]+_not_found_(cpt|cnt)/',
  47. // timeline
  48. '/^_bx_timeline_alert_module_/',
  49. '/^_bx_timeline_alert_action_/',
  50. // sites
  51. '/^_bx_sites_txt_status_/',
  52. '/^_bx_sites_txt_payment_type_/',
  53. '/^_bx_sites_paypal_duration_/',
  54. // developer
  55. '/^_bx_dev_pgt_txt_manage_/',
  56. // antispam
  57. '/^_bx_antispam_chain_/',
  58. '/^_bx_antispam_type_/',
  59. );
  60. $this->_aOptionsSystem = array (
  61. 'dirs' => array (
  62. '' => array ('*.php'),
  63. 'install/' => array('*.php', '*.sql'),
  64. 'studio/' => array('*.php', '*.html'),
  65. 'inc/' => array ('*.php', '*.js'),
  66. 'modules/' => array('*.php', '*.html', '*.sql'),
  67. 'template/' => array('*.php', '*.html'),
  68. ),
  69. );
  70. $this->_aOptionsModule = array (
  71. 'dirs' => array (
  72. "modules/{module_dir}/" => array ('*.php'),
  73. "modules/{module_dir}/install/sql/" => array('*.sql'),
  74. "modules/{module_dir}/classes/" => array('*.php'),
  75. "modules/{module_dir}/js/" => array ('*.js'),
  76. "modules/{module_dir}/template/" => array('*.php', '*.html'),
  77. "modules/{module_dir}/inc/" => array ('*.php'),
  78. ),
  79. );
  80. }
  81. public function main()
  82. {
  83. $a = getopt('hqsm:l:i:f:u:');
  84. if (isset($a['h']))
  85. $this->finish(0, $this->getHelp());
  86. if (isset($a['q']))
  87. $this->_bQuiet = true;
  88. if (isset($a['s']))
  89. $this->_bSortOutput = true;
  90. if (isset($a['m']))
  91. $this->_sModule = $a['m'];
  92. if (isset($a['l']))
  93. $this->_iLimit = (int)$a['l'];
  94. if (isset($a['i']))
  95. $this->_sInputFile = $a['i'];
  96. if (!$this->_sInputFile || !file_exists($this->_sInputFile))
  97. $this->finish(2, 'Input file not exist - ' . $this->_sInputFile);
  98. if (isset($a['f']))
  99. $this->_sOutputFoundFile = $a['f'];
  100. if (isset($a['u']))
  101. $this->_sOutputUnusedFile = $a['u'];
  102. $aOptions = $this->_sModule ? $this->prepareModuleConfig($this->_aOptionsModule) : $this->_aOptionsSystem;
  103. $iTimeStart = microtime ();
  104. $this->output('Processing ' . ($this->_iLimit > 0 ? "first {$this->_iLimit} strings of " : '') . $this->_sInputFile . ' ' . ($this->_sModule ? 'module(' . $this->_sModule .')' : 'system') . ' language file...');
  105. $this->output('');
  106. $oXmlParser = BxDolXmlParser::getInstance();
  107. $sXmlContent = file_get_contents($this->_sInputFile);
  108. $LANG = $oXmlParser->getValues($sXmlContent, 'string');
  109. $LANG_FOUND = array();
  110. $LANG_LOST = array();
  111. $iCount = 1;
  112. foreach ($LANG as $sKey => $sString) {
  113. if ($this->findLangKey($sKey, $aOptions))
  114. $LANG_FOUND[$sKey] = $sString;
  115. else
  116. $LANG_LOST[$sKey] = $sString;
  117. if (++$iCount > $this->_iLimit && $this->_iLimit > 0)
  118. break;
  119. }
  120. if (!$this->_bQuiet) {
  121. $this->output('Found language strings:');
  122. $this->output('-----------------------');
  123. empty($LANG_FOUND) ? $this->output('Empty') : $this->output($this->xmlExport($LANG_FOUND), false);
  124. $this->output('');
  125. }
  126. if ($this->_bSortOutput)
  127. ksort($LANG_FOUND);
  128. if ($this->_sOutputFoundFile && !empty($LANG_FOUND))
  129. file_put_contents($this->_sOutputFoundFile, $this->xmlExport($LANG_FOUND));
  130. if (!$this->_bQuiet) {
  131. $this->output('Unused language strings:');
  132. $this->output('------------------------');
  133. empty($LANG_LOST) ? $this->output('Empty') : $this->output($this->xmlExport($LANG_LOST), false);
  134. $this->output('');
  135. }
  136. if ($this->_bSortOutput)
  137. ksort($LANG_LOST);
  138. if ($this->_sOutputUnusedFile && !empty($LANG_LOST))
  139. file_put_contents($this->_sOutputUnusedFile, $this->xmlExport($LANG_LOST));
  140. $i1 = explode(' ', microtime ());
  141. $i2 = explode(' ', $iTimeStart);
  142. $iSec = round(($i1[0]+$i1[1]) - ($i2[0]+$i2[1]), 3);
  143. $this->output("Time ($iCount): " . $iSec . ' sec');
  144. if ($this->_iLimit > 0) {
  145. $iTotalSec = round(count($LANG) / $iCount * (($i1[0]+$i1[1]) - ($i2[0]+$i2[1])), 3);
  146. $this->output("Estimated Total Time (" . count($LANG) . "): " . $iTotalSec . ' sec (' . round($iTotalSec / 60.0, 3) . ' min)');
  147. }
  148. $this->finish(empty($LANG_LOST) ? 0 : 1);
  149. }
  150. protected function getHelp()
  151. {
  152. $n = 21;
  153. $s = "Usage: php check_unused_lang.php [options]\n";
  154. $s .= str_pad("\t -h", $n) . "Print this help\n";
  155. $s .= str_pad("\t -q", $n) . "Quiet\n";
  156. $s .= str_pad("\t -s", $n) . "Sort output\n";
  157. $s .= str_pad("\t -m <vendor/module>", $n) . "Process as module lang file, vendor and module folder names must be specified\n";
  158. $s .= str_pad("\t -l <number>", $n) . "Limit number of processing strings (usefull for estimating output time)\n";
  159. $s .= str_pad("\t -i <input_file>", $n) . "Input XML language file\n";
  160. $s .= str_pad("\t -f <output_file>", $n) . "Output XML file for found strings\n";
  161. $s .= str_pad("\t -u <output_file>", $n) . "Output XML file for unused strings\n";
  162. $s .= "\n";
  163. $s .= "Return codes:\n";
  164. $s .= str_pad("\t 0", 5) . "Success - no unused language strings\n";
  165. $s .= str_pad("\t 1", 5) . "Unused language strings were found\n";
  166. $s .= str_pad("\t 2", 5) . "Error occured\n";
  167. return $s;
  168. }
  169. protected function findLangKey($sKey, $aOptions)
  170. {
  171. foreach ($this->_aAlwaysTrue as $sReg)
  172. if (preg_match($sReg, $sKey))
  173. return true;
  174. foreach ($aOptions['dirs'] as $sDir => $aExt) {
  175. $sDepth = $sDir ? '' : ' -depth 1 ';
  176. if ($this->_sModule) {
  177. $aDir = explode('/', $sDir);
  178. $sDepth = !$sDir || $this->_sModule == ($aDir[(count($aDir) - 3)] . '/' . $aDir[(count($aDir) - 2)]) ? ' -depth 1 ' : '';
  179. }
  180. $sDir = BX_DIRECTORY_PATH_ROOT . $sDir;
  181. if (!file_exists($sDir))
  182. continue;
  183. foreach ($aExt as $sExt) {
  184. $sResults = `find $sDir $sDepth -name "$sExt" -exec grep "\<$sKey\>" {} \;`;
  185. if ($sResults)
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. protected function xmlExport($a, $sRoot = 'resources', $bCData = true)
  192. {
  193. $s = "<" . $sRoot . ">\n";
  194. foreach ($a as $sKey => $sString) {
  195. if ($bCData)
  196. $sString = '<![CDATA[' . $sString . ']]>';
  197. $s .= "\t".'<string name="' . $sKey . '">' . $sString . "</string>\n";
  198. }
  199. $s .= "</" . $sRoot . ">\n";
  200. return $s;
  201. }
  202. protected function prepareModuleConfig ($a) {
  203. if (!$this->_sModule)
  204. return $a;
  205. $aRet = array();
  206. $aRet['dirs'] = array();
  207. foreach ($a['dirs'] as $sDir => $aPatterns) {
  208. $sDir = str_replace('{module_dir}', $this->_sModule, $sDir);
  209. $aRet['dirs'][$sDir] = $aPatterns;
  210. }
  211. return $aRet;
  212. }
  213. protected function finish ($iCode, $sMsg = null, $bAddLn = true)
  214. {
  215. if (!$this->_bQuiet && null !== $sMsg)
  216. echo $sMsg . ($bAddLn ? "\n" : '');
  217. exit($iCode);
  218. }
  219. protected function output ($sMsg, $bAddLn = true)
  220. {
  221. if (!$this->_bQuiet)
  222. echo $sMsg . ($bAddLn ? "\n" : '');
  223. }
  224. }
  225. $o = new BxDolCheckUnusedLangsCmd();
  226. $o->main();
  227. /** @} */