diff --git a/DESCRIPTION b/DESCRIPTION index 88d83cfd80b412e7845d224b1f45f1289766702e..8219e5e8abd447985d76bc327e14f015ed2e451c 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.2 +Version: 0.1.3 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 1cd9b358c425e5484ba4ec2b78317062e35dcda1..e14d0e21b03b55659dbff25cb17a411eb2416cfb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,5 @@ importFrom(R6,R6Class) importFrom(httr,POST) importFrom(httr,content) importFrom(httr,stop_for_status) +importFrom(httr,upload_file) +importFrom(stringr,str_replace_all) diff --git a/R/class.R b/R/class.R index 80969316806c2354a7e72f838f7e0bee5634d685..92b92b85729a708cc02b696e247e94bb8b283cf9 100644 --- a/R/class.R +++ b/R/class.R @@ -41,6 +41,10 @@ library(httr) #' \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{resetContent(baseContent="")}}{To reset the content of the page for the next post +#' \describe{ +#' \item{\code{baseContent}}{The wikitext base for the next post} +#' }} #' \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{ #' \item{\code{theContent}}{Either text (using mediawiki syntax) or path to a file} @@ -128,6 +132,13 @@ Mediawikir <- R6Class("Mediawikir", self$page_name <- pageName }, + ###### + # resetContent + ###### + resetContent = function(aContent=""){ + self$content <- aContent + }, + ###### # addContent ###### @@ -233,7 +244,6 @@ Mediawikir <- R6Class("Mediawikir", ) library(stringr) - #'Batchator: Batch input to mediawiki based on a csv and a template #' #'@docType class @@ -246,7 +256,9 @@ Batchator <- R6Class("Batchator", author = NULL, wiki = NULL, page_prefix ="", + page_names=NULL, template = NULL, + field_list = NULL, content = NULL, ###### @@ -280,11 +292,43 @@ Batchator <- R6Class("Batchator", ###### loadContent = function(file_path){ tryCatch({ - self$content <- read.csv(file=file_path, header = FALSE) + csvM <- read.csv(file=file_path, header = FALSE) + self$field_list <- as.vector(t(csvM[1,])) + self$page_names <- csvM[-1,1] + self$content <- csvM[-1,] }, error = function(e){ print(paste(e$message, "No content loaded")) }) + }, + + ###### + # applyTemplate + ###### + applyTemplate = function(){ + tryCatch({ + if (is.null(self$content) | + is.null(self$field_list) | + is.null(self$page_names) | + is.null(self$template)) { + warning("Either no content or no template, use loadContent and/or loadTemplate.") + } + else{ + for(i in 1:nrow(self$content)){ + self$wiki$resetContent(str_replace_all(self$template, + setNames(as.vector(t(self$content[i,])), + self$field_list))) + print(paste("Content of ",self$page_prefix,self$page_names[i],sep = "")) + print(str_replace_all(self$template, + setNames(as.vector(t(self$content[i,])), + self$field_list))) + } + } + }, + error = function(e){ + print(e$message) + }) + } ) ) diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd index ebfcbb0dabb48c495acd161b8353bde9a45b74dc..24f3296c3b58df2313b5cf665a4ac8486dd8646a 100644 --- a/man/Mediawikir.Rd +++ b/man/Mediawikir.Rd @@ -42,6 +42,10 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use \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{resetContent(baseContent="")}}{To reset the content of the page for the next post + \describe{ + \item{\code{baseContent}}{The wikitext base for the next post} + }} \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{ \item{\code{theContent}}{Either text (using mediawiki syntax) or path to a file}