check_unused_lang.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinInstall Dolphin Install
  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. );
  59. $this->_aOptionsSystem = array (
  60. 'dirs' => array (
  61. '' => array ('*.php'),
  62. 'install/' => array('*.php', '*.sql'),
  63. 'studio/' => array('*.php', '*.html'),
  64. 'inc/' => array ('*.php', '*.js'),
  65. 'modules/' => array('*.php', '*.html', '*.sql'),
  66. 'template/' => array('*.php', '*.html'),
  67. ),
  68. );
  69. $this->_aOptionsModule = array (
  70. 'dirs' => array (
  71. "modules/{module_dir}/" => array ('*.php'),
  72. "modules/{module_dir}/install/sql/" => array('*.sql'),
  73. "modules/{module_dir}/classes/" => array('*.php'),
  74. "modules/{module_dir}/js/" => array ('*.js'),
  75. "modules/{module_dir}/template/" => array('*.php', '*.html'),
  76. "modules/{module_dir}/inc/" => array ('*.php'),
  77. ),
  78. );
  79. }
  80. public function main()
  81. {
  82. $a = getopt('hqsm:l:i:f:u:');
  83. if (isset($a['h']))
  84. $this->finish(0, $this->getHelp());
  85. if (isset($a['q']))
  86. $this->_bQuiet = true;
  87. if (isset($a['s']))
  88. $this->_bSortOutput = true;
  89. if (isset($a['m']))
  90. $this->_sModule = $a['m'];
  91. if (isset($a['l']))
  92. $this->_iLimit = (int)$a['l'];
  93. if (isset($a['i']))
  94. $this->_sInputFile = $a['i'];
  95. if (!$this->_sInputFile || !file_exists($this->_sInputFile))
  96. $this->finish(2, 'Input file not exist - ' . $this->_sInputFile);
  97. if (isset($a['f']))
  98. $this->_sOutputFoundFile = $a['f'];
  99. if (isset($a['u']))
  100. $this->_sOutputUnusedFile = $a['u'];
  101. $aOptions = $this->_sModule ? $this->prepareModuleConfig($this->_aOptionsModule) : $this->_aOptionsSystem;
  102. $iTimeStart = microtime ();
  103. $this->output('Processing ' . ($this->_iLimit > 0 ? "first {$this->_iLimit} strings of " : '') . $this->_sInputFile . ' ' . ($this->_sModule ? 'module(' . $this->_sModule .')' : 'system') . ' language file...');
  104. $this->output('');
  105. $oXmlParser = BxDolXmlParser::getInstance();
  106. $sXmlContent = file_get_contents($this->_sInputFile);
  107. $LANG = $oXmlParser->getValues($sXmlContent, 'string');
  108. $LANG_FOUND = array();
  109. $LANG_LOST = array();
  110. $iCount = 1;
  111. foreach ($LANG as $sKey => $sString) {
  112. if ($this->findLangKey($sKey, $aOptions))
  113. $LANG_FOUND[$sKey] = $sString;
  114. else
  115. $LANG_LOST[$sKey] = $sString;
  116. if (++$iCount > $this->_iLimit && $this->_iLimit > 0)
  117. break;
  118. }
  119. if (!$this->_bQuiet) {
  120. $this->output('Found language strings:');
  121. $this->output('-----------------------');
  122. empty($LANG_FOUND) ? $this->output('Empty') : $this->output($this->xmlExport($LANG_FOUND), false);
  123. $this->output('');
  124. }
  125. if ($this->_bSortOutput)
  126. ksort($LANG_FOUND);
  127. if ($this->_sOutputFoundFile && !empty($LANG_FOUND))
  128. file_put_contents($this->_sOutputFoundFile, $this->xmlExport($LANG_FOUND));
  129. if (!$this->_bQuiet) {
  130. $this->output('Unused language strings:');
  131. $this->output('------------------------');
  132. empty($LANG_LOST) ? $this->output('Empty') : $this->output($this->xmlExport($LANG_LOST), false);
  133. $this->output('');
  134. }
  135. if ($this->_bSortOutput)
  136. ksort($LANG_LOST);
  137. if ($this->_sOutputUnusedFile && !empty($LANG_LOST))
  138. file_put_contents($this->_sOutputUnusedFile, $this->xmlExport($LANG_LOST));
  139. $i1 = explode(' ', microtime ());
  140. $i2 = explode(' ', $iTimeStart);
  141. $iSec = round(($i1[0]+$i1[1]) - ($i2[0]+$i2[1]), 3);
  142. $this->output("Time ($iCount): " . $iSec . ' sec');
  143. if ($this->_iLimit > 0) {
  144. $iTotalSec = round(count($LANG) / $iCount * (($i1[0]+$i1[1]) - ($i2[0]+$i2[1])), 3);
  145. $this->output("Estimated Total Time (" . count($LANG) . "): " . $iTotalSec . ' sec (' . round($iTotalSec / 60.0, 3) . ' min)');
  146. }
  147. $this->finish(empty($LANG_LOST) ? 0 : 1);
  148. }
  149. protected function getHelp()
  150. {
  151. $n = 21;
  152. $s = "Usage: php check_unused_lang.php [options]\n";
  153. $s .= str_pad("\t -h", $n) . "Print this help\n";
  154. $s .= str_pad("\t -q", $n) . "Quiet\n";
  155. $s .= str_pad("\t -s", $n) . "Sort output\n";
  156. $s .= str_pad("\t -m <vendor/module>", $n) . "Process as module lang file, vendor and module folder names must be specified\n";
  157. $s .= str_pad("\t -l <number>", $n) . "Limit number of processing strings (usefull for estimating output time)\n";
  158. $s .= str_pad("\t -i <input_file>", $n) . "Input XML language file\n";
  159. $s .= str_pad("\t -f <output_file>", $n) . "Output XML file for found strings\n";
  160. $s .= str_pad("\t -u <output_file>", $n) . "Output XML file for unused strings\n";
  161. $s .= "\n";
  162. $s .= "Return codes:\n";
  163. $s .= str_pad("\t 0", 5) . "Success - no unused language strings\n";
  164. $s .= str_pad("\t 1", 5) . "Unused language strings were found\n";
  165. $s .= str_pad("\t 2", 5) . "Error occured\n";
  166. return $s;
  167. }
  168. protected function findLangKey($sKey, $aOptions)
  169. {
  170. foreach ($this->_aAlwaysTrue as $sReg)
  171. if (preg_match($sReg, $sKey))
  172. return true;
  173. foreach ($aOptions['dirs'] as $sDir => $aExt) {
  174. $sDepth = $sDir ? '' : ' -depth 1 ';
  175. if ($this->_sModule) {
  176. $aDir = explode('/', $sDir);
  177. $sDepth = !$sDir || $this->_sModule == ($aDir[(count($aDir) - 3)] . '/' . $aDir[(count($aDir) - 2)]) ? ' -depth 1 ' : '';
  178. }
  179. $sDir = BX_DIRECTORY_PATH_ROOT . $sDir;
  180. if (!file_exists($sDir))
  181. continue;
  182. foreach ($aExt as $sExt) {
  183. $sResults = `find $sDir $sDepth -name "$sExt" -exec grep "\<$sKey\>" {} \;`;
  184. if ($sResults)
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. protected function xmlExport($a, $sRoot = 'resources', $bCData = true)
  191. {
  192. $s = "<" . $sRoot . ">\n";
  193. foreach ($a as $sKey => $sString) {
  194. if ($bCData)
  195. $sString = '<![CDATA[' . $sString . ']]>';
  196. $s .= "\t".'<string name="' . $sKey . '">' . $sString . "</string>\n";
  197. }
  198. $s .= "</" . $sRoot . ">\n";
  199. return $s;
  200. }
  201. protected function prepareModuleConfig ($a) {
  202. if (!$this->_sModule)
  203. return $a;
  204. $aRet = array();
  205. $aRet['dirs'] = array();
  206. foreach ($a['dirs'] as $sDir => $aPatterns) {
  207. $sDir = str_replace('{module_dir}', $this->_sModule, $sDir);
  208. $aRet['dirs'][$sDir] = $aPatterns;
  209. }
  210. return $aRet;
  211. }
  212. protected function finish ($iCode, $sMsg = null, $bAddLn = true)
  213. {
  214. if (!$this->_bQuiet && null !== $sMsg)
  215. echo $sMsg . ($bAddLn ? "\n" : '');
  216. exit($iCode);
  217. }
  218. protected function output ($sMsg, $bAddLn = true)
  219. {
  220. if (!$this->_bQuiet)
  221. echo $sMsg . ($bAddLn ? "\n" : '');
  222. }
  223. }
  224. $o = new BxDolCheckUnusedLangsCmd();
  225. $o->main();
  226. /** @} */