Skip to content

Commit 6d5a355

Browse files
committed
bug fix with create_item
1 parent 1ba517d commit 6d5a355

10 files changed

+58
-39
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: wbdataset
22
Title: Making Datasets Truly Interoperable and Reusable in R with Wikibase
3-
Version: 0.1.1048
4-
Date: 2024-04-19
3+
Version: 0.1.1050
4+
Date: 2024-04-20
55
Authors@R:
66
c(person(given="Daniel", family="Antal",
77
email= "[email protected]",

R/copy_wikidata_item.R

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
#' @export
7373

7474
copy_wikidata_item <- function(
75-
qid_on_wikidata = "Q4",
76-
qid_equivalence_property = "P35",
77-
language = c("en", "nl", "hu"),
75+
qid_on_wikidata,
76+
qid_equivalence_property,
77+
language = "en",
7878
classification_property = NA_character_,
7979
classification_id = NA_character_,
80-
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
80+
wikibase_api_url,
8181
data_curator = NULL,
8282
log_file_name = NULL,
8383
csrf,
@@ -87,7 +87,6 @@ copy_wikidata_item <- function(
8787
data_curator <- resolve_from_session("data_curator", data_curator, wikibase_session)
8888
log_file_name <- resolve_from_session("log_file_name", log_file_name, wikibase_session)
8989
wikibase_api_url <- resolve_from_session("wikibase_api_url", wikibase_api_url, wikibase_session)
90-
equivalence_property <- resolve_from_session("wikibase_api_url", equivalence_property, wikibase_session)
9190
classification_property <- resolve_from_session("wikibase_api_url", classification_property, wikibase_session)
9291
csrf <- resolve_from_session("csrf", csrf, wikibase_session)
9392
qid_equivalence_property <- resolve_from_session("qid_equivalence_property", qid_equivalence_property, wikibase_session)
@@ -98,8 +97,8 @@ copy_wikidata_item <- function(
9897
wikibase_api_url = wikibase_api_url,
9998
classification_property = classification_property,
10099
classification_id = classification_id,
101-
equivalence_property = equivalence_property,
102-
equivalence_id = equivalence_id,
100+
equivalence_property = NA_character_,
101+
equivalence_id = NA_character_,
103102
csrf = csrf,
104103
data_curator = data_curator,
105104
validated_action = "copy_wikidata_property()"

R/copy_wikidata_property.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@
7575

7676
copy_wikidata_property <- function(
7777
pid_on_wikidata,
78-
pid_equivalence_property = "P2",
78+
pid_equivalence_property,
7979
language = "en",
80-
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
80+
wikibase_api_url,
8181
classification_property = NA_character_,
8282
classification_id = NA_character_,
8383
equivalence_property = NA_character_,

R/create_item.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,28 +176,26 @@ create_item <- function(label,
176176
# See get_csrf, get_csrf_token.
177177
csrf_token <- get_csrf_token(csrf)
178178

179-
if(!is_valid_csrf(csrf_token)) {
179+
if(!is_valid_csrf_token(csrf_token)) {
180180
stop("create_item(, csrf): csfr does not seem to be valid.")
181181
}
182182

183-
safe_edit <- purrr::safely(call_wbeditentity)
183+
# safe_edit <- purrr::safely(call_wbeditentity)
184184

185185
# Posting the new property ----------------------------------------------
186-
new_item <-safe_edit(
187-
csrf_token = csrf,
188-
wikibase_api_url = wikibase_api_url,
189-
entity_data = json_data,
190-
new_entity_type = "item",
191-
csrf_handle = csrf,
192-
bot = TRUE,
193-
summary = glue::glue("Creating item for '{label}'")
186+
new_item <- httr::POST(
187+
wikibase_api_url,
188+
body = list(
189+
action = "wbeditentity",
190+
new = "item",
191+
data = datastring,
192+
token = csrf_token,
193+
format = "json"
194+
),
195+
encode = "form",
196+
handle = csrf
194197
)
195198

196-
if (!is.null(new_item$error)) {
197-
warning("create_item(): Failed to create item — ", new_item$error$message)
198-
return(NULL) # or structured error return
199-
}
200-
201199
# See if the created POST via wbeditentity was successful
202200
created_item_response <- httr::content(new_item,
203201
as = "parsed",

R/create_property.R

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ create_property <- function(label,
125125
validated_action = "create_property()"
126126
)
127127

128-
129-
130128
# Save the time of running the code
131129
action_time <- Sys.time()
132130
action_timestamp <- action_timestamp_create()
@@ -185,13 +183,9 @@ create_property <- function(label,
185183
# See get_csrf, get_csrf_token.
186184
csrf_token <- get_csrf_token(csrf)
187185

188-
assertthat::assert_that(!is.null(csrf_token),
189-
msg = "You do not have a CSRF token"
190-
)
191-
192-
assertthat::assert_that(nchar(csrf_token) == 42,
193-
msg = "Your CSRF token should have 42 characters."
194-
)
186+
if(!is_valid_csrf_token(csrf_token)) {
187+
stop("create_item(, csrf): csfr does not seem to be valid.")
188+
}
195189

196190
# Posting the new property ----------------------------------------------
197191
new_property <- httr::POST(

R/utils.R

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ get_return_ds_structure <- function() {
3333
)
3434
}
3535

36-
3736
#' @title Check if a CSRF token appears valid
3837
#' @description Validates the basic structure of a MediaWiki-style CSRF token.
3938
#' @param csrf_token A single character string representing the token.
@@ -45,3 +44,29 @@ is_valid_csrf_token <- function(csrf_token) {
4544
}
4645
ifelse(nchar(csrf_token)>10, TRUE, FALSE)
4746
}
47+
48+
#' @keywords internal
49+
is_valid_wikibase_datatype <- function(x) {
50+
valid_datatypes <- c(
51+
"wikibase-item",
52+
"wikibase-property",
53+
"external-id",
54+
"url",
55+
"commonsMedia",
56+
"string",
57+
"monolingualtext",
58+
"quantity",
59+
"time",
60+
"globe-coordinate",
61+
"math",
62+
"geo-shape",
63+
"tabular-data",
64+
"musical-notation",
65+
"wikibase-lexeme",
66+
"wikibase-form",
67+
"wikibase-sense"
68+
)
69+
70+
x <- as.character(x)
71+
x %in% valid_datatypes
72+
}

R/validate_create_entity_args.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ validate_create_entity_args <- function(label,
2020
stop(validated_action, ": 'label' must be a non-empty character string.")
2121
}
2222

23-
if (is.na(language) || !is.character(language) || length(language) < 1 || any(nchar(language))==0 ) {
23+
if (any(is.na(language)) || !any(is.character(language)) || length(language) < 1 || any(nchar(language))<0 ) {
2424
stop(validated_action, ": 'language' must be a non-empty character vector.")
2525
}
2626

@@ -83,7 +83,7 @@ validate_copy_entity_args <- function(language,
8383
stop("validate_copy_entity_args(..., validated_action) : 'validated_action' must be a non-empty character string.")
8484
}
8585

86-
if (is.na(language) || !is.character(language) || length(language) < 1 || any(nchar(language))==0 ) {
86+
if (any(is.na(language)) || !any(is.character(language)) || length(language) < 1 || any(nchar(language))<0 ) {
8787
stop(validated_action, ": 'language' must be a non-empty character vector.")
8888
}
8989

_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ reference:
3939
contents:
4040
- get_wikidata_item
4141
- get_property_definition
42+
- get_claim
4243
- get_claims
4344
- left_join_column
4445
- title: "Writing"
@@ -49,6 +50,7 @@ reference:
4950
- create_property
5051
- add_statement
5152
- add_id_statement
53+
- new_wikibase_session
5254
- title: "Copying"
5355
desc: >
5456
Copying from a Wikibase instance to another one. Currently only copying from

tests/testthat/test-copy_wikidata_property.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ test_that("copy_wikidata_property() fails when CSRF token is missing or invalid"
1111
test_that("copy_wikidata_property() fails when data curator is not a person", {
1212
expect_error(copy_wikidata_property(
1313
pid_on_wikidata = "P31",
14+
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
1415
csrf = NULL,
1516
data_curator = 123
1617
), regexp = "must be a person")

tests/testthat/test-validate_create_entity_args.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test_that("validate_create_entity_args(..., csrf)", {
5555

5656

5757
test_that("validate_copy_entity_args(..., validated_action)", {
58-
expect_error(validate_copy_entity_args(language="en",
58+
expect_error(validate_copy_entity_args(language=c("en", "hu", "nl"),
5959
"https://reprexbase.eu/jekyll/api.php",
6060
equivalence_property = "P11",
6161
equivalence_id = "Q12",

0 commit comments

Comments
 (0)