From 358ad4942861eaa516d0618916f33b3e10c671fe Mon Sep 17 00:00:00 2001 From: Mathieu Loiseau <loizbek@lezinter.net> Date: Tue, 13 Mar 2018 16:37:23 +0100 Subject: [PATCH] =?UTF-8?q?S4=20=E2=86=92=20R6=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DESCRIPTION | 4 +- NAMESPACE | 5 + R/class.R | 157 ++++++++++++++++------------ man/Mediawikir-class.Rd | 17 --- man/Mediawikir.Rd | 45 ++++++++ man/connect-Mediawikir-method.Rd | 12 --- man/initialize-Mediawikir-method.Rd | 22 ---- 7 files changed, 144 insertions(+), 118 deletions(-) delete mode 100644 man/Mediawikir-class.Rd create mode 100644 man/Mediawikir.Rd delete mode 100644 man/connect-Mediawikir-method.Rd delete mode 100644 man/initialize-Mediawikir-method.Rd diff --git a/DESCRIPTION b/DESCRIPTION index c5bb2bf..b5091b0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: mediawikir Type: Package Title: Connects to a mediawiki instance and writes in it what you want -Version: 0.1.0 +Version: 0.1.1 Author: Mathieu Loiseau 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 @@ -9,4 +9,4 @@ License: MIT Encoding: UTF-8 LazyData: true RoxygenNote: 6.0.1 -Imports: RCurl +Imports: httr,R6 diff --git a/NAMESPACE b/NAMESPACE index 6ae9268..da6a873 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,2 +1,7 @@ # Generated by roxygen2: do not edit by hand +export(Mediawikir) +importFrom(R6,R6Class) +importFrom(httr,POST) +importFrom(httr,content) +importFrom(httr,stop_for_status) diff --git a/R/class.R b/R/class.R index bb76052..2c4094c 100644 --- a/R/class.R +++ b/R/class.R @@ -1,68 +1,95 @@ -#'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) -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) - }) +library(R6) -#'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 -#'@param user the user to which all changes to the wiki will be credited -#'@param pass the user's password +#'@docType class +#'@importFrom R6 R6Class +#'@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 -#' new("Mediawikir", "http://wiki.example.com", "admin", "theadmin'spassword") -setMethod("initialize", "Mediawikir", function (.Object, instance_url, user, pass){ - tryCatch({ - .Object@url <- paste(instance_url,"/","api.php",sep="") - .Object@user_name <- user - .Object@auth_token=connect(.Object, pass) - }, - error = function(e){ - print(e$message) - .Object <- FALSE - } - ) - return(.Object) -}) +#' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage") +#'@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)} +#'} +Mediawikir <- R6Class("Mediawikir", + 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 + }) + } + ) +) diff --git a/man/Mediawikir-class.Rd b/man/Mediawikir-class.Rd deleted file mode 100644 index 3d655ae..0000000 --- a/man/Mediawikir-class.Rd +++ /dev/null @@ -1,17 +0,0 @@ -% 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)} -}} - diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd new file mode 100644 index 0000000..d2c6c3a --- /dev/null +++ b/man/Mediawikir.Rd @@ -0,0 +1,45 @@ +% 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} diff --git a/man/connect-Mediawikir-method.Rd b/man/connect-Mediawikir-method.Rd deleted file mode 100644 index 9f6ae36..0000000 --- a/man/connect-Mediawikir-method.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% 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) -} diff --git a/man/initialize-Mediawikir-method.Rd b/man/initialize-Mediawikir-method.Rd deleted file mode 100644 index 9637bf8..0000000 --- a/man/initialize-Mediawikir-method.Rd +++ /dev/null @@ -1,22 +0,0 @@ -% 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") -} -- GitLab