/** * Get the SPARQL fragment to set the dc:references statements. * * @param int $post_id The post ID. * * @return string The SPARQL fragment (or an empty string). */ function wl_get_sparql_post_references($post_id) { // Get the post URI. $post_uri = wordlift_esc_sparql(wl_get_entity_uri($post_id)); // Get the related entities IDs. $related = wl_core_get_related_entity_ids($post_id); // Build the SPARQL fragment. $sparql = ''; foreach ($related as $id) { $uri = wordlift_esc_sparql(wl_get_entity_uri($id)); $sparql .= "<{$post_uri}> dct:references <{$uri}> . "; } return $sparql; }
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; }
/** * 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)); } }
/** * Called when a user is updated. * * @param int $user_id The user ID. * * @return true if successful otherwise false. */ function wl_update_user_profile($user_id) { wl_write_log("wl_update_user_profile [ user id :: {$user_id} ]"); // Get the site language setting. $language = wl_configuration_get_site_language(); // Get the user. $user = get_userdata($user_id); // Get the user URI. $uri = wl_sparql_escape_uri(wl_get_user_uri($user_id)); // Get the first/last name and the posts URL. $first_name = wordlift_esc_sparql($user->first_name); $last_name = wordlift_esc_sparql($user->last_name); $posts_url = wl_sparql_escape_uri(get_author_posts_url($user_id)); $query = rl_sparql_prefixes(); $query .= <<<EOF DELETE { <{$uri}> schema:givenName ?o } WHERE { <{$uri}> schema:givenName ?o }; DELETE { <{$uri}> schema:familyName ?o } WHERE { <{$uri}> schema:familyName ?o }; DELETE { <{$uri}> schema:url ?o } WHERE { <{$uri}> schema:url ?o }; INSERT DATA { <{$uri}> schema:givenName '{$first_name}'@{$language} . <{$uri}> schema:familyName '{$last_name}'@{$language} . <{$uri}> schema:url <{$posts_url}> . } EOF; // Execute the query. return rl_execute_sparql_update_query($query); }