|
13 | 13 | */ |
14 | 14 |
|
15 | 15 | 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 | +} |
0 commit comments