/** * @param $results * @param $content * * @return null Null in case of failure. */ function wl_embed_entities($results, $content) { // Prepare the regex pattern. $pattern = '/<span class="textannotation" id="([^"]+)"[^>]*>([^<]+)<\\/span>/im'; // This var will contain the output matches. $matches = array(); // Return null if no match found. if (false === preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) { return null; } // For each match, embed the related entity. foreach ($matches as $match) { $full = $match[0]; $id = $match[1]; $text = $match[2]; // echo "[ id :: $id ][ text :: $text ]\n"; $text_annotation = $results['text_annotations'][$id]; $entities = $text_annotation['entities']; // echo "[ text annotation :: " . $text_annotation['id'] . "][ entities count :: " . count( $entities ) . " ]\n"; $entity_annotation = wl_get_entity_annotation_best_match($entities); // Get the entity, its ID and type. $entity = $entity_annotation['entity']; $entity_id = $entity->{'@id'}; $entity_type = wl_get_entity_type($entity); $entity_class = $entity_type['class']; $entity_type_uri = $entity_type['uri']; // Create the new span with the entity reference. $replace = '<span class="textannotation ' . $entity_class . '" ' . 'id="' . $id . '" ' . 'itemid="' . $entity_id . '" ' . 'itemscope="itemscope" ' . 'itemtype="' . $entity_type_uri . '">' . '<span itemprop="name">' . htmlentities($text) . '</span></span>'; $content = str_replace($full, $replace, $content); // echo "[ id :: $id ]\n"; // echo "[ sel_prefix :: $sel_prefix ]\n"; // echo "[ sel_suffix :: $sel_suffix ]\n"; // echo "[ sel_text :: $sel_text ]\n"; // echo "[ pattern :: $pattern ]\n"; // echo "[ replace :: $replace ]\n"; // echo "[ content length (before) :: " . strlen( $content ) . " ]\n"; // echo "[ entity id :: $entity_id ][ entity type :: $entity_type ]\n"; } return $content; }
/** * Test saving entities passed via a metabox. */ function testEntitiesViaArray() { // Create a post. $post_id = $this->createPost(); $this->assertTrue(is_numeric($post_id)); $post = get_post($post_id); $this->assertNotNull($post); // Read the entities from the mock-up analysis. $analysis_results = wl_parse_file(dirname(__FILE__) . '/' . self::FILENAME . '.json'); $this->assertTrue(is_array($analysis_results)); // For each entity get the label, type, description and thumbnails. $this->assertTrue(isset($analysis_results['entities'])); // Get a reference to the entities. $text_annotations = $analysis_results['text_annotations']; $best_entities = array(); foreach ($text_annotations as $id => $text_annotation) { $entity_annotation = wl_get_entity_annotation_best_match($text_annotation['entities']); $entity = $entity_annotation['entity']; $entity_id = $entity->{'@id'}; if (!array_key_exists($entity_id, $best_entities)) { $best_entities[$entity_id] = $entity; } } // Accumulate the entities in an array. $entities = array(); foreach ($best_entities as $uri => $entity) { // Label if (!isset($entity->{'http://www.w3.org/2000/01/rdf-schema#label'}->{'@value'})) { var_dump($entity); } $this->assertTrue(isset($entity->{'http://www.w3.org/2000/01/rdf-schema#label'}->{'@value'})); $label = $entity->{'http://www.w3.org/2000/01/rdf-schema#label'}->{'@value'}; $this->assertFalse(empty($label)); // Type // $type = wl_get_entity_type($entity); // $this->assertFalse(empty($type)); // Description $description = wl_get_entity_description($entity); $this->assertNotNull($description); // Images $images = wl_get_entity_thumbnails($entity); $this->assertTrue(is_array($images)); // Save the entity to the entities array. $entities = array_merge_recursive($entities, array($uri => array('uri' => $uri, 'label' => $label, 'main_type' => 'http://schema.org/Thing', 'type' => array(), 'description' => $description, 'images' => $images))); } // Save the entities in the array. $entity_posts = wl_save_entities($entities); // Publish $entity_post_ids = array_map(function ($item) { return $item->ID; }, $entity_posts); foreach ($entity_post_ids as $entity_id) { wp_publish_post($entity_id); } // TODO: need to bind entities with posts. wl_core_add_relation_instances($post_id, WL_WHAT_RELATION, $entity_post_ids); $this->assertCount(sizeof($entity_post_ids), wl_core_get_related_entity_ids($post_id)); // TODO: synchronize data. // NOTICE: this requires a published post! wl_linked_data_push_to_redlink($post_id); // Check that the entities are created in WordPress. $this->assertCount(count($entities), $entity_posts); // Check that each entity is bound to the post. $entity_ids = array(); foreach ($entity_posts as $post) { // Store the entity IDs for future checks. array_push($entity_ids, $post->ID); // Get the related posts IDs. $rel_posts = wl_core_get_related_post_ids($post->ID); $this->assertCount(1, $rel_posts); // The post must be the one the test created. $this->assertEquals($post_id, $rel_posts[0]); } // Check that the post references the entities. $rel_entities = wl_core_get_related_entity_ids($post_id); $this->assertEquals(count($entity_ids), count($rel_entities)); foreach ($entity_ids as $id) { $this->assertTrue(in_array($id, $rel_entities)); } // Check that the locally saved entities and the remotely saved ones match. $this->checkEntities($entity_posts); // Check that the locally saved post data match the ones on Redlink. $this->checkPost($post_id); // Check the post references, that they match between local and remote. $this->checkPostReferences($post_id); // Delete the post. $this->deletePost($post_id); }