addlang.php 3.5 KB

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