source: extensions/plugin_lang_analysis/admin.php @ 23472

Last change on this file since 23472 was 23472, checked in by mistic100, 11 years ago

somes fixes and improvments

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