fixThreads.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 convert the «silence» value in «Threads» column of
  22. «Instances» table to «limited», to fix a little bug in a previous release
  23. of getinstinfo.php.
  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 Threads="silence"');
  47. while ($row=mysqli_fetch_assoc($res)) $insts[]=$row;
  48. $cinsts=count($insts);
  49. $i=0;
  50. $now=time();
  51. foreach ($insts as $inst) {
  52. $i++;
  53. echo('Working on instance '.$i.'/'.$cinsts.' ('.round(100/$cinsts*$i,2).'%) with ID = '.$inst['ID'].' and URI = «'.$inst['URI'].'».'.N);
  54. $que='UPDATE Instances SET Threads="limited" WHERE ID='.$inst['ID'];
  55. echo 'Executing query «'.$que.'» ...'.N;
  56. myq($link,$que);
  57. }
  58. mysqli_close($link);
  59. echo('Done updating '.$cinsts.' «Instances» records :-).'.N);
  60. exit(0);
  61. // functions
  62. function myq(&$l,$q) {
  63. try {
  64. $res=mysqli_query($l,$q);
  65. }
  66. catch (Exception $e) {
  67. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  68. exit(3);
  69. }
  70. if ($res===false) {
  71. echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
  72. exit(3);
  73. }
  74. return($res);
  75. }
  76. function mexit($msg,$code) {
  77. global $link;
  78. if (isset($link) && $link!==false) mysqli_close($link);
  79. if ($code>0)
  80. fwrite(STDERR,$msg);
  81. else
  82. echo($msg);
  83. exit($code);
  84. }
  85. function utstd($val) {
  86. if (is_null($val)) return(null);
  87. $val=round($val);
  88. return(date('Y-m-d H:i:s',$val));
  89. }
  90. function pr($val) {
  91. if (is_null($val)) return('NULL');
  92. return($val);
  93. }
  94. function eecho($msg) {
  95. echo($msg);
  96. }
  97. ?>