2020-10-13 08:21:26 +02:00
#!/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 " );
$opts = array (
2020-10-14 08:37:41 +02:00
'setold' => false ,
'setblack' => false ,
2020-10-20 22:06:00 +02:00
'shuffle' => false ,
2020-10-13 08:21:26 +02:00
'deadline' => 60 * 24 * 60 * 60 , // se un'istanza non risponde da 60 giorni dichiararla morta
'newline' => 30 * 24 * 60 * 60 , // se un'istanza è nuova, dopo 30 giorni non lo è più
);
$help = ' mustool . php
DESCRIZIONE
mustool . php fa tante cose sul database delle istanze di Mastodon Help .
SINOSSI
mustool . php [ opzioni ] < azione [ azione ... ] >
AZIONI
2020-10-14 08:37:41 +02:00
setold
2020-10-13 08:21:26 +02:00
setta New = 0 per le istanze che non sono più nuove .
2020-10-14 08:37:41 +02:00
setblack
2020-10-13 08:21:26 +02:00
Setta Blacklisted = 1 per le istanze presenti nella blacklist ,
Blacklisted = 0 per quelle che non lo sono .
2020-10-20 22:06:00 +02:00
shuffle
Randomizza la lista delle istanze ( i valori della colonna RPos )
2020-10-13 08:21:26 +02:00
OPZIONI
- h , -- help
Mostra questo aiuto ed esce .
This program comes with ABSOLUTELY NO WARRANTY ; for details see the source .
This is free software , and you are welcome to redistribute it under
certain conditions ; see < http :// www . gnu . org / licenses /> for details . ' . N ;
$dosome = false ;
for ( $i = 1 ; $i < $argc ; $i ++ ) {
if ( substr ( $argv [ $i ], 0 , 1 ) == '-' ) {
switch ( $argv [ $i ]) {
case '-h' :
case '--help' :
mexit ( $help , 0 );
break ;
default :
mexit ( 'L’ opzione «' . $argv [ $i ] . '» è sconosciuta (usa «-h» per vedere la guida).' . N , 1 );
break ;
}
2020-10-14 08:37:41 +02:00
} elseif ( $argv [ $i ] == 'setold' ) {
2020-10-13 08:21:26 +02:00
$dosome = true ;
2020-10-14 08:37:41 +02:00
$opts [ 'setold' ] = true ;
} elseif ( $argv [ $i ] == 'setblack' ) {
2020-10-13 08:21:26 +02:00
$dosome = true ;
2020-10-14 08:37:41 +02:00
$opts [ 'setblack' ] = true ;
2020-10-20 22:06:00 +02:00
} elseif ( $argv [ $i ] == 'shuffle' ) {
$dosome = true ;
$opts [ 'shuffle' ] = true ;
2020-10-13 08:21:26 +02:00
} else {
mexit ( '«' . $argv [ $i ] . '» non è un’ azione conosciuta (usa «-h» per vedere la guida).' . N , 1 );
}
}
if ( ! $dosome ) mexit ( 'Nessuna azione specificata (usa «-h» per vedere la guida).' . N , 1 );
use function mysqli_real_escape_string as myesc ;
function mexit ( $msg , $code ) {
global $link ;
echo ( $msg );
if ( $link )
mysqli_close ( $link );
exit ( $code );
}
2020-10-18 06:53:27 +02:00
$inifp = __DIR__ . '/../conf/mustard.ini' ;
2020-10-13 08:21:26 +02:00
$iniarr =@ parse_ini_file ( $inifp )
or mexit ( 'Impossibile aprire il file di configurazione «' . $inifp . '»' . N , 1 );
$link =@ mysqli_connect ( $iniarr [ 'db_host' ], $iniarr [ 'db_admin_name' ], $iniarr [ 'db_admin_password' ], $iniarr [ 'db_name' ], $iniarr [ 'db_port' ], $iniarr [ 'db_socket' ])
or mexit ( 'Impossibile connettersi al server MySQL: ' . mysqli_connect_error () . N , 1 );
mysqli_set_charset ( $link , 'utf8mb4' )
or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 1 );
2020-10-14 08:37:41 +02:00
if ( $opts [ 'setold' ]) {
2020-10-13 08:21:26 +02:00
echo ( 'Setto New=0 per le istanze non più nuove ... ' );
mysqli_query ( $link , 'UPDATE Instances SET New=0 WHERE ' . time () . '-FirstSeen > ' . $opts [ 'newline' ]) or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 2 );
echo ( 'fatto! Righe coinvolte: ' . mysqli_affected_rows ( $link ) . N );
}
2020-10-14 08:37:41 +02:00
if ( $opts [ 'setblack' ]) {
2020-10-13 08:21:26 +02:00
echo ( 'Setto Blacklisted=1 per le istanze blacklistate ... ' );
mysqli_query ( $link , 'UPDATE Instances SET Blacklisted=1 WHERE URI IN (SELECT Domain FROM Blacklist)' ) or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 2 );
echo ( 'fatto! Righe coinvolte: ' . mysqli_affected_rows ( $link ) . N );
echo ( 'Setto Blacklisted=0 per le istanze non blacklistate ... ' );
mysqli_query ( $link , 'UPDATE Instances SET Blacklisted=0 WHERE URI NOT IN (SELECT Domain FROM Blacklist)' ) or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 2 );
echo ( 'fatto! Righe coinvolte: ' . mysqli_affected_rows ( $link ) . N );
}
2020-10-20 22:06:00 +02:00
if ( $opts [ 'shuffle' ]) {
echo ( 'Randomizzo i valori della colonna RPos ... ' );
$res = mysqli_query ( $link , 'SELECT ID FROM Instances' ) or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 2 );
$i = 0 ;
while ( $row = mysqli_fetch_assoc ( $res )) {
$i ++ ;
$buf [ $row [ 'ID' ]] = $i ;
}
shuffle ( $buf );
foreach ( $buf as $key => $val )
mysqli_query ( $link , 'UPDATE Instances SET RPos=' . $val . ' WHERE ID=' . $key ) or mexit ( __LINE__ . ': ' . mysqli_error ( $link ) . N , 2 );
echo ( 'fatto!' . N );
}
2020-10-13 08:21:26 +02:00
mysqli_close ( $link );
exit ( 0 );
?>