From 49bf84e4a1bb9a2c04535c40c80c9354e3fa8274 Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Sun, 27 May 2012 18:47:38 -0500 Subject: [PATCH] Added script to detect unused language variables - found around 2400 for a total of 7700 variables currently managed. Conclusion: we could considerably reduce the load on new translators! - refs #4804 --- main/cron/lang/list_unused_langvars.php | 103 ++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 main/cron/lang/list_unused_langvars.php diff --git a/main/cron/lang/list_unused_langvars.php b/main/cron/lang/list_unused_langvars.php new file mode 100644 index 0000000000..adacc62749 --- /dev/null +++ b/main/cron/lang/list_unused_langvars.php @@ -0,0 +1,103 @@ +"; + +// now get all terms found in all PHP files of Chamilo (this takes some +// time and memory) +$used_terms = array(); +$l = strlen(api_get_path(SYS_PATH)); +$files = get_all_php_files(api_get_path(SYS_PATH)); +// Browse files +foreach ($files as $file) { + //echo 'Analyzing '.$file."
"; + $shortfile = substr($file,$l); + //echo 'Analyzing '.$shortfile."
"; + $lines = file($file); + // Browse lines inside file $file + foreach ($lines as $line) { + $myterms = array(); + $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/',$line,$myterms); + if ($res > 0) { + foreach($myterms[1] as $term) { + if (substr($term,0,4)=='lang') { $term = substr($term,4); } + $used_terms[$term] = $shortfile; + } + } else { + $res = 0; + $res = preg_match_all('/\{[\'"](\\w*)[\'"]\|get_lang\}/',$line,$myterms); + if ($res > 0) { + foreach($myterms[1] as $term) { + if (substr($term,0,4)=='lang') { $term = substr($term,4); } + $used_terms[$term] = $shortfile; + } + } + } + } + flush(); +} + +// Compare defined terms VS used terms. Used terms should be smaller than +// defined terms, and this should prove the concept that there are much +// more variables than what we really use +if (count($used_terms)<1) { + die("No used terms
\n"); +} else { + echo "The following terms were defined but never used:
\n"; +} +$i = 1; +foreach ($defined_terms as $term => $file) { + // remove "lang" prefix just in case + if (substr($term,0,4)=='lang') { $term = substr($term,4); } + if (!isset($used_terms[$term])) { + echo "\n"; + $i++; + } +} +echo "
$i$term
\n"; + + +function get_all_php_files($base_path) { + $list = scandir($base_path); + $files = array(); + foreach ($list as $item) { + if (substr($item,0,1)=='.') {continue;} + $special_dirs = array(api_get_path(SYS_TEST_PATH),api_get_path(SYS_COURSE_PATH),api_get_path(SYS_LANG_PATH),api_get_path(SYS_ARCHIVE_PATH)); + if (in_array($base_path.$item.'/',$special_dirs)) {continue;} + if (is_dir($base_path.$item)) { + $files = array_merge($files,get_all_php_files($base_path.$item.'/')); + } else { + //only analyse php files + $sub = substr($item,-4); + if ($sub == '.php' or $sub == '.tpl') { + $files[] = $base_path.$item; + } + } + } + $list = null; + return $files; +}