source: extensions/plugin_lang_analysis/admin.php @ 31232

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

improve display of files list

File size: 6.4 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  $default_lang_files = get_loaded_in_main($_GET['plugin_id']);
28 
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  global $language_files, $default_lang_files;
46  populate_plugin_files($files, $saved_files);
47
48  $template->assign(array(
49    'PLA_STEP' => 'config',
50    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
51    'PLA_FILES' => $files,
52    'PLA_LANG_FILES' => array_keys($language_files),
53    'F_ACTION' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
54    'U_BACK' => PLA_ADMIN,
55    ));
56}
57
58/* ANALYSIS */
59else if (isset($_GET['analyze']))
60{
61  // save
62  if (isset($_POST['files']))
63  {
64    $files = $_POST['files'];
65    clean_files_from_config($files);
66   
67    $content = "<?php\nreturn ";
68    $content.= var_export($files, true);
69    $content.= ";\n";
70   
71    @mkdir(PLA_DATA, true, 0755);
72    file_put_contents(PLA_DATA.$_GET['plugin_id'].'.php', $content);
73  }
74  else
75  {
76    $files = include(PLA_DATA.$_GET['plugin_id'].'.php');
77  }
78 
79  $counts = array('ok'=>0,'missing'=>0,'useless'=>0);
80 
81  // get strings list
82  $strings = analyze_files($_GET['plugin_id'], $files);
83 
84  // load language files
85  $lang_common = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/common.lang.php');
86  $lang_admin = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/admin.lang.php');
87 
88  $language_files = list_plugin_languages_files($_GET['plugin_id']);
89  foreach ($language_files as $name => $path)
90  {
91    $lang_plugin[ $name ] = load_language_file(PHPWG_PLUGINS_PATH.$_GET['plugin_id'].$path);
92  }
93 
94  // analyse
95  foreach ($strings as $string => &$string_data)
96  {
97    // find where the string is defined
98    $string_data['in_common'] = array_key_exists($string, $lang_common);
99    $string_data['in_admin'] = array_key_exists($string, $lang_admin);
100    $string_data['in_plugin'] = array();
101    foreach ($language_files as $name => $path)
102    {
103      if (array_key_exists($string, $lang_plugin[$name])) $string_data['in_plugin'][] = $name;
104    }
105   
106    // very rare case
107    if (count($string_data['in_plugin'])>1)
108    {
109      $string_data['warnings'][] = l10n('This string is translated in multiple files');
110    }
111   
112    $missing = $useless = $ok = false;
113    $string_data['is_admin'] = true;
114   
115    // analyse for each file where the string exists
116    foreach ($string_data['files'] as $file => &$file_data)
117    {
118      // the string is "admin" if all files are "admin"
119      $string_data['is_admin'] &= $file_data['is_admin'];
120     
121      // find if the string is translated in one of the language files included in this file
122      $exists = count(array_intersect($file_data['lang_files'], $string_data['in_plugin'])) > 0;
123     
124      // useless if translated in the plugin AND in common or admin
125      if ($exists && ($string_data['in_common'] || ($file_data['is_admin'] && $string_data['in_admin'])))
126      {
127        $file_data['stat'] = 'useless';
128        $useless = true;
129      }
130      // missing if not translated in the plugin NOR in common or admin
131      else if (!$exists && !$string_data['in_common'] && (!$file_data['is_admin'] || !$string_data['in_admin']))
132      {
133        $file_data['stat'] = 'missing';
134        $missing = true;
135      }
136      // else ok
137      else
138      {
139        $file_data['stat'] = 'ok';
140        $ok = true;
141      }
142    }
143    unset($file_data);
144   
145    // string is missing if at least missing in one file
146    if ($missing)
147    {
148      $string_data['stat'] = 'missing';
149      $counts['missing']++;
150    }
151    // string is useless if useless in all files
152    else if ($useless && !$ok)
153    {
154      $string_data['stat'] = 'useless';
155      $counts['useless']++;
156    }
157    // else ok
158    else
159    {
160      // another very rare case
161      if ($useless)
162      {
163        $string_data['warnings'][] = l10n('This string is useless in some files');
164      }
165     
166      $string_data['stat'] = 'ok';
167      $counts['ok']++;
168    }
169  }
170  unset($string_data);
171 
172  // unused strings
173  $unused = array();
174  foreach ($language_files as $name => $path)
175  {
176    $unused = array_merge($unused, array_diff_key($lang_plugin[ $name ], $strings));
177  }
178 
179  foreach ($unused as $string => $translation)
180  {
181    $string_data = array(
182      'files' => array(),
183      'in_common' => array_key_exists($string, $lang_common),
184      'in_admin' => array_key_exists($string, $lang_admin),
185      'in_plugin' => array(),
186      'stat' => 'useless',
187      'is_admin' => false,
188      'warning' => array(l10n('This string is not used anywhere in the plugin')),
189      );
190     
191    foreach ($language_files as $name => $path)
192    {
193      if (array_key_exists($string, $lang_plugin[$name])) $string_data['in_plugin'][] = $name;
194    }
195   
196    $strings[ $string ] = $string_data;
197    $counts['useless']++;
198  }
199 
200  uksort($strings, 'strnatcasecmp'); // natural sort
201  $counts['total'] = array_sum($counts);
202 
203  $template->assign(array(
204    'PLA_STEP' => 'analysis',
205    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
206    'PLA_STRINGS' => $strings,
207    'PLA_LANG_FILES' => array_keys($language_files),
208    'PLA_COUNTS' => $counts,
209    'U_BACK' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;config',
210    'U_REFRESH' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
211    ));
212}
213
214// template vars
215$template->assign(array(
216  'PLA_PATH'=> PLA_PATH, 
217  'PLA_ABS_PATH'=> realpath(PLA_PATH).'/', 
218  'PLA_ADMIN' => PLA_ADMIN,
219  ));
220
221$template->set_filename('pla_content', realpath(PLA_PATH.'template/main.tpl'));
222$template->assign_var_from_handle('ADMIN_CONTENT', 'pla_content');
Note: See TracBrowser for help on using the repository browser.