Skip to content

Commit bb2f704

Browse files
committed
Many improvements for the new version
1 parent 914c44b commit bb2f704

31 files changed

Lines changed: 487 additions & 449 deletions

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
33
Version: 0.1.1034
4-
Date: 2024-03-26
4+
Date: 2024-03-30
55
Authors@R:
66
c(person(given="Daniel", family="Antal",
77
email= "daniel.antal@dataobservatory.eu",
@@ -38,9 +38,9 @@ Imports:
3838
glue,
3939
here,
4040
httr,
41+
jsonlite,
4142
magrittr,
4243
purrr,
43-
readr,
4444
utils
4545
Language: en-US
4646
VignetteBuilder: knitr

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export("%>%")
44
export(add_id_statement)
55
export(add_item_statement)
66
export(add_statement)
7-
export(add_wikibase_url_statement)
87
export(check_existing_item)
98
export(check_existing_property)
109
export(copy_wikidata_item)

NEWS.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# wbdataset
22

3-
# 0.1.1023
4-
* check_existing_item, check_existing_property
3+
# 0.1.1034
4+
* Remove the unnecessary `log_path` parameter from the create and copy function
5+
families.
6+
* Many corrections in the formatting and the contents of the function reference.
7+
8+
# wbdataset 0.1.1023
9+
* [check_existing_item()], [check_existing_property()]
510

6-
# 0.1.1014
11+
# wbdataset 0.1.1014
712
* copy_wikidata_item, copy_wikidata_property works with one or several items/properties
813

9-
# 0.1.1
14+
# wbdataset 0.1.1
1015
* 0.1.0003: first long-form article, package website.
1116
* 0.1.1: MediaWiki API vignette and functions.
1217

R/add_id_statement.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
#' @rdname add_statement
2-
#' @inheritParams add_statement
2+
#' @description See
3+
#' \link[https://www.wikidata.org/w/api.php?action=help&modules=wbcreateclaim]{MediaWiki
4+
#' API help}
5+
#' @param qid The QID of the item in the Wikibase instance that you use.
6+
#' @param pid The PID of the equivalent Wikidata (or reference Wikibase) URI.
7+
#' @param o The object of the semantic statement.
38
#' @param wikibase_type Defaults to \code{"external-id"}.
9+
#' @param wikibase_api_url The full URL of the Wikibase API, which is the
10+
#' address that the \code{wbdataset} R client sends requests to when
11+
#' interacting with the knowledge base. For example,
12+
#' \code{'https://reprexbase.eu/demowiki/api.php'}. The URL must end with
13+
#' api.php.
14+
#' @param csrf The CSRF token of your session, received with
15+
#' \code{\link{get_csrf}}.
416
#' @importFrom httr POST content
517
#' @importFrom glue glue
618
#' @export
7-
add_id_statement <- function(qid, pid, o,
8-
wikibase_type = "external-id",
9-
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
10-
csrf) {
19+
add_id_statement <- function(
20+
qid, pid, o,
21+
wikibase_type = "external-id",
22+
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
23+
csrf) {
24+
1125
datavalue <- paste0('"', o, '"')
1226
datavalue
1327

R/add_item_statement.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#' @rdname add_statement
2-
#' @inheritParams add_statement
3-
#' @param wikibase_type Defaults to \code{'item'}.
4-
#' @param o The item to be added to the statement as an object in the semantic triple.
2+
#' @param qid The QID of the item in the Wikibase instance that you use.
3+
#' @param pid The PID of the equivalent Wikidata (or reference Wikibase) URI.
4+
#' @param o The object of the semantic statement.
5+
#' @param wikibase_type It must be \code{'item'}.
56
#' @export
67

78
add_item_statement <- function(qid, pid, o,

R/add_statement.R

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
#' @title Add statements
2-
#' @description
3-
#' See \link[https://www.wikidata.org/w/api.php?action=help&modules=wbcreateclaim]{MediaWiki API help}
4-
#'
2+
#' @description See
3+
#' \link[https://www.wikidata.org/w/api.php?action=help&modules=wbcreateclaim]{MediaWiki
4+
#' API help}
55
#' @param qid The QID of the item in the Wikibase instance that you use.
66
#' @param pid The PID of the equivalent Wikidata (or reference Wikibase) URI.
77
#' @param o The object of the semantic statement.
88
#' @param wikibase_type A \code{'item'}, \code{'numeric'}, or \code{'string'}.
9-
#' @param wikibase_api_url \code{\link{"https://reprexbase.eu/demowiki/api.php"}}.
10-
#' @param csrf The CSRF token of your session, received with \code{\link{get_csrf}}.
9+
#' @param wikibase_api_url The full URL of the Wikibase API, which is the
10+
#' address that the \code{wbdataset} R client sends requests to when
11+
#' interacting with the knowledge base. For example,
12+
#' \code{'https://reprexbase.eu/demowiki/api.php'}. The URL must end with
13+
#' api.php.
14+
#' @param csrf The CSRF token of your session, received with
15+
#' \code{\link{get_csrf}}.
1116
#' @return A data.frame with four columns:
1217
#' \code{id} containing the statement ID, \code{qid} with the QID,
1318
#' \code{o} with the PID, and \code{p} with the wikidata_uri.
1419
#' @export
1520

16-
add_statement <- function(qid, pid, o,
17-
wikibase_type,
18-
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
19-
csrf) {
21+
add_statement <- function(
22+
qid, pid, o,
23+
wikibase_type,
24+
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
25+
csrf) {
26+
2027
if (wikibase_type == "string") {
2128
add_item_statement(
2229
qid = qid, pid = pid, o = o,
@@ -35,59 +42,3 @@ add_statement <- function(qid, pid, o,
3542
stop("Error in add_statement(..., wikibase_type): '", wikibase_type, "' is not recognised (yet).")
3643
}
3744
}
38-
39-
#' @rdname add_statement
40-
#' @inheritParams add_statement
41-
#' @param wikidata_uri The QID on Wikidata (or a reference Wikibase).
42-
#' @export
43-
44-
add_wikibase_url_statement <- function(qid,
45-
pid,
46-
wikibase_url,
47-
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
48-
csrf_token) {
49-
datavalue <- gsub(
50-
"changeuri", wikibase_url,
51-
'{"claims":[{"mainsnak":{"snaktype":"value","property":"pid","datavalue":{"value":"changeuri","type":"string"}},"type":"statement","rank":"normal"}]}'
52-
)
53-
54-
datavalue
55-
datavalue <- gsub("pid", pid, datavalue)
56-
57-
claim_body <- list(
58-
action = "wbeditentity",
59-
id = qid, # in the new wikibase
60-
property = pid, # in the wikibase where you write
61-
data = datavalue,
62-
# data = '{"claims":[{"mainsnak":{"snaktype":"value","property":"P1","datavalue":{"value":"https://www.wikidata.org/wiki/Q43878","type":"string"}},"type":"statement","rank":"normal"}]}',
63-
token = csrf_token,
64-
format = "json"
65-
)
66-
67-
new_claim <- httr::POST(
68-
wikibase_api_url,
69-
body = claim_body,
70-
encode = "form",
71-
handle = response_csrf
72-
)
73-
74-
response <- content(new_claim)
75-
response
76-
77-
if ("error" %in% names(response)) {
78-
return(data.frame(
79-
statement_id = NA_character_,
80-
wikibase_qid = qid,
81-
pid = NA_character_,
82-
url = NA_character_
83-
))
84-
}
85-
if (response$success == 1) {
86-
data.frame(
87-
statement_id = l[[1]]$id,
88-
wikibase_qid = response$entity$id,
89-
pid = pid,
90-
url = l[[1]]$mainsnak$datavalue$value
91-
)
92-
}
93-
}

R/check_existing_item.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
check_existing_item <- function(action = "create_item",
1515
search_term,
1616
language,
17-
action_timestamp = NULL,
1817
equivalence_property = NA_character_,
1918
equivalence_id = NA_character_,
2019
classification_property = NA_character_,

R/check_existing_property.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#' \href{https://www.wikidata.org/w/api.php?action=help&modules=wbsearchentities}{MediaWiki
77
#' action=wbsearchentities}.
88
#' @param action Defaults to \code{"create_property"}.
9+
#' @param classification_property The instance of, or subclass of, or superclass
10+
#' of property. Defaults to \code{NA_character} when not used.
11+
#' @param classification_id The QID of the class. Defaults to
12+
#' \code{NA_character} when not used.
913
#' @param search_term A label in the given language, for example, "Estonia".
1014
#' @inheritParams create_property
1115
#' @return A data.frame or NULL.
@@ -15,7 +19,6 @@ check_existing_property <- function(
1519
action = "create_property",
1620
search_term,
1721
language,
18-
action_timestamp = NULL,
1922
equivalence_property = NA_character_,
2023
equivalence_id = NA_character_,
2124
classification_property = NA_character_,
@@ -24,6 +27,7 @@ check_existing_property <- function(
2427
data_curator = person("Unknown", "Person"),
2528
wikibase_api_url,
2629
csrf) {
30+
2731
action_timestamp <- action_timestamp_create()
2832
action_time <- Sys.time()
2933

R/copy_wikidata_item.R

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@
1414
#' replaces the more general \code{equivalence_id}, because we must use QID
1515
#' for identification in an other Wikibase instance.
1616
#' @param qid_on_wikidata The QID of the item to be copied to your Wikibase. A
17-
#' single valid QID or a vector of several QIDs.
17+
#' single valid QID or a vector of several QIDs.
1818
#' @param qid_equivalence_property The QID in Wikibase that records the
1919
#' equivalent Wikidata QID as an external ID.
20-
#' @param language A vector of language codes, for example, \code{c("en",
21-
#' "et")}.
20+
#' @param language Defaults to \code{c("en", "nl", "hu")}. A character string of
21+
#' the languages in which the users wants to receive the labels and
22+
#' descriptions of the property. The vector of languages must use \href{https://en.wikipedia.org/wiki/IETF_language_tag}{BCP
23+
#' 47}-compliant language tags (e.g., "en" for English, "nl" for Dutch and "hu"
24+
#' for Hungarian.)
2225
#' @param classification_property The instance of, or subclass of, or superclass
2326
#' of property. Defaults to \code{NA_character} when not used.
2427
#' @param classification_id The QID of the class. Defaults to
2528
#' \code{NA_character} when not used.
26-
#' @param wikibase_api_url For example,
27-
#' \code{'https://reprexbase.eu/demowiki/api.php'}.
29+
#' @param wikibase_api_url The full URL of the Wikibase API, which is the
30+
#' address that the \code{wbdataset} R client sends requests to when
31+
#' interacting with the knowledge base. For example,
32+
#' \code{'https://reprexbase.eu/demowiki/api.php'}. The URL must end with
33+
#' api.php.
2834
#' @param data_curator The name of the data curator who runs the function and
29-
#' creates the log file, created with \link[utils]{person}.
30-
#' @param log_path A path to save the log file. Defaults to the return value of
31-
#' \code{\link{tempdir()}}.
32-
#' @param log_file An explicitly stated full path to a possible CSV log file,
33-
#' defaults to \code{NULL}. If the value is \code{NULL}, no log file will be
34-
#' created.
35+
#' creates the log file, created with \link[utils]{person}.
36+
#' @param log_file_name An explicitly stated full path to a possible CSV log
37+
#' file, defaults to \code{NULL}. If the value is \code{NULL}, no log file
38+
#' will be created.
3539
#' @param csrf The CSRF token of your session, received with
3640
#' \code{\link{get_csrf}}.
3741
#' @param wikibase_session An optional list that contains any of the values of
@@ -40,24 +44,25 @@
4044
#' \code{csrf} (for repeated use in a session.)
4145
#' @importFrom assertthat assert_that
4246
#' @importFrom utils person
43-
#' @return Returns a dataset_df object. The columns are:
44-
#' \itemize{
45-
#' \item{"rowid"}{ A row identifier. }
46-
#' \item{"action"}{ The create_item() function name. }
47-
#' \item{"id_on_target"}{ The new item identifier (QID) on the targeted Wikibase.}
48-
#' \item{"label"}{ The propery label}
49-
#' \item{"description"}{ The description label}
50-
#' \item{"language"}{ The language code of the label.}
51-
#' \item{"datatype"}{ The datatype of the property, for example, `string`}
52-
#' \item{"wikibase_api_url"}{ The MediaWiki API URL where the new property is created.}
53-
#' \item{"equivalence_property"}{ The PID that connects an equivalence ID to the property.}
54-
#' \item{"equivalence_id"}{ The ID of an equivalent property defined elsewhere.}
55-
#' \item{"classification_property"}{ The PID that connects the item to a superclass, or class.}
56-
#' \item{"classification_id"}{ The QID of a class, subclass or superclass.}
57-
#' \item{"success"}{ TRUE if successfully created, FALSE if there was an error.}
58-
#' \item{"comment"}{ A summary of the error messages(s), if success is FALSE.}
59-
#' \item{"time"}{ The time when the action started.}
60-
#' \item{"logfile"}{ The name of the CSV logfile.}
47+
#' @return Returns a \code{\link[dataset]{dataset_df}} object.
48+
#' The columns are:\cr
49+
#' \describe{
50+
#' \item{\code{rowid}}{A row identifier. }
51+
#' \item{\code{action}}{\code{copy_wikidata_item}}
52+
#' \item{\code{id_on_target}}{The new item identifier (QID) on the targeted Wikibase.}
53+
#' \item{\code{label}}{The propery label}
54+
#' \item{\code{description}}{The description label}
55+
#' \item{\code{language}}{The language code of the label.}
56+
#' \item{\code{datatype}}{The datatype of the property, for example, `string`}
57+
#' \item{\code{wikibase_api_url}}{The MediaWiki API URL where the new property is created.}
58+
#' \item{\code{equivalence_property}}{The PID that connects an equivalence ID to the property.}
59+
#' \item{\code{equivalence_id}}{The ID of an equivalent property defined elsewhere.}
60+
#' \item{\code{classification_property}}{The PID that connects the item to a superclass, or class.}
61+
#' \item{\code{classification_id}}{The QID of a class, subclass or superclass.}
62+
#' \item{\code{success}}{TRUE if successfully created, FALSE if there was an error.}
63+
#' \item{\code{comment}}{A summary of the error messages(s), if success is FALSE.}
64+
#' \item{\code{time}}{The time when the action started.}
65+
#' \item{\code{logfile}}{The name of the CSV logfile.}
6166
#' }
6267
#' The number of rows corresponds to the length of the qid_on_wikidata vector.
6368
#' @export
@@ -70,7 +75,6 @@ copy_wikidata_item <- function(
7075
classification_id = NA_character_,
7176
wikibase_api_url = "https://reprexbase.eu/jekyll/api.php",
7277
data_curator = NULL,
73-
log_path = tempdir(),
7478
log_file_name = NULL,
7579
csrf,
7680
wikibase_session = NULL) {
@@ -99,10 +103,6 @@ copy_wikidata_item <- function(
99103
wikibase_api_url <- wikibase_session$wikibase_api_url
100104
}
101105

102-
if (!is.null(wikibase_session$log_path)) {
103-
log_path <- wikibase_session$log_path
104-
}
105-
106106
if (!is.null(wikibase_session$log_file_name)) {
107107
log_file_name <- wikibase_session$log_file_name
108108
}
@@ -115,7 +115,6 @@ copy_wikidata_item <- function(
115115
# Assertions for correct inputs ------------------------------------------------
116116

117117
if (is.null(data_curator)) data_curator <- person("Jane", "Doe")
118-
if (is.null(log_path)) log_path <- tempdir()
119118

120119
assertthat::assert_that(
121120
inherits(data_curator, "person"),
@@ -133,7 +132,6 @@ copy_wikidata_item <- function(
133132
language = language,
134133
wikibase_api_url = wikibase_api_url,
135134
data_curator = data_curator,
136-
log_path = log_path,
137135
csrf = csrf,
138136
wikibase_session = wikibase_session
139137
)
@@ -251,7 +249,6 @@ copy_wikidata_item <- function(
251249
action = "copy_item",
252250
search_term = default_label,
253251
language = "en",
254-
action_timestamp = action_timestamp,
255252
equivalence_property = qid_equivalence_property,
256253
equivalence_id = qid_on_wikidata,
257254
classification_property = classification_property,
@@ -572,7 +569,6 @@ copy_wikidata_items <- function(qid_on_wikidata,
572569
language,
573570
wikibase_api_url,
574571
data_curator,
575-
log_path,
576572
csrf,
577573
wikibase_session) {
578574
# Ensure that QIDs are used in the loop ----------------------------
@@ -605,10 +601,6 @@ copy_wikidata_items <- function(qid_on_wikidata,
605601
wikibase_api_url <- wikibase_session$wikibase_api_url
606602
}
607603

608-
if (!is.null(wikibase_session$log_path)) {
609-
log_path <- wikibase_session$log_path
610-
}
611-
612604
if (!is.null(wikibase_session$csrf)) {
613605
csrf <- wikibase_session$csrf
614606
}
@@ -624,7 +616,6 @@ copy_wikidata_items <- function(qid_on_wikidata,
624616
language = language,
625617
wikibase_api_url = wikibase_api_url,
626618
data_curator = data_curator,
627-
log_path = log_path,
628619
csrf = csrf
629620
)
630621
}

0 commit comments

Comments
 (0)