/** * Retrieve timeline events. * * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. * * @param int $post_id The post ID. * @return array An array of event posts. */ function wl_shortcode_timeline_get_events($post_id = null) { // Build list of event-entities. if (is_null($post_id) || !is_numeric($post_id)) { // Global timeline. Get entities from the latest posts. $latest_posts_ids = get_posts(array('numberposts' => 50, 'fields' => 'ids', 'post_type' => 'post', 'post_status' => 'publish')); if (empty($latest_posts_ids)) { // There are no posts. return array(); } // Collect entities related to latest posts $entity_ids = array(); foreach ($latest_posts_ids as $id) { $entity_ids = array_merge($entity_ids, wl_core_get_related_entity_ids($id, array('status' => 'publish'))); } if (empty($entity_ids)) { return array(); } } else { // Post-specific timeline. Search for entities in the post itself. $entity_ids = wl_core_get_related_entity_ids($post_id); } wl_write_log("wl_shortcode_timeline_get_events [ entity IDs :: " . join(', ', $entity_ids) . " ]"); return get_posts(array('post__in' => $entity_ids, 'post_type' => WL_ENTITY_TYPE_NAME, 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array('relation' => 'AND', array('key' => WL_CUSTOM_FIELD_CAL_DATE_START, 'value' => null, 'compare' => '!='), array('key' => WL_CUSTOM_FIELD_CAL_DATE_END, 'value' => null, 'compare' => '!=')))); }
/** * Displays the meta box contents (called by *add_meta_box* callback). * * @param WP_Post $post The current post. */ function wl_entities_box_content($post) { // wl_write_log( "wl_entities_box_content [ post id :: $post->ID ]" ); // Angularjs edit-post widget wrapper echo '<div id="wordlift-edit-post-outer-wrapper"></div>'; // Angularjs edit-post widget classification boxes configuration $classification_boxes = unserialize(WL_CORE_POST_CLASSIFICATION_BOXES); // Array to store all related entities ids $all_referenced_entities_ids = array(); // Add selected entities to classification_boxes foreach ($classification_boxes as $i => $box) { // Build the proper relation name $relation_name = $box['id']; // wl_write_log( "Going to related of $relation_name" ); // Get entity ids related to the current post for the given relation name (both draft and published entities) $draft_entity_ids = wl_core_get_related_entity_ids($post->ID, array('predicate' => $relation_name, 'status' => 'draft')); $publish_entity_ids = wl_core_get_related_entity_ids($post->ID, array('predicate' => $relation_name, 'status' => 'publish')); $entity_ids = array_unique(array_merge($draft_entity_ids, $publish_entity_ids)); // Store the entity ids for all the 4W $all_referenced_entities_ids = array_merge($all_referenced_entities_ids, $entity_ids); // Transform entity ids array in entity uris array array_walk($entity_ids, function (&$entity_id) { // Retrieve the entity uri for the given entity id $entity_id = wl_get_entity_uri($entity_id); }); // Enhance current box selected entities $classification_boxes[$i]['selectedEntities'] = $entity_ids; } // Json encoding for classification boxes structure $classification_boxes = json_encode($classification_boxes); // Ensure there are no repetitions of the referenced entities $all_referenced_entities_ids = array_unique($all_referenced_entities_ids); // Build the entity storage object $referenced_entities_obj = array(); foreach ($all_referenced_entities_ids as $referenced_entity) { $entity = wl_serialize_entity($referenced_entity); $referenced_entities_obj[$entity['id']] = $entity; } $referenced_entities_obj = empty($referenced_entities_obj) ? '{}' : json_encode($referenced_entities_obj); $default_thumbnail_path = WL_DEFAULT_THUMBNAIL_PATH; $dataset_uri = wl_configuration_get_redlink_dataset_uri(); echo <<<EOF <script type="text/javascript"> jQuery( function() { \tif ('undefined' == typeof window.wordlift) { \twindow.wordlift = {} \twindow.wordlift.entities = {} \t\t \t} \twindow.wordlift.classificationBoxes = {$classification_boxes}; \twindow.wordlift.entities = {$referenced_entities_obj}; \twindow.wordlift.currentPostId = {$post->ID}; \t\t\twindow.wordlift.defaultThumbnailPath = '{$default_thumbnail_path}'; \t\t\twindow.wordlift.datasetUri = '{$dataset_uri}'; }); </script> EOF; }
/** * Retrieve timeline events. * * @since 3.1.0 * * @uses wl_core_get_related_entity_ids() to retrieve the entities referenced by the specified post. * * @param int $post_id The post ID. * * @return array An array of event posts. */ public function get_events($post_id = null) { // Get the entity IDs either from the entities related to the specified post or from the last 50 published // posts if no post has been specified. $ids = is_numeric($post_id) ? wl_core_get_related_entity_ids($post_id) : $this->entity_service->get_all_related_to_last_50_published_posts(); // Add the post itself if it's an entity. if (is_numeric($post_id) && $this->entity_service->is_entity($post_id)) { $ids[] = $post_id; } // If there's no entities, return an empty array right away. if (0 === sizeof($ids)) { $this->log_service->trace("No events found [ post id :: {$post_id} ]"); return array(); } $this->log_service->trace("Getting events [ entity ids :: " . join(', ', $ids) . " ]"); return get_posts(array('post__in' => $ids, 'post_type' => Wordlift_Entity_Service::TYPE_NAME, 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => array('relation' => 'AND', array('key' => Wordlift_Schema_Service::FIELD_DATE_START, 'value' => null, 'compare' => '!='), array('key' => Wordlift_Schema_Service::FIELD_DATE_END, 'value' => null, 'compare' => '!=')), 'tax_query' => array('taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'field' => 'slug', 'terms' => 'event'))); }
/** * Recursive function used to retrieve related content starting from a post ID. * * @uses wl_core_get_related_post_ids() to get the list of post ids that reference an entity. * * @param int $entity_id The entity post ID. * @param int $depth Max number of entities in output. * @param array $related An existing array of related entities. * @return array */ function wl_shortcode_chord_get_relations($entity_id, $depth = 2, $related = null) { // Search for more entities only if we did not exceed $depth or $max_size $max_size = 30; if (!is_null($related)) { if (count($related['entities']) > $max_size || $depth <= 0) { return $related; } } wl_write_log("wl_shortcode_chord_get_relations [ post id :: {$entity_id} ][ depth :: {$depth} ][ related? :: " . (is_null($related) ? 'yes' : 'no') . " ]"); // Create a related array which will hold entities and relations. if (is_null($related)) { $related = array('entities' => array($entity_id), 'relations' => array()); } // Get related entities $related_entity_ids = wl_core_get_related_entity_ids($entity_id, array('status' => 'publish')); // Get related posts (only id the current node is an entity) $related_post_ids = array(); if (get_post_type($entity_id) == WL_ENTITY_TYPE_NAME) { $related_post_ids = wl_core_get_related_post_ids($entity_id, array('status' => 'publish')); } // Merge results. $related_ids = array_merge($related_post_ids, $related_entity_ids); $related_ids = array_unique($related_ids); // TODO: List of entities ($rel) should be ordered by interest factors. shuffle($related_ids); // Now we have all the related IDs. foreach ($related_ids as $related_id) { // TODO: does it make sense to set an array post ID > related ID? The *wl_shortcode_chord_get_graph* // method is going anyway to *refactor* the data structure. So here the structure may be optimized in terms // of readability and performance. $related['relations'][] = array($entity_id, $related_id); if (!in_array($related_id, $related['entities'])) { //Found new related entity! $related['entities'][] = $related_id; $related = wl_shortcode_chord_get_relations($related_id, $depth - 1, $related); } } // End condition 2: no more entities to search for. return $related; }
/** * Retrieve geomap places. If $post_id is null the return places blog wide * * @uses wl_core_get_related_entities() to retrieve the entities referenced by the specified post. * * @param int $post_id The post ID. * @return array An array of place posts. */ function wl_shortcode_geomap_get_places($post_id = null) { // If $post_id is null or is not numeric it means this is a global geomap $is_global = is_null($post_id) || !is_numeric($post_id); // If the current one is not a global geomap, retrieve related entities ids if ($is_global) { $related_ids = array(); } else { $related_ids = wl_core_get_related_entity_ids($post_id, array('status' => 'publish')); } // If is not a global geomap, an empty $related_ids means that no entities are related to the post // An empty array can be returned in this case if (!$is_global && empty($related_ids)) { return array(); } // Retrieve all 'published' places with geo coordinates defined // If $place_ids is not empty, it's used to limit query results to the current post related places // Please note that when $place_ids is an empty array, the 'post__in' parameter is not considered in the query $places = get_posts(array('post__in' => $related_ids, 'post_type' => WL_ENTITY_TYPE_NAME, 'nopaging' => true, 'post_status' => 'published', 'meta_query' => array('relation' => 'AND', array('key' => WL_CUSTOM_FIELD_GEO_LATITUDE, 'value' => null, 'compare' => '!='), array('key' => WL_CUSTOM_FIELD_GEO_LONGITUDE, 'value' => null, 'compare' => '!=')))); return $places; }
/** * 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); }
/** * Retrieve geomap places. If $post_id is null the return places blog wide * * @uses wl_core_get_related_entities() to retrieve the entities referenced by the specified post. * * @param int $post_id The post ID. * * @return array An array of place posts. */ function wl_shortcode_geomap_get_places($post_id = null) { // If $post_id is null or is not numeric it means this is a global geomap $is_global = is_null($post_id) || !is_numeric($post_id); // If the current one is not a global geomap, retrieve related entities ids if ($is_global) { $related_ids = array(); } else { $related_ids = wl_core_get_related_entity_ids($post_id, array('status' => 'publish')); // Also include current entity if (Wordlift_Entity_Service::get_instance()->is_entity($post_id)) { $related_ids[] = $post_id; } } // If is not a global geomap, an empty $related_ids means that no entities are related to the post // An empty array can be returned in this case if (!$is_global && empty($related_ids)) { return array(); } // Retrieve all 'published' places with geo coordinates defined // If $related_ids is not empty, it's used to limit query results to the current post related places // Please note that when $place_ids is an empty array, the 'post__in' parameter is not considered in the query return get_posts(array('post__in' => $related_ids, 'post_type' => Wordlift_Entity_Service::TYPE_NAME, 'nopaging' => true, 'post_status' => 'publish', 'meta_query' => array('relation' => 'AND', array('key' => Wordlift_Schema_Service::FIELD_GEO_LATITUDE, 'value' => null, 'compare' => '!='), array('key' => Wordlift_Schema_Service::FIELD_GEO_LONGITUDE, 'value' => null, 'compare' => '!=')), 'tax_query' => array('taxonomy' => Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'field' => 'slug', 'terms' => 'place'))); }
function testEntityMetadataAreProperlyUpdated() { $fake = $this->prepareFakeGlobalPostArrayFromFile('/assets/fake_global_post_array_with_one_entity_linked_as_what.json'); $_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 $content = <<<EOF <span itemid="{$original_entity_uri}">My entity</span> EOF; // Create a post referincing to the created entity $post_id = wl_create_post($content, 'my-post', 'A post', 'draft'); // Here the entity should have been created $original_entity = wl_get_entity_post_by_uri($original_entity_uri); $this->assertNotNull($original_entity); // Store entity type, images and sameAs (needed later) $original_type = wl_schema_get_types($original_entity->ID); $original_thumbnails = $this->getThumbs($original_entity->ID); $original_sameAs = wl_schema_get_value($original_entity->ID, 'sameAs'); // Query the same entity using the Redlink URI $entity_uri = wl_get_entity_uri($original_entity->ID); $e = wl_get_entity_post_by_uri($entity_uri); $this->assertEquals($original_entity, $e); // The entity description should be the same we expect $raw_entity = current(array_values($fake['wl_entities'])); $this->assertEquals($raw_entity['description'], $original_entity->post_content); // The entity is related as what predicate $related_entity_ids = wl_core_get_related_entity_ids($post_id, array("predicate" => "what")); $this->assertCount(1, $related_entity_ids); // Ensure there are no other relation instances $relation_instances = wl_tests_get_relation_instances_for($post_id); $this->assertCount(1, $relation_instances); /* Now Post is saved again with the same mentioned entity: * - with different type * - with different description * - with one more image * - with one modified sameAs * - as WHO instead fo WHAT */ $fake = $this->prepareFakeGlobalPostArrayFromFile('/assets/fake_global_post_array_with_one_entity_linked_as_who_and_modified_data.json'); $_POST = $fake; // The entity url should be the same we expect $raw_entity = current(array_values($fake['wl_entities'])); $raw_entity_uri = $raw_entity['uri']; $new_content = <<<EOF <span itemid="{$raw_entity_uri}">My entity</span> EOF; // Update the post content (to force existing entities update) wp_update_post(array('ID' => $post_id, 'post_content' => $new_content)); // Verify the mentioned entity was already into DB... $updated_entity = wl_get_entity_post_by_uri($raw_entity_uri); $this->assertEquals($original_entity->ID, $updated_entity->ID); $this->assertEquals($original_entity->post_title, $updated_entity->post_title); // ... but some properties changed! $this->assertNotEquals($original_entity, $updated_entity); // Verify entity type has been updated $updated_type = wl_schema_get_types($updated_entity->ID); $this->assertNotEquals($original_type, $updated_type); $this->assertEquals(array('http://schema.org/Organization'), $updated_type); // Verify entity description has been updated $this->assertEquals($raw_entity['description'], $updated_entity->post_content); // Verify entity images have been updated (one was added) $updated_thumbnails = $this->getThumbs($updated_entity->ID); $this->assertNotEquals($original_thumbnails, $updated_thumbnails); $this->assertContains($original_thumbnails[0], $updated_thumbnails); $this->assertCount(2, $updated_thumbnails); // There is one more $this->assertContains('Netherlands_vs_Ivory_Coast', $updated_thumbnails[1]); // ... about soccer // Verify entity sameAs have been updated $updated_sameAs = wl_schema_get_value($updated_entity->ID, 'sameAs'); $this->assertNotEquals($original_sameAs, $updated_sameAs); $this->assertContains($original_sameAs[1], $updated_sameAs); $this->assertNotContains($original_sameAs[0], $updated_sameAs); $this->assertContains('http://sv.dbpedia.org/page/Reason', $updated_sameAs); // Verify the entity is now related as who predicate $related_entity_ids = wl_core_get_related_entity_ids($post_id, array("predicate" => "who")); $this->assertCount(1, $related_entity_ids); // Ensure there are no other relation instances $relation_instances = wl_tests_get_relation_instances_for($post_id); $this->assertCount(1, $relation_instances); }
/** * Find all posts related to a given $object_id * If $predicate is omitted, $predicate filter is not applied * Not use it directly. Use wl_core_get_related_posts or wl_core_get_related_posts_ids instead. * * @param int $object_id The entity ID or the post ID. * @param string $predicate Name of the relation: null | 'what' | 'where' | 'when' | 'who' * @param string $post_status Filter by post status null | 'publish' | 'draft' | 'pending' | 'trash'. null means *any* post status * * @return (array) Array of objects. */ function wl_core_inner_get_related_posts($get, $item_id, $predicate = null, $post_status = null) { // Retrieve the post object $post = get_post($item_id); if (null === $post) { return array(); } if ("entity" === $post->post_type) { if ($results = wl_core_get_posts(array('get' => $get, 'post_type' => 'post', 'post_status' => $post_status, 'related_to' => $item_id, 'as' => 'subject', 'with_predicate' => $predicate))) { return $results; } } else { if ($results = wl_core_get_posts(array('get' => $get, 'post_type' => 'post', 'post_status' => $post_status, 'post__not_in' => array($item_id), 'related_to__in' => wl_core_get_related_entity_ids($post->ID), 'as' => 'subject', 'with_predicate' => $predicate))) { return $results; } } // If wl_core_get_posts return false then an empty array is returned return array(); }
/** * Get the SPARQL fragment to set the dc:references statements. * * @param int $post_id The post ID. * * @return string The SPARQL fragment (or an empty string). */ function wl_get_sparql_post_references($post_id) { // Get the post URI. $post_uri = wordlift_esc_sparql(wl_get_entity_uri($post_id)); // Get the related entities IDs. $related = wl_core_get_related_entity_ids($post_id); // Build the SPARQL fragment. $sparql = ''; foreach ($related as $id) { $uri = wordlift_esc_sparql(wl_get_entity_uri($id)); $sparql .= "<{$post_uri}> dct:references <{$uri}> . "; } return $sparql; }
function create_MIT_Center_for_Collective_Intelligence($related_post_id) { $uri = 'http://dbpedia.org/resource/MIT_Center_for_Collective_Intelligence'; $label = 'MIT Center for Collective Intelligence'; $type = 'http://schema.org/Organization'; $description = file_get_contents(dirname(__FILE__) . '/assets/mit_center_for_cognitive_intelligence.txt'); $images = array(); $same_as = array('http://rdf.freebase.com/ns/m.04n2n64'); $entity_post = wl_save_entity($uri, $label, $type, $description, array(), $images, $related_post_id, $same_as); // Check that the type is set correctly. $types = wl_get_entity_rdf_types($entity_post->ID); $this->assertEquals(0, count($types)); // $this->assertEquals( 'organization', $types[0]->slug ); // Check that Tim Berners-Lee is related to this resource. $related_entities = wl_core_get_related_entity_ids($entity_post->ID); $this->assertEquals(1, count($related_entities)); $this->assertEquals($related_post_id, $related_entities[0]); return $entity_post->ID; }
/** * Calculate rating for a given entity * Rating depends from following criteria * * 1. Is the current entity related to at least 1 post? * 2. Is the current entity content post not empty? * 3. Is the current entity related to at least 1 entity? * 4. Is the entity published? * 5. There is a a thumbnail associated to the entity? * 6. Has the entity a sameas defined? * 7. Are all schema.org required metadata compiled? * * Each positive check means +1 in terms of rating score * * @since 3.3.0 * * @param int $post_id The entity post id. * * @return int An array representing the rating obj. */ public function calculate_rating_for($post_id) { // If it's not an entity, return. if (!$this->is_entity($post_id)) { return; } // Retrieve the post object $post = get_post($post_id); // Rating value $score = 0; // Store warning messages $warnings = array(); // Is the current entity related to at least 1 post? 0 < count(wl_core_get_related_post_ids($post->ID)) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_POSTS, 'wordlift')); // Is the post content not empty? !empty($post->post_content) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_CONTENT_POST, 'wordlift')); // Is the current entity related to at least 1 entity? // Was the current entity already disambiguated? 0 < count(wl_core_get_related_entity_ids($post->ID)) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_RELATED_ENTITIES, 'wordlift')); // Is the entity published? 'publish' === get_post_status($post->ID) ? $score++ : array_push($warnings, __(self::RATING_WARNING_IS_PUBLISHED, 'wordlift')); // Has a thumbnail? has_post_thumbnail($post->ID) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_THUMBNAIL, 'wordlift')); // Get all post meta keys for the current post global $wpdb; $query = $wpdb->prepare("SELECT DISTINCT(meta_key) FROM {$wpdb->postmeta} WHERE post_id = %d", $post->ID); // Check intersection between available meta keys // and expected ones arrays to detect missing values $available_meta_keys = $wpdb->get_col($query); // If each expected key is contained in available keys array ... in_array(Wordlift_Schema_Service::FIELD_SAME_AS, $available_meta_keys) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_SAME_AS, 'wordlift')); $schema = wl_entity_type_taxonomy_get_type($post_id); $expected_meta_keys = null === $schema['custom_fields'] ? array() : array_keys($schema['custom_fields']); $intersection = array_intersect($expected_meta_keys, $available_meta_keys); // If each expected key is contained in available keys array ... count($intersection) === count($expected_meta_keys) ? $score++ : array_push($warnings, __(self::RATING_WARNING_HAS_COMPLETED_METADATA, 'wordlift')); // Finally return score and warnings return array('raw_score' => $score, 'traffic_light_score' => $this->convert_raw_score_to_traffic_light($score), 'percentage_score' => $this->convert_raw_score_to_percentage($score), 'warnings' => $warnings); }
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 testWlCoreGetRelatedEntityIdsForAnEntity() { // Create 2 posts and 1 entities $entity_0_id = wl_create_post('', 'entity0', 'An Entity', 'draft', 'entity'); $entity_1_id = wl_create_post('', 'entity1', 'An Entity', 'draft', 'entity'); $entity_2_id = wl_create_post('', 'entity2', 'An Entity', 'draft', 'entity'); // Insert relations wl_core_add_relation_instance($entity_0_id, WL_WHERE_RELATION, $entity_1_id); wl_core_add_relation_instance($entity_0_id, WL_WHO_RELATION, $entity_2_id); // Check relation are retrieved as expected $result = wl_core_get_related_entity_ids($entity_0_id); $this->assertCount(2, $result); $this->assertTrue(in_array($entity_1_id, $result)); $this->assertTrue(in_array($entity_2_id, $result)); $result = wl_core_get_related_entity_ids($entity_0_id, array('predicate' => WL_WHERE_RELATION)); $this->assertCount(1, $result); $this->assertTrue(in_array($entity_1_id, $result)); $result = wl_core_get_related_entity_ids($entity_0_id, array('predicate' => WL_WHO_RELATION)); $this->assertCount(1, $result); $this->assertTrue(in_array($entity_2_id, $result)); }
/** * Check that the post is referencing the related entities. * * @param int $post_id The post ID. */ function checkPostReferences($post_id) { // Get the post. $post = get_post($post_id); $this->assertNotNull($post); // Get the post Redlink URI. $uri = wordlift_esc_sparql(wl_get_entity_uri($post->ID)); // Prepare the SPARQL query to select label and URL. $sparql = "SELECT DISTINCT ?uri WHERE { <{$uri}> dct:references ?uri . }"; // Send the query and get the response. $response = rl_sparql_select($sparql); $this->assertFalse(is_wp_error($response)); $body = $response['body']; $matches = array(); $count = preg_match_all('/^(?P<uri>[^\\r]*)/im', $body, $matches, PREG_SET_ORDER); $this->assertTrue(is_numeric($count)); $entity_ids = wl_core_get_related_entity_ids($post->ID); // wl_write_log( "[ entity IDs :: " . join( ', ', $entity_ids ) . " ][ size of entity IDs :: " . sizeof( $entity_ids ) . " ][ count :: $count ][ post ID :: $post->ID ]" ); // // if ( $count !== ( 1 + sizeof( $entity_ids ) ) ) { // wl_write_log( "[ sparql :: $sparql ][ body :: $body ]" ); // } // Expect only one match (headers + expected entities). $this->assertEquals($count, sizeof($entity_ids) + 1); $entity_uris = wl_post_ids_to_entity_uris($entity_ids); for ($i = 1; $i < $count; $i++) { $entity_uri = $matches[$i]['uri']; // Check that the URI is in the array. $this->assertTrue(in_array($entity_uri, $entity_uris)); } }
function testPublishingUnpublishingPosts() { // create two entities $entity_1_id = wl_create_post('', 'entity-1', uniqid('entity', true), 'draft', 'entity'); $entity_2_id = wl_create_post('', 'entity-2', uniqid('entity', true), 'draft', 'entity'); $entity_1_uri = wl_get_entity_uri($entity_1_id); $entity_2_uri = wl_get_entity_uri($entity_2_id); $body_1 = <<<EOF <span itemid="{$entity_1_uri}">Entity 1</span> <span itemid="{$entity_2_uri}">Entity 2</span> EOF; $body_2 = <<<EOF <span itemid="{$entity_2_uri}">Entity 2</span> EOF; // create a post as a draft. $post_1_id = wl_create_post($body_1, 'post-1', uniqid('post', true), 'draft', 'post'); // check the post is not published on Redlink. $lines = $this->getPostTriples($post_1_id); $this->assertEquals(1, sizeof($lines)); $this->assertCount(2, wl_core_get_related_entity_ids($post_1_id)); // TODO tmp assertions: check callback order wl_update_post_status($post_1_id, 'publish'); wl_update_post_status($post_1_id, 'draft'); $this->assertEquals('draft', get_post_status($entity_1_id)); // publish the post. wp_publish_post($post_1_id); // wl_update_post_status( $post_1_id, 'publish' ); $this->assertCount(2, wl_core_get_related_entity_ids($post_1_id)); // check the post is published on Redlink. $lines = $this->getPostTriples($post_1_id); $this->assertCount(10, $lines); // check all entities published $lines = $this->getPostTriples($entity_1_id); $this->assertCount(4, $lines); $this->assertEquals('publish', get_post_status($entity_1_id)); $lines = $this->getPostTriples($entity_2_id); $this->assertCount(4, $lines); $this->assertEquals('publish', get_post_status($entity_2_id)); // unpublish the post. wl_update_post_status($post_1_id, 'draft'); $this->assertCount(2, wl_core_get_related_entity_ids($post_1_id)); // check the post is not published on Redlink. $lines = $this->getPostTriples($post_1_id); $this->assertCount(1, $lines); // create another post $post_2_id = wl_create_post($body_2, 'post-2', uniqid('post', true), 'draft', 'post'); // check all entities published $lines = $this->getPostTriples($entity_1_id); $this->assertCount(1, $lines); // publish post 2 wl_update_post_status($post_2_id, 'publish'); $this->assertCount(1, wl_core_get_related_entity_ids($post_2_id)); // check post 2 is published on Redlink $lines = $this->getPostTriples($post_2_id); $this->assertCount(9, $lines); // publish post 1 wl_update_post_status($post_1_id, 'publish'); $this->assertCount(2, wl_core_get_related_entity_ids($post_1_id)); // check post 1 is published on Redlink $lines = $this->getPostTriples($post_1_id); $this->assertCount(10, $lines); $lines = $this->getPostTriples($entity_1_id); $this->assertCount(4, $lines); $lines = $this->getPostTriples($entity_2_id); $this->assertCount(4, $lines); // unpublish post 1 wl_update_post_status($post_1_id, 'draft'); $this->assertCount(2, wl_core_get_related_entity_ids($post_1_id)); $lines = $this->getPostTriples($post_1_id); $this->assertCount(1, $lines); // check only entity 1 unpublished $lines = $this->getPostTriples($entity_1_id); $this->assertCount(1, $lines); $lines = $this->getPostTriples($entity_2_id); $this->assertCount(4, $lines); }