Refactored language management

This commit is contained in:
pezcurrel 2022-12-28 18:34:57 +01:00
parent 01f9465625
commit e8d588c0f2

View file

@ -614,64 +614,50 @@ eecho(1,'query: «'.$query.'».'.N);
if (!$opts['dryrun']) myq($link,$query,__LINE__);
if (is_null($oidata)) {
(!$opts['dryrun']) ? $instid=mysqli_insert_id($link) : $instid=0;
notify('«<a href="viewinst.php?id='.$instid.'">'.$opts['hostname'].'</a>» is a NEW instance! :-)',1);
if ($instanswered) {
$instlangs=langs($instid,$opts['hostname'],false,__LINE__);
if (!$opts['dryrun'])
foreach ($instlangs as $row)
myq($link,'INSERT INTO InstLangs (InstID, LangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')',__LINE__);
$instourlangs=langs($instid,$opts['hostname'],true,__LINE__);
// if instourlangs is empty and instlangs is not, set instourlangs as instlangs
if (count($instourlangs)==0 && count($instlangs)>0)
$instourlangs=$instlangs;
if (!$opts['dryrun'])
foreach ($instourlangs as $row)
myq($link,'INSERT INTO InstOurLangs (InstID, OurLangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')',__LINE__);
}
} else {
$instid=$oidata['ID'];
if ($instanswered) {
$res=myq($link,'SELECT InstID, LangID, Pos, Code FROM InstLangs LEFT JOIN Languages ON Languages.ID=LangID WHERE InstID='.$instid.' ORDER BY Pos ASC',__LINE__);
$oldinstlangs=[];
while ($row=mysqli_fetch_assoc($res))
$oldinstlangs[]=$row;
$instlangs=langs($instid,$opts['hostname'],false,__LINE__);
if ($instlangs!=$oldinstlangs && !$opts['dryrun']) {
myq($link,'DELETE FROM InstLangs WHERE InstID='.$instid,__LINE__);
foreach ($instlangs as $row)
myq($link,'INSERT INTO InstLangs (InstID, LangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')',__LINE__);
}
if ($idata['OurLangsLock']==0) {
$instourlangs=langs($instid, $opts['hostname'], true, __LINE__);
// if instourlangs is empty and instlangs is not, set instourlangs as instlangs
if (count($instourlangs)==0 && count($instlangs)>0)
$instourlangs=$instlangs;
if (count($instourlangs)>0 && !$opts['dryrun']) {
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
foreach ($instourlangs as $row)
myq($link,'INSERT INTO InstOurLangs (InstID, OurLangID, Pos) VALUES ('.$row['InstID'].', '.$row['LangID'].', '.$row['Pos'].')',__LINE__);
}
}
}
}
// from here we know for sure $instid
if (!$opts['dryrun']) myq($link,'INSERT INTO InstChecks (InstID, Time, Status) VALUES ('.$instid.', '.$now.', '.$idata['WasLastCheckOk'].')',__LINE__);
// yes: we don't do anything on InstLangs and InstOurLangs otherwise
if ($instanswered && isset($idata['languages']) && is_array($idata['languages']) && count($idata['languages'])>0) {
eecho(1,'«'.$opts['hostname'].'»: declared languages: '.implode(', ',$idata['languages']).N);
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstLangs WHERE InstID='.$instid,__LINE__);
$pos=0;
foreach ($idata['languages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],__LINE__);
$pos++;
myq($link,'INSERT INTO InstLangs SET InstID='.$instid.', LangID='.$langid.', Pos='.$pos,__LINE__);
}
}
// we try to detect languages only if it's set to the default "en", otherwise it has been explicitly set by admin and we assume it's the mostly used language
if ($idata['languages'][0]=='en') {
$idata['ourlanguages']=get_instance_langs($opts['hostname']);
while (count($idata['ourlanguages'])>5)
array_pop($languages);
foreach ($idata['ourlanguages'] as $key=>$lang)
$idata['ourlanguages'][$key]=str_replace('-','_',$lang);
eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);//+++
} else {
$idata['ourlanguages']=$idata['languages'];
}
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
$pos=0;
foreach ($idata['ourlanguages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],__LINE__);
$pos++;
myq($link,'INSERT INTO InstOurLangs SET InstID='.$instid.', OurLangID='.$langid.', Pos='.$pos,__LINE__);
}
}
}
if (isset($idata['activity']) && is_array($idata['activity'])) {
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstActivity WHERE InstID='.$instid,__LINE__);
@ -1209,60 +1195,34 @@ function get_instance_langs($host) {
return $languages;
}
function langs($instid, $hostname, $auto, $line) {
global $idata, $link, $opts;
$retlangs=[];
$languages=[];
// even if $auto is true, set it to false (don't do autodection of languages based on last toots) if api/v1/instance returned a language different from the default "en": assume instead it is right, because it has been explicitly set
if (isset($idata['languages'][0]) && $idata['languages'][0]!='en')
$auto=false;
if ($auto) {
$languages=get_instance_langs($hostname);
} elseif (isset($idata['languages']) && is_array($idata['languages'])) {
$languages=$idata['languages'];
}
if (count($languages)==0) {
return($retlangs);
} else {
while (count($languages)>5)
array_pop($languages);
foreach ($languages as $key=>$val)
$languages[$key]=str_replace('-','_',$val);
if ($auto)
eecho(1,'«'.$hostname.'»: detected languages: '.implode(', ',$languages).N);
else
eecho(1,'«'.$hostname.'»: declared languages: '.implode(', ',$languages).N);
$pos=0;
foreach($languages as $lang) {
$res=myq($link,'SELECT * FROM Languages WHERE Code=\''.myesc($link,$lang).'\'',$line);
if (mysqli_num_rows($res)<1) {
$code=myesc($link,truncs($lang,'Languages','Code','«'.$hostname.'»'));
$NameOrig=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,$lang)),'Languages','NameOrig','«'.$hostname.'»'));
$NamePt_BR=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'pt_BR')),'Languages','NamePT_BR','«'.$hostname.'»'));
$NameDe=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'de')),'Languages','NameDE','«'.$hostname.'»'));
$NameUk=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'uk')),'Languages','NameUK','«'.$hostname.'»'));
$NameCa=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'ca')),'Languages','NameCA','«'.$hostname.'»'));
$NameEn=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'en')),'Languages','NameEN','«'.$hostname.'»'));
$NameEs=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'es')),'Languages','NameES','«'.$hostname.'»'));
$NameFr=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'fr')),'Languages','NameFR','«'.$hostname.'»'));
$NameGl=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'gl')),'Languages','NameGL','«'.$hostname.'»'));
$NameIt=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'it')),'Languages','NameIT','«'.$hostname.'»'));
$q = 'INSERT INTO Languages (ID, Code, NameOrig, NamePT_BR, NameDE, NameUK, NameCA, NameEN, NameES, NameFR, NameGL, NameIT) VALUES (NULL, \''.$code.'\', \''.$NameOrig.'\', \''.$NamePt_BR.'\', \''.$NameDe.'\', \''.$NameUk.'\', \''.$NameCa.'\', \''.$NameEn.'\', \''.$NameEs.'\', \''.$NameFr.'\', \''.$NameGl.'\', \''.$NameIt.'\')';
if (!$opts['dryrun']) {
myq($link,$q,$line);
$langid=mysqli_insert_id($link);
} else {
$langid=0;
}
} else {
$row=mysqli_fetch_assoc($res);
$langid=$row['ID'];
}
$pos++;
$retlangs[]=['InstID'=>$instid,'LangID'=>$langid,'Pos'=>$pos,'Code'=>$lang];
function getlangid(&$link,$lang,$hostname,$line) {
$res=myq($link,'SELECT * FROM Languages WHERE Code=\''.myesc($link,$lang).'\'',$line);
$nrows=mysqli_num_rows($res);
if ($nrows==0) {
$code=myesc($link,truncs($lang,'Languages','Code','«'.$hostname.'»'));
$NameOrig=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,$lang)),'Languages','NameOrig','«'.$hostname.'»'));
$NamePt_BR=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'pt_BR')),'Languages','NamePT_BR','«'.$hostname.'»'));
$NameDe=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'de')),'Languages','NameDE','«'.$hostname.'»'));
$NameUk=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'uk')),'Languages','NameUK','«'.$hostname.'»'));
$NameCa=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'ca')),'Languages','NameCA','«'.$hostname.'»'));
$NameEn=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'en')),'Languages','NameEN','«'.$hostname.'»'));
$NameEs=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'es')),'Languages','NameES','«'.$hostname.'»'));
$NameFr=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'fr')),'Languages','NameFR','«'.$hostname.'»'));
$NameGl=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'gl')),'Languages','NameGL','«'.$hostname.'»'));
$NameIt=myesc($link,truncs(mb_ucfirst(locale_get_display_name($lang,'it')),'Languages','NameIT','«'.$hostname.'»'));
$q='INSERT INTO Languages (ID, Code, NameOrig, NamePT_BR, NameDE, NameUK, NameCA, NameEN, NameES, NameFR, NameGL, NameIT) VALUES (NULL, \''.$code.'\', \''.$NameOrig.'\', \''.$NamePt_BR.'\', \''.$NameDe.'\', \''.$NameUk.'\', \''.$NameCa.'\', \''.$NameEn.'\', \''.$NameEs.'\', \''.$NameFr.'\', \''.$NameGl.'\', \''.$NameIt.'\')';
if (!$opts['dryrun']) {
myq($link,$q,$line);
$langid=mysqli_insert_id($link);
} else {
$langid=0;
}
} else {
//if ($nrows==1) ...... notify() that there's more than 1 record with code=$lang
$row=mysqli_fetch_assoc($res);
$langid=$row['ID'];
}
return($retlangs);
return($langid);
}
?>