From cf8d95963a527e44e7fdd6d27d11f8ef93c0493f Mon Sep 17 00:00:00 2001
From: Mathieu Loiseau <mathieu.loiseau@univ-grenoble-alpes.fr>
Date: Fri, 7 Dec 2018 19:09:57 +0100
Subject: [PATCH] now needs to upload content

---
 DESCRIPTION       |  2 +-
 NAMESPACE         |  2 ++
 R/class.R         | 48 +++++++++++++++++++++++++++++++++++++++++++++--
 man/Mediawikir.Rd |  4 ++++
 4 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/DESCRIPTION b/DESCRIPTION
index 88d83cf..8219e5e 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 1cd9b35..e14d0e2 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 8096931..92b92b8 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 ebfcbb0..24f3296 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}
-- 
GitLab