updInsertTS.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. define('N',"\n");
  16. define('SNAME',basename(__FILE__));
  17. define('CONFIGFP',__DIR__.'/../../conf/mustard.ini');
  18. $help='SYNOPSYS
  19. '.SNAME.' [options]
  20. DESCRIPTION
  21. This is a script to set the «InsertTS» field of «Instances» table for
  22. instances which where checked the first time when «InsertTS» was not
  23. implented yet.
  24. OPTIONS
  25. -h, --help
  26. Show this help text and exit.'.N;
  27. for ($i=1; $i<$argc; $i++) {
  28. if ($argv[$i]=='-h' || $argv[$i]=='--help') {
  29. mexit($help,0);
  30. } else {
  31. mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
  32. }
  33. }
  34. use function mysqli_real_escape_string as myesc;
  35. $iniarr=@parse_ini_file(CONFIGFP)
  36. or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
  37. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  38. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
  39. // for php versions < 8
  40. if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
  41. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  42. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); }
  43. // for php versions < 8
  44. if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true);
  45. $insts=[];
  46. $res=myq($link,'SELECT * FROM Instances WHERE InsertTS IS NULL');
  47. while ($row=mysqli_fetch_assoc($res)) $insts[]=$row;
  48. $cinsts=count($insts);
  49. $i=0;
  50. $now=time();
  51. $duemilasedici=mktime(0,0,0,1,1,2016);
  52. foreach ($insts as $inst) {
  53. $i++;
  54. echo('Working on instance '.$i.'/'.$cinsts.' ('.round(100/$cinsts*$i,2).'%) with ID = '.$inst['ID'].' and URI = «'.$inst['URI'].'».'.N);
  55. $insertts=null;
  56. if (!is_null($inst['FirstSeen'])) {
  57. echo('«FirstSeen» is not null: '.$inst['FirstSeen'].'; using it.'.N);
  58. $insertts=$inst['FirstSeen'];
  59. } elseif (!is_null($inst['AdmCreatedAt'])) {
  60. echo('«AdmCreatedAt» is not null: '.$inst['AdmCreatedAt'].'; using it.'.N);
  61. $insertts=round($inst['AdmCreatedAt']);
  62. } else {
  63. echo('Could not guess a value for «InsertTS», using 2016.'.N);
  64. $insertts=$duemilasedici;
  65. }
  66. myq($link,'UPDATE Instances SET InsertTS=\''.$insertts.'\' WHERE ID='.$inst['ID']);
  67. echo('Updated InsertTS :-)'.N.'---'.N);
  68. }
  69. mysqli_close($link);
  70. echo('Done updating '.$cinsts.' «Instances» records :-).'.N);
  71. exit(0);
  72. // functions
  73. function myq(&$l,$q) {
  74. try {
  75. $res=mysqli_query($l,$q);
  76. }
  77. catch (Exception $e) {
  78. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  79. exit(3);
  80. }
  81. if ($res===false) {
  82. echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
  83. exit(3);
  84. }
  85. return($res);
  86. }
  87. function mexit($msg,$code) {
  88. global $link;
  89. if (isset($link) && $link!==false) mysqli_close($link);
  90. if ($code>0)
  91. fwrite(STDERR,$msg);
  92. else
  93. echo($msg);
  94. exit($code);
  95. }
  96. function utstd($val) {
  97. if (is_null($val)) return(null);
  98. $val=round($val);
  99. return(date('Y-m-d H:i:s',$val));
  100. }
  101. function pr($val) {
  102. if (is_null($val)) return('NULL');
  103. return($val);
  104. }
  105. function eecho($msg) {
  106. echo($msg);
  107. }
  108. ?>