source: extensions/plugin_lang_analysis/admin.php @ 23467

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

allow multiple language files in a plugin

File size: 6.2 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/* PLUGINS LIST */
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/* FILES LIST */
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' => $language_files,
68    'F_ACTION' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'].'&amp;analyze',
69    'U_BACK' => PLA_ADMIN,
70    ));
71}
72else
73{
74  // save
75  if (isset($_POST['files']))
76  {
77    $files = array();
78    foreach ($_POST['files'] as $file => $data)
79    {
80      $files[$file] = array(
81        'path' => $file,
82        'is_admin' => $data['is_admin']=='true',
83        'lang_files' => array(),
84        );
85      if (!empty($data['lang_files']))
86      {
87        $files[$file]['lang_files'] = array_keys(array_filter($data['lang_files'], create_function('$f', 'return $f=="true";')));
88      }
89    }
90   
91    $content = "<?php\nreturn ";
92    $content.= var_export($files, true);
93    $content.= ";\n?>";
94   
95    @mkdir(PLA_PATH.'_data/', true, 0755);
96    file_put_contents(PLA_PATH.'_data/'.$_GET['plugin_id'].'.php', $content);
97  }
98  else
99  {
100    $files = include(PLA_PATH.'_data/'.$_GET['plugin_id'].'.php');
101  }
102 
103  $strings = array();
104  $counts = array('ok'=>0,'missing'=>0,'useless'=>0);
105 
106  // get strings list
107  foreach ($files as $file => $file_data)
108  {
109    $file_strings = analyze_file($_GET['plugin_id'].$file);
110   
111    foreach ($file_strings as $string => $lines)
112    {
113      $strings[ $string ]['files'][ $file ] = $file_data + array('lines' => $lines);
114    }
115  }
116 
117  // load language files
118  $lang_common = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/common.lang.php');
119  $lang_admin = load_language_file(PHPWG_ROOT_PATH.'language/en_UK/admin.lang.php');
120 
121  $language_files = list_plugin_languages_files($_GET['plugin_id']);
122  foreach ($language_files as $name => $path)
123  {
124    $lang_plugin[ $name ] = load_language_file(PHPWG_PLUGINS_PATH.$_GET['plugin_id'].$path);
125  }
126 
127  // analyze
128  foreach ($strings as $string => &$string_data)
129  {
130    // find where teh string is defined
131    $string_data['in_common'] = array_key_exists($string, $lang_common);
132    $string_data['in_admin'] = array_key_exists($string, $lang_admin);
133    $string_data['in_plugin'] = array();
134    foreach ($language_files as $name => $path)
135    {
136      if (array_key_exists($string, $lang_plugin[$name])) $string_data['in_plugin'][] = $name;
137    }
138   
139    $missing = $useless = $ok = false;
140    $string_data['is_admin'] = true;
141   
142    // analyze for each file where the string exists
143    foreach ($string_data['files'] as $file => &$file_data)
144    {
145      // the string is "admin" if all files are "admin"
146      $string_data['is_admin'] &= $file_data['is_admin'];
147     
148      // find if the string is translated in one of the language files included in this file
149      $exists = false;
150      foreach ($file_data['lang_files'] as $lang_file)
151      {
152        if (in_array($lang_file, $string_data['in_plugin']))
153        {
154          $exists = true;
155          break;
156        }
157      }
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      $string_data['stat'] = 'ok';
196      $counts['ok']++;
197    }
198  }
199  unset($string_data);
200 
201  uksort($strings, 'strnatcasecmp');
202  $counts['total'] = array_sum($counts);
203 
204  $template->assign(array(
205    'PLA_STEP' => 'analysis',
206    'PLA_PLUGIN' => $plugins->fs_plugins[ $_GET['plugin_id'] ],
207    'PLA_STRINGS' => $strings,
208    'PLA_LANG_FILES' => $language_files,
209    'PLA_COUNTS' => $counts,
210    'U_BACK' => PLA_ADMIN.'&amp;plugin_id='.$_GET['plugin_id'],
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');
223
224?>
Note: See TracBrowser for help on using the repository browser.