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

Commit 73afd31

Browse files
authored
Merge pull request #2578 from ec-europa/ISAICP-6686
ISAICP-6686: Remove spam entities.
2 parents 4dd625d + acae08a commit 73afd31

3 files changed

Lines changed: 111 additions & 25 deletions

File tree

web/modules/custom/joinup_core/joinup_core.deploy.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,91 @@
1313
*/
1414

1515
declare(strict_types = 1);
16+
17+
use Drupal\asset_release\Entity\AssetRelease;
18+
use Drupal\solution\Entity\SolutionInterface;
19+
20+
/**
21+
* Delete orphaned distributions.
22+
*/
23+
function joinup_core_deploy_0107500(array &$sandbox = []): string {
24+
$sparql = \Drupal::getContainer()->get('sparql.endpoint');
25+
$sparql_storage = \Drupal::entityTypeManager()->getStorage('rdf_entity');
26+
27+
if (empty($sandbox['ids'])) {
28+
$entities_query = <<<QUERY
29+
SELECT DISTINCT ?id
30+
WHERE {
31+
?id a <http://www.w3.org/ns/dcat#Distribution> .
32+
?id <http://joinup.eu/rdf_entity/group> ?group .
33+
FILTER NOT EXISTS { ?group a ?type }
34+
}
35+
QUERY;
36+
$entity_ids = $sparql->query($entities_query);
37+
$sandbox['ids'] = array_map(function (stdClass $resource): string {
38+
$resource = $resource->id;
39+
return $resource->getUri();
40+
}, $entity_ids->getArrayCopy());
41+
$sandbox['max'] = count($sandbox['ids']);
42+
$sandbox['count'] = 0;
43+
}
44+
45+
$entity_ids = array_splice($sandbox['ids'], 0, 5);
46+
foreach ($sparql_storage->loadMultiple($entity_ids) as $entity) {
47+
$entity->skip_notification = TRUE;
48+
$entity->delete();
49+
}
50+
51+
$sandbox['count'] += count($entity_ids);
52+
$sandbox['#finished'] = $sandbox['count'] / $sandbox['max'];
53+
return "Deleted {$sandbox['count']} out of {$sandbox['max']} orphaned distributions.";
54+
}
55+
56+
/**
57+
* Delete spam content from the specific user.
58+
*/
59+
function joinup_core_deploy_0107501(array &$sandbox = []): void {
60+
$mysql = \Drupal::database();
61+
$sparql = \Drupal::getContainer()->get('sparql.endpoint');
62+
$sparql_storage = \Drupal::entityTypeManager()->getStorage('rdf_entity');
63+
$file_storage = \Drupal::entityTypeManager()->getStorage('file');
64+
$user_storage = \Drupal::entityTypeManager()->getStorage('user');
65+
$file_system = \Drupal::service('file_system');
66+
67+
$entities_query = <<<QUERY
68+
SELECT DISTINCT ?id
69+
WHERE {
70+
?id <http://joinup.eu/owner/uid> 747137
71+
}
72+
QUERY;
73+
$entity_ids = $sparql->query($entities_query);
74+
$entity_ids = array_map(function (stdClass $resource): string {
75+
$resource = $resource->id;
76+
return $resource->getUri();
77+
}, $entity_ids->getArrayCopy());
78+
foreach ($sparql_storage->loadMultiple($entity_ids) as $entity) {
79+
if ($entity instanceof SolutionInterface || $entity instanceof AssetRelease) {
80+
if ($ids = $entity->getDistributionIds()) {
81+
foreach ($sparql_storage->loadMultiple($ids) as $entity) {
82+
$entity->skip_notification = TRUE;
83+
$entity->delete();
84+
}
85+
}
86+
}
87+
$entity->skip_notification = TRUE;
88+
$entity->delete();
89+
}
90+
91+
// Delete files.
92+
$results = $mysql->query('select fid, filename, uri from file_managed where uid = 747137;')->fetchAll();
93+
foreach ($results as $result) {
94+
if ($file = $file_storage->load($result->fid)) {
95+
$file->delete();
96+
}
97+
else {
98+
$file_system->delete($result->uri);
99+
}
100+
}
101+
102+
$user_storage->load(747137)->delete();
103+
}

web/modules/custom/joinup_core/joinup_core.post_update.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,3 @@
1414
*/
1515

1616
declare(strict_types = 1);
17-
18-
/**
19-
* Update the text format of the abstract field for collections.
20-
*/
21-
function joinup_core_post_update_0107400(&$sandbox): void {
22-
$graphs = [
23-
'http://joinup.eu/collection/draft',
24-
'http://joinup.eu/collection/published',
25-
];
26-
27-
// This query updates the text format of the abstract field for collections.
28-
// The field was updated to have a new sole format but the existing data were
29-
// not updated.
30-
foreach ($graphs as $graph) {
31-
$query = <<<QUERY
32-
WITH <{$graph}>
33-
DELETE { ?entity_id <http://joinup.eu/text_format> "basic_html"^^<http://www.w3.org/2001/XMLSchema#string> }
34-
INSERT { ?entity_id <http://joinup.eu/text_format> "essential_html"^^<http://www.w3.org/2001/XMLSchema#string> }
35-
WHERE { ?entity_id <http://joinup.eu/text_format> "basic_html"^^<http://www.w3.org/2001/XMLSchema#string> }
36-
QUERY;
37-
38-
\Drupal::getContainer()->get('sparql.endpoint')->query($query);
39-
}
40-
41-
}

web/modules/custom/joinup_notification/joinup_notification.module

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ declare(strict_types = 1);
99

1010
use Drupal\Core\Entity\EntityInterface;
1111
use Drupal\Core\Render\Markup;
12+
use Drupal\asset_distribution\Entity\AssetDistributionInterface;
13+
use Drupal\asset_distribution\Exception\MissingDistributionParentException;
1214
use Drupal\joinup_community_content\Entity\CommunityContentInterface;
15+
use Drupal\joinup_group\Entity\GroupContentInterface;
16+
use Drupal\joinup_group\Exception\MissingGroupException;
1317
use Drupal\joinup_notification\Event\NotificationEvent;
1418
use Drupal\joinup_notification\NotificationEvents;
1519
use Drupal\rdf_entity\RdfInterface;
@@ -116,6 +120,25 @@ function joinup_notification_rdf_entity_update(EntityInterface $entity) {
116120
* Implements hook_ENTITY_TYPE_delete().
117121
*/
118122
function joinup_notification_rdf_entity_predelete(EntityInterface $entity) {
123+
// Avoid sending notifications e.g. when orphaned entities are deleted.
124+
if ($entity instanceof AssetDistributionInterface) {
125+
try {
126+
$entity->getParent();
127+
}
128+
catch (MissingDistributionParentException $e) {
129+
return;
130+
}
131+
}
132+
133+
if ($entity instanceof GroupContentInterface) {
134+
try {
135+
$entity->getGroup();
136+
}
137+
catch (MissingGroupException $e) {
138+
return;
139+
}
140+
}
141+
119142
joinup_notification_dispatch_notification('delete', NotificationEvents::RDF_ENTITY_CRUD, $entity);
120143
}
121144

0 commit comments

Comments
 (0)