MastodonHelp/web/clitools/addlang.php

125 lines
4.1 KiB
PHP
Raw Normal View History

2020-05-08 18:03:11 +02:00
#!/usr/bin/php
2020-04-21 12:35:53 +02:00
<?php
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-11-23 18:44:36 +01:00
const N="\n";
if (!isset($argv[1]) || $argv[1]=='--help' || $argv[1]=='-h') {
echo('[[[ DESCRIPTION ]]]'.N.' Adds a language to the «Languages» table.'.N.'[[[ SYNTAX ]]]'.N.' addlang.php <language code>'.N);
exit(0);
}
$newlangcode=strtolower($argv[1]);
$newlang=strtoupper($newlangcode);
$test=locale_get_display_name($newlangcode,'en');
if ($test==$newlangcode) {
echo('Sorry, «'.$newlangcode.'» is an unknown language code.'.N);
exit(1);
} else {
echo('Adding language «'.$newlangcode.'» («'.$test.'»)...'.N);
}
2020-04-21 12:35:53 +02:00
$email='pezcurrel@tiscali.it';
2023-12-26 11:17:54 +01:00
require __DIR__.'/../lib/mb_ucfirst.php';
2020-04-21 12:35:53 +02:00
use function mysqli_real_escape_string as myesc;
2020-10-18 06:53:27 +02:00
$inifp=__DIR__.'/../conf/mustard.ini';
2020-04-21 12:35:53 +02:00
$iniarr=@parse_ini_file($inifp)
or mexit('Impossibile aprire il file di configurazione «'.$inifp.'»'.N,1);
$link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket'])
or mexit('Impossibile connettersi al server MySQL: '.mysqli_connect_error().N,1);
mysqli_set_charset($link,'utf8mb4')
or mexit(mysqli_error($link).N,1);
2022-11-23 18:44:36 +01:00
$que='ALTER TABLE `Languages` ADD `Name'.$newlang.'` VARCHAR(64) NULL DEFAULT NULL AFTER `NameOrig`';
echo($que.N);
mysqli_query($link,$que)
or mexit(mysqli_error($link).N,2);
2020-04-21 12:35:53 +02:00
$res=mysqli_query($link,'SELECT ID, Code FROM Languages')
or mexit(mysqli_error($link).N,2);
while ($row=mysqli_fetch_assoc($res)) {
$que='UPDATE Languages SET Name'.$newlang.'=\''.myesc($link,mb_ucfirst(locale_get_display_name($row['Code'],$newlangcode))).'\' WHERE ID='.$row['ID'];
echo($que.N);
mysqli_query($link,$que)
2022-11-23 18:44:36 +01:00
or mexit(mysqli_error($link).N,2);
2020-04-21 12:35:53 +02:00
}
2022-02-12 10:02:07 +01:00
/*$contextopts=array(
2020-04-21 12:35:53 +02:00
'http'=>array(
'timeout'=>5
),
'socket'=>array(
'tcp_nodelay'=>true
)
);
$context=stream_context_create($contextopts);
$addrkeys=array('neighbourhood','borough','suburb','city','municipality','county','district','province','region','state','country');
$res=mysqli_query($link,'SELECT ID, OSMID, NameIT FROM Localities')
or mexit(mysqli_error($link).N,2);
while ($row=mysqli_fetch_assoc($res)) {
$good=false;
$url='https://nominatim.openstreetmap.org/lookup?osm_ids=R'.$row['OSMID'].'&format=json&accept-language='.$newlangcode.'&addressdetails=1&email='.$email;
$osmdil=@file_get_contents($url,false,$context);
if ($osmdil!==false) {
$osmdil=json_decode($osmdil,true);
if (array_key_exists(0,$osmdil) && array_key_exists('address',$osmdil[0])) {
//$osmdil[0]['address']=array(); // simula errore
$dispname=array();
foreach ($addrkeys as $addrkey)
if (array_key_exists($addrkey,$osmdil[0]['address']))
$dispname[]=$osmdil[0]['address'][$addrkey];
if (count($dispname)>0) {
$dispname=array_unique($dispname);
$dispname=implode(', ',$dispname);
$good=true;
} else {
echo($row['NameIT'].': No useful «address» data found in OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
}
} else {
echo($row['NameIT'].': No «address» found in OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
}
} else {
echo($row['NameIT'].': Couldnt fetch OpenStreetMap lookup data for lang «'.$newlangcode.'».'.N);
}
if ($good) {
$que='UPDATE Localities SET Name'.$newlang.'=\''.myesc($link,$dispname).'\' WHERE ID='.$row['ID'];
echo($que.N);
mysqli_query($link,$que)
or mexit(mysqli_error($link).N,3);
}
2022-02-12 10:02:07 +01:00
}*/
2020-04-21 12:35:53 +02:00
mysqli_close($link);
2022-11-23 18:44:36 +01:00
echo('done :-)'.N);
2020-04-21 12:35:53 +02:00
exit(0);
function mexit($msg,$rv) {
global $link;
if ($link)
mysqli_close($link);
echo($msg);
exit($rv);
}
?>