2020-11-29 01:55:31 +01:00
< ? php
2023-08-20 13:58:18 +02:00
include " config.inc.php " ;
include ( " $file_newsportal " );
2020-11-29 01:55:31 +01:00
2023-08-20 13:58:18 +02:00
if ( filemtime ( $spooldir . '/' . $config_name . '-expire-timer' ) + 86400 > time ()) {
exit ();
}
$lockfile = $lockdir . '/' . $config_name . '-spoolnews.lock' ;
$pid = file_get_contents ( $lockfile );
if ( posix_getsid ( $pid ) === false || ! is_file ( $lockfile )) {
2020-11-29 01:55:31 +01:00
print " Starting expire... \n " ;
file_put_contents ( $lockfile , getmypid ()); // create lockfile
2023-08-20 13:58:18 +02:00
} else {
2020-11-29 01:55:31 +01:00
print " expire currently running \n " ;
2023-08-20 13:58:18 +02:00
exit ();
}
$webserver_group = $CONFIG [ 'webserver_user' ];
$logfile = $logdir . '/expire.log' ;
$grouplist = file ( $config_dir . '/' . $config_name . '/groups.txt' , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
foreach ( $grouplist as $groupline ) {
$groupname = explode ( ' ' , $groupline );
$group = $groupname [ 0 ];
if ( $group [ 0 ] == ':' ) {
continue ;
}
2023-08-27 16:05:53 +02:00
// Delete over $max_articles_per_group if so configured in $OVERRIDES
2023-08-20 13:58:18 +02:00
$expire_conf = $CONFIG [ 'expire_days' ];
2023-08-27 16:05:53 +02:00
$override_days = convert_max_articles_to_days ( $group );
2023-08-20 13:58:18 +02:00
$expire_user = get_config_value ( 'expire.conf' , $group );
2023-08-27 16:05:53 +02:00
/*
* Order of preference is :
* 1. value in $config_dir / expire . conf
* 2. value in section config file OR $config_dir / overrides . inc . php
* whichever is lower
*/
$expire = $expire_conf ;
if ( $override_days ) {
$expire = $override_days ;
}
2023-08-20 13:58:18 +02:00
if ( $expire_user !== false ) {
$expire = $expire_user ;
}
2023-08-27 16:05:53 +02:00
$expire = trim ( $expire );
2023-08-20 13:58:18 +02:00
if ( $expire < 1 ) {
2023-10-04 15:27:08 +02:00
vacuum_group_database ( $group );
2023-08-20 13:58:18 +02:00
continue ;
}
$expireme = time () - ( $expire * 86400 );
$showme = date ( 'd M, Y' , $expireme );
2020-12-19 02:14:39 +01:00
2023-08-27 16:05:53 +02:00
echo " Expire $group articles before " . $showme . " ( " . $expire . " ) days \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Expiring articles before " . $showme . " ( " . $expire . " ) days " , FILE_APPEND );
2023-08-20 13:58:18 +02:00
if ( $CONFIG [ 'article_database' ] == '1' ) {
$database = $spooldir . '/' . $group . '-articles.db3' ;
if ( is_file ( $database )) {
$articles_dbh = article_db_open ( $database );
2023-08-23 21:25:21 +02:00
$articles_stmt = $articles_dbh -> prepare ( 'DELETE FROM articles WHERE newsgroup=:newsgroup AND number=:number' );
2023-08-20 13:58:18 +02:00
}
}
// Expire tradspool and remove from newsportal
2023-08-23 21:25:21 +02:00
echo " Expiring articles database, overview database and writing history... \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Expiring articles database, overview database and writing history... " , FILE_APPEND );
2023-08-16 18:41:13 +02:00
2023-08-20 13:58:18 +02:00
$database = $spooldir . '/articles-overview.db3' ;
2023-08-16 18:41:13 +02:00
$dbh = overview_db_open ( $database );
2024-04-01 02:10:55 +02:00
$query = $dbh -> prepare ( 'SELECT number FROM overview WHERE newsgroup=:newsgroup AND CAST(date AS int)<:expireme' );
2023-08-20 13:58:18 +02:00
$query -> execute ([
':newsgroup' => $group ,
':expireme' => $expireme
]);
2023-08-25 13:21:06 +02:00
$get_row = array ();
while ( $query_row = $query -> fetch ()) {
$get_row [] = $query_row ;
}
2024-04-01 02:10:55 +02:00
$stmt = $dbh -> prepare ( 'DELETE FROM overview WHERE newsgroup=:newsgroup AND CAST(date AS int)<:expireme' );
2023-08-16 18:41:13 +02:00
$grouppath = preg_replace ( '/\./' , '/' , $group );
$status = " deleted " ;
$statusdate = time ();
$statusreason = " expired " ;
2023-08-20 13:58:18 +02:00
$i = 0 ;
2023-08-27 16:05:53 +02:00
foreach ( $get_row as $row ) {
2023-08-20 13:58:18 +02:00
if ( is_file ( $spooldir . '/articles/' . $grouppath . '/' . $row [ 'number' ])) {
unlink ( $spooldir . '/articles/' . $grouppath . '/' . $row [ 'number' ]);
2023-08-08 14:54:15 +02:00
}
2023-08-23 21:25:21 +02:00
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Expiring: " . $row [ 'number' ], FILE_APPEND );
if ( $CONFIG [ 'article_database' ] == '1' ) {
try {
$articles_dbh -> setAttribute ( PDO :: ATTR_ERRMODE , PDO :: ERRMODE_EXCEPTION );
$articles_stmt -> execute ([
':newsgroup' => $group ,
':number' => $row [ 'number' ]
]);
} catch ( Exception $e ) {
echo 'Caught exception: ' . $e -> getMessage ();
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Caught exception: " . $e -> getMessage (), FILE_APPEND );
}
}
2023-08-16 18:41:13 +02:00
add_to_history ( $group , $row [ 'number' ], $row [ 'msgid' ], $status , $statusdate , $statusreason , $statusnotes );
2023-08-20 13:58:18 +02:00
$i ++ ;
2020-11-29 01:55:31 +01:00
}
2023-08-20 13:58:18 +02:00
$stmt -> execute ([
':newsgroup' => $group ,
':expireme' => $expireme
]);
2023-08-16 18:41:13 +02:00
$dbh = null ;
2023-08-23 21:25:21 +02:00
if ( $articles_dbh ) {
2023-08-26 12:43:11 +02:00
// Delete any extraneous articles from group-articles database
2024-04-01 02:10:55 +02:00
$articles_stmt = $articles_dbh -> prepare ( 'DELETE FROM articles WHERE newsgroup=:newsgroup AND CAST(date AS int)<:expireme' );
2023-08-26 12:43:11 +02:00
$articles_stmt -> execute ([
':newsgroup' => $group ,
':expireme' => $expireme
]);
2023-08-23 21:25:21 +02:00
$articles_dbh = null ;
}
2023-08-16 18:41:13 +02:00
unlink ( $lockfile );
2023-08-20 13:58:18 +02:00
touch ( $spooldir . '/' . $config_name . '-expire-timer' );
2023-08-29 15:38:55 +02:00
if ( $i > 50 ) {
echo " Rebuilding threads for " . $group . " ... \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Rebuilding threads... " , FILE_APPEND );
unlink ( $spooldir . '/' . $group . '/-info.txt' );
$ns = nntp_open ();
if ( ! $ns ) {
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " Failed to connect to " . $CONFIG [ 'remote_server' ] . " : " . $CONFIG [ 'remote_port' ], FILE_APPEND );
exit ();
}
thread_load_newsserver ( $ns , $group , 0 );
}
2023-10-04 15:27:08 +02:00
vacuum_group_database ( $group );
2023-08-20 13:58:18 +02:00
echo " Expired " . $i . " articles for " . $group . " \n " ;
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " Expired " . $i . " articles " , FILE_APPEND );
2023-08-19 17:03:25 +02:00
}
2023-08-27 16:05:53 +02:00
2023-10-04 15:27:08 +02:00
function vacuum_group_database ( $group )
{
global $spooldir , $logfile , $config_name , $OVERRIDES , $CONFIG ;
if ( $CONFIG [ 'article_database' ] == '1' ) {
$database = $spooldir . '/' . $group . '-articles.db3' ;
if ( $article_dbh = article_db_open ( $database )) {
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " VACUUM article database... " , FILE_APPEND );
$article_stmt = $article_dbh -> prepare ( 'VACUUM' );
$article_stmt -> execute ();
$article_dbh = null ;
}
}
$database = $spooldir . '/' . $group . '-data.db3' ;
if ( $data_dbh = threads_db_open ( $database )) {
file_put_contents ( $logfile , " \n " . format_log_date () . " " . $config_name . " " . $group . " VACUUM threads database... " , FILE_APPEND );
$data_stmt = $data_dbh -> prepare ( 'VACUUM' );
$data_stmt -> execute ();
$data_dbh = null ;
}
}
2023-08-27 16:05:53 +02:00
function convert_max_articles_to_days ( $group )
{
global $spooldir , $OVERRIDES , $CONFIG ;
if ( $OVERRIDES [ 'max_articles_per_group' ] > 0 ) {
$count = $OVERRIDES [ 'max_articles_per_group' ];
} else {
return false ;
}
$database = $spooldir . '/articles-overview.db3' ;
$overview_dbh = overview_db_open ( $database );
2024-04-01 02:10:55 +02:00
$overview_query = $overview_dbh -> prepare ( 'SELECT * FROM overview WHERE newsgroup=:newsgroup ORDER BY CAST(date AS int) DESC LIMIT :count' );
2023-08-27 16:05:53 +02:00
$overview_query -> execute ([
':newsgroup' => $group ,
':count' => $count
]);
$i = 0 ;
while ( $row = $overview_query -> fetch ()) {
$i ++ ;
if ( $i == $count ) {
$found = $row ;
}
}
$overview_dbh = null ;
if ( $found ) {
$days = (( time () - $found [ 'date' ]) / 86400 );
return ( round ( $days ));
} else {
return false ;
}
}