Skip to content
Snippets Groups Projects
Commit 50c3b0c3 authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

S4 archive

parent c3087f04
Branches archive_S4
No related tags found
No related merge requests found
#'A Mediawikir controller serves to connect to a mediawiki API as a registered user in order to edit automatically certain documents based on R scripts
#'
#'@slot url the mediawiki instance base url
#'@slot auth_token the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)
#'@slot page_name the page which will be modified (including its namespace)
setClass("Mediawikir",
representation(
url = "character",
user_name = "character",
auth_token = "character",
page_name = "character",
content = "character"))
#'Creates a new Mediawikir object instance
#'
#'@param instance_url the base url for the mediawiki instance in which one wants to store R output
#'@param user the user to which all changes to the wiki will be credited
#'@param pass the user's password
#'@param page the wiki page that will be modified
#'@return a Mediawikir object (or FALSE on error)
#'@examples
#' new("Mediawikir", "http://wiki.example.com", "admin", "theadmin'spassword")
setMethod("initialize", "Mediawikir", function (.Object, instance_url, user, pass, page){
tryCatch({
.Object@url <- paste(instance_url,"/","api.php",sep="")
.Object@user_name <- user
.Object@page_name <- page
.Object@auth_token=connect(.Object, pass)
},
error = function(e){
print(e$message)
.Object <- FALSE
}
)
return(.Object)
})
#'Adds content (that will be sent to the mediawiki instance by the "uploadContent" method)
#'
#'@param text the content to be added
#'@param content_type the type of content, Default: "p" (paragraph)
#'@examples
#TODO
setGeneric("connect", function(.Object, pass){})
#'Connects to mediawiki instance and gets token (to be called by constructor)
#'
#'@return the token of the connection (0 on error)
setMethod("connect", "Mediawikir", function(.Object, pass){
tryCatch(
{ #get login token
query <- httr::POST(.Object@url,
body = list(action="query",
meta="tokens",
type="login",
lgname = .Object@user_name,
format= "json"))
httr::stop_for_status(query)
#parse query response to retrieve token
response <- httr::content(query, "parsed", "application/json")
token <- response$query$tokens$logintoken
#login query
query <- httr::POST(.Object@url,
body = list(action="clientlogin",
username = .Object@user_name,
password = pass,
logintoken = token,
loginreturnurl="http://wiki.lezinter.net",
format= "json"))
httr::stop_for_status(query)
#parse query response to make sure login worked
response <- httr::content(query, "parsed", "application/json")
if(response$clientlogin$status!="PASS"){
stop(paste(response$clientlogin,"\n"))
}
else{
return(token)
}
},
error = function(e)
{
print(paste("Could not login to", .Object@url, e$message))
token <- 0
}
)
return(token)
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment