Skip to content

Commit

Permalink
improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
antaldaniel committed Feb 21, 2025
1 parent 0cce548 commit 25df012
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 21 deletions.
21 changes: 11 additions & 10 deletions R/copy_wikidata_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ copy_wikidata_item <- function(
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
data_curator = NULL,
log_path = tempdir(),
csrf) {
csrf,
wikibase_session = NULL) {

if (!is.null(wikibase_session)) {
# For repeated queries you can add your variables directly or in a list

Expand Down Expand Up @@ -336,7 +338,7 @@ copy_wikidata_item <- function(
label = created_item_label[[1]]$value,
description = created_item_description[[1]]$value,
language = created_item_label[[1]]$language,
datatype = "item",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
Expand Down Expand Up @@ -406,7 +408,7 @@ copy_wikidata_item <- function(
label = existing_label,
description = existing_description,
language = language,
datatype = "<not retrieved>",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
Expand All @@ -426,12 +428,12 @@ copy_wikidata_item <- function(
# Return an emptier data.frame if there was some error

# Print out the error message verbatim to terminal
message(created_property_response$error)
message(created_item_response$error)

# Wrap the main error types into the logfile and return data
error_comments <- paste(
unlist(
lapply(created_property_response$error$messages, function(x) x$name)
lapply(created_item_response$error$messages, function(x) x$name)
),
collapse = "|"
)
Expand All @@ -442,7 +444,7 @@ copy_wikidata_item <- function(
label = "<not retrieved>",
description = "<not retrieved>",
language = "<not retrieved>",
datatype = "<not retrieved>",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = qid_equivalence_property,
equivalence_id = qid_on_wikidata,
Expand Down Expand Up @@ -472,7 +474,7 @@ copy_wikidata_item <- function(
action = return_dataframe$action,
id_on_target = defined(
return_dataframe$id_on_target,
label = paste0("QID on ", wikibase_api_url),
label = paste0("ID on ", wikibase_api_url),
namespace = wikibase_api_url
),
label = defined(
Expand All @@ -481,7 +483,7 @@ copy_wikidata_item <- function(
),
description = defined(
return_dataframe$description,
label = "Description of item"
label = "Description of entity"
),
language = defined(
return_dataframe$language,
Expand All @@ -496,7 +498,7 @@ copy_wikidata_item <- function(
),
equivalence_id = defined(
return_dataframe$equivalence_id,
label = "Equivalent QID on Wikidata",
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
success = return_dataframe$success,
Expand All @@ -508,7 +510,6 @@ copy_wikidata_item <- function(
"Wikibase Copy Item Log (",
strftime(action_time, "%Y-%m-%d %H:%M:%OS0"), ")"
),
description = description_text,
creator = data_curator,
dataset_date = Sys.Date()
)
Expand Down
11 changes: 7 additions & 4 deletions R/copy_wikidata_property.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ copy_wikidata_property <- function(
log_path = tempdir(),
csrf,
wikibase_session = NULL) {

if (!is.null(wikibase_session)) {
# For repeated queries you can add your variables directly or in a list

Expand Down Expand Up @@ -103,6 +104,7 @@ copy_wikidata_property <- function(
msg = 'copy_wikidata_item(..., data_curator): data_curator must be a person, like person("Jane, "Doe").'
)

property_wikibase_datatype <- "<not retrieved>" # set default value

if (length(pid_on_wikidata) > 1) {
# Run this function in a loop if there are several PIDs to copy
Expand Down Expand Up @@ -270,6 +272,7 @@ copy_wikidata_property <- function(
datatype = response$entities[[1]]$datatype
)

property_wikibase_datatype <- response$entities[[1]]$datatype
datastring

## Getting the user's CSRF token for writing.
Expand Down Expand Up @@ -401,7 +404,7 @@ copy_wikidata_property <- function(
label = existing_label,
description = existing_description,
language = language,
datatype = "<not retrieved>",
datatype = property_wikibase_datatype,
wikibase_api_url = wikibase_api_url,
equivalence_property = pid_equivalence_property,
equivalence_id = pid_on_wikidata,
Expand Down Expand Up @@ -438,7 +441,7 @@ copy_wikidata_property <- function(
label = "<not retrieved>",
description = "<not retrieved>",
language = "<not retrieved>",
datatype = "<not retrieved>",
datatype = property_wikibase_datatype,
wikibase_api_url = wikibase_api_url,
equivalence_property = pid_equivalence_property,
equivalence_id = pid_on_wikidata,
Expand Down Expand Up @@ -468,7 +471,7 @@ copy_wikidata_property <- function(
action = return_dataframe$action,
id_on_target = defined(
return_dataframe$id_on_target,
label = paste0("PID on ", wikibase_api_url),
label = paste0("ID on ", wikibase_api_url),
namespace = wikibase_api_url
),
label = defined(
Expand Down Expand Up @@ -504,7 +507,7 @@ copy_wikidata_property <- function(
"Wikibase Copy Property Log (",
strftime(action_time, "%Y-%m-%d %H:%M:%OS0"), ")"
),
description = description_text,
description_text,
creator = data_curator,
dataset_date = Sys.Date()
),
Expand Down
64 changes: 60 additions & 4 deletions R/create_item.R
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ create_item <- function(label,
label = label,
description = created_item_response$entity$descriptions[[1]]$value,
language = created_item_response$entity$descriptions[[1]]$language,
datatype = "item",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
Expand Down Expand Up @@ -237,7 +237,7 @@ create_item <- function(label,
label = label,
description = "<description match not inspected>",
language = language,
datatype = "item",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
Expand Down Expand Up @@ -267,7 +267,7 @@ create_item <- function(label,
label = label,
description = description,
language = language,
datatype = "item",
datatype = "wikibase-item",
wikibase_api_url = wikibase_api_url,
equivalence_property = equivalence_property,
equivalence_id = equivalence_id,
Expand All @@ -284,5 +284,61 @@ create_item <- function(label,
fileEncoding = "UTF-8")
}

return_dataframe
description_text <- paste0(
"Attempted and successful item creation on Wikibase to ",
wikibase_api_url, " with wbdataset:create_item() at ",
substr(as.character(action_time), 1, 19)
)

return_ds <- dataset_df(
action = return_dataframe$action,
id_on_target = defined(
return_dataframe$id_on_target,
label = paste0("ID on ", wikibase_api_url),
namespace = wikibase_api_url
),
label = defined(
return_dataframe$label,
label = "Label of entity"
),
description = defined(
return_dataframe$description,
label = "Description of entity"
),
language = defined(
return_dataframe$language,
label = "Language of label and description"
),
datatype = return_dataframe$datatype,
wikibase_api_url = wikibase_api_url,
equivalence_property = defined(
return_dataframe$equivalence_property,
label = paste0("Equivalence property on ", wikibase_api_url),
namespace = wikibase_api_url
),
equivalence_id = defined(
return_dataframe$equivalence_id,
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
success = return_dataframe$success,
comment = return_dataframe$comment,
time = return_dataframe$time,
logfile = return_dataframe$logfile,
dataset_bibentry = dublincore(
title = paste0(
"Wikibase Create Property Log (",
strftime(action_time, "%Y-%m-%d %H:%M:%OS0"), ")"
),
description_text,
creator = data_curator,
dataset_date = Sys.Date()
)
)

return_ds$rowid <- defined(paste0("wbi:", as.character(return_ds$id_on_target)),
namespace = wikibase_api_url
)

return_ds
}
60 changes: 58 additions & 2 deletions R/create_property.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ create_property <- function(label,


# Save the time of running the code
action_time <- Sys.time()
action_timestamp <- action_timestamp_create()
log_file_name <- paste0("wbdataset_create_property_", action_timestamp, ".csv")

Expand Down Expand Up @@ -222,7 +223,7 @@ create_property <- function(label,
# wikibase-validator-label-conflict: the property already exists, and this
# information should be returned to the user.
message_strings <- unlist(created_property_response$error$messages)
message(message_strings)

message_strings <- message_strings[which(grepl("Property:", message_strings))]
pattern <- "\\[\\[Property:*(.*?)\\|"
result <- regmatches(message_strings, regexec(pattern, message_strings))
Expand Down Expand Up @@ -284,5 +285,60 @@ create_property <- function(label,
fileEncoding = "UTF-8")
}

return_dataframe
description_text <- paste0(
"Attempted and successful property creation on Wikibase to ",
wikibase_api_url, " with wbdataset:create_property() at ",
substr(as.character(action_time), 1, 19)
)

return_ds <- dataset_df(
action = return_dataframe$action,
id_on_target = defined(
return_dataframe$id_on_target,
label = paste0("ID on ", wikibase_api_url),
namespace = wikibase_api_url
),
label = defined(
return_dataframe$label,
label = "Label of entity"
),
description = defined(
return_dataframe$description,
label = "Description of entity"
),
language = defined(
return_dataframe$language,
label = "Language of label and description"
),
datatype = return_dataframe$datatype,
wikibase_api_url = wikibase_api_url,
equivalence_property = defined(
return_dataframe$equivalence_property,
label = paste0("Equivalence property on ", wikibase_api_url),
namespace = wikibase_api_url
),
equivalence_id = defined(
return_dataframe$equivalence_id,
label = "Equivalent entity on Wikidata",
namespace = "https://www.wikidata.org/wiki/"
),
success = return_dataframe$success,
comment = return_dataframe$comment,
time = return_dataframe$time,
logfile = return_dataframe$logfile,
dataset_bibentry = dublincore(
title = paste0(
"Wikibase Create Property Log (",
strftime(action_time, "%Y-%m-%d %H:%M:%OS0"), ")"
),
creator = data_curator,
dataset_date = Sys.Date()
)
)

return_ds$rowid <- defined(paste0("wbi:", as.character(return_ds$id_on_target)),
namespace = wikibase_api_url
)

return_ds
}
3 changes: 2 additions & 1 deletion man/copy_wikidata_item.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25df012

Please sign in to comment.