addlang.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. const N="\n";
  16. if (!isset($argv[1]) || $argv[1]=='--help' || $argv[1]=='-h') {
  17. echo('[[[ DESCRIPTION ]]]'.N.' Adds a language to the «Languages» table.'.N.'[[[ SYNTAX ]]]'.N.' addlang.php <language code>'.N);
  18. exit(0);
  19. }
  20. $newlangcode=strtolower($argv[1]);
  21. $newlang=strtoupper($newlangcode);
  22. $test=locale_get_display_name($newlangcode,'en');
  23. if ($test==$newlangcode) {
  24. echo('Sorry, «'.$newlangcode.'» is an unknown language code.'.N);
  25. exit(1);
  26. } else {
  27. echo('Adding language «'.$newlangcode.'» («'.$test.'»)...'.N);
  28. }
  29. $email='pezcurrel@tiscali.it';
  30. require __DIR__.'/../lib/mb_ucfirst.php';
  31. use function mysqli_real_escape_string as myesc;
  32. $inifp=__DIR__.'/../conf/mustard.ini';
  33. $iniarr=@parse_ini_file($inifp)
  34. or mexit('Impossibile aprire il file di configurazione «'.$inifp.'»'.N,1);
  35. $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket'])
  36. or mexit('Impossibile connettersi al server MySQL: '.mysqli_connect_error().N,1);
  37. mysqli_set_charset($link,'utf8mb4')
  38. or mexit(mysqli_error($link).N,1);
  39. $que='ALTER TABLE `Languages` ADD `Name'.$newlang.'` VARCHAR(64) NULL DEFAULT NULL AFTER `NameOrig`';
  40. echo($que.N);
  41. mysqli_query($link,$que)
  42. or mexit(mysqli_error($link).N,2);
  43. $res=mysqli_query($link,'SELECT ID, Code FROM Languages')
  44. or mexit(mysqli_error($link).N,2);
  45. while ($row=mysqli_fetch_assoc($res)) {
  46. $que='UPDATE Languages SET Name'.$newlang.'=\''.myesc($link,mb_ucfirst(locale_get_display_name($row['Code'],$newlangcode))).'\' WHERE ID='.$row['ID'];
  47. echo($que.N);
  48. mysqli_query($link,$que)
  49. or mexit(mysqli_error($link).N,2);
  50. }
  51. /*$contextopts=array(
  52. 'http'=>array(
  53. 'timeout'=>5
  54. ),
  55. 'socket'=>array(
  56. 'tcp_nodelay'=>true
  57. )
  58. );
  59. $context=stream_context_create($contextopts);
  60. $addrkeys=array('neighbourhood','borough','suburb','city','municipality','county','district','province','region','state','country');
  61. $res=mysqli_query($link,'SELECT ID, OSMID, NameIT FROM Localities')
  62. or mexit(mysqli_error($link).N,2);
  63. while ($row=mysqli_fetch_assoc($res)) {
  64. $good=false;
  65. $url='https://nominatim.openstreetmap.org/lookup?osm_ids=R'.$row['OSMID'].'&format=json&accept-language='.$newlangcode.'&addressdetails=1&email='.$email;
  66. $osmdil=@file_get_contents($url,false,$context);
  67. if ($osmdil!==false) {
  68. $osmdil=json_decode($osmdil,true);
  69. if (array_key_exists(0,$osmdil) && array_key_exists('address',$osmdil[0])) {
  70. //$osmdil[0]['address']=array(); // simula errore
  71. $dispname=array();
  72. foreach ($addrkeys as $addrkey)
  73. if (array_key_exists($addrkey,$osmdil[0]['address']))
  74. $dispname[]=$osmdil[0]['address'][$addrkey];
  75. if (count($dispname)>0) {
  76. $dispname=array_unique($dispname);
  77. $dispname=implode(', ',$dispname);
  78. $good=true;
  79. } else {
  80. echo($row['NameIT'].': No useful «address» data found in OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
  81. }
  82. } else {
  83. echo($row['NameIT'].': No «address» found in OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
  84. }
  85. } else {
  86. echo($row['NameIT'].': Couldn’t fetch OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
  87. }
  88. if ($good) {
  89. $que='UPDATE Localities SET Name'.$newlang.'=\''.myesc($link,$dispname).'\' WHERE ID='.$row['ID'];
  90. echo($que.N);
  91. mysqli_query($link,$que)
  92. or mexit(mysqli_error($link).N,3);
  93. }
  94. }*/
  95. mysqli_close($link);
  96. echo('done :-)'.N);
  97. exit(0);
  98. function mexit($msg,$rv) {
  99. global $link;
  100. if ($link)
  101. mysqli_close($link);
  102. echo($msg);
  103. exit($rv);
  104. }
  105. ?>