/tools/lang2db.php

https://github.com/dbernar1/Project-Pier · PHP · 108 lines · 90 code · 16 blank · 2 comment · 24 complexity · 348d3273333f51c819c0733a691c5181 MD5 · raw file

  1. <?php
  2. $counter = 0;
  3. function walk($path, $filter) {
  4. //echo 'walking '.$path."\n";
  5. if (is_dir($path)) {
  6. walk_dir($path, $filter);
  7. }
  8. if(is_file($path)) {
  9. walk_file($path, $filter);
  10. }
  11. }
  12. function walk_dir($path, $filter) {
  13. if (preg_match('/\/language\/(.*)$/', $path, $matches)) {
  14. if (strpos($path, $filter)===false) return;
  15. $locale = $matches[1];
  16. //$locale = 'is_is';
  17. add2database($locale, $path);
  18. } else {
  19. if ($dh = opendir($path)) {
  20. while (($file = readdir($dh)) !== false) {
  21. if($file != '.' && $file != '..') {
  22. walk($path.'/'.$file, $filter);
  23. }
  24. }
  25. closedir($dh);
  26. }
  27. }
  28. }
  29. function walk_file($path, $filter) {
  30. }
  31. function add2database($locale, $path) {
  32. global $counter;
  33. echo "$path\n";
  34. $localefile = $locale . '.php';
  35. $parts = explode('_', $locale);
  36. $lc = $parts[0];
  37. $cc = $parts[1];
  38. if ($dh = opendir($path)) {
  39. $sql = "insert into `".DB_PREFIX."i18n_locales` (`name`, `description`, `country_code`, `language_code`) values( '$locale', 'Imported $path', '$cc', '$lc' ) ON DUPLICATE KEY UPDATE `id`=`id`;";
  40. mysql_query($sql);
  41. $sql = "select id from `".DB_PREFIX."i18n_locales` where `country_code` = '$cc' and `language_code` = '$lc';";
  42. $result = mysql_query($sql);
  43. $row = mysql_fetch_assoc($result);
  44. $locale_id = $row['id'];
  45. while (($file = readdir($dh)) !== false) {
  46. if ($file != '.' && $file != '..' && $file != $localefile) {
  47. $category=$file;
  48. $i=strrpos($file, '.');
  49. if ($i!==false) {
  50. $category=substr($file,0,$i);
  51. }
  52. $sql = "insert into `".DB_PREFIX."i18n_categories` (`name`, `description`) values( '$category', 'Imported $path/$file' ) ON DUPLICATE KEY UPDATE `id`=`id`;";
  53. mysql_query($sql);
  54. $sql = "select id from `".DB_PREFIX."i18n_categories` where `name` = '$category';";
  55. $result = mysql_query($sql);
  56. $row = mysql_fetch_assoc($result);
  57. $category_id = $row['id'];
  58. $filepath="$path/$file";
  59. $items = include $filepath;
  60. foreach($items as $k => $v) {
  61. $counter++;
  62. $sql = "insert into `".DB_PREFIX."i18n_values` (`locale_id`, `category_id`, `name`, `description`) values( '$locale_id', '$category_id', '$k', '".mysql_real_escape_string($v)."');";
  63. mysql_query($sql);
  64. $e = mysql_error();
  65. echo "$counter : $e : $sql\n";
  66. }
  67. }
  68. }
  69. closedir($dh);
  70. }
  71. }
  72. if(file_exists('../config/config.php')) {
  73. require_once '../config/config.php';
  74. } else {
  75. die('Failed to include config file.');
  76. } // if
  77. $log_data = file_exists('lang2db.log') ? file_get_contents('lang2db.log') : "=== lang2db ===";
  78. $link = @mysql_connect(DB_HOST, DB_USER, DB_PASS);
  79. if($link) {
  80. if(mysql_select_db(DB_NAME, $link)) {
  81. mysql_query("set names 'utf8'");
  82. echo '<html><head>';
  83. echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />';
  84. echo '</head><body>';
  85. echo '<xmp>';
  86. walk('../language', 'en_us');
  87. walk('../application/plugins', 'en_us');
  88. echo '</xmp>';
  89. echo '</body></html>';
  90. }
  91. }
  92. ?>