Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/cron/lang/list_unused_langvars.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
/**
4
 * Cron script to list unused, but defined, language variables.
5
 *
6
 * @package chamilo.cron.lang
7
 */
8
/**
9
 * Includes and declarations.
10
 */
11
exit();
12
require_once __DIR__.'/../../inc/global.inc.php';
13
$path = api_get_path(SYS_LANG_PATH).'english';
14
ini_set('memory_limit', '128M');
15
/**
16
 * Main code.
17
 */
18
$terms = [];
19
$list = SubLanguageManager::get_lang_folder_files_list($path);
20
foreach ($list as $entry) {
21
    $file = $path.'/'.$entry;
22
    if (is_file($file)) {
23
        $terms = array_merge($terms, SubLanguageManager::get_all_language_variable_in_file($file, true));
24
    }
25
}
26
// get only the array keys (the language variables defined in language files)
27
$defined_terms = array_flip(array_keys($terms));
28
$terms = null;
29
echo count($defined_terms)." terms were found in language files<br />";
30
31
// now get all terms found in all PHP files of Chamilo (this takes some
32
// time and memory)
33
$usedTerms = [];
34
$l = strlen(api_get_path(SYS_PATH));
35
$files = getAllPhpFiles(api_get_path(SYS_PATH));
36
$files[] = api_get_path(SYS_PATH).'main/install/data.sql';
37
// Browse files
38
foreach ($files as $file) {
39
    //echo 'Analyzing '.$file."<br />";
40
    $shortFile = substr($file, $l);
41
    //echo 'Analyzing '.$shortFile."<br />";
42
    $lines = file($file);
43
    $isDataSQL = false;
44
    if (substr($file, -21) === 'main/install/data.sql') {
45
        $isDataSQL = true;
46
    }
47
    // Browse lines inside file $file
48
    foreach ($lines as $line) {
49
        if ($isDataSQL) {
50
            // Check main/install/data.sql
51
            // Should recognize stuff like
52
            // INSERT INTO settings_current (variable, type, category, selected_value, title, comment) VALUES ('enable_profile_user_address_geolocalization', 'radio', 'User', 'false', 'EnableProfileUsersAddressGeolocalizationTitle', 'EnableProfileUsersAddressGeolocalizationComment');
53
            // INSERT INTO settings_options (variable, value, display_text) VALUES ('enable_profile_user_address_geolocalization', 'true', 'Yes');
54
            // ('show_teacher_data',NULL,'radio','Platform','true','ShowTeacherDataTitle','ShowTeacherDataComment',NULL,NULL, 1),
55
            $res = 0;
56
            $myTerms = [];
57
            $res = preg_match_all('/\'(\w*)\',/', $line, $myTerms);
58
            if ($res > 0) {
59
                foreach ($myTerms[1] as $term) {
60
                    if (substr($term, 0, 4) == 'lang') {
61
                        $term = substr($term, 4);
62
                    }
63
                    $usedTerms[$term] = $shortFile;
64
                }
65
            }
66
        } else {
67
            $myTerms = [];
68
            $res = preg_match_all('/get_lang\(\'(\\w*)\'\)/', $line, $myTerms);
69
            if ($res > 0) {
70
                foreach ($myTerms[1] as $term) {
71
                    if (substr($term, 0, 4) == 'lang') {
72
                        $term = substr($term, 4);
73
                    }
74
                    $usedTerms[$term] = $shortFile;
75
                }
76
            } else {
77
                $res = 0;
78
                $myTerms = [];
79
                // Should catch:
80
                // {{ 'CopyTextToClipboard' | get_lang }}
81
                // {{ "HelloX" | get_lang | format(show_user_info.user_info.complete_name) }}
82
                // {{ "StudentCourseProgressX" | get_lang | format(item.student_info.progress) }}
83
                $res = preg_match_all('/\{\s*[\'"](\w*)[\'"]\s*\|\s*get_lang\s*(\|\s*\w*(\s*\([\w_\.,\s]*\))?\s*)?\}/', $line, $myTerms);
84
                if ($res > 0) {
85
                    foreach ($myTerms[1] as $term) {
86
                        if (substr($term, 0, 4) == 'lang') {
87
                            $term = substr($term, 4);
88
                        }
89
                        $usedTerms[$term] = $shortFile;
90
                    }
91
                }
92
                // {{ display.panel('PersonalDataResponsibleOrganizationTitle' | get_lang , personal_data.responsible ) }}
93
                // {{ display.panel('PersonalDataIntroductionTitle' | get_lang , 'PersonalDataIntroductionText' | get_lang) }}
94
                $myTerms = [];
95
                $res = preg_match_all('/\{\s*[\w\.]*\([\'"](\w*)[\'"]\s*\|\s*get_lang\s*(,\s*[\w_\.,\s\|\'"]*\s*)?\)\s*\}/', $line, $myTerms);
96
                if ($res > 0) {
97
                    foreach ($myTerms[1] as $term) {
98
                        if (substr($term, 0, 4) == 'lang') {
99
                            $term = substr($term, 4);
100
                        }
101
                        $usedTerms[$term] = $shortFile;
102
                    }
103
                }
104
            }
105
        }
106
    }
107
    flush();
108
}
109
110
// Compare defined terms VS used terms. Used terms should be smaller than
111
// defined terms, and this should prove the concept that there are much
112
// more variables than what we really use
113
if (count($usedTerms) < 1) {
114
    exit("No used terms<br />\n");
115
} else {
116
    echo "The following terms were defined but never used: <br />\n<table>";
117
}
118
$i = 1;
119
foreach ($defined_terms as $term => $file) {
0 ignored issues
show
Comprehensibility Bug introduced by
$file is overwriting a variable from outer foreach loop.
Loading history...
120
    // remove "lang" prefix just in case
121
    if (substr($term, 0, 4) == 'lang') {
122
        $term = substr($term, 4);
123
    }
124
    if (!isset($usedTerms[$term])) {
125
        echo "<tr><td>$i</td><td>$term</td></tr>\n";
126
        $i++;
127
    }
128
}
129
echo "</table>\n";
130