예제 #1
0
 /**
  * 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));
     }
 }
예제 #2
0
/**
 * Count the number of triples in the dataset.
 * @return array|WP_Error|null An array if successful, otherwise WP_Error or NULL.
 */
function rl_count_triples()
{
    // Set the SPARQL query.
    $sparql = 'SELECT (COUNT(DISTINCT ?s) AS ?subjects) (COUNT(DISTINCT ?p) AS ?predicates) (COUNT(DISTINCT ?o) AS ?objects) ' . 'WHERE { ?s ?p ?o }';
    // Send the request.
    $response = rl_sparql_select($sparql);
    // Return the error in case of failure.
    if (is_wp_error($response) || 200 !== (int) $response['response']['code']) {
        $body = is_wp_error($response) ? $response->get_error_message() : $response['body'];
        wl_write_log("rl_count_triples : error [ url :: {$url} ][ response :: ");
        wl_write_log("\n" . var_export($response, true));
        wl_write_log("][ body :: ");
        wl_write_log("\n" . $body);
        wl_write_log("]");
        return $response;
    }
    // Get the body.
    $body = $response['body'];
    // Get the values.
    $matches = array();
    if (1 === preg_match('/(\\d+),(\\d+),(\\d+)/im', $body, $matches) && 4 === count($matches)) {
        // Return the counts.
        return array('subjects' => (int) $matches[1], 'predicates' => (int) $matches[2], 'objects' => (int) $matches[3]);
    }
    // No digits found in the response, return null.
    wl_write_log("rl_count_triples : unrecognized response [ body :: {$body} ]");
    return null;
}
예제 #3
0
 function getPostTriples($post_id)
 {
     // Get the post Redlink URI.
     $uri = wl_get_entity_uri($post_id);
     $uri_esc = wordlift_esc_sparql($uri);
     // Prepare the SPARQL query to select label and URL.
     $sparql = "SELECT DISTINCT ?p ?o WHERE { <{$uri_esc}> ?p ?o . }";
     // Send the query and get the response.
     $response = rl_sparql_select($sparql);
     $this->assertFalse(is_wp_error($response));
     $lines = array();
     foreach (explode("\n", $response['body']) as $line) {
         if (empty($line)) {
             continue;
         }
         $lines[] = preg_replace('/\\s+/', '', $line);
     }
     return $lines;
 }