fix_and_group_lang_codes.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. define('N',"\n");
  16. define('SNAME',basename(__FILE__));
  17. define('CONFIGFP',__DIR__.'/../../conf/mustard.ini');
  18. $help='SYNOPSYS
  19. '.SNAME.' [options]
  20. DESCRIPTION
  21. This is a script to fix and group same languages lang codes in the
  22. «Languages» table.
  23. OPTIONS
  24. -h, --help
  25. Show this help text and exit.'.N;
  26. for ($i=1; $i<$argc; $i++) {
  27. if ($argv[$i]=='-h' || $argv[$i]=='--help') {
  28. mexit($help,0);
  29. } else {
  30. mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
  31. }
  32. }
  33. use function mysqli_real_escape_string as myesc;
  34. $iniarr=@parse_ini_file(CONFIGFP)
  35. or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
  36. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  37. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
  38. // for php versions < 8
  39. if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
  40. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  41. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); }
  42. // for php versions < 8
  43. if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true);
  44. $langs=[];
  45. $wlangs=[];
  46. $res=myq($link,'SELECT * FROM Languages');
  47. while ($row=mysqli_fetch_assoc($res)) {
  48. $langs[]=$row;
  49. if (locale_canonicalize($row['Code'])!=$row['Code'])
  50. $wlangs[]=$row;
  51. }
  52. print_r($langs);
  53. print_r($wlangs);
  54. foreach ($wlangs as $wlang) {
  55. $oklang=getoklangid($wlang['Code'],$langs);
  56. if ($oklang!==false) {
  57. echo('Substituting all records with LangID = '.$wlang['ID'].' ('.$wlang['Code'].') with LangID = '.$oklang['ID'].' ('.$oklang['Code'].') in tables InstLangs and InstOurLangs.'.N);
  58. myq($link,'UPDATE InstLangs SET LangID='.$oklang['ID'].' WHERE LangID='.$wlang['ID']);
  59. myq($link,'UPDATE InstOurLangs SET OurLangID='.$oklang['ID'].' WHERE OurLangID='.$wlang['ID']);
  60. myq($link,'DELETE FROM Languages WHERE ID='.$wlang['ID']);
  61. } else {
  62. echo('Languages.ID = '.$wlang['ID'].N);
  63. myq($link,'UPDATE InstLangs SET LangID=112 WHERE LangID='.$wlang['ID']);
  64. myq($link,'UPDATE InstOurLangs SET OurLangID=112 WHERE OurLangID='.$wlang['ID']);
  65. myq($link,'DELETE FROM Languages WHERE ID='.$wlang['ID']);
  66. }
  67. }
  68. mysqli_close($link);
  69. exit(0);
  70. // functions
  71. function getoklangid($code,&$langs) {
  72. $cancode=locale_canonicalize($code);
  73. echo('Canonicalized form for «'.$code.'» is «'.$cancode.'».'.N);
  74. $oklang=null;
  75. foreach ($langs as $lang)
  76. if ($lang['Code']==$cancode) {
  77. $oklang=$lang;
  78. break;
  79. }
  80. if (is_null($oklang)) {
  81. echo('[!!!] Could not find a Languages record with Code = '.$cancode.'.'.N);
  82. return(false);
  83. } else {
  84. return($oklang);
  85. }
  86. }
  87. function myq(&$l,$q) {
  88. try {
  89. $res=mysqli_query($l,$q);
  90. }
  91. catch (Exception $e) {
  92. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  93. exit(3);
  94. }
  95. if ($res===false) {
  96. echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
  97. exit(3);
  98. }
  99. return($res);
  100. }
  101. function mexit($msg,$code) {
  102. global $link;
  103. if (isset($link) && $link!==false) mysqli_close($link);
  104. if ($code>0)
  105. fwrite(STDERR,$msg);
  106. else
  107. echo($msg);
  108. exit($code);
  109. }
  110. function utstd($val) {
  111. if (is_null($val)) return(null);
  112. $val=round($val);
  113. return(date('Y-m-d H:i:s',$val));
  114. }
  115. function pr($val) {
  116. if (is_null($val)) return('NULL');
  117. return($val);
  118. }
  119. function eecho($msg) {
  120. echo($msg);
  121. }
  122. ?>