View source
<?php
namespace Drupal\httpbl\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\system\Form\PrepareModulesEntityUninstallForm;
use Drupal\httpbl\Entity\Host;
use Drupal\Component\Render\FormattableMarkup;
class PrepareHttpblEntityUninstallForm extends PrepareModulesEntityUninstallForm {
protected $entityTypeId;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type_manager);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'httpbl_prepare_modules_entity_uninstall';
}
public function getQuestion() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this
->t('You are about to delete all @entity_type_plural + Ban_ip records banned by HttpBL. Are you sure you want to do this?', [
'@entity_type_plural' => $entity_type
->getPluralLabel(),
]);
}
public function getDescription() {
$check_link = Link::fromTextAndUrl(t('Please ensure all Http:BL Blocking is disabled'), Url::fromRoute('httpbl.admin_config'))
->toString();
$blacklist_report_link = Link::fromTextAndUrl(t('Http:BL blacklisted hosts'), Url::fromRoute('view.evaluated_hosts.page_banned'))
->toString();
$message = $this
->t('@check (otherwise new entities will be created during the process of deleting them).<br />', [
'@check' => $check_link,
]);
$message .= $this
->t('This action affects two tables <strong>(httpbl_host and ban_ip)</strong> and cannot be undone.<br />');
$message .= $this
->t('Any blacklisted hosts in Http:BL will also be removed from Ban if found there.<br />');
$message .= $this
->t('You can preview all @blacklisted here. These will be un-banned.<br />', [
'@blacklisted' => $blacklist_report_link,
]);
$message .= $this
->t('Make a backup of your database if you want to be able to restore these items.');
return $message;
}
public function getConfirmText() {
$entity_type = $this->entityTypeManager
->getDefinition($this->entityTypeId);
return $this
->t('Delete and Un-Ban all @entity_type_plural', [
'@entity_type_plural' => $entity_type
->getPluralLabel(),
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity_type_id = $form_state
->getValue('entity_type_id');
$entity_type_plural = $this->entityTypeManager
->getDefinition($entity_type_id)
->getPluralLabel();
$batch = [
'title' => t('Deleting @entity_type_plural', [
'@entity_type_plural' => $entity_type_plural,
]),
'operations' => [
[
[
__CLASS__,
'deleteContentEntities',
],
[
$entity_type_id,
],
],
],
'finished' => [
__CLASS__,
'moduleBatchFinished',
],
'progress_message' => '',
];
batch_set($batch);
}
public static function deleteContentEntities($entity_type_id, &$context) {
$storage = \Drupal::entityTypeManager()
->getStorage($entity_type_id);
$context['results']['entity_type_id'] = $entity_type_id;
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $storage
->getQuery()
->count()
->execute();
}
$entity_type = \Drupal::entityTypeManager()
->getDefinition($entity_type_id);
$entity_ids = $storage
->getQuery()
->sort($entity_type
->getKey('id'), 'ASC')
->range(0, 10)
->execute();
if ($entities = $storage
->loadMultiple($entity_ids)) {
$banManager = \Drupal::service('ban.ip_manager');
foreach ($entities as $key => $host) {
$host = Host::load($key);
$host_ip = $host
->getHostIp();
$banned = $banManager
->isBanned($host_ip);
if ($banned) {
$banManager
->unBanIp($host_ip);
$message = new FormattableMarkup('Unbanned @ip banned by Httpbl while uninstalled.', [
'@ip' => $host_ip,
]);
\Drupal::logger('httpbl')
->warning($message);
}
}
$storage
->delete($entities);
}
$context['sandbox']['progress'] = $context['sandbox']['max'] - $storage
->getQuery()
->count()
->execute();
if (count($entity_ids) > 0 && $context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
$context['message'] = t('Deleting items... Completed @percentage% (@current of @total).', [
'@percentage' => round(100 * $context['sandbox']['progress'] / $context['sandbox']['max']),
'@current' => $context['sandbox']['progress'],
'@total' => $context['sandbox']['max'],
]);
}
else {
$context['finished'] = 1;
}
}
}