diff --git a/DESCRIPTION b/DESCRIPTION index ff4f745..0eef261 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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= "daniel.antal@dataobservatory.eu", diff --git a/NAMESPACE b/NAMESPACE index e7576f8..923f8de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/add_item_statement.R b/R/add_item_statement.R new file mode 100644 index 0000000..3b5d03d --- /dev/null +++ b/R/add_item_statement.R @@ -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 + ) + } +} + diff --git a/R/copy_wikidata_item.R b/R/copy_wikidata_item.R index 8e1f229..9612a9c 100644 --- a/R/copy_wikidata_item.R +++ b/R/copy_wikidata_item.R @@ -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.} @@ -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(), @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/R/copy_wikidata_property.R b/R/copy_wikidata_property.R index 4fb1664..23bbc81 100644 --- a/R/copy_wikidata_property.R +++ b/R/copy_wikidata_property.R @@ -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, @@ -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.} @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/R/create_item.R b/R/create_item.R index 05928a6..2746599 100644 --- a/R/create_item.R +++ b/R/create_item.R @@ -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 @@ -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} @@ -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(), @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/R/create_property.R b/R/create_property.R index b2543f4..fbfd71f 100644 --- a/R/create_property.R +++ b/R/create_property.R @@ -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.} @@ -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, @@ -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, @@ -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, @@ -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, diff --git a/R/initialise_logfile.R b/R/initialise_logfile.R index d19e03c..ff81eb4 100644 --- a/R/initialise_logfile.R +++ b/R/initialise_logfile.R @@ -20,6 +20,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.} @@ -51,6 +53,8 @@ initialise_logfile <- function(logfile_description="A wbdataset logfile.", wikibase_api_url = NA_character_, equivalence_property = NA_character_, equivalence_id = NA_character_, + classification_property = NA_character_, + classification_id = NA_character_, success = FALSE, comment = NA_character_, time = NA_character_, diff --git a/_pkgdown.yml b/_pkgdown.yml index 050325c..eb98286 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -45,6 +45,7 @@ reference: desc: > Write to a Wikibase instance contents: + - initialise_logfile - create_item - create_property - add_statement diff --git a/man/add_statement.Rd b/man/add_statement.Rd index 7d9b96c..f96e528 100644 --- a/man/add_statement.Rd +++ b/man/add_statement.Rd @@ -1,7 +1,9 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/add_id_statement.R, R/add_statement.R +% Please edit documentation in R/add_id_statement.R, R/add_item_statement.R, +% R/add_statement.R \name{add_id_statement} \alias{add_id_statement} +\alias{add_item_statement} \alias{add_statement} \alias{add_wikibase_url_statement} \title{Add statements} @@ -15,6 +17,15 @@ add_id_statement( csrf ) +add_item_statement( + qid, + pid, + o, + wikibase_type = "item", + wikibase_api_url = NULL, + csrf +) + add_statement( qid, pid, diff --git a/man/copy_wikidata_item.Rd b/man/copy_wikidata_item.Rd index e183c60..bbca4af 100644 --- a/man/copy_wikidata_item.Rd +++ b/man/copy_wikidata_item.Rd @@ -11,6 +11,8 @@ copy_wikidata_item( 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(), @@ -21,11 +23,14 @@ copy_wikidata_item( copy_wikidata_items( qid_on_wikidata, qid_equivalence_property, + classification_property, + classification_id, language, wikibase_api_url, data_curator, log_path, - csrf + csrf, + wikibase_session ) copy_wikidata_properties( @@ -79,6 +84,8 @@ Returns a dataset_df object. The columns are: \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.} diff --git a/man/copy_wikidata_property.Rd b/man/copy_wikidata_property.Rd index 9cf8a2e..6a2396d 100644 --- a/man/copy_wikidata_property.Rd +++ b/man/copy_wikidata_property.Rd @@ -42,6 +42,12 @@ creates the log file, created with \link[utils]{person}.} parameters \code{qid_equivalence_property}, \code{language}, \code{wikibase_api_url}, \code{data_curator},\code{log_path} and \code{csrf} (for repeated use in a session.)} + +\item{classification_property}{The instance of, or subclass of, or superclass +of property. Defaults to \code{NA_character} when not used.} + +\item{classification_id}{The QID of the class. Defaults to +\code{NA_character} when not used.} } \value{ Returns a dataset_df object. The columns are: @@ -56,6 +62,8 @@ Returns a dataset_df object. The columns are: \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.} diff --git a/man/create_item.Rd b/man/create_item.Rd index 6b4faf0..f4714e2 100644 --- a/man/create_item.Rd +++ b/man/create_item.Rd @@ -10,6 +10,8 @@ create_item( 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(), @@ -34,6 +36,12 @@ another system's defintions. Defaults to \code{NA_character_}; if left missing, no equivalence relations is will be claimed.} +\item{classification_property}{The instance of, or subclass of, or superclass +of property. Defaults to \code{NA_character} when not used.} + +\item{classification_id}{The QID of the class. Defaults to +\code{NA_character} when not used.} + \item{wikibase_api_url}{For example, \code{'https://reprexbase.eu/demowiki/api.php'}.} @@ -64,6 +72,8 @@ are: \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} diff --git a/man/create_property.Rd b/man/create_property.Rd index b956b7e..1066dcd 100644 --- a/man/create_property.Rd +++ b/man/create_property.Rd @@ -68,6 +68,8 @@ are: \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.} diff --git a/man/initialise_logfile.Rd b/man/initialise_logfile.Rd index 3b39909..2e46877 100644 --- a/man/initialise_logfile.Rd +++ b/man/initialise_logfile.Rd @@ -34,6 +34,8 @@ Returns a dataset_df object. The columns are: \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.}