source: extensions/plugin_lang_analysis/admin.php @ 26607

Last change on this file since 26607 was 26607, checked in by mistic100, 10 years ago

allow to ignore a file

File size: 7.5 KB
Line 
1<?php
2defined('PLA_PATH') or die('Hacking attempt!');
3 
4global $template, $page;
5
6$page['active_menu'] = get_active_menu('updates');
7
8include_once(PLA_PATH . 'include/functions.inc.php');
9include_once(PHPWG_ROOT_PATH . 'admin/include/plugins.class.php');
10$plugins = new plugins();
11
12/* SELECT */
13if (!isset($_GET['plugin_id']))
14{
15  $template->assign(array(
16    'PLA_STEP' => 'select',
17    'PLA_PLUGINS' => $plugins->fs_plugins,
18    'F_ACTION' => PLA_ADMIN.'&amp;config',
19    ));
20}
21
22/* CONFIG */
23else if (isset($_GET['config']))
24{
25  $files = list_plugin_files($_GET['plugin_id']);
26  $language_files = list_plugin_languages_files($_GET['plugin_id']);
27 
28  $default_lang_files = get_loaded_in_main($_GET['plugin_id']);
29  if (empty($default_lang_files))
30  {
31    $default_lang_files = count($language_files)==1 ? array_keys($language_files) : (
32                            array_key_exists('plugin.lang', $language_files) ? array('plugin.lang') : array()
33                            );
34  }
35 
36  if (file_exists(PLA_DATA.$_GET['plugin_id'].'.php'))
37  {
38    $saved_files = include(PLA_DATA.$_GET['plugin_id'].'.php');
39  }
40  else
41  {
42    $saved_files = array();
43  }
44 
45  foreach ($files as &$file)
46  {
47    if (isset($saved_files[ $file ]))
48    {
49      $file = $saved_files[ $file ];
50      $file['lang_files'] = array_intersect($file['lang_files'], array_keys($language_files));
51    }
52    else
53    {
54      $file = array(
55        'path' => $file,
56        'is_admin' => strpos($file, '/admin') === 0 || strpos($file, 'admin.tpl') !== false,
57        'ignore' => false,
58        'lang_files' => $default_lang_files,
59        );
60    }
61  }
62  unset($file);
63 
64  $template->assign(array(
65    'PLA_STEP' => 'config',
66    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
67    'PLA_FILES' => $files,
68    'PLA_LANG_FILES' => array_keys($language_files),
69    'F_ACTION' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
70    'U_BACK' => PLA_ADMIN,
71    ));
72}
73
74/* ANALYSIS */
75else if (isset($_GET['analyze']))
76{
77  // save
78  if (isset($_POST['files']))
79  {
80    $files = array();
81    foreach ($_POST['files'] as $file => $data)
82    {
83      $files[ $file ] = array(
84        'path' => $file,
85        'is_admin' => $data['is_admin']=='true',
86        'ignore' => $data['ignore']=='true',
87        'lang_files' => array(),
88        );
89      if (!empty($data['lang_files']))
90      {
91        $files[ $file ]['lang_files'] = array_keys(array_filter($data['lang_files'], create_function('$f', 'return $f=="true";')));
92      }
93    }
94   
95    $content = "<?php\nreturn ";
96    $content.= var_export($files, true);
97    $content.= ";\n?>";
98   
99    @mkdir(PLA_DATA, true, 0755);
100    file_put_contents(PLA_DATA.$_GET['plugin_id'].'.php', $content);
101  }
102  else
103  {
104    $files = include(PLA_DATA.$_GET['plugin_id'].'.php');
105  }
106 
107  $strings = array();
108  $counts = array('ok'=>0,'missing'=>0,'useless'=>0);
109 
110  // get strings list
111  foreach ($files as $file => $file_data)
112  {
113    if ($file_data['ignore']) continue;
114
115    $file_strings = analyze_file($_GET['plugin_id'].$file);
116   
117    foreach ($file_strings as $string => $lines)
118    {
119      $strings[ $string ]['files'][ $file ] = $file_data + array('lines' => $lines);
120    }
121  }
122 
123  // load language files
124  $lang_common = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/common.lang.php');
125  $lang_admin = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/admin.lang.php');
126 
127  $language_files = list_plugin_languages_files($_GET['plugin_id']);
128  foreach ($language_files as $name => $path)
129  {
130    $lang_plugin[ $name ] = load_language_file(PHPWG_PLUGINS_PATH.$_GET['plugin_id'].$path);
131  }
132 
133  // analyse
134  foreach ($strings as $string => &$string_data)
135  {
136    // find where the string is defined
137    $string_data['in_common'] = array_key_exists($string, $lang_common);
138    $string_data['in_admin'] = array_key_exists($string, $lang_admin);
139    $string_data['in_plugin'] = array();
140    foreach ($language_files as $name => $path)
141    {
142      if (array_key_exists($string, $lang_plugin[$name])) $string_data['in_plugin'][] = $name;
143    }
144   
145    // very rare case
146    if (count($string_data['in_plugin'])>1)
147    {
148      $string_data['warnings'][] = l10n('This string is translated in multiple files');
149    }
150   
151    $missing = $useless = $ok = false;
152    $string_data['is_admin'] = true;
153   
154    // analyse for each file where the string exists
155    foreach ($string_data['files'] as $file => &$file_data)
156    {
157      // the string is "admin" if all files are "admin"
158      $string_data['is_admin'] &= $file_data['is_admin'];
159     
160      // find if the string is translated in one of the language files included in this file
161      $exists = count(array_intersect($file_data['lang_files'], $string_data['in_plugin'])) > 0;
162     
163      // useless if translated in the plugin AND in common or admin
164      if ($exists && ($string_data['in_common'] || ($file_data['is_admin'] && $string_data['in_admin'])))
165      {
166        $file_data['stat'] = 'useless';
167        $useless = true;
168      }
169      // missing if not translated in the plugin NOR in common or admin
170      else if (!$exists && !$string_data['in_common'] && (!$file_data['is_admin'] || !$string_data['in_admin']))
171      {
172        $file_data['stat'] = 'missing';
173        $missing = true;
174      }
175      // else ok
176      else
177      {
178        $file_data['stat'] = 'ok';
179        $ok = true;
180      }
181    }
182    unset($file_data);
183   
184    // string is missing if at least missing in one file
185    if ($missing)
186    {
187      $string_data['stat'] = 'missing';
188      $counts['missing']++;
189    }
190    // string is useless if useless in all files
191    else if ($useless && !$ok)
192    {
193      $string_data['stat'] = 'useless';
194      $counts['useless']++;
195    }
196    // else ok
197    else
198    {
199      // another very rare case
200      if ($useless)
201      {
202        $string_data['warnings'][] = l10n('This string is useless in some files');
203      }
204     
205      $string_data['stat'] = 'ok';
206      $counts['ok']++;
207    }
208  }
209  unset($string_data);
210 
211  // unused strings
212  $unused = array();
213  foreach ($language_files as $name => $path)
214  {
215    $unused = array_merge($unused, array_diff_key($lang_plugin[ $name ], $strings));
216  }
217 
218  foreach ($unused as $string => $translation)
219  {
220    $string_data = array(
221      'files' => array(),
222      'in_common' => array_key_exists($string, $lang_common),
223      'in_admin' => array_key_exists($string, $lang_admin),
224      'in_plugin' => array(),
225      'stat' => 'useless',
226      'is_admin' => false,
227      'warning' => array(l10n('This string is not used anywhere in the plugin')),
228      );
229     
230    foreach ($language_files as $name => $path)
231    {
232      if (array_key_exists($string, $lang_plugin[$name])) $string_data['in_plugin'][] = $name;
233    }
234   
235    $strings[ $string ] = $string_data;
236    $counts['useless']++;
237  }
238 
239  uksort($strings, 'strnatcasecmp'); // natural sort
240  $counts['total'] = array_sum($counts);
241 
242  $template->assign(array(
243    'PLA_STEP' => 'analysis',
244    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
245    'PLA_STRINGS' => $strings,
246    'PLA_LANG_FILES' => array_keys($language_files),
247    'PLA_COUNTS' => $counts,
248    'U_BACK' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;config',
249    'U_REFRESH' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
250    ));
251}
252
253// template vars
254$template->assign(array(
255  'PLA_PATH'=> PLA_PATH, 
256  'PLA_ABS_PATH'=> realpath(PLA_PATH).'/', 
257  'PLA_ADMIN' => PLA_ADMIN,
258  ));
259
260$template->set_filename('pla_content', realpath(PLA_PATH.'template/main.tpl'));
261$template->assign_var_from_handle('ADMIN_CONTENT', 'pla_content');
262
263?>
Note: See TracBrowser for help on using the repository browser.