Added validity check on declared and detected lang codes to getlangid function - when lang code is not valide it falls back to “en” default; added a getlangsidsarr function to cope with possible dupes and spare code

This commit is contained in:
pezcurrel 2023-01-01 19:21:21 +01:00
parent c039d85d76
commit 87fbbbd2f9

View file

@ -640,9 +640,9 @@ if ($instanswered && isset($idata['languages']) && is_array($idata['languages'])
eecho(1,'«'.$opts['hostname'].'»: declared languages: '.implode(', ',$idata['languages']).N);
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstLangs WHERE InstID='.$instid,__LINE__);
$langids=getlangsidsarr($idata['languages'],$link,$opts['hostname'],$opts['dryrun']);
$pos=0;
foreach ($idata['languages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],$opts['dryrun'],__LINE__);
foreach ($langids as $langid) {
$pos++;
myq($link,'INSERT INTO InstLangs SET InstID='.$instid.', LangID='.$langid.', Pos='.$pos,__LINE__);
}
@ -656,8 +656,6 @@ if ($instanswered && isset($idata['languages']) && is_array($idata['languages'])
$idata['ourlanguages']=get_instance_langs($opts['hostname']);
while (count($idata['ourlanguages'])>5)
array_pop($idata['ourlanguages']);
foreach ($idata['ourlanguages'] as $key=>$lang)
$idata['ourlanguages'][$key]=str_replace('-','_',$lang);// this is just cosmetic: lang code canonicalization happens in getlangid()
eecho(1,'«'.$opts['hostname'].'»: detected languages: '.implode(', ',$idata['ourlanguages']).N);
} else {
$idata['ourlanguages']=$idata['languages'];
@ -665,9 +663,9 @@ if ($instanswered && isset($idata['languages']) && is_array($idata['languages'])
}
if (!$opts['dryrun']) {
myq($link,'DELETE FROM InstOurLangs WHERE InstID='.$instid,__LINE__);
$langids=getlangsidsarr($idata['languages'],$link,$opts['hostname'],$opts['dryrun']);
$pos=0;
foreach ($idata['ourlanguages'] as $lang) {
$langid=getlangid($link,$lang,$opts['hostname'],$opts['dryrun'],__LINE__);
$pos++;
myq($link,'INSERT INTO InstOurLangs SET InstID='.$instid.', OurLangID='.$langid.', Pos='.$pos,__LINE__);
}
@ -1213,7 +1211,11 @@ function get_instance_langs($host) {
}
function getlangid(&$link,$lang,$hostname,$dryrun,$line) {
$lang=locale_canonicalize($lang);
$code=locale_canonicalize($lang);
if (preg_match('/^\s*$/',$lang)===1 || preg_match('/__/',$code)===1) {
notify('«'.$hostname.'»: «'.$lang.'» is not a valid language code, falling back to default «en».',2,true);
$code='en';
}
$res=myq($link,'SELECT * FROM Languages WHERE Code=\''.myesc($link,$lang).'\'',$line);
$nrows=mysqli_num_rows($res);
if ($nrows==0) {
@ -1243,4 +1245,14 @@ function getlangid(&$link,$lang,$hostname,$dryrun,$line) {
return($langid);
}
function getlangsidsarr(&$langs,&$link,$hostname,$dryrun) {
$langids=[];
foreach ($langs as $lang) {
$langid=getlangid($link,$lang,$hostname,$dryrun,__LINE__);
$langids[]=$langid;
}
$langids=array_unique($langids);
return($langids);
}
?>