Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2578 from ec-europa/ISAICP-6686
Browse files Browse the repository at this point in the history
ISAICP-6686: Remove spam entities.
  • Loading branch information
idimopoulos authored Sep 8, 2021
2 parents 4dd625d + acae08a commit 73afd31
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 25 deletions.
88 changes: 88 additions & 0 deletions web/modules/custom/joinup_core/joinup_core.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,91 @@
*/

declare(strict_types = 1);

use Drupal\asset_release\Entity\AssetRelease;
use Drupal\solution\Entity\SolutionInterface;

/**
* Delete orphaned distributions.
*/
function joinup_core_deploy_0107500(array &$sandbox = []): string {
$sparql = \Drupal::getContainer()->get('sparql.endpoint');
$sparql_storage = \Drupal::entityTypeManager()->getStorage('rdf_entity');

if (empty($sandbox['ids'])) {
$entities_query = <<<QUERY
SELECT DISTINCT ?id
WHERE {
?id a <http://www.w3.org/ns/dcat#Distribution> .
?id <http://joinup.eu/rdf_entity/group> ?group .
FILTER NOT EXISTS { ?group a ?type }
}
QUERY;
$entity_ids = $sparql->query($entities_query);
$sandbox['ids'] = array_map(function (stdClass $resource): string {
$resource = $resource->id;
return $resource->getUri();
}, $entity_ids->getArrayCopy());
$sandbox['max'] = count($sandbox['ids']);
$sandbox['count'] = 0;
}

$entity_ids = array_splice($sandbox['ids'], 0, 5);
foreach ($sparql_storage->loadMultiple($entity_ids) as $entity) {
$entity->skip_notification = TRUE;
$entity->delete();
}

$sandbox['count'] += count($entity_ids);
$sandbox['#finished'] = $sandbox['count'] / $sandbox['max'];
return "Deleted {$sandbox['count']} out of {$sandbox['max']} orphaned distributions.";
}

/**
* Delete spam content from the specific user.
*/
function joinup_core_deploy_0107501(array &$sandbox = []): void {
$mysql = \Drupal::database();
$sparql = \Drupal::getContainer()->get('sparql.endpoint');
$sparql_storage = \Drupal::entityTypeManager()->getStorage('rdf_entity');
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
$file_system = \Drupal::service('file_system');

$entities_query = <<<QUERY
SELECT DISTINCT ?id
WHERE {
?id <http://joinup.eu/owner/uid> 747137
}
QUERY;
$entity_ids = $sparql->query($entities_query);
$entity_ids = array_map(function (stdClass $resource): string {
$resource = $resource->id;
return $resource->getUri();
}, $entity_ids->getArrayCopy());
foreach ($sparql_storage->loadMultiple($entity_ids) as $entity) {
if ($entity instanceof SolutionInterface || $entity instanceof AssetRelease) {
if ($ids = $entity->getDistributionIds()) {
foreach ($sparql_storage->loadMultiple($ids) as $entity) {
$entity->skip_notification = TRUE;
$entity->delete();
}
}
}
$entity->skip_notification = TRUE;
$entity->delete();
}

// Delete files.
$results = $mysql->query('select fid, filename, uri from file_managed where uid = 747137;')->fetchAll();
foreach ($results as $result) {
if ($file = $file_storage->load($result->fid)) {
$file->delete();
}
else {
$file_system->delete($result->uri);
}
}

$user_storage->load(747137)->delete();
}
25 changes: 0 additions & 25 deletions web/modules/custom/joinup_core/joinup_core.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,3 @@
*/

declare(strict_types = 1);

/**
* Update the text format of the abstract field for collections.
*/
function joinup_core_post_update_0107400(&$sandbox): void {
$graphs = [
'http://joinup.eu/collection/draft',
'http://joinup.eu/collection/published',
];

// This query updates the text format of the abstract field for collections.
// The field was updated to have a new sole format but the existing data were
// not updated.
foreach ($graphs as $graph) {
$query = <<<QUERY
WITH <{$graph}>
DELETE { ?entity_id <http://joinup.eu/text_format> "basic_html"^^<http://www.w3.org/2001/XMLSchema#string> }
INSERT { ?entity_id <http://joinup.eu/text_format> "essential_html"^^<http://www.w3.org/2001/XMLSchema#string> }
WHERE { ?entity_id <http://joinup.eu/text_format> "basic_html"^^<http://www.w3.org/2001/XMLSchema#string> }
QUERY;

\Drupal::getContainer()->get('sparql.endpoint')->query($query);
}

}
23 changes: 23 additions & 0 deletions web/modules/custom/joinup_notification/joinup_notification.module
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ declare(strict_types = 1);

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Markup;
use Drupal\asset_distribution\Entity\AssetDistributionInterface;
use Drupal\asset_distribution\Exception\MissingDistributionParentException;
use Drupal\joinup_community_content\Entity\CommunityContentInterface;
use Drupal\joinup_group\Entity\GroupContentInterface;
use Drupal\joinup_group\Exception\MissingGroupException;
use Drupal\joinup_notification\Event\NotificationEvent;
use Drupal\joinup_notification\NotificationEvents;
use Drupal\rdf_entity\RdfInterface;
Expand Down Expand Up @@ -116,6 +120,25 @@ function joinup_notification_rdf_entity_update(EntityInterface $entity) {
* Implements hook_ENTITY_TYPE_delete().
*/
function joinup_notification_rdf_entity_predelete(EntityInterface $entity) {
// Avoid sending notifications e.g. when orphaned entities are deleted.
if ($entity instanceof AssetDistributionInterface) {
try {
$entity->getParent();
}
catch (MissingDistributionParentException $e) {
return;
}
}

if ($entity instanceof GroupContentInterface) {
try {
$entity->getGroup();
}
catch (MissingGroupException $e) {
return;
}
}

joinup_notification_dispatch_notification('delete', NotificationEvents::RDF_ENTITY_CRUD, $entity);
}

Expand Down

0 comments on commit 73afd31

Please sign in to comment.