Skip to content

Commit

Permalink
0.1.1020 initalise_logfile, add classification id and property to ite…
Browse files Browse the repository at this point in the history
…m creation
  • Loading branch information
antaldaniel committed Mar 2, 2025
1 parent 54f17d4 commit 9002120
Show file tree
Hide file tree
Showing 15 changed files with 217 additions and 4 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: wbdataset
Title: Making Datasets Truly Interoperable and Reusable in R with Wikibase
Version: 0.1.1019
Date: 2024-02-24
Version: 0.1.1020
Date: 2024-03-02
Authors@R:
c(person(given="Daniel", family="Antal",
email= "[email protected]",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(add_id_statement)
export(add_item_statement)
export(add_statement)
export(add_wikibase_url_statement)
export(copy_wikidata_item)
Expand Down
73 changes: 73 additions & 0 deletions R/add_item_statement.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#' @rdname add_statement
#' @inheritParams add_statement
#' @param wikibase_type Defaults to \code{'item'}.
#' @param o The item to be added to the statement as an object in the semantic triple.
#' @export

add_item_statement <- function(qid, pid, o,
wikibase_type = "item",
wikibase_api_url=NULL,
csrf) {

# api.php?action=wbcreateclaim&entity=Q4115189&property=P9003&snaktype=value&value={"entity-type":"item","numeric-id":1}

assertthat::assert_that(!is.null(wikibase_api_url),
msg="No wikibase_api_url is given")

csrf_token <- get_csrf_token(csrf = csrf)

base_string <- '{"entity-type":"item","numeric-id":numid}'
new_numeric_id <- gsub("Q", "", o)
datavalue <- gsub('numid', new_numeric_id, base_string)
datavalue

claim_body <- list(
action = "wbcreateclaim",
entity = qid,
property = pid,
snaktype = "value",
value = datavalue,
# '"datavalue":{"value":"https://www.wikidata.org/wiki/Q43878","type":"string"}}',
token = csrf_token,
format = "json")

new_claim <- httr::POST(
wikibase_api_url,
body = list(
action = "wbcreateclaim",
entity = qid,
property = pid,
snaktype = "value",
value = datavalue,
# '"datavalue":{"value":"https://www.wikidata.org/wiki/Q43878","type":"string"}}',
token = csrf_token,
format = "json"),
encode = "form",
handle = csrf
)

response <- httr::content(new_claim, as = "parsed", type = "application/json")

if ("error" %in% names(response)) {
#invalid snak error at type mismatch
warning_message <- glue::glue("Error in 'wbcreateclaim' wrapper add_item_statement():\n", response$error$code, ": ", response$error$info)
warning(warning_message)

return(data.frame(
id = NA_character_,
s = qid,
o = NA_character_,
p = NA_character_
))
}

if (response$success == 1) {
data.frame(
id = response$claim$id,
s = qid,
o = pid,
p = response$claim$mainsnak$datavalue$value$id
)
}
}

28 changes: 28 additions & 0 deletions R/copy_wikidata_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#' \item{"wikibase_api_url"}{ The MediaWiki API URL where the new property is created.}
#' \item{"equivalence_property"}{ The PID that connects an equivalence ID to the property.}
#' \item{"equivalence_id"}{ The ID of an equivalent property defined elsewhere.}
#' \item{"classification_property"}{ Not applicable for properties.}
#' \item{"classification_id"}{ Not applicable for properties.}
#' \item{"success"}{ TRUE if successfully created, FALSE if there was an error.}
#' \item{"comment"}{ A summary of the error messages(s), if success is FALSE.}
#' \item{"time"}{ The time when the action started.}
Expand All @@ -57,6 +59,8 @@ copy_wikidata_item <- function(
qid_on_wikidata = "Q4",
qid_equivalence_property = "P35",
language = c("en", "nl", "hu"),
classification_property = NA_character_,
classification_id = NA_character_,
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
data_curator = NULL,
log_path = tempdir(),
Expand Down Expand Up @@ -109,6 +113,8 @@ copy_wikidata_item <- function(
return_log_file <- copy_wikidata_items(
qid_on_wikidata = qid_on_wikidata,
qid_equivalence_property = qid_equivalence_property,
classification_property = classification_property,
classification_id = classification_id,
language = language,
wikibase_api_url = wikibase_api_url,
data_curator = data_curator,
Expand Down Expand Up @@ -190,6 +196,8 @@ copy_wikidata_item <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
classification_property = classification_property,
classification_id = classification_id,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -343,6 +351,8 @@ copy_wikidata_item <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
classification_property = classification_property,
classification_id = classification_id,
success = TRUE,
comment = "",
time = action_timestamp,
Expand Down Expand Up @@ -413,6 +423,8 @@ copy_wikidata_item <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
classification_property = classification_property,
classification_id = classification_id,
success = FALSE,
comment = "Wikibase validator label conflict: label-language pair already exists.",
time = action_timestamp,
Expand Down Expand Up @@ -449,6 +461,8 @@ copy_wikidata_item <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
classification_property = classification_property,
classification_id = classification_id,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -502,6 +516,16 @@ copy_wikidata_item <- function(
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
classification_property = defined(
return_dataframe$classification_property,
label = "A property relationship to a class or superclass",
namespace = wikibase_api_url
),
classification_id = defined(
return_dataframe$classification_id,
label = "Superclass or class on the target instance.",
namespace = wikibase_api_url
),
success = return_dataframe$success,
comment = return_dataframe$comment,
time = return_dataframe$time,
Expand All @@ -527,6 +551,8 @@ copy_wikidata_item <- function(
#' @keywords internal
copy_wikidata_items <- function(qid_on_wikidata,
qid_equivalence_property,
classification_property,
classification_id,
language,
wikibase_api_url,
data_curator,
Expand Down Expand Up @@ -577,6 +603,8 @@ copy_wikidata_items <- function(qid_on_wikidata,
copy_wikidata_item(
qid_on_wikidata = x,
qid_equivalence_property = qid_equivalence_property,
classification_property = classification_property,
classification_id = classification_property,
language = language,
wikibase_api_url = wikibase_api_url,
data_curator = data_curator,
Expand Down
24 changes: 24 additions & 0 deletions R/copy_wikidata_property.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#' non-authenticated sources, this should be changed.)
#' @param pid_equivalence_property The PID in Wikibase that records the
#' equivalent Wikidata PID as an external ID.
#' @param classification_property The instance of, or subclass of, or superclass
#' of property. Defaults to \code{NA_character} when not used.
#' @param classification_id The QID of the class. Defaults to
#' \code{NA_character} when not used.
#' @param language A vector of language codes, for example, \code{c("en",
#' "et")}.
#' @param wikibase_api_url For example,
Expand Down Expand Up @@ -47,6 +51,8 @@
#' \item{"wikibase_api_url"}{ The MediaWiki API URL where the new property is created.}
#' \item{"equivalence_property"}{ The PID that connects an equivalence ID to the property.}
#' \item{"equivalence_id"}{ The ID of an equivalent property defined elsewhere.}
#' \item{"classification_property"}{ Not applicable for properties.}
#' \item{"classification_id"}{ Not applicable for properties.}
#' \item{"success"}{ TRUE if successfully created, FALSE if there was an error.}
#' \item{"comment"}{ A summary of the error messages(s), if success is FALSE.}
#' \item{"time"}{ The time when the action started.}
Expand Down Expand Up @@ -191,6 +197,8 @@ copy_wikidata_property <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = pid_on_wikidata,
classification_property = NA_character_,
classification_id = NA_character_,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -344,6 +352,8 @@ copy_wikidata_property <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = pid_equivalence_property,
equivalence_id = pid_on_wikidata,
classification_property = NA_character_,
classification_id = NA_character_,
success = TRUE,
comment = "",
time = action_timestamp,
Expand Down Expand Up @@ -409,6 +419,8 @@ copy_wikidata_property <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = pid_equivalence_property,
equivalence_id = pid_on_wikidata,
classification_property = NA_character_,
classification_id = NA_character_,
success = FALSE,
comment = "Wikibase validator label conflict: label-language pair already exists.",
time = action_timestamp,
Expand Down Expand Up @@ -446,6 +458,8 @@ copy_wikidata_property <- function(
wikibase_api_url = wikibase_api_url,
equivalence_property = pid_equivalence_property,
equivalence_id = pid_on_wikidata,
classification_property = NA_character_,
classification_id = NA_character_,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -498,6 +512,16 @@ copy_wikidata_property <- function(
return_dataframe$equivalence_id,
label = "Equivalent PID on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
classification_property = defined(
return_dataframe$classification_property,
label = "A property relationship to a class or superclass",
namespace = wikibase_api_url
),
classification_id = defined(
return_dataframe$classification_id,
label = "Superclass or class on the target instance.",
namespace = wikibase_api_url
),
success = return_dataframe$success,
comment = return_dataframe$comment,
Expand Down
24 changes: 24 additions & 0 deletions R/create_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#' another system's defintions. Defaults to
#' \code{NA_character_}; if left missing, no equivalence relations is will be
#' claimed.
#' @param classification_property The instance of, or subclass of, or superclass
#' of property. Defaults to \code{NA_character} when not used.
#' @param classification_id The QID of the class. Defaults to
#' \code{NA_character} when not used.
#' @param wikibase_api_url For example,
#' \code{'https://reprexbase.eu/demowiki/api.php'}.
#' @param data_curator The name of the data curator who runs the function and
Expand All @@ -44,6 +48,8 @@
#' \item{"wikibase_api_url"}{ The MediaWiki API URL where the new property is created}
#' \item{"equivalence_property"}{ The PID that connects an equivalence ID to the property}
#' \item{"equivalence_id"}{ The ID of an equivalent item defined elsewhere}
#' \item{"classification_property"}{ The PID that connects the item to a superclass, or class.}
#' \item{"classification_id"}{ The QID of a class, subclass or superclass.}
#' \item{"success"}{ TRUE if successfully created, FALSE if there was an error}
#' \item{"comment"}{ A summary of the error messages(s), if success is FALSE}
#' \item{"time"}{ The time when the action started}
Expand Down Expand Up @@ -74,6 +80,8 @@ create_item <- function(label,
language,
equivalence_property = NA_character_,
equivalence_id = NA_character_,
classification_property = NA_character_,
classification_id = NA_character_,
wikibase_api_url,
data_curator = NULL,
log_path = tempdir(),
Expand Down Expand Up @@ -200,6 +208,8 @@ create_item <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classificaiton_property = classification_property,
classification_id = classification_id,
success = TRUE,
comment = NA_character_,
time = action_timestamp,
Expand Down Expand Up @@ -241,6 +251,8 @@ create_item <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classificaiton_property = classification_property,
classification_id = classification_id,
success = FALSE,
comment = "wikibase-validator-label-conflict, the label-language pair already exists.",
time = action_timestamp,
Expand Down Expand Up @@ -271,6 +283,8 @@ create_item <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classificaiton_property = classification_property,
classification_id = classification_id,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -321,6 +335,16 @@ create_item <- function(label,
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
classification_property = defined(
return_dataframe$classification_property,
label = "A property relationship to a class or superclass",
namespace = wikibase_api_url
),
classification_id = defined(
return_dataframe$classification_id,
label = "Superclass or class on the target instance.",
namespace = wikibase_api_url
),
success = return_dataframe$success,
comment = return_dataframe$comment,
time = return_dataframe$time,
Expand Down
18 changes: 18 additions & 0 deletions R/create_property.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
#' \item{"wikibase_api_url"}{ The MediaWiki API URL where the new property is created.}
#' \item{"equivalence_property"}{ The PID that connects an equivalence ID to the property.}
#' \item{"equivalence_id"}{ The ID of an equivalent property defined elsewhere.}
#' \item{"classification_property"}{ Not applicable for properties.}
#' \item{"classification_id"}{ Not applicable for properties.}
#' \item{"success"}{ TRUE if successfully created, FALSE if there was an error.}
#' \item{"comment"}{ A summary of the error messages(s), if success is FALSE.}
#' \item{"time"}{ The time when the action started.}
Expand Down Expand Up @@ -204,6 +206,8 @@ create_property <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classification_property = NA_character_,
classification_id = NA_character_,
success = TRUE,
comment = NA_character_,
time = action_timestamp,
Expand Down Expand Up @@ -242,6 +246,8 @@ create_property <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classification_property = NA_character_,
classification_id = NA_character_,
success = FALSE,
comment = "wikibase-validator-label-conflict, the label-language pair already exists.",
time = action_timestamp,
Expand Down Expand Up @@ -272,6 +278,8 @@ create_property <- function(label,
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
classification_property = NA_character_,
classification_id = NA_character_,
success = FALSE,
comment = error_comments,
time = action_timestamp,
Expand Down Expand Up @@ -322,6 +330,16 @@ create_property <- function(label,
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
classification_property = defined(
return_dataframe$classification_property,
label = "A property relationship to a class or superclass",
namespace = wikibase_api_url
),
classification_id = defined(
return_dataframe$classification_id,
label = "Superclass or class on the target instance.",
namespace = wikibase_api_url
),
success = return_dataframe$success,
comment = return_dataframe$comment,
time = return_dataframe$time,
Expand Down
Loading

0 comments on commit 9002120

Please sign in to comment.