check_unused_lang.php 9.0 KB

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