/** * Delete the specified post from the triple store. * * @param array|int $post An array of post data */ function rl_delete_post($post) { $post_id = is_numeric($post) ? $post : $post->ID; // hide all entities that are not referenced by any published post. foreach (wl_core_get_related_entity_ids($post_id) as $entity_id) { // check if there is at least one referencing post published. $is_published = array_reduce(wl_core_get_related_post_ids($entity_id), function ($carry, $item) { $post = get_post($item); return $carry || 'publish' === $post->post_status; }); // set the entity to draft if no referencing posts are published. if (!$is_published) { wl_update_post_status($entity_id, 'draft'); } } // get the entity URI (valid also for posts) $uri_esc = wl_sparql_escape_uri(wl_get_entity_uri($post_id)); wl_write_log("rl_delete_post [ post id :: {$post_id} ][ uri esc :: {$uri_esc} ]"); // create the SPARQL statement by joining the SPARQL prefixes and deleting any known predicate. $stmt = rl_sparql_prefixes(); foreach (wl_predicates() as $predicate) { $stmt .= "DELETE { <{$uri_esc}> {$predicate} ?o . } WHERE { <{$uri_esc}> {$predicate} ?o . };\n" . "DELETE { ?s {$predicate} <{$uri_esc}> . } WHERE { ?s {$predicate} <{$uri_esc}> . };\n"; } // if the post is an entity and has exported properties, delete the related predicates. if (WL_ENTITY_TYPE_NAME === $post->post_type) { $type = wl_entity_type_taxonomy_get_type($post->ID); if (isset($type['custom_fields'])) { foreach ($type['custom_fields'] as $field => $params) { // TODO: enclose in <> only if predicate starts with http(s):// $predicate = '<' . $params['predicate'] . '>'; $stmt .= "DELETE { <{$uri_esc}> {$predicate} ?o . } WHERE { <{$uri_esc}> {$predicate} ?o . };\n"; } } } // finally execute the query. rl_execute_sparql_update_query($stmt); }
function testEntityWithAlternativeLabelIsProperlyOverridden() { $original_label = uniqid('entity-original', true); // Create an entity $entity_id = wl_create_post('', 'entity-1', $original_label, 'draft', 'entity'); // Check that there are no related posts for the entity $related_post_ids = wl_core_get_related_post_ids($entity_id, array("predicate" => "what")); $this->assertCount(0, $related_post_ids); // Generate e label and set it as alternative label for the new entity $alternative_label = uniqid('entity-alternative', true); Wordlift_Entity_Service::get_instance()->set_alternative_labels($entity_id, $alternative_label); // Check that the alternative label is properly set $labels = Wordlift_Entity_Service::get_instance()->get_alternative_labels($entity_id); $this->assertCount(1, $labels); $this->assertContains($alternative_label, $labels); // Force post status to publish: this triggers the save_post hook wl_update_post_status($entity_id, 'publish'); // Check that entity label is properly mapped on entity post title $this->assertEquals($original_label, get_post($entity_id)->post_title); // Notice that the uri is generated trough the original label // while the current label is the alternative one $fake = $this->prepareFakeGlobalPostArrayFromFile('/assets/fake_global_post_array_with_one_existing_entity_linked_as_what.json', array('CURRENT_URI' => $this->buildEntityUriForLabel($original_label), 'CURRENT_LABEL' => $alternative_label)); $_POST = $fake; // Retrieve the entity uri (the first key in wl_entities associative aray) $original_entity_uri = current(array_keys($fake['wl_entities'])); // Reference the entity to the post content trough its alternative label $content = <<<EOF <span itemid="{$original_entity_uri}">{$alternative_label}</span> EOF; // Create a post referincing to the created entity $post_id = wl_create_post($content, 'my-post', 'A post', 'draft'); // Check that entity label is STILL properly mapped on entity post title $this->assertEquals($original_label, get_post($entity_id)->post_title); $expected_entity_uri = $this->buildEntityUriForLabel($original_label); $entity_uri = wl_get_entity_uri($entity_id); $this->assertEquals($entity_uri, $expected_entity_uri); // And it should be related to the post as what predicate $related_entity_ids = wl_core_get_related_entity_ids($post_id, array("predicate" => "what")); $this->assertCount(1, $related_entity_ids); $this->assertContains($entity_id, $related_entity_ids); }
function testRedlinkIsUpdatedWhenRelatedEntityIsTrashed() { // Create draft entity $e_id = wl_create_post('ciao', 'entity-1', uniqid('entity', true), 'draft', 'entity'); $e_uri = wl_get_entity_uri($e_id); $body = <<<EOF <span itemid="{$e_uri}">Entity 1</span> EOF; // Create draft post mentioning the entity $p_id = wl_create_post($body, 'post-1', uniqid('post', true), 'draft', 'post'); // Publish the post (and related entities) wl_update_post_status($p_id, 'publish'); // Verify the post triples contain a reference to the entity $lines = $this->getPostTriples($p_id); $this->assertCount(9, $lines); // Trash the entity wl_update_post_status($e_id, 'trash'); // Verify the post triples does no more contain a reference to the entity $lines = $this->getPostTriples($p_id); $this->assertCount(8, $lines); }