Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
locale_backup.php
1 <?php
52 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
53 error_reporting(E_ALL);
54 $SYSTEM_ROOT = '';
55 $exs = Array();
56 
57 
58 // from cmd line
59 $cli = true;
60 
61 if ((php_sapi_name() == 'cli')) {
62  if (isset($_SERVER['argv'][1])) {
63  $SYSTEM_ROOT = $_SERVER['argv'][1];
64  }
65  $err_msg = "You need to supply the path to the System Root as the first argument\n";
66 
67 } else {
68  $err_msg = '
69  <div style="background-color: red; color: white; font-weight: bold;">
70  You can only run the '.$_SERVER['argv'][0].' script from the command line
71  </div>
72  ';
73 }
74 
75 if (empty($SYSTEM_ROOT)) {
76  echo $err_msg;
77  exit();
78 }
79 
80 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
81  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
82  exit();
83 }
84 
85 // only use console stuff if we're running from the command line
86 if ($cli) {
87  require_once 'Console/Getopt.php';
88 
89  $shortopt = '';
90  $longopt = Array('locale=', 'output=');
91 
92  $con = new Console_Getopt;
93  $args = $con->readPHPArgv();
94  array_shift($args); // remove the system root
95  $options = $con->getopt($args, $shortopt, $longopt);
96 
97  if (is_array($options[0])) {
98  $opt_list = get_console_list($options[0]);
99  $locale_list = $opt_list['locale'];
100  }
101 
102 }
103 
104 // if locale list empty, do everything
105 if (empty($locale_list)) {
106  echo "\nWARNING: You did not specify a --locale parameter. This is okay but be aware that all locales will be compiled, which may take a while if you have multiple locales on your system\n\n";
107 }
108 
109 require_once $SYSTEM_ROOT.'/core/include/init.inc';
110 
111 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
112 
113 // log in as root
114 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
115  echo "Failed login in as root user\n";
116  exit()
117 }
118 
119 // get the list of functions used during install
120 require_once $SYSTEM_ROOT.'/install/install.inc';
121 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
122 require_once 'XML/Tree.php';
123 
124 // list of languages where we need to compile these for
125 $string_locales = Array();
126 $error_locales = Array();
127 $message_locales = Array();
128 $all_screens = Array();
129 $screens = Array();
130 
131 // flag that controls when 'compiling edit interfaces' message is printed
132 $first_ei = true;
133 
134 if (!empty($opt_list['dir'])) {
135  $locale_backup_dir = $opt_list['dir'];
136 } else {
137  $locale_backup_dir = SQ_DATA_PATH.'/temp/locale_backup';
138 }
139 
140 create_directory($locale_backup_dir);
141 
142 // do it for each asset type ...
143 $asset_types = $GLOBALS['SQ_SYSTEM']->am->getAssetTypes();
144 
145 // ... but also, give the asset type array a little base asset injection
146 array_unshift($asset_types, Array(
147  'type_code' => 'asset',
148  'dir' => 'core/include/asset_edit',
149  'name' => 'Base Asset',
150  ));
151 
152 foreach($GLOBALS['SQ_SYSTEM']->getInstalledPackages() as $package) {
153  if ($package['code_name'] == '__core__') continue;
154  array_unshift($asset_types, Array(
155  'type_code' => '',
156  'dir' => 'packages/'.$package['code_name'],
157  'name' => $package['name'],
158  ));
159 }
160 
161 // also add top-level global strings - this will not appear on screen as there
162 // are no static screens for it (as it is not really an asset), but is important
163 // to catch languages which may only have strings in the top level
164 array_unshift($asset_types, Array(
165  'type_code' => '',
166  'dir' => 'core',
167  'name' => 'Global Strings',
168  ));
169 
170 $locale_names = array_keys($locale_list);
171 foreach($locale_names as $locale) {
172  $locale_parts = $GLOBALS['SQ_SYSTEM']->lm->getCumulativeLocaleParts($locale);
173  foreach($locale_parts as $locale_part) {
174  if (!in_array($locale_part, $locale_names)) {
175  $locale_list[$locale_part] = $locale_list[$locale];
176  }
177  }
178 }
179 
180 foreach ($asset_types as $asset_type) {
181 
182  $type_code = $asset_type['type_code'];
183 
184  $local_screen_dir = SQ_DATA_PATH.'/private/asset_types/'.$type_code.'/localised_screens';
185  $matches = Array();
186 
187  $base_path = SQ_SYSTEM_ROOT.'/'.$asset_type['dir'].'/locale';
188  $dirs_to_read = Array($base_path);
189 
190  while (!empty($dirs_to_read)) {
191  $dir_read = array_shift($dirs_to_read);
192 
193  $d = @opendir($dir_read);
194  if ($d) {
195 
196  // work out the locale name by taking the directory and replacing
197  // the slashes with the appropriate underscore and (possibly) at sign
198  $locale_name = str_replace($base_path.'/', '', $dir_read);
199  if (($slash_pos = strpos($locale_name, '/')) !== false) {
200  $locale_name{$slash_pos} = '_';
201  if (($slash_pos = strpos($locale_name, '/')) !== false) {
202  $locale_name{$slash_pos} = '@';
203  }
204  }
205 
206  while (false !== ($entry = readdir($d))) {
207  if (($entry == '..') || ($entry == '.') || ($entry == 'CVS')) continue;
208 
209  if (is_dir($dir_read.'/'.$entry)) {
210  if (!in_array($dir_read.'/'.$entry, $dirs_to_read)) {
211  $dirs_to_read[] = $dir_read.'/'.$entry;
212  }
213  }
214 
215  // get the locale parts
216  $locale_parts = $GLOBALS['SQ_SYSTEM']->lm->getCumulativeLocaleParts($locale_name);
217 
218  if (preg_match('|lang\_((static_)?screen\_.*)\.xml|', $entry, $matches)) {
219  // screen files
220  if (!empty($locale_list) && (!in_array($locale_name, array_keys($locale_list))
221  || (!in_array('all', $locale_list[$locale_name])
222  && !in_array('screens', $locale_list[$locale_name])))) {
223  continue;
224  }
225 
226  if (!isset($screens[$locale_name])) {
227  $screens[$locale_name] = Array();
228  }
229 
230  $screens[$locale_name][] = $dir_read.'/lang_'.$matches[1].'.xml';
231 
232  } else if (preg_match('|lang\_strings\.xml|', $entry, $matches)) {
233  // string files
234  foreach($locale_parts as $locale_part) {
235  if (!in_array($locale_part, $string_locales)) {
236  $string_locales[$locale_part][] = $base_path;
237  }
238  }
239 
240  } else if (preg_match('|lang\_errors\.xml|', $entry, $matches)) {
241  // error files
242  foreach($locale_parts as $locale_part) {
243  if (!in_array($locale_part, $error_locales)) {
244  $error_locales[$locale_part][] = $base_path;
245  }
246  }
247 
248  } else if (preg_match('|lang\_messages\.xml|', $entry, $matches)) {
249  // internal message files
250  foreach($locale_parts as $locale_part) {
251  if (!in_array($locale_part, $message_locales)) {
252  $message_locales[$locale_part][] = $base_path;
253  }
254  }
255  }
256  }
257  closedir($d);
258 
259  }
260  }
261 
262  //$all_screens = Array();
263  $d = @opendir(SQ_SYSTEM_ROOT.'/'.$asset_type['dir']);
264  if ($d) {
265 
266  while (false !== ($entry = readdir($d))) {
267  if (preg_match('|edit\_interface\_((static_)?screen\_.*).xml|', $entry, $matches)) {
268  $all_screens[] = $base_path.'/'.$entry;
269  }
270  }
271  closedir($d);
272 
273  }
274 
275 }
276 
277 
278 // compile string locales...
279 foreach($string_locales as $locale => $loc_files) {
280  $loc_files = array_unique($loc_files);
281  if (!empty($locale_list) && !in_array('strings', array_get_index($locale_list, $locale, Array())) &&
282  !in_array('all', array_get_index($locale_list, $locale, Array()))) {
283  continue;
284  }
285  $string_file = '<?xml version="1.0" ?>'."\n";
286  $string_file .= '<files locale="'.$locale.'">'."\n";
287 
288  foreach($loc_files as $loc_file) {
289  $string_file .= '<file source="'.substr(str_replace(SQ_SYSTEM_ROOT, '', $loc_file),1).'" name="lang_strings.xml">'."\n";
290  $string_file .= str_replace('<?xml version="1.0" ?>', '',
291  file_get_contents($loc_file.'/'.str_replace('_', '/', str_replace('@', '/', $locale)).'/lang_strings.xml')."\n");
292  $string_file .= '</file>'."\n";
293  }
294 
295  $string_file .= '</files>'."\n";
296 
297  create_directory($locale_backup_dir.'/'.$locale);
298  string_to_file($string_file, $locale_backup_dir.'/'.$locale.'/strings.xml');
299 }
300 
301 
302 // compile error locales...
303 $error_locales = array_unique($error_locales);
304 foreach($error_locales as $locale => $loc_files) {
305  $loc_files = array_unique($loc_files);
306  if (!empty($locale_list) && !in_array('errors', array_get_index($locale_list, $locale, Array())) &&
307  !in_array('all', array_get_index($locale_list, $locale, Array()))) {
308  continue;
309  }
310  $error_file = '<?xml version="1.0" ?>'."\n";
311  $error_file .= '<files locale="'.$locale.'">'."\n";
312 
313  foreach($loc_files as $loc_file) {
314  $error_file .= '<file source="'.substr(str_replace(SQ_SYSTEM_ROOT, '', $loc_file),1).'" name="lang_errors.xml">'."\n";
315  $error_file .= str_replace('<?xml version="1.0" ?>', '',
316  file_get_contents($loc_file.'/'.str_replace('_', '/', str_replace('@', '/', $locale)).'/lang_errors.xml')."\n");
317 
318  //$error_file .= process_errors_file($loc_file.'/'.str_replace('_', '/', str_replace('@', '/', $locale)).'/lang_errors.xml');
319  $error_file .= '</file>'."\n";
320  }
321 
322  $error_file .= '</files>'."\n";
323 
324  create_directory($locale_backup_dir.'/'.$locale);
325  string_to_file($error_file, $locale_backup_dir.'/'.$locale.'/errors.xml');
326 }
327 
328 
329 // compile internal message locales...
330 foreach($message_locales as $locale => $loc_files) {
331  $loc_files = array_unique($loc_files);
332  if (!empty($locale_list) && !in_array('messages', array_get_index($locale_list, $locale, Array())) &&
333  !in_array('all', array_get_index($locale_list, $locale, Array()))) {
334  continue;
335  }
336  $message_file = '<?xml version="1.0" ?>'."\n";
337  $message_file .= '<files locale="'.$locale.'">'."\n";
338 
339  foreach($loc_files as $loc_file) {
340  $message_file .= '<file source="'.substr(str_replace(SQ_SYSTEM_ROOT, '', $loc_file),1).'" name="lang_messages.xml">'."\n";
341  $message_file .= str_replace('<?xml version="1.0" ?>', '',
342  file_get_contents($loc_file.'/'.str_replace('_', '/', str_replace('@', '/', $locale)).'/lang_messages.xml')."\n");
343  $message_file .= '</file>'."\n";
344  }
345 
346  $message_file .= '</files>'."\n";
347 
348  create_directory($locale_backup_dir.'/'.$locale);
349  string_to_file($message_file, $locale_backup_dir.'/'.$locale.'/internal_messages.xml');
350 }
351 
352 // finally, localised screens...
353 foreach($screens as $locale => $loc_files) {
354  $loc_files = array_unique($loc_files);
355  if (!empty($locale_list) && !in_array('screens', array_get_index($locale_list, $locale, Array())) &&
356  !in_array('all', array_get_index($locale_list, $locale, Array()))) {
357  continue;
358  }
359  $message_file = '<?xml version="1.0" ?>'."\n";
360  $message_file .= '<files locale="'.$locale.'">'."\n";
361 
362  foreach($loc_files as $loc_file) {
363  $folder = substr($loc_file, 0, strpos($loc_file, '/locale/') + strlen('/locale'));
364  $file_name = substr($loc_file, strrpos($loc_file, '/') + 1);
365 
366  $message_file .= '<file source="'.substr(str_replace(SQ_SYSTEM_ROOT, '', $folder),1).'" name="'.$file_name.'">'."\n";
367  $message_file .= str_replace('<?xml version="1.0" ?>', '',
368  file_get_contents($loc_file)."\n");
369  $message_file .= '</file>'."\n";
370  }
371 
372  $message_file .= '</files>'."\n";
373 
374  create_directory($locale_backup_dir.'/'.$locale);
375  string_to_file($message_file, $locale_backup_dir.'/'.$locale.'/screens.xml');
376 }
377 
378 // End of script
379 exit(0);
380 
381 
382 
393 function get_console_list($options)
394 {
395  $list = Array('locale' => Array(), 'dir' => '');
396 
397  foreach ($options as $option) {
398  // if nothing set, skip this entry
399  if (!isset($option[0]) || !isset($option[1])) continue;
400 
401  switch($option[0]) {
402  case '--locale':
403  // Now process the list
404  $parts = explode('-', $option[1]);
405 
406  $types = Array();
407  if ((count($parts) == 2) && strlen($parts[1])) {
408  $types = explode(',', $parts[1]);
409  } else {
410  $types = Array('all');
411  }
412 
413  $list['locale'][$parts[0]] = $types;
414  break;
415 
416  case '--output':
417  $list['dir'] = $option[1];
418  break;
419  }
420 
421  }
422 
423  return $list;
424 
425 }//end get_console_list()
426 ?>