コード例 #1
0
 function test_delete()
 {
     $users_ids = get_users(array('fields' => 'id'));
     foreach ($users_ids as $user_id) {
         // Get the URI.
         $user_uri = Wordlift_User_Service::get_instance()->get_uri($user_id);
         $query = Wordlift_Query_Builder::new_instance()->delete()->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o')->build() . Wordlift_Query_Builder::new_instance()->delete()->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o')->build() . Wordlift_Query_Builder::new_instance()->delete()->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o')->build() . Wordlift_Query_Builder::new_instance()->delete()->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o')->build();
         $this->log_service->info($query);
     }
 }
/**
 * Push the provided entity post to Redlink.
 *
 * @param object $entity_post An entity post instance.
 */
function wl_push_entity_post_to_redlink($entity_post)
{
    // Get the Entity service instance to perform actions related to entities.
    $entity_service = Wordlift_Entity_Service::get_instance();
    // Only handle published entities.
    if (!$entity_service->is_entity($entity_post->ID) || 'publish' !== $entity_post->post_status) {
        wl_write_log("wl_push_entity_post_to_redlink : not an entity or not published [ post type :: {$entity_post->post_type} ][ post status :: {$entity_post->post_status} ]");
        return;
    }
    // get the entity URI and the SPARQL escaped version.
    $uri = wl_get_entity_uri($entity_post->ID);
    $uri_e = wl_sparql_escape_uri($uri);
    // If the URI ends with a trailing slash, then we have a problem.
    if ('/' === substr($uri, -1, 1)) {
        wl_write_log("wl_push_entity_post_to_redlink : the URI is invalid [ post ID :: {$entity_post->ID} ][ URI :: {$uri} ]");
        return;
    }
    // Get the site language in order to define the literals language.
    $site_language = wl_configuration_get_site_language();
    // get the title and content as label and description.
    $label = wordlift_esc_sparql($entity_post->post_title);
    $descr = wordlift_esc_sparql(wp_strip_all_tags(strip_shortcodes($entity_post->post_content)));
    $permalink = wl_sparql_escape_uri(get_permalink($entity_post->ID));
    // wl_write_log( "wl_push_entity_post_to_redlink [ entity post id :: $entity_post->ID ][ uri :: $uri ][ label :: $label ]" );
    // create a new empty statement.
    $delete_stmt = '';
    $sparql = '';
    // delete on RL all statements regarding properties set from WL (necessary when changing entity type)
    $all_custom_fields = wl_entity_taxonomy_get_custom_fields();
    $predicates_to_be_deleted = array();
    foreach ($all_custom_fields as $type => $fields) {
        foreach ($fields as $cf) {
            $predicate = $cf['predicate'];
            if (!in_array($predicate, $predicates_to_be_deleted)) {
                $predicates_to_be_deleted[] = $predicate;
                $delete_stmt .= "DELETE { <{$uri_e}> <{$predicate}> ?o } WHERE  { <{$uri_e}> <{$predicate}> ?o };\n";
            }
        }
    }
    // set the same as.
    $same_as = wl_schema_get_value($entity_post->ID, 'sameAs');
    foreach ($same_as as $same_as_uri) {
        $same_as_uri_esc = wl_sparql_escape_uri($same_as_uri);
        $sparql .= "<{$uri_e}> owl:sameAs <{$same_as_uri_esc}> . \n";
    }
    // set the label
    $sparql .= "<{$uri_e}> rdfs:label \"{$label}\"@{$site_language} . \n";
    // Set the alternative labels.
    $alt_labels = $entity_service->get_alternative_labels($entity_post->ID);
    foreach ($alt_labels as $alt_label) {
        $sparql .= sprintf('<%s> rdfs:label "%s"@%s . ', $uri_e, Wordlift_Query_Builder::escape_value($alt_label), $site_language);
    }
    // set the URL
    $sparql .= "<{$uri_e}> schema:url <{$permalink}> . \n";
    // set the description.
    if (!empty($descr)) {
        $sparql .= "<{$uri_e}> schema:description \"{$descr}\"@{$site_language} . \n";
    }
    $main_type = wl_entity_type_taxonomy_get_type($entity_post->ID);
    if (null != $main_type) {
        $main_type_uri = wl_sparql_escape_uri($main_type['uri']);
        $sparql .= " <{$uri_e}> a <{$main_type_uri}> . \n";
        // The type define custom fields that hold additional data about the entity.
        // For example Events may have start/end dates, Places may have coordinates.
        // The value in the export fields must be rewritten as triple predicates, this
        // is what we're going to do here.
        //		wl_write_log( 'wl_push_entity_post_to_redlink : checking if entity has export fields [ type :: ' . var_export( $main_type, true ) . ' ]' );
        if (isset($main_type['custom_fields'])) {
            foreach ($main_type['custom_fields'] as $field => $settings) {
                // wl_write_log( "wl_push_entity_post_to_redlink : entity has export fields" );
                $predicate = wordlift_esc_sparql($settings['predicate']);
                if (!isset($settings['export_type']) || empty($settings['export_type'])) {
                    $type = null;
                } else {
                    $type = $settings['export_type'];
                }
                foreach (get_post_meta($entity_post->ID, $field) as $value) {
                    $sparql .= " <{$uri_e}> <{$predicate}> ";
                    if (!is_null($type) && substr($type, 0, 4) == 'http') {
                        // Type is defined by a raw uri (es. http://schema.org/PostalAddress)
                        // Extract uri if the value is numeric
                        if (is_numeric($value)) {
                            $value = wl_get_entity_uri($value);
                        }
                        $sparql .= '<' . wl_sparql_escape_uri($value) . '>';
                    } else {
                        // Type is defined in another way (es. xsd:double)
                        $sparql .= '"' . wordlift_esc_sparql($value) . '"^^' . wordlift_esc_sparql($type);
                    }
                    $sparql .= " . \n";
                }
            }
        }
    }
    // Get the entity types.
    $type_uris = wl_get_entity_rdf_types($entity_post->ID);
    // Support type are only schema.org ones: it could be null
    foreach ($type_uris as $type_uri) {
        $type_uri = wl_sparql_escape_uri($type_uri);
        $sparql .= "<{$uri_e}> a <{$type_uri}> . \n";
    }
    // get related entities.
    $related_entities_ids = wl_core_get_related_entity_ids($entity_post->ID);
    if (is_array($related_entities_ids)) {
        foreach ($related_entities_ids as $entity_post_id) {
            $related_entity_uri = wl_sparql_escape_uri(wl_get_entity_uri($entity_post_id));
            // create a two-way relationship.
            $sparql .= " <{$uri_e}> dct:relation <{$related_entity_uri}> . \n";
            $sparql .= " <{$related_entity_uri}> dct:relation <{$uri_e}> . \n";
        }
    }
    // Add SPARQL stmts to write the schema:image.
    $sparql .= wl_get_sparql_images($uri, $entity_post->ID);
    $query = rl_sparql_prefixes() . <<<EOF
    {$delete_stmt}
    DELETE { <{$uri_e}> rdfs:label ?o } WHERE  { <{$uri_e}> rdfs:label ?o };
    DELETE { <{$uri_e}> owl:sameAs ?o . } WHERE  { <{$uri_e}> owl:sameAs ?o . };
    DELETE { <{$uri_e}> schema:description ?o . } WHERE  { <{$uri_e}> schema:description ?o . };
    DELETE { <{$uri_e}> schema:url ?o . } WHERE  { <{$uri_e}> schema:url ?o . };
    DELETE { <{$uri_e}> a ?o . } WHERE  { <{$uri_e}> a ?o . };
    DELETE { <{$uri_e}> dct:relation ?o . } WHERE  { <{$uri_e}> dct:relation ?o . };
    DELETE { <{$uri_e}> schema:image ?o . } WHERE  { <{$uri_e}> schema:image ?o . };
    INSERT DATA { {$sparql} };
EOF;
    rl_execute_sparql_update_query($query);
}
コード例 #3
0
 /**
  * Get the insert query.
  *
  * @since 3.1.7
  *
  * @param int $user_id The user id.
  *
  * @return false|string The insert query or false in case of failure.
  */
 private function get_insert_query($user_id)
 {
     // Get the URI, return if there's none.
     if (false === ($user_uri = $this->get_uri($user_id))) {
         return false;
     }
     // Try to get the user data, in case of failure return false.
     if (false === ($user = get_userdata($user_id))) {
         return false;
     }
     // Build the insert query.
     $query = Wordlift_Query_Builder::new_instance()->insert()->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI)->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name)->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname)->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname)->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, !empty($user->user_url) ? $user->user_url : get_author_posts_url($user_id))->build();
     return $query;
 }