Truncate instead of saying if you will; minor changes

This commit is contained in:
pezcurrel 2024-08-19 13:46:35 +02:00
parent 7b279af856
commit 55e7890222

View file

@ -338,7 +338,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4
eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v2...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v2/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
if (make(['domain', 'title', 'version', 'source_url', 'description', 'usage', 'thumbnail', 'languages', 'configuration', 'registrations', 'contact', 'rules'],$buf)) {
@ -399,7 +399,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4
eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance extended description from API v1...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/extended_description',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
eecho(1,'«'.$opts['hostname'].'»: got instance extended description from API v1 :-)'.N);
@ -415,7 +415,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4
eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance domain blocks from API v1...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/domain_blocks',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
eecho(1,'«'.$opts['hostname'].'»: got instance domain blocks from API v1 :-)'.N);
@ -456,7 +456,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='4
eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance info from API v1...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
if (make(['uri', 'title', 'short_description', 'description', 'email', 'version', 'urls', 'stats', 'thumbnail', 'languages', 'registrations', 'approval_required', 'contact_account'],$buf)) {
@ -534,7 +534,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='2
eecho(0,'«'.$opts['hostname'].'»: trying to fetch instance activity info from API v1...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v1/instance/activity',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
eecho(1,'«'.$opts['hostname'].'»: got instance activity info from API v1 :-)'.N);
@ -553,7 +553,7 @@ if ($idata['IsMastodon'] && !is_null($idata['Version']) && $idata['Version']>='3
if ($idata['Version']>='3.5.0') $url.='/tags';
$buf=@gurl($url,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$opts['hostname']}»");
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
eecho(1,'«'.$opts['hostname'].'»: got instance tags trends info from API v1 :-)'.N);
@ -641,19 +641,9 @@ foreach ($idata as $key=>$val) {
} elseif (is_null($val)) {
$set[]=$key.'=NULL';
} elseif (is_int($val)) {
if (willtrunc($val,'Instances',$key)) {
$msg='«'.$opts['hostname'].'»: value «'.$val.'» is less than min. admitted value or greater than max. admitted value for column «'.$key.'» of table «Instances». Shutting down.';
notify($msg,3,false);
mexit($msg.N,2);
}
$set[]=$key.'='.$val;
$set[]=$key.'='.truncn($val, 'Instances', $key, '«'.$opts['hostname'].'»');
} elseif (is_string($val)) {
if (willtrunc($val,'Instances',$key)) {
$msg='«'.$opts['hostname'].'»: value «'.nocrnl($val).'» is too long for column «'.$key.'» of table «Instances». Shutting down.';
notify($msg,3,false);
mexit($msg.N,2);
}
$set[]=$key.'=\''.myesc($link,$val).'\'';
$set[]=$key.'=\''.myesc($link,truncs($val, 'Instances', $key, '«'.$opts['hostname'].'»')).'\'';
} else {
mexit('$idata[\''.$key.'\'] value has unmanaged type, see code around line '.__LINE__.'.'.N,3);
}
@ -875,7 +865,7 @@ if ($instanswered && $opts['fetchusers'] && $idata['IsMastodon'] && !is_null($id
eecho(0,'«'.$opts['hostname'].'»: trying to fetch chunk '.($chunk+1).' of users info from directory API (attempt '.($att+1).'/'.$opts['udiratts'].')...'.N);
$buf=@gurl('https://'.$opts['hostname'].'/api/v1/directory?local=1&order=new&limit='.$limit.'&offset='.$offset,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
$xrlr=ckratelimit($buf['headers']);
$xrlr=ckratelimit($buf['headers'],"«{$opts['hostname']}»");
eecho(1,'«'.$opts['hostname'].'»: got chunk '.($chunk+1).' of users info from directory API on attempt '.($att+1).'/'.$opts['udiratts'].' (xrlr: '.$xrlr.') :-)'.N);
$buf=@json_decode($buf['cont'],true);
if (is_array($buf)) {
@ -918,7 +908,7 @@ if ($instanswered && $opts['fetchusers'] && $idata['IsMastodon'] && !is_null($id
eecho(0,'«'.$opts['hostname'].'»: trying to fetch tags for user «'.$user['username'].'»...'.N);
$tags=@gurl('https://'.$opts['hostname'].'/api/v1/accounts/'.$user['id'].'/featured_tags',$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($tags['cont']!==false) {
ckratelimit($tags['headers']);
ckratelimit($tags['headers'],"«{$opts['hostname']}»");
$tags=@json_decode($tags['cont'],true);
if (is_array($tags) && count($tags)>0) {
eecho(1,'«'.$opts['hostname'].'»: got '.count($tags).' tag(s) for user «'.$user['username'].'» :-)'.N);
@ -1077,13 +1067,14 @@ function truncs($str,$tab,$col,$ctx) {
if (is_null($str)) return(null);
if ($iswin)
$tab=strtolower($tab);
$size=$tables[$tab][$col];
$len=mb_strlen($str,'UTF-8');
if ($len>$size) {
$str=mb_substr($str,0,$size-1,'UTF-8').'…';
notify($ctx.': had to truncate string to '.$size.' chars to be able to insert it into «'.$col.'» column in «'.$tab.'» table.',3);
}
return($str);
$ostr=$str;
if (mb_strlen($str,'UTF-8')>$tables[$tab][$col])
$str=strip_tags($str);
if (mb_strlen($str,'UTF-8')>$tables[$tab][$col])
$str=mb_substr($str,0,$tables[$tab][$col]-1,'UTF-8').'…';
if ($str!=$ostr)
notify($ctx.': had to shrink input string to '.$tables[$tab][$col].' chars to be able to insert it into «'.$col.'» column in «'.$tab.'» table.',2);
return $str;
}
function truncn($num,$tab,$col,$ctx) {
@ -1092,10 +1083,10 @@ function truncn($num,$tab,$col,$ctx) {
$tab=strtolower($tab);
if (is_numeric($num)) {
if ($num>$tables[$tab][$col]['max']) {
notify($ctx.': had to ceil «'.$num.'» to «'.$tables[$tab][$col]['max'].'», ie the maximum value it can have in column «'.$col.'» of table «'.$tab.'».',3);
notify($ctx.': had to ceil «'.$num.'» to «'.$tables[$tab][$col]['max'].'», ie the maximum value it can have in column «'.$col.'» of table «'.$tab.'».',2);
$num=$tables[$tab][$col]['max'];
} elseif ($num<$tables[$tab][$col]['min']) {
notify($ctx.': had to floor «'.$num.'» to «'.$tables[$tab][$col]['min'].'», ie the minimum value it can have in column «'.$col.'» of table «'.$tab.'»).',3);
notify($ctx.': had to floor «'.$num.'» to «'.$tables[$tab][$col]['min'].'», ie the minimum value it can have in column «'.$col.'» of table «'.$tab.'»).',2);
$num=$tables[$tab][$col]['min'];
}
} else {
@ -1146,8 +1137,8 @@ function mdasortbykey(&$arr,$key,$rev=false) {
function make($keys,&$arr) {
foreach ($keys as $key)
if (!array_key_exists($key,$arr))
return(false);
return(true);
return false;
return true ;
}
function myv(&$link,$var) {
@ -1170,7 +1161,7 @@ function datetots($date) {
return(mktime(0,0,0,$date[1],$date[2],$date[0]));
}
function ckratelimit($headers) {
function ckratelimit($headers,$ctx) {
$headers=explode("\r\n",$headers);
$buff=[];
array_shift($headers);
@ -1181,7 +1172,7 @@ function ckratelimit($headers) {
if (isset($headers['date']) && isset($headers['x-ratelimit-reset']) && isset($headers['x-ratelimit-remaining'])) {
if ($headers['x-ratelimit-remaining']==0) {
$stosl=strtotime($headers['x-ratelimit-reset'])-strtotime($headers['date'])+1;
eecho(2,'reached rate limit, sleeping for '.ght($stosl,null,0).' ...'.N);
eecho(2,$ctx.': reached rate limit, sleeping for '.ght($stosl,null,0).' ...'.N);
sleep($stosl);
}
return($headers['x-ratelimit-remaining']);
@ -1190,7 +1181,7 @@ function ckratelimit($headers) {
if (!isset($headers['date'])) $missing[]='date';
if (!isset($headers['x-ratelimit-reset'])) $missing[]='x-ratelimit-reset';
if (!isset($headers['x-ratelimit-remaining'])) $missing[]='x-ratelimit-remaining';
eecho(2,'ckratelimit: headers did not contain «'.implode('», «',$missing).'» item(s)!'.N);
eecho(2,$ctx.': ckratelimit: headers did not contain «'.implode('», «',$missing).'» item(s)!'.N);
return(false);
}
}
@ -1207,7 +1198,7 @@ function get_api($host, $path) {
global $opts;
$buf = @gurl('https://'.$host.$path,$opts['conntimeout'],$opts['functimeout'],['Accept: application/json']);
if ($buf['cont']!==false) {
ckratelimit($buf['headers']);
ckratelimit($buf['headers'],"«{$host}»");
$data = json_decode($buf['cont'], true);
return $data;
} else {