Got rid of akeavinn; other minor changes

This commit is contained in:
pezcurrel 2022-12-21 07:54:11 +01:00
parent d803d6f667
commit 0d74dbf243

View file

@ -209,10 +209,10 @@ if ($opts['logminmsglev']<4) {
}
$mastodons=[];
$res=myq($link,'SELECT Name FROM Platforms WHERE Name=\''.myesc($link,$idata['Software']).'\' AND Consider=1',__LINE__);
$res=myq($link,'SELECT Name FROM Platforms WHERE Consider=1',__LINE__);
while ($row=mysqli_fetch_assoc($res))
$mastodons[]=preg_quote($row['Name'],'/');
if (count($mastodons)<1) mexit('in table «Platforms», there is no platform to be considered!',1);
if (count($mastodons)<1) mexit('in table «Platforms», there is no platform to be considered!'.N,1);
$mastodons=implode('|',$mastodons);
$instints=['ID', 'FirstSeen', 'IsMastodon', 'Dead', 'Priority', 'Visible', 'Noxious', 'NoxLastModTS', 'LocalityID', 'OurLangsLock', 'UserCount', 'StatusCount', 'DomainCount', 'ActiveUsersMonth', 'ActiveUsersHalfYear', 'RegOpen', 'RegReqApproval', 'MaxTootChars', 'AdmCreatedAt', 'LastCheckOk', 'GuestID', 'LastGuestEdit', 'InsertTS', 'RPos'];
@ -656,10 +656,9 @@ if (isset($idata['activity']) && is_array($idata['activity'])) {
myq($link,'DELETE FROM InstActivity WHERE InstID='.$instid,__LINE__);
$pos=0;
foreach ($idata['activity'] as $buf) {
if (akeavinn('week',$buf) && akeavinn('statuses',$buf) && akeavinn('logins',$buf) && akeavinn('registrations',$buf)) {
if (isset($buf['week']) && is_int($buf['week']) && isset($buf['statuses']) && is_int($buf['statuses']) && isset($buf['logins']) && is_int($buf['logins']) && isset($buf['registrations']) && is_int($buf['registrations'])) {
$pos++;
$query='INSERT INTO InstActivity (InstID, Week, Statuses, Logins, Registrations, Pos) VALUES (\''.$instid.'\', \''.myesc($link,$buf['week']).'\', \''.myesc($link,$buf['statuses']).'\', \''.myesc($link,$buf['logins']).'\', \''.myesc($link,$buf['registrations']).'\', '.$pos.')';
myq($link,$query,__LINE__);
myq($link,'INSERT INTO InstActivity (InstID, Week, Statuses, Logins, Registrations, Pos) VALUES ('.$instid.', '.$buf['week'].', '.$buf['statuses'].', '.$buf['logins'].', '.$buf['registrations'].', '.$pos.')',__LINE__);
}
}
}
@ -668,20 +667,22 @@ if (isset($idata['activity']) && is_array($idata['activity'])) {
if (isset($idata['trends']) && is_array($idata['trends'])) {
$trends=[];
foreach ($idata['trends'] as $buf) {
if (akeavinn('name',$buf) && akeavinn('url',$buf) && akeavinn('history',$buf) && is_array($buf['history'])) {
if (isset($buf['name']) && is_string($buf['name']) && isset($buf['url']) && is_string($buf['url']) && isset($buf['history']) && is_array($buf['history'])) {
$trend=0;
foreach ($buf['history'] as $row) {
if ($row['uses']>0)
$trend+=($row['accounts']/$row['uses']);
if (isset($row['day']) && is_int($row['day']) && isset($row['uses']) && is_int($row['uses']) && isset($row['accounts']) && is_int($row['uses'])) {
if ($row['uses']>0)
$trend+=($row['accounts']/$row['uses']);
$trends[]=[
'InstID'=>$instid,
'LastDay'=>$buf['history'][0]['day'],
'Name'=>$buf['name'],
'URL'=>$buf['url'],
'Pos'=>null,
'trend'=>$trend
];
}
}
$trends[]=[
'InstID'=>$instid,
'LastDay'=>$buf['history'][0]['day'],
'Name'=>$buf['name'],
'URL'=>$buf['url'],
'Pos'=>null,
'trend'=>$trend
];
}
}
mdasortbykey($trends,'trend',true);
@ -690,7 +691,7 @@ if (isset($idata['trends']) && is_array($idata['trends'])) {
$pos=0;
foreach ($trends as $trend) {
$pos++;
$query='INSERT INTO InstTrends (InstID, LastDay, Name, URL, Pos) VALUES ('.$trend['InstID'].', \''.$trend['LastDay'].'\', \''.myesc($link,truncs($trend['Name'],'InstTrends','Name','«'.$opts['hostname'].'»')).'\', \''.myesc($link,truncs($trend['URL'],'InstTrends','URL','«'.$opts['hostname'].'»')).'\', '.$pos.')';
$query='INSERT INTO InstTrends (InstID, LastDay, Name, URL, Pos) VALUES ('.$trend['InstID'].', \''.$trend['LastDay'].'\', \''.myesc($link, truncs($trend['Name'], 'InstTrends', 'Name', '«'.$opts['hostname'].'»')).'\', \''.myesc($link, truncs($trend['URL'], 'InstTrends', 'URL', '«'.$opts['hostname'].'»')).'\', '.$pos.')';
if (!$opts['dryrun']) myq($link,$query,__LINE__);
}
}
@ -904,6 +905,39 @@ function willtrunc($val,$tab,$col) {
return(false);
}
function truncs($str,$tab,$col,$ctx) {
global $tables, $iswin;
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);
}
function truncn($num,$tab,$col,$ctx) {
global $tables, $iswin;
if ($iswin)
$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);
$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);
$num=$tables[$tab][$col]['min'];
}
} else {
notify($ctx.': function «truncn»: expecting a number, got something else; returning «0».',3);
$num=0;
}
return($num);
}
function nocrnl($str) {
return(str_replace(["\r","\n"],['\\r','\\n'],$str));
}
@ -913,39 +947,11 @@ function b2i($bool) {
return($r);
}
//is array, array key exists and value is not null
function akeavinn($key,&$arr) {
if (is_array($arr) && array_key_exists($key,$arr) && !is_null($arr[$key]))
return(true);
else
return(false);
}
function isempty($str) {
(preg_match('/^\s*$/',$str)===1) ? $r=true : $r=false;
return($r);
}
function nempty($str) {
if (preg_match('/^\s*$/',$str)===1)
return(null);
else
return($str);
}
function subarimp($glue,$key,&$arr) {
$str='';
$i=1;
$carr=count($arr);
foreach ($arr as $inarr) {
$str.=$inarr[$key];
if ($i<$carr)
$str.=$glue;
$i++;
}
return($str);
}
function notify($msg,$lev,$doecho=true) {
// "$lev" is to be thought of as "$lev" param of function "eecho": 0=debug, 1=info, 2=warning, 3=error
global $link, $tables, $iswin, $opts;
@ -956,6 +962,72 @@ function notify($msg,$lev,$doecho=true) {
}
}
function mdasortbykey(&$arr,$key,$rev=false) {
$karr=[];
foreach ($arr as $akey=>$subarr)
$karr[round($subarr[$key]*10000000000000,0)]=[$akey,$subarr];
if (!$rev)
ksort($karr);
else
krsort($karr);
$arr=[];
foreach ($karr as $akey=>$subarr)
$arr[$subarr[0]]=$subarr[1];
}
// "multi array_key_exists"
function make($keys,&$arr) {
foreach ($keys as $key)
if (!array_key_exists($key,$arr))
return(false);
return(true);
}
function myv(&$link,$var) {
if (is_null($var)) {
return('NULL');
} elseif (is_bool($var)) {
if ($var)
return('1');
else
return('0');
} elseif (trim($var)=='') {
return('NULL');
} else {
return('\''.mysqli_real_escape_string($link,$var).'\'');
}
}
function datetots($date) {
$date=explode('-',$date);
return(mktime(0,0,0,$date[1],$date[2],$date[0]));
}
function ckratelimit($httpresphead) {
$headers=explode("\r\n",$httpresphead);
$buff=[];
array_shift($headers);
foreach ($headers as $header)
if (preg_match('/^([^:]+):(.*)$/Uu',$header,$matches)===1)
$buff[strtolower($matches[1])]=trim($matches[2]);
$headers=$buff;
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).' ...'.N);
sleep($stosl);
}
return($headers['x-ratelimit-remaining']);
} else {
$missing=[];
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: $httpresphead did not contain «'.implode('», «',$missing).'» header(s)!'.N);
return(false);
}
}
/** <LANGUAGE MANAGEMENT> */
/**
* Executes a call to Mastodon API.
@ -1094,93 +1166,6 @@ function get_instance_langs($host) {
return $languages;
}
// "multi array_key_exists"
function make($keys,&$arr) {
foreach ($keys as $key)
if (!array_key_exists($key,$arr))
return(false);
return(true);
}
function myv(&$link,$var) {
if (is_null($var)) {
return('NULL');
} elseif (is_bool($var)) {
if ($var)
return('1');
else
return('0');
} elseif (trim($var)=='') {
return('NULL');
} else {
return('\''.mysqli_real_escape_string($link,$var).'\'');
}
}
function datetots($date) {
$date=explode('-',$date);
return(mktime(0,0,0,$date[1],$date[2],$date[0]));
}
function ckratelimit($httpresphead) {
$headers=explode("\r\n",$httpresphead);
$buff=[];
array_shift($headers);
foreach ($headers as $header)
if (preg_match('/^([^:]+):(.*)$/Uu',$header,$matches)===1)
$buff[strtolower($matches[1])]=trim($matches[2]);
$headers=$buff;
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).' ...'.N);
sleep($stosl);
}
return($headers['x-ratelimit-remaining']);
} else {
$missing=[];
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: $httpresphead did not contain «'.implode('», «',$missing).'» header(s)!'.N);
return(false);
}
}
function truncs($str,$tab,$col,$ctx) {
global $tables, $iswin;
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);
}
function truncn($num,$tab,$col,$ctx) {
global $tables, $iswin;
if ($iswin)
$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);
$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);
$num=$tables[$tab][$col]['min'];
}
} else {
notify($ctx.': function «truncn»: expecting a number, got something else; returning «0».',3);
$num=0;
}
return($num);
}
function langs($instid, $hostname, $auto, $line) {
global $idata, $link, $opts;
$retlangs=[];
@ -1189,9 +1174,9 @@ function langs($instid, $hostname, $auto, $line) {
if (isset($idata['languages'][0]) && $idata['languages'][0]!='en')
$auto=false;
if ($auto) {
$languages = get_instance_langs($hostname);
} elseif (akeavinn('languages',$idata)) {
$languages = $idata['languages'];
$languages=get_instance_langs($hostname);
} elseif (isset($idata['languages']) && is_array($idata['languages'])) {
$languages=$idata['languages'];
}
if (count($languages)==0) {
return($retlangs);
@ -1237,17 +1222,4 @@ function langs($instid, $hostname, $auto, $line) {
return($retlangs);
}
function mdasortbykey(&$arr,$key,$rev=false) {
$karr=[];
foreach ($arr as $akey=>$subarr)
$karr[round($subarr[$key]*10000000000000,0)]=[$akey,$subarr];
if (!$rev)
ksort($karr);
else
krsort($karr);
$arr=[];
foreach ($karr as $akey=>$subarr)
$arr[$subarr[0]]=$subarr[1];
}
?>