Skip to content

Commit 37d7848

Browse files
authored
Merge pull request #21 from nevrome/master
functions for user login and page creation
2 parents 36a6d75 + e61b09a commit 37d7848

25 files changed

+385
-18
lines changed

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Type: Package
33
Title: A MediaWiki API Wrapper
44
Version: 1.5.0
55
Date: 2017-02-04
6-
Author: Oliver Keyes [aut, cre], Brock Tilbert [ctb]
6+
Author: Oliver Keyes [aut, cre], Brock Tilbert [ctb], Clemens Schmid [aut]
77
Maintainer: Oliver Keyes <ironholds@gmail.com>
88
Description: A wrapper for the MediaWiki API, aimed particularly at the
99
Wikimedia 'production' wikis, such as Wikipedia. It can be used to retrieve
@@ -12,7 +12,8 @@ Description: A wrapper for the MediaWiki API, aimed particularly at the
1212
License: MIT + file LICENSE
1313
Imports:
1414
httr,
15-
jsonlite
15+
jsonlite,
16+
magrittr
1617
Suggests:
1718
testthat,
1819
knitr,
@@ -21,4 +22,4 @@ Suggests:
2122
BugReports: https://github.com/Ironholds/WikipediR/issues
2223
URL: https://github.com/Ironholds/WikipediR/
2324
VignetteBuilder: knitr
24-
RoxygenNote: 5.0.1
25+
RoxygenNote: 6.0.1

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(categories_in_page)
4+
export(create_pages)
5+
export(login)
46
export(page_backlinks)
57
export(page_content)
68
export(page_external_links)
@@ -19,4 +21,5 @@ importFrom(httr,content)
1921
importFrom(httr,stop_for_status)
2022
importFrom(httr,user_agent)
2123
importFrom(jsonlite,fromJSON)
24+
importFrom(magrittr,"%>%")
2225
importFrom(utils,URLencode)

R/create_content.R

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#' request token for api action as signed in user
2+
#'
3+
#' helper function to request a user action token
4+
#'
5+
#' @param url a URL body
6+
#'
7+
#' @return a token string
8+
#'
9+
#' @importFrom magrittr "%>%"
10+
#'
11+
get_action_token <- function(url) {
12+
13+
# get login token
14+
response <- httr::modify_url(
15+
url,
16+
query = list(
17+
action = "query",
18+
meta = "tokens",
19+
format = "json"
20+
)
21+
) %>% httr::GET(
22+
) %>% httr::stop_for_status(
23+
)
24+
25+
# parse the response, check for API errors
26+
parsed_response <- response_parse(
27+
response = response, out_class = "actiontoken"
28+
)
29+
if(!is.null(parsed_response$error)){
30+
stop(
31+
"The API returned an error: ",
32+
parsed_response$error$code,
33+
" - ", parsed_response$error$info
34+
)
35+
}
36+
37+
parse_response(parsed_response) %>%
38+
return()
39+
}
40+
41+
#' wikimedia api page creation
42+
#'
43+
#' Create pages or category-pages on a wikimedia instance.
44+
#'
45+
#' @param url a URL body
46+
#' @param p_title vector with page title strings of new pages
47+
#' @param p_text vector with page content strings of new pages
48+
#' @param category switch to decide, if the pages should be
49+
#' created as category-pages
50+
#'
51+
#' @return TRUE
52+
#'
53+
#' @export
54+
create_pages <- function(url, p_title, p_text, category = FALSE){
55+
56+
if(length(p_title) != length(p_text)) {
57+
stop(
58+
"The length of the vectors p_title and p_text
59+
is not equal."
60+
)
61+
}
62+
63+
# get action token
64+
token <- NA
65+
token <- get_action_token(url)
66+
if(token %>% is.na){
67+
stop("Problems with the action token request.")
68+
}
69+
70+
# create page(s) (with progress bar)
71+
pb <- utils::txtProgressBar(
72+
min = 0, max = length(p_title), style = 3
73+
)
74+
75+
res <- c()
76+
for (i in 1:length(p_title)) {
77+
res[i] <- create_page(
78+
url, p_title[i], p_text[i], category, token
79+
)
80+
utils::setTxtProgressBar(pb, i)
81+
}
82+
83+
close(pb)
84+
85+
all(res) %>%
86+
return()
87+
}
88+
89+
#' wikimedia api page creation (single pages)
90+
#'
91+
#' helper function to do the actual api requests for page
92+
#' and category-page creation
93+
#'
94+
#' @param url a URL body
95+
#' @param p_title page title string of new page
96+
#' @param p_text page content string of new page
97+
#' @param category switch to decide, if the page should be
98+
#' created as category-page
99+
#' @param token action token to perform the request
100+
#'
101+
#' @return TRUE
102+
#'
103+
create_page <- function(url, p_title, p_text, category, token){
104+
105+
# create page
106+
response <- httr::modify_url(
107+
url,
108+
query = list(
109+
action = "edit",
110+
title = ifelse(
111+
category,
112+
paste0("Category:", p_title),
113+
p_title
114+
),
115+
text = p_text,
116+
format = "json"
117+
)
118+
) %>% httr::POST(
119+
body = list(token = token)
120+
) %>% httr::stop_for_status(
121+
)
122+
123+
# parse the response, check for API errors
124+
parsed_response <- response_parse(
125+
response = response, out_class = "createpage"
126+
)
127+
if(!is.null(parsed_response$error)){
128+
stop(
129+
"The API returned an error: ",
130+
parsed_response$error$code,
131+
" - ", parsed_response$error$info
132+
)
133+
}
134+
135+
parse_response(parsed_response) %>%
136+
return()
137+
}

R/login.R

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#' request token to start client login
2+
#'
3+
#' helper function to request a user login token
4+
#'
5+
#' @param url a URL body
6+
#' @param user a username of a registered user
7+
#'
8+
#' @return a token string
9+
#'
10+
#' @importFrom magrittr "%>%"
11+
#'
12+
get_prelogin_token <- function(url, user) {
13+
14+
# get login token
15+
response <- httr::modify_url(
16+
url,
17+
query = list(
18+
action = "login",
19+
lgname = user,
20+
format = "json"
21+
)
22+
) %>% httr::POST(
23+
) %>% httr::stop_for_status(
24+
)
25+
26+
# parse the response, check for API errors
27+
parsed_response <- response_parse(
28+
response = response, out_class = "prelogintoken"
29+
)
30+
if(!is.null(parsed_response$error)){
31+
stop(
32+
"The API returned an error: ",
33+
parsed_response$error$code,
34+
" - ", parsed_response$error$info
35+
)
36+
}
37+
38+
parse_response(parsed_response) %>%
39+
return()
40+
}
41+
42+
#' wikimedia api user login
43+
#'
44+
#' Login to a wikimedia instance to trigger api requests
45+
#' as a registered user. This function only allows the
46+
#' very basic login with username and password. Wikimedia
47+
#' setups that require more sophisticated login methods
48+
#' are not supported.
49+
#'
50+
#' @param url a URL body
51+
#' @param user a username of a registered user
52+
#' @param pw the password of said user
53+
#'
54+
#' @return TRUE
55+
#'
56+
#' @export
57+
login <- function(url, user, pw){
58+
59+
# get prelogin token
60+
token <- get_prelogin_token(url, user)
61+
62+
# login
63+
response <- httr::modify_url(
64+
url,
65+
query = list(
66+
action = "clientlogin",
67+
username = user,
68+
rememberMe = 1,
69+
loginreturnurl = url,
70+
format = "json"
71+
)
72+
) %>%
73+
httr::POST(
74+
body = list(
75+
logintoken = token,
76+
password = pw
77+
)
78+
) %>% httr::stop_for_status(
79+
)
80+
81+
# parse the response, check for API errors
82+
parsed_response <- response_parse(
83+
response = response, out_class = "login"
84+
)
85+
if(!is.null(parsed_response$error)){
86+
stop(
87+
"The API returned an error: ",
88+
parsed_response$error$code,
89+
" - ", parsed_response$error$info
90+
)
91+
}
92+
93+
parse_response(parsed_response) %>%
94+
return()
95+
}

R/parse.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,24 @@ parse_response.pageinfo <- function(x){
8484
return(x)
8585
})
8686
return(results)
87+
}
88+
89+
parse_response.prelogintoken <- function(x){
90+
x <- x$login$token
91+
return(x)
92+
}
93+
94+
parse_response.login <- function(x){
95+
x <- x$clientlogin$status == "PASS"
96+
return(x)
97+
}
98+
99+
parse_response.actiontoken <- function(x){
100+
x <- x$query$tokens$csrftoken
101+
return(x)
102+
}
103+
104+
parse_response.createpage <- function(x){
105+
x <- x$edit$result == "Success"
106+
return(x)
87107
}

man/WikipediR.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/categories_in_page.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_page.Rd

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/create_pages.Rd

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_action_token.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)