diff --git a/DESCRIPTION b/DESCRIPTION index b5091b002310b0db5122a669822c5566500f487e..88d83cfd80b412e7845d224b1f45f1289766702e 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.1 +Version: 0.1.2 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 diff --git a/NAMESPACE b/NAMESPACE index da6a87318a82859761cc46eeba6f957a1abbd619..1cd9b358c425e5484ba4ec2b78317062e35dcda1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(Batchator) export(Mediawikir) importFrom(R6,R6Class) importFrom(httr,POST) diff --git a/R/class.R b/R/class.R index f5b294a4850e84d984ebaa5df7b213196b14d9ba..987b1b3e2266e243fcdd221cf8b91ffaf1a6d19f 100644 --- a/R/class.R +++ b/R/class.R @@ -1,4 +1,5 @@ library(R6) +library(httr) #'Mediawikir: write R output to mediawiki #' @@ -18,21 +19,26 @@ library(R6) #'@field content the future content of \code{page_name}, which will be incrementally constructed using object methods #'@field user_name the user to credit for the changes #'@examples -#' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage") +#' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword") +#' mwHandler$setPage("wikiPage") #' mwHandler$addContent("This is an introduction to Mediawikir") #' mwHandler$addContent("First step","h2") #' mwHandler$addContent("First step of the first step","h3") -#' mwHandler$uploadFile("path/toFile.png","My beautiful image.png") +#' fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png") +#' mwHandler$addContent(paste("[[",fileName,"]]",sep="")) #' mwHandler$postContent() #'@section Methods: #'\describe{ -#' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of: +#' \item{\code{initialize(instance_url, user, 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{setPage(pageName)}}{To set the page for the next post +#' \describe{ +#' \item{\code{pageName}}{The name of the wiki page which contains the changes} +#' }} #' \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)} #' \item{\code{addContent(theContent, type="p")}}{To add content that will later on be posted to mediawiki. This is where structuring elements are provided #' \describe{ @@ -58,7 +64,7 @@ Mediawikir <- R6Class("Mediawikir", url = NULL, user_name = NULL, auth_token = NULL, - page_name = NULL, + page_name = "", content = "", connect = function(pass){ token <- 1 @@ -102,11 +108,10 @@ Mediawikir <- R6Class("Mediawikir", ###### # initialize ###### - initialize = function (instance_url, user, page, pass){ + initialize = function (instance_url, user, 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){ @@ -115,6 +120,13 @@ Mediawikir <- R6Class("Mediawikir", }) }, + ###### + # setPage + ###### + setPage = function(pageName){ + self$page_name <- pageName + }, + ###### # addContent ###### @@ -135,29 +147,35 @@ Mediawikir <- R6Class("Mediawikir", postContent = function(){ success <- TRUE tryCatch( - { #get edit token - query <- httr::POST(self$url, - body = list(action = "query", - prop = "info|revisions", - meta = "tokens", - rvprop = "timestamp", - titles = self$page_name, - format = "json")) - httr::stop_for_status(query) - response <- httr::content(query, "parsed", "application/json") - #perform edit - query <- httr::POST(self$url, - body = list(action = "edit", - title = self$page_name, - #basetimestamp = "TODO", - summary = "R generated content", - format = "json", - text = self$content, - token = response$query$tokens$csrftoken)) - httr::stop_for_status(query) - response <- httr::content(query, "parsed", "application/json") - if(response$edit$result != "Success"){ - stop(paste("Failed to edit ",self$page_name,"\n",response)) + { #Verify that a page has been selected + if(self$page_name == ""){ + warning("No page to write to, use “setPage†method before calling “postContentâ€.") + } + else{ + #get edit token + query <- httr::POST(self$url, + body = list(action = "query", + prop = "info|revisions", + meta = "tokens", + rvprop = "timestamp", + titles = self$page_name, + format = "json")) + httr::stop_for_status(query) + response <- httr::content(query, "parsed", "application/json") + #perform edit + query <- httr::POST(self$url, + body = list(action = "edit", + title = self$page_name, + #basetimestamp = "TODO", + summary = "R generated content", + format = "json", + text = self$content, + token = response$query$tokens$csrftoken)) + httr::stop_for_status(query) + response <- httr::content(query, "parsed", "application/json") + if(response$edit$result != "Success"){ + stop(paste("Failed to edit ",self$page_name,"\n",response)) + } } }, error = function(e){ @@ -212,3 +230,31 @@ Mediawikir <- R6Class("Mediawikir", } ) ) + + +#'Batchator: Batch input to mediawiki based on a csv and a template +#' +#'@docType class +#'@export +#'@keywords mediawiki +#'@description Uses mediawikir to create a batch of pages +Batchator <- R6Class("Batchator", + public = list( + author = NULL, + wiki = NULL, + prefix ="", + ###### + # initialize + ###### + initialize = function (wiki,author,pass,prefix){ + tryCatch({ + self$wiki <- Mediawikir$new(wiki, author, pass) + self$prefix <- prefix + }, + error = function(e){ + print(e$message) + self <- FALSE + }) + } + ) +) diff --git a/man/Batchator.Rd b/man/Batchator.Rd new file mode 100644 index 0000000000000000000000000000000000000000..93afe1b8463a76b2d0c9c858f3f80ea3df3b5b42 --- /dev/null +++ b/man/Batchator.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/class.R +\docType{class} +\name{Batchator} +\alias{Batchator} +\title{Batchator: Batch input to mediawiki based on a csv and a template} +\format{An object of class \code{R6ClassGenerator} of length 24.} +\usage{ +Batchator +} +\description{ +Uses mediawikir to create a batch of pages +} +\keyword{mediawiki} diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd index 2306265a67652650a9400f363de21c4579bfd745..ebfcbb0dabb48c495acd161b8353bde9a45b74dc 100644 --- a/man/Mediawikir.Rd +++ b/man/Mediawikir.Rd @@ -31,13 +31,16 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use \section{Methods}{ \describe{ - \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of: + \item{\code{initialize(instance_url, user, 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{setPage(pageName)}}{To set the page for the next post + \describe{ + \item{\code{pageName}}{The name of the wiki page which contains the changes} + }} \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)} \item{\code{addContent(theContent, type="p")}}{To add content that will later on be posted to mediawiki. This is where structuring elements are provided \describe{ @@ -61,11 +64,13 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use } \examples{ - mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage") + mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword") + mwHandler$setPage("wikiPage") mwHandler$addContent("This is an introduction to Mediawikir") mwHandler$addContent("First step","h2") mwHandler$addContent("First step of the first step","h3") - mwHandler$uploadFile("path/toFile.png","My beautiful image.png") + fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png") + mwHandler$addContent(paste("[[",fileName,"]]",sep="")) mwHandler$postContent() } \keyword{mediawiki}