126 lines
3.8 KiB
PHP
126 lines
3.8 KiB
PHP
#!/usr/bin/php
|
||
<?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/>.
|
||
*/
|
||
|
||
define('N',"\n");
|
||
define('SNAME',basename(__FILE__));
|
||
define('CONFIGFP',__DIR__.'/../../conf/mustard.ini');
|
||
|
||
$help='SYNOPSYS
|
||
'.SNAME.' [options]
|
||
DESCRIPTION
|
||
This is a script to set the «InsertTS» field of «Instances» table for
|
||
instances which where checked the first time when «InsertTS» was not
|
||
implented yet.
|
||
OPTIONS
|
||
-h, --help
|
||
Show this help text and exit.'.N;
|
||
|
||
for ($i=1; $i<$argc; $i++) {
|
||
if ($argv[$i]=='-h' || $argv[$i]=='--help') {
|
||
mexit($help,0);
|
||
} else {
|
||
mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
|
||
}
|
||
}
|
||
|
||
use function mysqli_real_escape_string as myesc;
|
||
|
||
$iniarr=@parse_ini_file(CONFIGFP)
|
||
or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
|
||
|
||
try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
|
||
catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
|
||
// for php versions < 8
|
||
if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
|
||
try { $res=mysqli_set_charset($link,'utf8mb4'); }
|
||
catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); }
|
||
// for php versions < 8
|
||
if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true);
|
||
|
||
$insts=[];
|
||
$res=myq($link,'SELECT * FROM Instances WHERE InsertTS IS NULL');
|
||
while ($row=mysqli_fetch_assoc($res)) $insts[]=$row;
|
||
$cinsts=count($insts);
|
||
$i=0;
|
||
$now=time();
|
||
$duemilasedici=mktime(0,0,0,1,1,2016);
|
||
foreach ($insts as $inst) {
|
||
$i++;
|
||
echo('Working on instance '.$i.'/'.$cinsts.' ('.round(100/$cinsts*$i,2).'%) with ID = '.$inst['ID'].' and URI = «'.$inst['URI'].'».'.N);
|
||
$insertts=null;
|
||
if (!is_null($inst['FirstSeen'])) {
|
||
echo('«FirstSeen» is not null: '.$inst['FirstSeen'].'; using it.'.N);
|
||
$insertts=$inst['FirstSeen'];
|
||
} elseif (!is_null($inst['AdmCreatedAt'])) {
|
||
echo('«AdmCreatedAt» is not null: '.$inst['AdmCreatedAt'].'; using it.'.N);
|
||
$insertts=round($inst['AdmCreatedAt']);
|
||
} else {
|
||
echo('Could not guess a value for «InsertTS», using 2016.'.N);
|
||
$insertts=$duemilasedici;
|
||
}
|
||
myq($link,'UPDATE Instances SET InsertTS=\''.$insertts.'\' WHERE ID='.$inst['ID']);
|
||
echo('Updated InsertTS :-)'.N.'---'.N);
|
||
}
|
||
mysqli_close($link);
|
||
echo('Done updating '.$cinsts.' «Instances» records :-).'.N);
|
||
exit(0);
|
||
|
||
|
||
// functions
|
||
|
||
function myq(&$l,$q) {
|
||
try {
|
||
$res=mysqli_query($l,$q);
|
||
}
|
||
catch (Exception $e) {
|
||
echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
|
||
exit(3);
|
||
}
|
||
if ($res===false) {
|
||
echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
|
||
exit(3);
|
||
}
|
||
return($res);
|
||
}
|
||
|
||
function mexit($msg,$code) {
|
||
global $link;
|
||
if (isset($link) && $link!==false) mysqli_close($link);
|
||
if ($code>0)
|
||
fwrite(STDERR,$msg);
|
||
else
|
||
echo($msg);
|
||
exit($code);
|
||
}
|
||
|
||
function utstd($val) {
|
||
if (is_null($val)) return(null);
|
||
$val=round($val);
|
||
return(date('Y-m-d H:i:s',$val));
|
||
}
|
||
|
||
function pr($val) {
|
||
if (is_null($val)) return('NULL');
|
||
return($val);
|
||
}
|
||
|
||
function eecho($msg) {
|
||
echo($msg);
|
||
}
|
||
|
||
?>
|