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

S4 → R6 objects

parent c3087f04
No related branches found
No related tags found
No related merge requests found
Package: mediawikir Package: mediawikir
Type: Package Type: Package
Title: Connects to a mediawiki instance and writes in it what you want Title: Connects to a mediawiki instance and writes in it what you want
Version: 0.1.0 Version: 0.1.1
Author: Mathieu Loiseau Author: Mathieu Loiseau
Maintainer: Mathieu Loiseau <loiseaum@univ-grenoble-alpes.fr> Maintainer: Mathieu Loiseau <loiseaum@univ-grenoble-alpes.fr>
Description: It can be used to post the output of an R script directly into a mediawiki instance Description: It can be used to post the output of an R script directly into a mediawiki instance
...@@ -9,4 +9,4 @@ License: MIT ...@@ -9,4 +9,4 @@ License: MIT
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
RoxygenNote: 6.0.1 RoxygenNote: 6.0.1
Imports: RCurl Imports: httr,R6
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(Mediawikir)
importFrom(R6,R6Class)
importFrom(httr,POST)
importFrom(httr,content)
importFrom(httr,stop_for_status)
#'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 library(R6)
#'
#'@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)
setClass("Mediawikir", representation(url = "character", user_name = "character", auth_token = "character"))
setGeneric("connect", function(.Object, pass){})
#'Connects to mediawiki instance and gets token (to be called by constructor)
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)
})
#'Creates a new Mediawikir object instance #'Mediawikir: write R output to mediawiki
#' #'
#'@param instance_url the base url for the mediawiki instance in which one wants to store R output #'@docType class
#'@param user the user to which all changes to the wiki will be credited #'@importFrom R6 R6Class
#'@param pass the user's password #'@importFrom httr POST
#'@importFrom httr stop_for_status
#'@importFrom httr content
#'@export
#'@keywords mediawiki
#'@description 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
#'@format An \code{\link{R6Class}} generator object
#'@return Object of \code{\link{R6Class}} with methods to interact with mediawiki instance
#'@field url the mediawiki instance base url
#'@field auth_token the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)
#'@field page_name the page which will be modified (including its namespace)
#'@field content the future content of \code{page_name}, which will be incrementally constructed using object methods
#'@examples #'@examples
#' new("Mediawikir", "http://wiki.example.com", "admin", "theadmin'spassword") #' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage")
setMethod("initialize", "Mediawikir", function (.Object, instance_url, user, pass){ #'@section Methods:
tryCatch({ #'\describe{
.Object@url <- paste(instance_url,"/","api.php",sep="") #' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of:\describe{
.Object@user_name <- user #' \item{\code{instance_url}}{The base url to the mediawiki instance}
.Object@auth_token=connect(.Object, pass) #' \item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki}
}, #' \item{\code{page}}{The wiki page which contains the changes}
error = function(e){ #' \item{\code{pass}}{The user's password}
print(e$message) #' }}
.Object <- FALSE #' \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)}
} #'}
) Mediawikir <- R6Class("Mediawikir",
return(.Object) public = list(
}) url = NULL,
user_name = NULL,
auth_token = NULL,
page_name = NULL,
content = "",
connect = function(pass){
"une super méthode"
tryCatch(
{ #get login token
query <- httr::POST(self$url,
body = list(action="query",
meta="tokens",
type="login",
lgname = self$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(self$url,
body = list(action="clientlogin",
username = self$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", self$url, e$message))
token <- 0
}
)
return(token)
},
######
# initialize
######
initialize = function (instance_url, user, page, pass){
tryCatch({
self$url <- paste(instance_url,"/","api.php",sep="")
self$user_name <- user
self$page_name <- page
self$auth_token <- self$connect(pass)
},
error = function(e){
print(e$message)
self <- FALSE
})
}
)
)
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class.R
\docType{class}
\name{Mediawikir-class}
\alias{Mediawikir-class}
\title{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}
\description{
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
}
\section{Slots}{
\describe{
\item{\code{url}}{the mediawiki instance base url}
\item{\code{auth_token}}{the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)}
}}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class.R
\docType{class}
\name{Mediawikir}
\alias{Mediawikir}
\title{Mediawikir: write R output to mediawiki}
\format{An \code{\link{R6Class}} generator object}
\usage{
Mediawikir
}
\value{
Object of \code{\link{R6Class}} with methods to interact with mediawiki instance
}
\description{
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
}
\section{Fields}{
\describe{
\item{\code{url}}{the mediawiki instance base url}
\item{\code{auth_token}}{the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)}
\item{\code{page_name}}{the page which will be modified (including its namespace)}
\item{\code{content}}{the future content of \code{page_name}, which will be incrementally constructed using object methods}
}}
\section{Methods}{
\describe{
\item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of:\describe{
\item{\code{instance_url}}{The base url to the mediawiki instance}
\item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki}
\item{\code{page}}{The wiki page which contains the changes}
\item{\code{pass}}{The user's password}
}}
\item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)}
}
}
\examples{
mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage")
}
\keyword{mediawiki}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class.R
\docType{methods}
\name{connect,Mediawikir-method}
\alias{connect,Mediawikir-method}
\title{Connects to mediawiki instance and gets token (to be called by constructor)}
\usage{
\S4method{connect}{Mediawikir}(.Object, pass)
}
\description{
Connects to mediawiki instance and gets token (to be called by constructor)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class.R
\docType{methods}
\name{initialize,Mediawikir-method}
\alias{initialize,Mediawikir-method}
\title{Creates a new Mediawikir object instance}
\usage{
\S4method{initialize}{Mediawikir}(.Object, instance_url, user, pass)
}
\arguments{
\item{instance_url}{the base url for the mediawiki instance in which one wants to store R output}
\item{user}{the user to which all changes to the wiki will be credited}
\item{pass}{the user's password}
}
\description{
Creates a new Mediawikir object instance
}
\examples{
new("Mediawikir", "http://wiki.example.com", "admin", "theadmin'spassword")
}
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