forked from openeuropa/rdf_entity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf_entity.drush.inc
73 lines (67 loc) · 1.85 KB
/
rdf_entity.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @file
* Purge the triple store from the CLI.
*/
use Drush\Log\LogLevel;
use Drupal\search_api\Entity\Server;
use Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend;
/**
* Implements hook_drush_command().
*/
function rdf_entity_drush_command() {
$items = [];
$items['rdf-entity-purge'] = [
'description' => 'Delete the whole index at the Sparql endpoint.',
'examples' => [
'drush rdf-entity-purge' => dt('Purge the triple store.'),
'drush rep' => dt('Alias to purge the triple store.'),
],
'aliases' => ['rep'],
];
return $items;
}
/**
* Delete all data from the endpoint.
*/
function drush_rdf_entity_purge() {
/** @var Drupal\rdf_entity\Database\Driver\sparql\ConnectionInterface $endpoint */
$endpoint = \Drupal::service('sparql_endpoint');
$query = "SELECT COUNT (?s) AS ?count WHERE {?s ?p ?o}";
$response = $endpoint->query($query);
$count = 0;
foreach ($response as $value) {
$count = (string) $value->count;
}
// Issuing a delete on an empty store throws errors, so only delete if needed.
if ($count) {
$query = <<<QUERY
DELETE {
GRAPH ?g {
?entity ?field ?value
}
}
WHERE {
GRAPH ?g {
?entity ?field ?value .
}
}
QUERY;
$endpoint->query($query);
$search_api_instances = [
'solr_published' => 'published',
'solr_unpublished' => 'unpublished',
];
foreach ($search_api_instances as $server => $index) {
$backend = Server::load($server)->getBackend();
if ($backend instanceof SearchApiSolrBackend && $backend->getSolrConnector()->pingCore()) {
$index = $backend->getServer()->getIndexes()[$index];
$backend->deleteAllIndexItems($index);
}
}
drush_log('All triples have been removed.', LogLevel::INFO);
}
else {
drush_log('Triple store is empty, nothing to delete.', LogLevel::INFO);
}
}