Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
compile_locale.php
1 <?php
28 ini_set('memory_limit', -1);
29 error_reporting(E_ALL);
30 $SYSTEM_ROOT = '';
31 $exs = Array();
32 
33 
34 // from cmd line
35 $cli = TRUE;
36 
37 if ((php_sapi_name() == 'cli')) {
38  if (isset($_SERVER['argv'][1])) {
39  $SYSTEM_ROOT = $_SERVER['argv'][1];
40  }
41  $err_msg = "ERROR: You need to supply the path to the System Root as the first argument\n";
42 
43 } else {
44  $err_msg = '
45  <div style="background-color: red; color: white; font-weight: bold;">
46  You can only run the '.$_SERVER['argv'][0].' script from the command line
47  </div>
48  ';
49 }
50 
51 if (empty($SYSTEM_ROOT)) {
52  $err_msg .= "Usage: php install/step_03.php <PATH_TO_MATRIX>\n";
53  echo $err_msg;
54  exit();
55 }
56 
57 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
58  $err_msg = "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
59  $err_msg .= "Usage: php install/step_03.php <PATH_TO_MATRIX>\n";
60  echo $err_msg;
61  exit();
62 }
63 
64 // only use console stuff if we're running from the command line
65 if ($cli) {
66  require_once 'Console/Getopt.php';
67 
68  $shortopt = '';
69  $longopt = Array('locale=');
70 
71  $con = new Console_Getopt;
72  $args = $con->readPHPArgv();
73  array_shift($args); // remove the system root
74  $options = $con->getopt($args, $shortopt, $longopt);
75 
76  if (is_array($options[0])) {
77  $locale_list = get_console_list($options[0]);
78  }
79 
80 }
81 
82 if (empty($locale_list)) {
83  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";
84  sleep(2);
85 }
86 
87 // dont set SQ_INSTALL flag before this include because we want
88 // a complete load now that the database has been created
89 
90 define('SQ_SYSTEM_ROOT', $SYSTEM_ROOT);
91 require_once $SYSTEM_ROOT.'/core/include/init.inc';
92 
93 // Clean up any remembered data.
94 require_once $SYSTEM_ROOT.'/core/include/deja_vu.inc';
95 $deja_vu = new Deja_Vu();
96 if ($deja_vu->enabled()) $deja_vu->forgetAll();
97 
98 // get the list of functions used during install
99 
100 require_once $SYSTEM_ROOT.'/install/install.inc';
101 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
102 
103 // firstly let's check that we are OK for the version
104 if (version_compare(PHP_VERSION, SQ_REQUIRED_PHP_VERSION, '<')) {
105  trigger_error('<i>'.SQ_SYSTEM_LONG_NAME.'</i> requires PHP Version '.SQ_REQUIRED_PHP_VERSION.'.<br/> You may need to upgrade.<br/> Your current version is '.PHP_VERSION, E_USER_ERROR);
106 }
107 
108 // let everyone know we are installing
109 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
110 
111 // regenerate the configs
112 if (!regenerate_configs()) {
113  trigger_error('Config Generation Failed', E_USER_ERROR);
114 }
115 
116 // list of languages where we need to compile these for
117 $string_locales = Array();
118 $error_locales = Array();
119 $message_locales = Array();
120 
121 // flag that controls when 'compiling edit interfaces' message is printed
122 $first_ei = TRUE;
123 
124 $asset_screen_dir = SQ_DATA_PATH.'/private/asset_types/asset/localised_screens';
125 create_directory($asset_screen_dir);
126 
127 // do it for each asset type ...
128 $asset_types = $GLOBALS['SQ_SYSTEM']->am->getAssetTypes();
129 
130 // ... but also, give the asset type array a little base asset injection
131 $base_asset = Array(
132  'type_code' => 'asset',
133  'dir' => 'core/include/asset_edit',
134  'name' => 'Base Asset',
135  );
136 array_unshift($asset_types, $base_asset);
137 
138 // also add top-level global strings - this will not appear on screen as there
139 // are no static screens for it (as it is not really an asset), but is important
140 // to catch languages which may only have strings in the top level
141 $global_strings = Array(
142  'type_code' => '',
143  'dir' => 'core',
144  'name' => 'Global Strings',
145  );
146 array_unshift($asset_types, $global_strings);
147 
148 $locale_names = array_keys($locale_list);
149 foreach ($locale_names as $locale) {
150  list($country,$lang,$variant) = $GLOBALS['SQ_SYSTEM']->lm->getLocaleParts($locale);
151  if (!in_array($country, $locale_names)) {
152  $locale_list[$country] = $locale_list[$locale];
153  }
154 
155  if (!empty($lang)) {
156  if (!in_array($country.'_'.$lang, $locale_names)) {
157  $locale_list[$country.'_'.$lang] = $locale_list[$locale];
158  }
159 
160  if (!empty($variant)) {
161  if (!in_array($country.'_'.$lang.'@'.$variant, $locale_names)) {
162  $locale_list[$country.'_'.$lang.'@'.$variant] = $locale_list[$locale];
163  }
164  }
165  }
166 }
167 
168 foreach ($asset_types as $asset_type) {
169 
170  $type_code = $asset_type['type_code'];
171 
172  $local_screen_dir = SQ_DATA_PATH.'/private/asset_types/'.$type_code.'/localised_screens';
173  $matches = Array();
174  $screens = Array();
175 
176  $base_path = SQ_SYSTEM_ROOT.'/'.$asset_type['dir'].'/locale';
177  $dirs_to_read = Array($base_path);
178 
179  while (!empty($dirs_to_read)) {
180  $dir_read = array_shift($dirs_to_read);
181 
182  $d = @opendir($dir_read);
183  if ($d) {
184 
185  // work out the locale name by taking the directory and replacing
186  // the slashes with the appropriate underscore and (possibly) at sign
187  $locale_name = str_replace($base_path.'/', '', $dir_read);
188  if (($slash_pos = strpos($locale_name, '/')) !== FALSE) {
189  $locale_name{$slash_pos} = '_';
190  if (($slash_pos = strpos($locale_name, '/')) !== FALSE) {
191  $locale_name{$slash_pos} = '@';
192  }
193  }
194 
195  while (FALSE !== ($entry = readdir($d))) {
196  if (($entry{0} == '.') || ($entry == 'CVS')) {
197  continue;
198  }
199 
200  if (is_dir($dir_read.'/'.$entry)) {
201  $dirs_to_read[] = $dir_read.'/'.$entry;
202  }
203 
204  if (preg_match('|^lang\_((static_)?screen\_.*)\.xml$|', $entry, $matches)) {
205  if (!empty($locale_list) && (!in_array($locale_name, array_keys($locale_list))
206  || (!in_array('all', $locale_list[$locale_name])
207  && !in_array('screens', $locale_list[$locale_name])))) {
208  continue;
209  }
210 
211  if (!isset($screens[$locale_name])) {
212  $screens[$locale_name] = Array();
213  }
214 
215  $screens[$locale_name][] = Array(
216  'dir' => $dir_read,
217  'screen' => $matches[1],
218  );
219  } else if (preg_match('|^lang\_strings\.xml$|', $entry, $matches)) {
220  list($country,$lang,$variant) = $GLOBALS['SQ_SYSTEM']->lm->getLocaleParts($locale_name);
221  if (!in_array($country, $string_locales)) {
222  $string_locales[] = $country;
223  }
224 
225  if (!empty($lang)) {
226  if (!in_array($country.'_'.$lang, $string_locales)) {
227  $string_locales[] = $country.'_'.$lang;
228  }
229 
230  if (!empty($variant)) {
231  if (!in_array($country.'_'.$lang.'@'.$variant, $string_locales)) {
232  $string_locales[] = $country.'_'.$lang.'@'.$variant;
233  }
234  }
235  }
236  } else if (preg_match('|^lang\_errors\.xml$|', $entry, $matches)) {
237  list($country,$lang,$variant) = $GLOBALS['SQ_SYSTEM']->lm->getLocaleParts($locale_name);
238  if (!in_array($country, $error_locales)) {
239  $error_locales[] = $country;
240  }
241 
242  if (!empty($lang)) {
243  if (!in_array($country.'_'.$lang, $error_locales)) {
244  $error_locales[] = $country.'_'.$lang;
245  }
246 
247  if (!empty($variant)) {
248  if (!in_array($country.'_'.$lang.'@'.$variant, $error_locales)) {
249  $error_locales[] = $country.'_'.$lang.'@'.$variant;
250  }
251  }
252  }
253  } else if (preg_match('|^lang\_messages\.xml$|', $entry, $matches)) {
254  list($country,$lang,$variant) = $GLOBALS['SQ_SYSTEM']->lm->getLocaleParts($locale_name);
255  if (!in_array($country, $message_locales)) {
256  $message_locales[] = $country;
257  }
258 
259  if (!empty($lang)) {
260  if (!in_array($country.'_'.$lang, $message_locales)) {
261  $message_locales[] = $country.'_'.$lang;
262  }
263 
264  if (!empty($variant)) {
265  if (!in_array($country.'_'.$lang.'@'.$variant, $message_locales)) {
266  $message_locales[] = $country.'_'.$lang.'@'.$variant;
267  }
268  }
269  }
270  }
271  }//end while
272 
273  closedir($d);
274 
275  }//end if
276  }//end while
277 
278  $all_screens = Array();
279  $d = @opendir(SQ_SYSTEM_ROOT.'/'.$asset_type['dir']);
280  if ($d) {
281 
282  while (FALSE !== ($entry = readdir($d))) {
283  if (preg_match('|^edit\_interface\_((static_)?screen\_.*).xml$|', $entry, $matches)) {
284  $all_screens[] = $matches[1];
285  }
286  }
287  closedir($d);
288 
289  }
290 
291  // if there are edit interface files AND there are screens that we are localising...
292  if (!empty($all_screens) && !empty($screens)) {
293  if ($first_ei) {
294  $first_ei = FALSE;
295  echo 'Compiling localised edit interfaces...'."\n";
296  }
297  echo $asset_type['type_code'].' ('.$asset_type['name'].')';
298  }
299 
300  if (!empty($screens)) {
301  foreach ($screens as $locale => $locale_screens) {
302 
303  foreach ($locale_screens as $screen_type) {
304 
305  if (!file_exists($local_screen_dir)) {
306  create_directory($local_screen_dir);
307  }
308 
309  $screen_xml = NULL;
310  if (strpos($screen_type['screen'], 'static_') === 0) {
311  $screen_xml = build_localised_static_screen($type_code, $screen_type['screen'], $locale);
312  } else {
313  $screen_xml = build_localised_screen($type_code, $screen_type['screen'], $locale);
314  }
315 
316  string_to_file($screen_xml->asXML(), $local_screen_dir.'/'.$screen_type['screen'].'.'.$locale);
317  echo '.';
318  }
319  }
320  }
321 
322  if (!empty($all_screens) && !empty($screens)) {
323  echo "\n";
324  }
325 
326 }//end foreach asset_type
327 
328 // compile the strings for each locale where a lang_strings.xml exists
329 foreach ($string_locales as $locale) {
330  if (!empty($locale_list) && (!in_array($locale, array_keys($locale_list))
331  || (!in_array('all', $locale_list[$locale])
332  && !in_array('strings', $locale_list[$locale])))) {
333  continue;
334  }
335 
336  echo 'Compiling strings for locale '.$locale."\n";
337  build_locale_string_file($locale);
338 }
339 
340 // then, compile errors for each locale (using lang_errors.xml)
341 foreach ($error_locales as $locale) {
342  if (!empty($locale_list) && (!in_array($locale, array_keys($locale_list))
343  || (!in_array('all', $locale_list[$locale])
344  && !in_array('errors', $locale_list[$locale])))) {
345  continue;
346  }
347 
348  echo 'Compiling localised errors for locale '.$locale."\n";
349  build_locale_error_file($locale);
350 }
351 
352 // finally, compile internal messages for each locale (using lang_messages.xml)
353 foreach ($message_locales as $locale) {
354 if (!empty($locale_list) && (!in_array($locale, array_keys($locale_list))
355  || (!in_array('all', $locale_list[$locale])
356  && !in_array('messages', $locale_list[$locale])))) {
357  continue;
358  }
359 
360  echo 'Compiling localised internal messages for locale '.$locale."\n";
361  build_locale_internal_messages_file($locale);
362 }
363 
364 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
365 
366 foreach ($exs as $str) {
367  print "$str\n";
368 }
369 
370 
381 function get_console_list($options)
382 {
383  $list = Array();
384 
385  foreach ($options as $option) {
386  // if nothing set, skip this entry
387  if (!isset($option[0]) || !isset($option[1])) {
388  continue;
389  }
390 
391  if ($option[0] != '--locale') continue;
392 
393  // Now process the list
394  $parts = explode('-', $option[1]);
395 
396  $types = Array();
397  if (count($parts) == 2 && strlen($parts[1])) {
398  $types = explode(',', $parts[1]);
399  } else {
400  $types = Array('all');
401  }
402 
403  $list[$parts[0]] = $types;
404  }
405 
406  return $list;
407 
408 }//end get_console_list()
409 
410 
411 ?>