コード例 #1
0
/**
 * Build an URI for the specified user ID.
 *
 * @param int $user_id The user ID.
 *
 * @return null|string Null if the user is not found, or the URI.
 */
function wl_build_user_uri($user_id)
{
    // Get the user with the specified ID.
    $user = wl_get_user($user_id);
    // If the user is not found return null.
    if (false === $user) {
        wl_write_log("wl_build_user_uri : no user found [ user id :: {$user_id} ]");
        return null;
    }
    // Build the ID using the First and Last Name.
    if (!(empty($user->first_name) && empty($user->last_name))) {
        $id = wl_sanitize_uri_path($user->first_name . ' ' . $user->last_name);
    } else {
        // If there's no First and Last Name use the user ID.
        $id = $user_id;
    }
    //    $uri = sprintf(
    //        'http://data.redlink.io/%s/%s/%s/%s',
    //        wl_configuration_get_redlink_user_id(),
    //        wl_configuration_get_redlink_dataset_name(),
    //        'user',
    //        $id
    //    );
    // Create the URL (dataset base URI has a trailing slash).
    $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'user', $id);
    // Check that the URI doesn't exist already. If it exists, add a numeric suffix.
    $base_uri = $uri;
    $counter = 1;
    while (null !== wl_get_user_by_uri($uri)) {
        $uri = $base_uri . "_" . $counter++;
    }
    wl_write_log("wl_build_user_uri [ user id :: {$user_id} ][ uri :: {$uri} ]");
    return $uri;
}
コード例 #2
0
/**
 * Build the entity URI given the entity's post.
 *
 * @uses wl_sanitize_uri_path() to sanitize the post title.
 * @uses wl_configuration_get_redlink_dataset_uri() to get the dataset base URI.
 *
 * @param int $post_id The post ID
 *
 * @return string The URI of the entity
 */
function wl_build_entity_uri($post_id)
{
    // Get the post.
    $post = get_post($post_id);
    if (null === $post) {
        wl_write_log("wl_build_entity_uri : error [ post ID :: {$post_id} ][ post :: null ]");
        return null;
    }
    // Create an ID given the title.
    $path = wl_sanitize_uri_path($post->post_title);
    // If the path is empty, i.e. there's no title, use the post ID as path.
    if (empty($path)) {
        $path = "id/{$post->ID}";
    }
    // Create the URL (dataset base URI has a trailing slash).
    $url = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), $post->post_type, $path);
    // wl_write_log( "wl_build_entity_uri [ post_id :: $post->ID ][ type :: $post->post_type ][ title :: $post->post_title ][ url :: $url ]" );
    return $url;
}
コード例 #3
0
 function testEkkehardBohmer()
 {
     $this->assertEquals('ekkehard_bohmer', wl_sanitize_uri_path('Ekkehard Böhmer'));
 }
 function buildEntityUriForLabel($label)
 {
     return sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label));
 }
コード例 #5
0
/**
 * Save the post to the triple store. Also saves the entities locally and on the triple store.
 *
 * @since 3.0.0
 *
 * @param int $post_id The post id being saved.
 */
function wl_linked_data_save_post_and_related_entities($post_id)
{
    // Ignore auto-saves
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // get the current post.
    $post = get_post($post_id);
    remove_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
    wl_write_log("[ post id :: {$post_id} ][ autosave :: false ][ post type :: {$post->post_type} ]");
    // Store mapping between tmp new entities uris and real new entities uri
    $entities_uri_mapping = array();
    // Store classification box mapping
    $entities_predicates_mapping = null;
    // Save the entities coming with POST data.
    if (isset($_POST['wl_entities']) && isset($_POST['wl_boxes'])) {
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_entities) :: ");
        wl_write_log(json_encode($_POST['wl_entities']));
        wl_write_log("]");
        wl_write_log("[ post id :: {$post_id} ][ POST(wl_boxes) :: ");
        wl_write_log(json_encode($_POST['wl_boxes'], true));
        wl_write_log("]");
        $entities_via_post = $_POST['wl_entities'];
        $boxes_via_post = $_POST['wl_boxes'];
        foreach ($entities_via_post as $entity_uri => $entity) {
            // Local entities have a tmp uri with 'local-entity-'
            // These uris need to be rewritten here and replaced in the content
            if (preg_match('/^local-entity-.+/', $entity_uri) > 0) {
                // Build the proper uri
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($entity['label']));
                // Populate the mapping
                $entities_uri_mapping[$entity_uri] = $uri;
                // Override the entity obj
                $entities_via_post[$entity_uri]['uri'] = $uri;
            }
        }
        // Populate the $entities_predicates_mapping
        // Local Redlink uris need to be used here
        foreach ($boxes_via_post as $predicate => $entity_uris) {
            foreach ($entity_uris as $entity_uri) {
                wl_write_log("Going to map predicates for uri {$entity_uri} ");
                // Retrieve the entity label needed to build the uri
                $label = $entities_via_post[stripslashes($entity_uri)]['label'];
                $uri = sprintf('%s/%s/%s', wl_configuration_get_redlink_dataset_uri(), 'entity', wl_sanitize_uri_path($label));
                wl_write_log("Going to map predicate {$predicate} to uri {$uri} ");
                $entities_predicates_mapping[$uri][] = $predicate;
            }
        }
        // Save entities and push them to Redlink
        // TODO: pass also latitude, longitude, etc.
        wl_save_entities(array_values($entities_via_post), $post_id);
    }
    // Replace tmp uris in content post if needed
    $updated_post_content = $post->post_content;
    // Save each entity and store the post id.
    foreach ($entities_uri_mapping as $tmp_uri => $uri) {
        $updated_post_content = str_replace($tmp_uri, $uri, $updated_post_content);
    }
    // Update the post content
    wp_update_post(array('ID' => $post->ID, 'post_content' => $updated_post_content));
    // Extract related/referenced entities from text.
    $disambiguated_entities = wl_linked_data_content_get_embedded_entities($updated_post_content);
    // Reset previously saved instances
    wl_core_delete_relation_instances($post_id);
    // Save relation instances
    foreach (array_unique($disambiguated_entities) as $referenced_entity_id) {
        wl_write_log(" Going to manage relation between Post {$post_id} and {$referenced_entity_id}");
        if ($entities_predicates_mapping) {
            wl_write_log(" Going to manage relation instances according to the following mapping");
            // Retrieve the entity uri
            $referenced_entity_uri = wl_get_entity_uri($referenced_entity_id);
            // Retrieve predicates for the current uri
            if (isset($entities_predicates_mapping[$referenced_entity_uri])) {
                foreach ($entities_predicates_mapping[$referenced_entity_uri] as $predicate) {
                    wl_write_log(" Going to add relation with predicate {$predicate}");
                    wl_core_add_relation_instance($post_id, $predicate, $referenced_entity_id);
                }
            } else {
                wl_write_log("Entity uri {$referenced_entity_uri} missing in the mapping");
                wl_write_log($entities_predicates_mapping);
            }
        } else {
            // Just for unit tests
            wl_core_add_relation_instance($post_id, 'what', $referenced_entity_id);
        }
        // TODO Check if is needed
        wl_linked_data_push_to_redlink($referenced_entity_id);
    }
    // Push the post to Redlink.
    wl_linked_data_push_to_redlink($post->ID);
    add_action('wl_linked_data_save_post', 'wl_linked_data_save_post_and_related_entities');
}