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 #2481 from ec-europa/ISAICP-6523
Browse files Browse the repository at this point in the history
ISAICP-6523: Apply SA-CORE-2021-003
  • Loading branch information
idimopoulos authored May 27, 2021
2 parents 81abf6f + 14b0504 commit 519d616
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 195 deletions.
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 0 additions & 162 deletions web/modules/custom/joinup_core/joinup_core.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,165 +13,3 @@
*/

declare(strict_types = 1);

/**
* Restore the field policy domain node field into the new topic field.
*/
function joinup_core_deploy_0107100(array &$sandbox): void {
// WARNING: Needs to run first among the deploy hooks.
$database = \Drupal::database();
$schema = $database->schema();
$field_schema_definition = [
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'description' => 'The ID of the target entity.',
];
$indexes = [
'fields' => [
'field_topic_target_id' => $field_schema_definition,
],
'indexes' => [
'field_topic_target_id' => ['field_topic_target_id'],
],
];

$schema->dropTable('node__field_topic');
$schema->renameTable('node__field_topic_backup', 'node__field_topic');
// The "changeField" happens in the deploy phase so that we can use the API
// to perform the changes because the command to change the field name differs
// even from mariaDB to MySQL.
$schema->changeField('node__field_topic', 'field_policy_domain_target_id', 'field_topic_target_id', $field_schema_definition, $indexes);
$schema->dropIndex('node__field_topic', 'field_policy_domain_target_id');
$database->query("ALTER TABLE node__field_topic COMMENT 'Data storage for node field field_topic.'");

$schema->dropTable('node_revision__field_topic');
$schema->renameTable('node_revision__field_topic_backup', 'node_revision__field_topic');
$schema->changeField('node_revision__field_topic', 'field_policy_domain_target_id', 'field_topic_target_id', $field_schema_definition, $indexes);
$schema->dropIndex('node_revision__field_topic', 'field_policy_domain_target_id');
$database->query("ALTER TABLE node_revision__field_topic COMMENT 'Revision archive storage for node field field_topic.'");

$sparql_endpoint = \Drupal::getContainer()->get('sparql.endpoint');

$query = <<<QUERY
DELETE { GRAPH <http://topic> { ?entity <http://www.w3.org/2004/02/skos/core#inScheme> <http://joinup.eu/policy-domain> } }
INSERT { GRAPH <http://topic> { ?entity <http://www.w3.org/2004/02/skos/core#inScheme> <http://joinup.eu/vocabulary/topic> } }
WHERE { GRAPH <http://topic> { ?entity <http://www.w3.org/2004/02/skos/core#inScheme> <http://joinup.eu/policy-domain> } }
QUERY;
$sparql_endpoint->query($query);

// Update the references from <http://joinup.eu/policy-domain> to
// <http://joinup.eu/vocabulary/topic> in all graphs that have the
// topic reference.
$query = <<<QUERY
DELETE { GRAPH ?g { ?entity <http://policy_domain> ?value } }
INSERT { GRAPH ?g { ?entity <http://joinup.eu/vocabulary/topic> ?value } }
WHERE { GRAPH ?g { ?entity <http://policy_domain> ?value } }
QUERY;
$sparql_endpoint->query($query);

// Change the policy-domain term in the ID of the topics.
// The "http://joinup.eu/ontology/policy-domain" contains 39 characters. So
// We start from character 40 but we subtract only 39 characters from the
// length due because the characters start at 0.
$query = <<<QUERY
WITH <http://topic>
DELETE { ?oldUri ?p ?o }
INSERT { ?newUri ?p ?o }
WHERE {
?oldUri ?p ?o .
BIND(IRI(CONCAT("http://joinup.eu/ontology/topic", SUBSTR(STR(?oldUri), 40, STRLEN(STR(?oldUri)) - 39))) as ?newUri)
}
QUERY;
$sparql_endpoint->query($query);

// Update all references to the policy domain terms in SPARQL.
$query = <<<QUERY
DELETE { GRAPH ?g { ?s ?p ?oldUri } }
INSERT { GRAPH ?g { ?s ?p ?newUri } }
WHERE {
GRAPH ?g {
?s ?p ?oldUri .
FILTER isIri(?oldUri) .
FILTER (CONTAINS(STR(?oldUri), "http://joinup.eu/ontology/policy-domain")) .
BIND(IRI(CONCAT("http://joinup.eu/ontology/topic", SUBSTR(STR(?oldUri), 40, STRLEN(STR(?oldUri)) - 39))) as ?newUri)
}
}
QUERY;
$sparql_endpoint->query($query);

// Update all instances of the policy domain terms in the node field tables.
$tables = [
'node__field_topic',
'node_revision__field_topic',
];
foreach ($tables as $table) {
$query = <<<QUERY
UPDATE {$table}
SET `field_topic_target_id` =
REPLACE(`field_topic_target_id`, 'http://joinup.eu/ontology/policy-domain', 'http://joinup.eu/ontology/topic');
QUERY;
$database->query($query);
}

// Cleanup the {cachetags} table.
$database->delete('cachetags')
->condition('tag', '%policy_domain%', 'LIKE')
->execute();

// Update the user professional domain.
$query = <<<QUERY
UPDATE user__field_user_professional_domain
SET `field_user_professional_domain_target_id` =
REPLACE(`field_user_professional_domain_target_id`, 'http://joinup.eu/ontology/policy-domain', 'http://joinup.eu/ontology/topic');
QUERY;
$database->query($query);

// Update the name of the pathauto_state.taxonomy_term in the key_value pair.
$query = <<<QUERY
UPDATE key_value
SET `name` =
REPLACE(`name`, 'http://joinup.eu/ontology/policy-domain', 'http://joinup.eu/ontology/topic')
WHERE collection = 'pathauto_state.taxonomy_term'
QUERY;
$database->query($query);
}

/**
* Update the paragraph content listings.
*/
function joinup_core_deploy_0107101(array &$sandbox): string {
$database = \Drupal::database();
if (empty($sandbox['items'])) {
$sandbox['items'] = $database->query("SELECT entity_id, revision_id, delta, langcode, deleted, field_content_listing_value FROM paragraph_revision__field_content_listing WHERE field_content_listing_value LIKE '%policy_domain%' OR field_content_listing_value LIKE '%policy-domain';")->fetchAll();
$sandbox['max'] = count($sandbox['items']);
$sandbox['progress'] = 0;
}

$tables = [
'paragraph__field_content_listing',
'paragraph_revision__field_content_listing',
];

$items = array_splice($sandbox['items'], 0, 50);
foreach ($items as $item) {
$value = unserialize($item->field_content_listing_value);
$value['query_presets'] = str_replace(['policy_domain', 'policy-domain'], 'topic', $value['query_presets']);
$item->field_content_listing_value = serialize($value);

foreach ($tables as $table) {
$database->update($table)
->fields((array) $item)
->condition('entity_id', $item->entity_id)
->condition('revision_id', $item->revision_id)
->condition('deleted', $item->deleted)
->condition('delta', $item->delta)
->condition('langcode', $item->langcode)
->execute();
}
}

$sandbox['progress'] += count($items);
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? 1 : (float) $sandbox['progress'] / (float) $sandbox['max'];
return "Processed {$sandbox['progress']} out of {$sandbox['max']} items.";
}
26 changes: 0 additions & 26 deletions web/modules/custom/joinup_core/joinup_core.post_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,3 @@
*/

declare(strict_types = 1);

/**
* Backup the field policy domain node field.
*/
function joinup_core_post_update_0107100(&$sandbox) {
// WARNING: Needs to run last among the post update hooks.
$schema = \Drupal::database()->schema();
$schema->renameTable('node__field_policy_domain', 'node__field_topic_backup');
$schema->renameTable('node_revision__field_policy_domain', 'node_revision__field_topic_backup');

$queries = [
'CREATE TABLE `node__field_policy_domain` LIKE `node__field_topic_backup`;',
'CREATE TABLE `node_revision__field_policy_domain` LIKE `node_revision__field_topic_backup`;',
];

foreach ($queries as $query) {
// Core was using the same query until removed in
// https://www.drupal.org/files/issues/2061879-9.patch.
\Drupal::database()->query($query);
}

\Drupal::getContainer()->get('sparql.endpoint')->query('MOVE <http://policy_domain> TO <http://topic>');
\Drupal::configFactory()->getEditable('taxonomy.vocabulary.policy_domain')->delete();
\Drupal::keyValue('config.entity.key_store.rdf_entity_mapping')->deleteAll();
\Drupal::keyValue('config.entity.key_store.taxonomy_vocabulary')->deleteAll();
}

0 comments on commit 519d616

Please sign in to comment.