source: extensions/plugin_lang_analysis/admin.php @ 27153

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

allow to ignore a file

File size: 7.5 KB
RevLine 
[23421]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');
[23472]9include_once(PHPWG_ROOT_PATH . 'admin/include/plugins.class.php');
[23467]10$plugins = new plugins();
[23421]11
[23472]12/* SELECT */
[23421]13if (!isset($_GET['plugin_id']))
14{
15  $template->assign(array(
16    'PLA_STEP' => 'select',
[23467]17    'PLA_PLUGINS' => $plugins->fs_plugins,
[25677]18    'F_ACTION' => PLA_ADMIN.'&amp;config',
[23421]19    ));
20}
21
[23472]22/* CONFIG */
[25677]23else if (isset($_GET['config']))
[23421]24{
25  $files = list_plugin_files($_GET['plugin_id']);
[23467]26  $language_files = list_plugin_languages_files($_GET['plugin_id']);
[23421]27 
[23467]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 
[23473]36  if (file_exists(PLA_DATA.$_GET['plugin_id'].'.php'))
[23421]37  {
[23473]38    $saved_files = include(PLA_DATA.$_GET['plugin_id'].'.php');
[23421]39  }
40  else
41  {
42    $saved_files = array();
43  }
44 
45  foreach ($files as &$file)
46  {
[23472]47    if (isset($saved_files[ $file ]))
[23421]48    {
[23472]49      $file = $saved_files[ $file ];
[23467]50      $file['lang_files'] = array_intersect($file['lang_files'], array_keys($language_files));
[23421]51    }
52    else
53    {
54      $file = array(
55        'path' => $file,
[26059]56        'is_admin' => strpos($file, '/admin') === 0 || strpos($file, 'admin.tpl') !== false,
[26607]57        'ignore' => false,
58        'lang_files' => $default_lang_files,
[23421]59        );
60    }
61  }
62  unset($file);
63 
64  $template->assign(array(
65    'PLA_STEP' => 'config',
[23467]66    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
[23421]67    'PLA_FILES' => $files,
[23472]68    'PLA_LANG_FILES' => array_keys($language_files),
[23421]69    'F_ACTION' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
70    'U_BACK' => PLA_ADMIN,
71    ));
72}
[23472]73
74/* ANALYSIS */
[25677]75else if (isset($_GET['analyze']))
[23421]76{
77  // save
78  if (isset($_POST['files']))
79  {
80    $files = array();
[23467]81    foreach ($_POST['files'] as $file => $data)
[23421]82    {
[23472]83      $files[ $file ] = array(
[23421]84        'path' => $file,
[23467]85        'is_admin' => $data['is_admin']=='true',
[26607]86        'ignore' => $data['ignore']=='true',
[23467]87        'lang_files' => array(),
[23421]88        );
[23467]89      if (!empty($data['lang_files']))
90      {
[23472]91        $files[ $file ]['lang_files'] = array_keys(array_filter($data['lang_files'], create_function('$f', 'return $f=="true";')));
[23467]92      }
[23421]93    }
94   
95    $content = "<?php\nreturn ";
[23467]96    $content.= var_export($files, true);
[23421]97    $content.= ";\n?>";
98   
[23473]99    @mkdir(PLA_DATA, true, 0755);
100    file_put_contents(PLA_DATA.$_GET['plugin_id'].'.php', $content);
[23421]101  }
102  else
103  {
[23473]104    $files = include(PLA_DATA.$_GET['plugin_id'].'.php');
[23421]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  {
[26607]113    if ($file_data['ignore']) continue;
114
[23421]115    $file_strings = analyze_file($_GET['plugin_id'].$file);
116   
117    foreach ($file_strings as $string => $lines)
118    {
[23467]119      $strings[ $string ]['files'][ $file ] = $file_data + array('lines' => $lines);
[23421]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 
[23467]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 
[23472]133  // analyse
[23421]134  foreach ($strings as $string => &$string_data)
135  {
[23472]136    // find where the string is defined
[23421]137    $string_data['in_common'] = array_key_exists($string, $lang_common);
138    $string_data['in_admin'] = array_key_exists($string, $lang_admin);
[23467]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    }
[23421]144   
[23472]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   
[23467]151    $missing = $useless = $ok = false;
152    $string_data['is_admin'] = true;
153   
[23472]154    // analyse for each file where the string exists
[23467]155    foreach ($string_data['files'] as $file => &$file_data)
[23421]156    {
[23467]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
[23472]161      $exists = count(array_intersect($file_data['lang_files'], $string_data['in_plugin'])) > 0;
[23467]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      }
[23421]181    }
[23467]182    unset($file_data);
183   
184    // string is missing if at least missing in one file
185    if ($missing)
[23421]186    {
187      $string_data['stat'] = 'missing';
188      $counts['missing']++;
189    }
[23467]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
[23421]197    else
198    {
[23472]199      // another very rare case
200      if ($useless)
201      {
202        $string_data['warnings'][] = l10n('This string is useless in some files');
203      }
204     
[23421]205      $string_data['stat'] = 'ok';
206      $counts['ok']++;
207    }
208  }
209  unset($string_data);
210 
[23488]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;
[23584]236    $counts['useless']++;
[23488]237  }
238 
[23472]239  uksort($strings, 'strnatcasecmp'); // natural sort
[23421]240  $counts['total'] = array_sum($counts);
241 
242  $template->assign(array(
243    'PLA_STEP' => 'analysis',
[23467]244    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
[23421]245    'PLA_STRINGS' => $strings,
[23472]246    'PLA_LANG_FILES' => array_keys($language_files),
[23421]247    'PLA_COUNTS' => $counts,
[25677]248    'U_BACK' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;config',
[26059]249    'U_REFRESH' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
[23421]250    ));
251}
252
253// template vars
254$template->assign(array(
255  'PLA_PATH'=> PLA_PATH, 
[23472]256  'PLA_ABS_PATH'=> realpath(PLA_PATH).'/', 
[23421]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.