Ejemplo n.º 1
0
 function testSaveExistingImages()
 {
     $images = array('http://upload.wikimedia.org/wikipedia/commons/f/ff/Tim_Berners-Lee-Knight.jpg', 'http://upload.wikimedia.org/wikipedia/commons/3/3a/Tim_Berners-Lee_closeup.jpg', 'http://upload.wikimedia.org/wikipedia/commons/c/c2/Tim_Berners-Lee_2012.jpg', 'http://upload.wikimedia.org/wikipedia/commons/3/3a/Tim_Berners-Lee_closeup.jpg');
     $entity_post = wl_save_entity(array('uri' => 'http://example.org/entity', 'label' => 'Entity', 'main_type' => 'http://schema.org/Thing', 'description' => 'An example entity.', 'type_uris' => array(), 'related_post_id' => null, 'image' => $images, 'same_as' => array()));
     // Get all the attachments for the entity post.
     $attachments = wl_get_attachments($entity_post->ID);
     // Check that there is one attachment.
     $this->assertEquals(3, count($attachments));
     // Check that the attachments are found by source URL.
     foreach ($images as $image) {
         $image_post = wl_get_attachment_for_source_url($entity_post->ID, $image);
         $this->assertNotNull($image_post);
     }
 }
/**
 * Saves the values of wordlift metaboxes set in the entity editor page
 */
function wl_entity_metabox_save($post_id)
{
    if (!isset($_POST['wl_metaboxes'])) {
        return;
    }
    // Loop over the wl_metaboxes array and save metaboxes values
    foreach ($_POST['wl_metaboxes'] as $meta_name => $meta_values) {
        // First, verify nonce is set for this meta
        $nonce_name = 'wordlift_' . $meta_name . '_entity_box_nonce';
        $nonce_verify = 'wordlift_' . $meta_name . '_entity_box';
        if (!isset($_POST[$nonce_name])) {
            return $post_id;
        }
        // Verify that the nonce is valid.
        if (!wp_verify_nonce($_POST[$nonce_name], $nonce_verify)) {
            return $post_id;
        }
        // Delete values before updating
        delete_post_meta($post_id, $meta_name);
        // Save the property value(s)
        if (isset($meta_name) && isset($meta_values) && $meta_values !== '') {
            // There can be one or more property values, so we force to array:
            if (!is_array($meta_values)) {
                $meta_values = array($meta_values);
            }
            foreach ($meta_values as $meta_value) {
                // If the meta expects an entity...
                $expecting_uri = wl_get_meta_type($meta_name) === WL_DATA_TYPE_URI;
                // ...and the user inputs an entity that is not present in the db...
                $absent_from_db = is_null(wl_get_entity_post_by_uri($meta_value));
                // ...and that is not a external uri
                $name_is_uri = strpos($meta_value, 'http') === 0;
                if ($expecting_uri && $absent_from_db && !$name_is_uri) {
                    // ...we create a new entity!
                    $new_entity = wl_save_entity('', $meta_value, WL_ENTITY_TYPE_NAME, '');
                    // Assign type
                    $constraints = wl_get_meta_constraints($meta_name);
                    $type = 'http://schema.org/' . $constraints['uri_type'];
                    wl_set_entity_main_type($new_entity->ID, $type);
                    // TODO: directly publish the new entity
                    // Update the value that will be saved as meta
                    $meta_value = wl_get_entity_uri($new_entity->ID);
                }
                // TODO: use WL methods
                add_post_meta($post_id, $meta_name, $meta_value);
            }
        }
    }
    // Push changes on RedLink
    wl_linked_data_push_to_redlink($post_id);
}
/**
 * Save the specified entities to the local storage.
 *
 * @param array $entities An array of entities.
 * @param int $related_post_id A related post ID.
 *
 * @return array An array of posts.
 */
function wl_save_entities($entities, $related_post_id = null)
{
    wl_write_log("[ entities count :: " . count($entities) . " ][ related post id :: {$related_post_id} ]");
    // Prepare the return array.
    $posts = array();
    // Save each entity and store the post id.
    foreach ($entities as $entity) {
        $uri = $entity['uri'];
        $label = $entity['label'];
        // This is the main type URI.
        $main_type_uri = $entity['main_type'];
        // the preferred type.
        $type_uris = isset($entity['type']) ? $entity['type'] : array();
        $description = $entity['description'];
        $images = isset($entity['image']) ? is_array($entity['image']) ? $entity['image'] : array($entity['image']) : array();
        $same_as = isset($entity['sameas']) ? is_array($entity['sameas']) ? $entity['sameas'] : array($entity['sameas']) : array();
        // Bring properties inside an associative array
        $entity_properties = array('uri' => $uri, 'label' => $label, 'main_type_uri' => $main_type_uri, 'description' => $description, 'type_uris' => $type_uris, 'images' => $images, 'related_post_id' => $related_post_id, 'same_as' => $same_as);
        // Save the entity.
        $entity_post = wl_save_entity($entity_properties);
        // Store the post in the return array if successful.
        if (null !== $entity_post) {
            array_push($posts, $entity_post);
        }
    }
    return $posts;
}
Ejemplo n.º 4
0
 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;
 }
/**
 * Save the specified entities to the local storage.
 *
 * @param array $entities An array of entities.
 * @param int $related_post_id A related post ID.
 *
 * @return array An array of posts.
 */
function wl_save_entities($entities, $related_post_id = null)
{
    // wl_write_log( "[ entities count :: " . count( $entities ) . " ][ related post id :: $related_post_id ]" );
    // Prepare the return array.
    $posts = array();
    // Save each entity and store the post id.
    foreach ($entities as $entity) {
        // Update entity data with related post
        $entity['related_post_id'] = $related_post_id;
        // Save the entity.
        $entity_post = wl_save_entity($entity);
        // Store the post in the return array if successful.
        if (null !== $entity_post) {
            array_push($posts, $entity_post);
        }
    }
    return $posts;
}