`id` != \''.$this->getUpdate()->getUpdateId().'\' AND
`chat_id` NOT IN (SELECT `id` FROM `chat` WHERE `chat_id` = `chat`.`id`) AND
(`message_id` IS NOT NULL AND `message_id` IN (SELECT f.id FROM `message` f WHERE `date` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['telegram_update'])).'\')) OR
(`edited_message_id` IS NOT NULL AND `edited_message_id` IN (SELECT f.id FROM `edited_message` f WHERE `edit_date` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['telegram_update'])).'\')) OR
(`inline_query_id` IS NOT NULL AND `inline_query_id` IN (SELECT f.id FROM `inline_query` f WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['telegram_update'])).'\')) OR
(`chosen_inline_result_id` IS NOT NULL AND `chosen_inline_result_id` IN (SELECT f.id FROM `chosen_inline_result` f WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['telegram_update'])).'\')) OR
(`callback_query_id` IS NOT NULL AND `callback_query_id` IN (SELECT f.id FROM `callback_query` f WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['telegram_update'])).'\'))';
$queries[]='DELETE FROM `user_chat` WHERE `user_id` IN (SELECT f.id FROM `user` f WHERE `updated_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['chat'])).'\')'.PHP_EOL;
$queries[]='DELETE FROM `inline_query` WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['inline_query'])).'\' AND `id` NOT IN (SELECT `inline_query_id` FROM `telegram_update` WHERE `inline_query_id` = `inline_query`.`id`)';
$queries[]='DELETE FROM `chosen_inline_result` WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['chosen_inline_result'])).'\' AND `id` NOT IN (SELECT `chosen_inline_result_id` FROM `telegram_update` WHERE `chosen_inline_result_id` = `chosen_inline_result`.`id`)';
$queries[]='DELETE FROM `callback_query` WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['callback_query'])).'\' AND `id` NOT IN (SELECT `callback_query_id` FROM `telegram_update` WHERE `callback_query_id` = `callback_query`.`id`)'.PHP_EOL;
$queries[]='DELETE FROM `edited_message` WHERE `edit_date` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['edited_message'])).'\' AND `id` NOT IN (SELECT `message_id` FROM `telegram_update` WHERE `edited_message_id` = `edited_message`.`id`)'.PHP_EOL;
$queries[]='DELETE FROM `message` WHERE `date` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['message'])).'\' AND `id` NOT IN (SELECT `message_id` FROM `callback_query` WHERE `message_id` = `message`.`id`) AND `id` NOT IN (SELECT `message_id` FROM `telegram_update` WHERE `message_id` = `message`.`id`)'.PHP_EOL;
$queries[]='DELETE FROM `botan_shortener` WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['botan_shortener'])).'\'';
$queries[]='DELETE FROM `request_limiter` WHERE `created_at` < \''.date('Y-m-d H:i:s',strtotime('-'.$settings['time_to_clean']['request_limiter'])).'\'';
$data['text']='Only available in a private chat.';
returnRequest::sendMessage($data);
}
$settings=$this->getSettings($text);
$queries=$this->getQueries($settings);
$tables='';
foreach($settings['tables_to_clean']as$table){
if(!empty($tables)){
$tables.=', ';
}
$tables.='*'.$table.'*';
$time=$settings['time_to_clean'][$table];
if(isset($time)){
$tables.=' ('.$time.')';
}
}
$data['text']='Cleaning up tables:'.PHP_EOL.' '.$tables;
Request::sendMessage($data);
$rows=0;
$pdo=DB::getPdo();
try{
$pdo->beginTransaction();
foreach($queriesas$query){
$dbq=$pdo->prepare($query);
if($dbq->execute()){
$rows+=$dbq->rowCount();
}else{
TelegramLog::error('Error while executing query: '.$query.PHP_EOL);
}
}
}catch(PDOException$e){
$pdo->rollBack();// rollback changes on exception (useful if you want to track down error - you can't replicate it when some of the data is already deleted...)
thrownewTelegramException($e->getMessage());
}finally{
$pdo->commit();// commit changes to the database and end transaction