11# ' @title Get property definition
22# ' @description Receive the label and description of a property on the basis of
3- # ' its PID from a Wikibase instance. It will not add further statements about
4- # ' the property.
3+ # ' its PID from a Wikibase instance. It will not add further statements about
4+ # ' the property.
55# ' @details Currently the language has a choose a default, \code{"en"}, for
66# ' cases where the user-chosen language return empty labels and descriptions.
77# ' This feature may be elaborated or changed later. The function receives
1010# ' as aliases themselves can break the tidiness of the returned data.
1111# ' @param pid The PID of the property in the Wikibase instance (or Wikidata
1212# ' itself).
13- # ' @param wikibase_api_url Defaults to
14- # ' \code{"https://www.wikidata.org/w/api.php"}, may be replaced with a similar
15- # ' API address of a Wikibase instance. Private instances may require an
16- # ' authenticated session.
17- # ' @param return_type Defaults to \code{"data.frame"} that is suitable for receiving
18- # ' the information in stand-alone use. The \code{"JSON"} passes on a JSON
19- # ' string in the format that you may need it in further Wikibase API calls.
13+ # ' @param wikibase_api_url The full URL of the Wikibase API, which is the
14+ # ' address that the \code{wbdataset} R client sends requests to when
15+ # ' interacting with the knowledge base. In this case it defaults to
16+ # ' \code{'https://www.wikidata.org/w/api.php'}, Wikidata itself, where no CSRF
17+ # ' is needed.
18+ # ' @param csrf The CSRF token of your session, received with
19+ # ' \code{\link{get_csrf}}, not needed if
20+ # ' \code{wikibase_api_url="https://www.wikidata.org/w/api.php"}. Defaults to
21+ # ' \code{NULL}.
22+ # ' @param return_type Defaults to \code{"data.frame"} that is suitable for
23+ # ' receiving the information in stand-alone use. The \code{"JSON"} passes on a
24+ # ' JSON string in the format that you may need it in further Wikibase API
25+ # ' calls.
26+ # ' @param language Defaults to \code{c("en", "nl", "hu")}. A character string of
27+ # ' the languages in which the users wants to receive the labels and
28+ # ' descriptions of the property. The vector of languages must use \href{https://en.wikipedia.org/wiki/IETF_language_tag}{BCP
29+ # ' 47}-compliant language tags (e.g., "en" for English, and "hu"
30+ # ' for Hungarian.)
2031# ' @return A data.frame of the \code{PID} with the labels and descriptions of
2132# ' the property in the selected languages. Alternatively, when
2233# ' \code{return_type="JSON"}, the same data prepared for use in a subsequent
3041# ' get_property_definition(pid = "P2047", return_type = "data.frame")
3142# '
3243# ' # Receive JSON for copying with wbeditidentiy
33- # ' get_property_definition(pid = "P2047", languages = c("en", "hu"))
44+ # ' get_property_definition(pid = "P2047", language = c("en", "hu"))
3445# ' @export
3546
3647get_property_definition <- function (
3748 pid ,
3849 language = c(" en" , " nl" , " hu" ),
3950 wikibase_api_url = " https://www.wikidata.org/w/api.php" ,
40- return_type = " JSON" ) {
51+ return_type = " JSON" ,
52+ csrf = NULL ) {
53+
4154 # # Ensure that the pid is a character string starting with P followed by
4255 # # numbers.
4356 pid <- as.character(pid )
@@ -74,9 +87,10 @@ get_property_definition <- function(
7487 safely_post <- purrr :: safely(httr :: POST , NULL )
7588
7689 recevied_claim <- safely_post(
77- " https://www.wikidata.org/w/api.php " ,
90+ wikibase_api_url ,
7891 body = claim_body ,
79- encode = " form"
92+ encode = " form" ,
93+ handle = csrf
8094 )
8195
8296 if (! is.null(recevied_claim $ error )) {
@@ -97,12 +111,30 @@ get_property_definition <- function(
97111 )
98112 }
99113
100- if (! is_response_success(response )) { # internal assertion for susccessful response
114+
115+ pid_is_missing <- ifelse(! is.null(response [[1 ]][[1 ]]$ missing ), TRUE , FALSE )
116+
117+
118+ if (! is_response_success(response )) {
119+ # internal assertion for successful response
101120 # Exception: retrieval of the property was not successful, even though we
102121 # did not get an explicit error before.
103122 message(" Could not access " , pid )
104123 message(response $ error $ messages [[1 ]]) # print the error message for debugging
124+ if (return_type == " data.frame" ) {
125+ return (error_data_frame )
126+ }
127+ if (return_type != " data.frame" ) { # in any other case send JSON
128+ return (error_json )
129+ }
130+ }
131+
132+ if (pid_is_missing ) {
133+ # No such property
134+ message(" Property does not exist " , pid )
135+ message(response $ error $ messages [[1 ]]) # print the error message for debugging
105136 if (return_type == " data.frame" ) { # if the user needs a data.frame
137+ error_data_frame $ success <- TRUE
106138 return (error_data_frame )
107139 }
108140 if (return_type != " data.frame" ) { # in any other case send JSON
@@ -178,6 +210,7 @@ get_property_definition <- function(
178210 data.frame (
179211 language = names(descriptions_vector ),
180212 description = as.character(descriptions_vector ),
213+ datatype = rep(response $ entities [[1 ]]$ datatype , length(descriptions_vector )),
181214 success = TRUE
182215 ),
183216 by = " language"
@@ -193,3 +226,4 @@ get_property_definition <- function(
193226 )
194227 }
195228}
229+
0 commit comments