-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_languages.php
executable file
·164 lines (137 loc) · 4.01 KB
/
check_languages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/* This is a temp array */
$lang = array();
/**
* This function is just an overload of an existing function in elgg.
*
* @param string $lg Language of the translation.
* @param array $translations Array containing translation.
* @return null
*/
function add_translation($lg, $translations) {
global $lang;
$lang[$lg] = $translations;
}
/**
* This class is used to check if the language files associated
* with a plugin contain keys which are really used by the plugin.
*
* @package Tools
* @class PluginChecker
*/
class PluginChecker {
var $_plugin; // Contains the name of the plugin.
var $_lang; // Contains the set of key => translation.
var $_files; // Contains the PHP files of the plugin.
/**
* This function return an array which contains all the php files in the directory $c_dir.
*
* @param string $c_dir Current directory.
* @param string $path Current path (set to . at the beginning).
* @return an array containing all the PHP files.
*/
private function getPhpFiles($c_dir, $path) {
/* We enter in the current directory */
chdir($c_dir);
$path = $path . '/' . $c_dir;
/* This var will contains all the PHP files. */
$files = array();
/* We open the dir to read all the files */
if (!$dir = opendir('.')) {
return $files;
}
while (false !== ($file = readdir($dir))) {
/* If the file is a directory, we enter in it. */
if (is_dir($file)) {
if ($file != 'languages' && $file != '.' && $file != '..') {
$files = array_merge($files, $this->getPhpFiles($file, $path));
}
} else {
/* If the file is a PHP files, we add it into the list. */
if (substr($file, -4, 4) == '.php') {
$files[] = $path . '/' . $file;
}
}
}
closedir($dir);
chdir('..');
return $files;
}
/**
* This function load all the languages files from the plugin directory.
*
* @param string $plugin Name of the plugin (and it's dirname).
* @return null
*/
function load($plugin) {
global $lang;
$this->_plugin = $plugin;
$lang = array();
/* We check if the languages directory exists */
if (!file_exists('./' . $plugin . '/languages')) {
return;
}
/* We check if the directory is openable. */
if (!$dir = opendir('./' . $plugin . '/languages/')) {
return;
}
/* For each file in the directory, we open all file with the php extension. */
while (false !== ($file = readdir($dir))) {
if (!is_dir($file) && substr($file, -4, 4) == '.php') {
include_once($plugin . '/languages/' . $file);
}
}
closedir($dir);
$this->_lang = $lang;
$this->_files = $this->getPhpFiles($plugin, '.');
}
/**
* This function check if all the keys are matched in all the PHP files of the plugins.
*
* @return null
*/
function check() {
if (!empty($this->_lang)) {
foreach ($this->_lang AS $lg => $tr) {
/* Initialize the starting state. */
foreach ($tr AS $key => $stc) {
$matched[$key] = false;
}
/* In each file, we check if keys are matched. */
foreach ($this->_files AS $file) {
$handle = fopen($file, "r");
$size = filesize($file);
if ($size == 0) {
echo "The file $file is empty.<br>\n";
} else {
$contents = fread($handle, $size);
/* We check each key on the contents. */
foreach ($tr AS $key => $stc) {
if (strpos($contents, "'$key'") !== false || strpos($contents, "\"$key\"") !== false) {
$matched[$key] = true;
}
}
}
fclose($handle);
}
/* Finally, we print the keys unmatched. */
foreach ($tr AS $key => $stc) {
if ($matched[$key] == false) {
echo "$key from the language $lg is not found in the plugin " . $this->_plugin . "<br>\n";
}
}
}
}
}
}
/* Open the current directory containing all the plugins. */
$dir = opendir('.');
/* For each file in the directory, we load the plugin's language */
while (false !== ($file = readdir($dir))) {
if ($file != '.' && $file != '..' && is_dir($file)) {
$pl = new PluginChecker();
$pl->load($file);
$pl->check();
}
}
closedir($dir);