setName(self::NAME) ->setDefinition( [ new InputOption( 'days-old', 'd', InputOption::VALUE_OPTIONAL, 'Purge records older than this number of days. Defaults to 365.', 365 ), new InputOption('dry-run', 'r', InputOption::VALUE_NONE, 'Performs a dry run. Shows no. of affected rows. Won\'t actually delete anything.'), new InputOption('gdpr', 'g', InputOption::VALUE_NONE, 'Deletes records of inactive users to fulfill GDPR requirements.'), ] ) ->setHelp( <<<'EOT' %command.name% purges records of anonymous contacts (unless the --gdpr flag is set) that are older than 365 days. Adjust the threshold by using --days-old. %command.name% --gdpr purges records of anonymous and identified contacts. The command purges only identified contacts that were inactive for more than 3 years (1095 days). If you set --gdpr then %command.name% will ignore --days-old. The threshold is hard coded to 1095 days. This is security measure to prevent accidental loss of contact data. Examples: php %command.full_name% Deletes records of anonymous contacts older than 365 days. php %command.full_name% --days-old=90 Deletes records of anonymous contacts older than 90 days. php %command.full_name% --gdpr Deletes records of anonymous and inactive identified contacts older than 1095 days. Add --dry-run to do a dry run without deleting any records. php %command.full_name% --dry-run Shows you how many records of anonymous contacts %command.name% will purge. The %command.name% command dispatches the CoreEvents::MAINTENANCE_CLEANUP_DATA event in order to purge old data (data must be supported by event listeners, as not all data is applicable to be purged). EOT ); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { if (!$this->checkRunStatus($input, $output)) { return \Symfony\Component\Console\Command\Command::SUCCESS; } $daysOld = $input->getOption('days-old'); $dryRun = (bool) $input->getOption('dry-run'); $noInteraction = $input->getOption('no-interaction'); $gdpr = $input->getOption('gdpr'); if (empty($daysOld) && empty($gdpr)) { // Safety catch; bail return \Symfony\Component\Console\Command\Command::FAILURE; } if (!empty($gdpr)) { // Override threshold to delete records of inactive users, default 3 years $daysOld = $this->coreParametersHelper->get('mautic.gdpr_user_purge_threshold', 1095); } if (empty($dryRun) && empty($noInteraction)) { /** @var \Symfony\Component\Console\Helper\SymfonyQuestionHelper $helper */ $helper = $this->getHelperSet()->get('question'); $question = new ConfirmationQuestion( ''.$this->translator->trans('mautic.maintenance.confirm_data_purge', ['%days%' => $daysOld]).' ', false ); if (!$helper->ask($input, $output, $question)) { $this->completeRun(); return \Symfony\Component\Console\Command\Command::SUCCESS; } } $event = new MaintenanceEvent($daysOld, !empty($dryRun), !empty($gdpr)); $this->dispatcher->dispatch($event, CoreEvents::MAINTENANCE_CLEANUP_DATA); $stats = $event->getStats(); $rows = []; foreach ($stats as $key => $count) { $rows[] = [$key, $count]; } $table = new Table($output); $table ->setHeaders([$this->translator->trans('mautic.maintenance.header.key'), $this->translator->trans('mautic.maintenance.header.records_affected')]) ->setRows($rows); $table->render(); if ('dev' == MAUTIC_ENV) { $output->writeln('Debug'); $debug = $event->getDebug(); foreach ($debug as $key => $query) { $output->writeln("$key"); $output->writeln($query); } } // store to audit log $this->storeToAuditLog($stats, $dryRun, $input->getOptions()); $this->completeRun(); return \Symfony\Component\Console\Command\Command::SUCCESS; } /** * @param array $stats * @param array|null> $options */ protected function storeToAuditLog(array $stats, bool $dryRun, array $options): void { $notEmptyStats = array_filter($stats); if (!$dryRun && count($notEmptyStats)) { $log = [ 'userName' => 'system', 'userId' => 0, 'bundle' => 'core', 'object' => 'maintenance', 'objectId' => 0, 'action' => 'cleanup', 'details' => [ 'options' => array_filter($options), 'stats' => $notEmptyStats, ], 'ipAddress' => $this->ipLookupHelper->getIpAddressFromRequest(), ]; $this->auditLogModel->writeToLog($log); } } protected static $defaultDescription = 'Updates the Mautic application'; }