searchdupes.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. require __DIR__.'/../lib/delinstbyid.php';
  19. $help='SYNOPSYS
  20. '.SNAME.' [options]
  21. DESCRIPTION
  22. This is a script to check the «Instances» table of mastostart database for
  23. multiple records with the same URI value.
  24. OPTIONS
  25. -d, --delete
  26. For each URI with multiple records, give the possibility to interactively
  27. choose which record to keep, and delete the others (default); or to
  28. automatically keep the record with the lowest ID and delete the others
  29. (see below).
  30. -I, --nonint
  31. Disable interactive mode.
  32. -h, --help
  33. Show this help text and exit.'.N;
  34. $opts=[
  35. 'delete'=>false,
  36. 'interactive'=>true
  37. ];
  38. for ($i=1; $i<$argc; $i++) {
  39. if ($argv[$i]=='-h' || $argv[$i]=='--help') {
  40. mexit($help,0);
  41. } elseif ($argv[$i]=='-d' || $argv[$i]=='--delete') {
  42. $opts['delete']=true;
  43. } elseif ($argv[$i]=='-I' || $argv[$i]=='--nonint') {
  44. $opts['interactive']=false;
  45. } else {
  46. mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
  47. }
  48. }
  49. use function mysqli_real_escape_string as myesc;
  50. function mexit($msg,$code) {
  51. global $link;
  52. if (isset($link) && $link!==false) mysqli_close($link);
  53. if ($code>0)
  54. fwrite(STDERR,$msg);
  55. else
  56. echo($msg);
  57. exit($code);
  58. }
  59. $iniarr=@parse_ini_file(CONFIGFP)
  60. or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
  61. try { $link=mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  62. catch (Exception $error) { mexit('Could not connect to mysql server: '.$error->getMessage().' (error code: '.$error->getCode().').'.N,1); }
  63. mysqli_set_charset($link,'utf8mb4');
  64. function myq(&$l,$q) {
  65. try {
  66. $res=mysqli_query($l,$q);
  67. }
  68. catch (Exception $e) {
  69. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  70. exit(3);
  71. }
  72. return($res);
  73. }
  74. $insts=[];
  75. $res=mysqli_query($link,'SELECT * FROM Instances');
  76. while ($row=mysqli_fetch_assoc($res))
  77. $insts[$row['URI']][]=$row;
  78. $tot=0;
  79. foreach ($insts as $uri=>$rows)
  80. if (count($rows)>1) $tot++;
  81. if ($tot>0 && $opts['delete']) {
  82. $i=0;
  83. foreach ($insts as $uri=>$rows) {
  84. $cr=count($rows);
  85. if ($cr>1) {
  86. $i++;
  87. echo('<<< '.$i.'/'.$tot.': '.$uri.' ('.$cr.' records) >>>'.N.N);
  88. $buff=[];
  89. foreach ($rows as $row) {
  90. $row['FirstSeen']=utstd($row['FirstSeen']);
  91. $row['NoxLastModTS']=utstd($row['NoxLastModTS']);
  92. $row['AdmCreatedAt']=utstd($row['AdmCreatedAt']);
  93. $row['LastGuestEdit']=utstd($row['LastGuestEdit']);
  94. $row['InsertTS']=utstd($row['InsertTS']);
  95. $out='';
  96. foreach ($row as $key=>$val)
  97. $out.=$key.': '.pr($val).'; ';
  98. $out=substr($out,0,-2);
  99. //echo($out.N.N);
  100. $buff[$row['ID']]=['id'=>$row['ID'],'row'=>$out];
  101. }
  102. asort($buff);
  103. $ii=0;
  104. $recs=[];
  105. foreach ($buff as $rec) {
  106. $ii++;
  107. $recs[$ii]=$rec;
  108. echo($ii.': '.$rec['row'].N.N);
  109. }
  110. if ($opts['interactive']) {
  111. $ans=[];
  112. for ($iii=1; $iii<=$ii; $iii++)
  113. $ans[]=$iii;
  114. $ans[]='e';
  115. $inp=null;
  116. while (!in_array($inp,$ans)) {
  117. echo('Which one do you want to keep? ['.implode('/',$ans).'] ');
  118. $inp=strtolower(trim(fgets(STDIN)));
  119. }
  120. } else {
  121. $inp=1;
  122. }
  123. if ($inp=='e') {
  124. mexit('Ok, bye :-)'.N,0);
  125. } else {
  126. $inp+=0;
  127. echo(N.'Ok, i’ll keep the record with ID='.$recs[$inp]['id'].N);
  128. foreach ($recs as $key=>$rec) {
  129. if ($key!=$inp) {
  130. $res=delinstbyid($link,$rec['id'],'eecho',N);
  131. if (!$res) mexit('ERROR: see the last message above this line.'.N,2);
  132. }
  133. }
  134. echo(N);
  135. }
  136. }
  137. }
  138. }
  139. echo('Total dupes: '.$tot.N);
  140. mysqli_close($link);
  141. exit(0);
  142. function utstd($val) {
  143. if (is_null($val)) return(null);
  144. $val=round($val);
  145. return(date('Y-m-d H:i:s',$val));
  146. }
  147. function pr($val) {
  148. if (is_null($val)) return('NULL');
  149. return($val);
  150. }
  151. function eecho($msg) {
  152. echo($msg);
  153. }
  154. ?>