/**
 * Push the provided post to Redlink (not suitable for entities).
 *
 * @param object $post A post instance.
 */
function wl_push_post_to_redlink($post)
{
    // Only handle published posts.
    if ('post' !== $post->post_type or 'publish' !== $post->post_status) {
        wl_write_log("wl_push_post_to_redlink : not a post or not published [ post type :: {$post->post_type} ][ post status :: {$post->post_status} ]");
        return;
    }
    // Get the post URI.
    $uri = wl_sparql_escape_uri(wl_get_entity_uri($post->ID));
    // If the URI ends with a trailing slash, then we have a problem.
    if ('/' === substr($uri, -1, 1)) {
        wl_write_log("wl_push_post_to_redlink : the URI is invalid [ post ID :: {$post->ID} ][ URI :: {$uri} ]");
        return;
    }
    // wl_write_log( "wl_push_post_to_redlink [ post id :: $post->ID ][ uri :: $uri ]" );
    // Get the site language in order to define the literals language.
    $site_language = wl_configuration_get_site_language();
    // save the author and get the author URI.
    $author_uri = wl_sparql_escape_uri(Wordlift_User_Service::get_instance()->get_uri($post->post_author));
    // Get other post properties.
    $date_published = wl_get_sparql_time(get_the_time('c', $post));
    $date_modified = wl_get_sparql_time(wl_get_post_modified_time($post));
    $title = wordlift_esc_sparql($post->post_title);
    $permalink = wl_sparql_escape_uri(get_permalink($post->ID));
    $user_comments_count = $post->comment_count;
    wl_write_log("wl_push_post_to_redlink [ post_id :: {$post->ID} ][ type :: {$post->post_type} ][ slug :: {$post->post_name} ][ title :: {$post->post_title} ][ date modified :: {$date_modified} ][ date published :: {$date_published} ]");
    // create the SPARQL query.
    $sparql = '';
    if (!empty($title)) {
        $sparql .= "<{$uri}> rdfs:label '{$title}'@{$site_language} . \n";
    }
    $sparql .= "<{$uri}> a <http://schema.org/BlogPosting> . \n";
    $sparql .= "<{$uri}> schema:url <{$permalink}> . \n";
    $sparql .= "<{$uri}> schema:datePublished {$date_published} . \n";
    $sparql .= "<{$uri}> schema:dateModified {$date_modified} . \n";
    if (!empty($author_uri)) {
        $sparql .= "<{$uri}> schema:author <{$author_uri}> . \n";
    }
    $sparql .= "<{$uri}> schema:interactionCount 'UserComments:{$user_comments_count}' . \n";
    // Add SPARQL stmts to write the schema:image.
    $sparql .= wl_get_sparql_images($uri, $post->ID);
    // Get the SPARQL fragment with the dcterms:references statement.
    $sparql .= wl_get_sparql_post_references($post->ID);
    // create the query:
    //  - remove existing references to entities.
    //  - set the new post information (including references).
    $query = rl_sparql_prefixes() . <<<EOF
            DELETE { <{$uri}> dct:references ?o . }
            WHERE  { <{$uri}> dct:references ?o . };
            DELETE { <{$uri}> schema:url ?o . }
            WHERE  { <{$uri}> schema:url ?o . };
            DELETE { <{$uri}> schema:datePublished ?o . }
            WHERE  { <{$uri}> schema:datePublished ?o . };
            DELETE { <{$uri}> schema:dateModified ?o . }
            WHERE  { <{$uri}> schema:dateModified ?o . };
            DELETE { <{$uri}> a ?o . }
            WHERE  { <{$uri}> a ?o . };
            DELETE { <{$uri}> rdfs:label ?o . }
            WHERE  { <{$uri}> rdfs:label ?o . };
            DELETE { <{$uri}> schema:image ?o . }
            WHERE  { <{$uri}> schema:image ?o . };
            DELETE { <{$uri}> schema:interactionCount ?o . }
            WHERE  { <{$uri}> schema:interactionCount ?o . };
            DELETE { <{$uri}> schema:author ?o . }
            WHERE  { <{$uri}> schema:author ?o . };
            INSERT DATA { {$sparql} };
EOF;
    // execute the query.
    rl_execute_sparql_update_query($query);
}
Example #2
0
    /**
     * Check that the local post data and the remote ones match.
     *
     * @param int $post_id The post ID to check.
     */
    function checkPost($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));
        wl_write_log("checkPost [ uri :: {$uri} ]");
        // Prepare the SPARQL query to select label and URL.
        $sparql = <<<EOF
SELECT DISTINCT ?author ?dateModified ?datePublished ?interactionCount ?url ?type ?label
WHERE {
    <{$uri}> schema:author ?author ;
           schema:dateModified ?dateModified ;
           schema:datePublished ?datePublished ;
           schema:interactionCount ?interactionCount ;
           schema:url ?url ;
           a ?type ;
           rdfs:label ?label .
}
EOF;
        // 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<author>.*),(?P<dateModified>.*),(?P<datePublished>.*),(?P<interactionCount>.*),(?P<url>.*),(?P<type>.*),(?P<label>[^\\r]*)/im', $body, $matches, PREG_SET_ORDER);
        $this->assertTrue(is_numeric($count));
        // Expect only one match (headers + one row).
        if (2 !== $count) {
            wl_write_log("checkPost [ uri :: {$uri} ][ body :: {$body} ][ count :: {$count} ][ count (expected) :: 2 ]");
        }
        // Expect only one match (headers + one row).
        $this->assertEquals(2, $count);
        // Focus on the first row.
        $match = $matches[1];
        $author = $match['author'];
        $date_modified = $match['dateModified'];
        $date_published = $match['datePublished'];
        $interaction_count = $match['interactionCount'];
        $url = $match['url'];
        $type = $match['type'];
        $label = $match['label'];
        $permalink = get_permalink($post_id);
        $post_author_url = Wordlift_User_Service::get_instance()->get_uri($post->post_author);
        $post_date_published = wl_tests_time_0000_to_000Z(get_post_time('c', false, $post));
        $post_date_modified = wl_tests_time_0000_to_000Z(wl_get_post_modified_time($post));
        $post_comment_count = 'UserComments:' . $post->comment_count;
        $post_entity_type = 'http://schema.org/BlogPosting';
        $post_title = $post->post_title;
        $this->assertEquals($post_author_url, $author);
        // We expect datetime not to differ more than 5 seconds.
        $this->assertLessThan(10, wl_tests_get_time_difference_in_seconds($post_date_published, $date_published));
        $this->assertLessThan(10, wl_tests_get_time_difference_in_seconds($post_date_modified, $date_modified));
        $this->assertEquals($post_comment_count, $interaction_count);
        $this->assertEquals($permalink, $url);
        $this->assertEquals($post_entity_type, $type);
        $this->assertEquals($post_title, $label);
    }