From 34ea860c800abbda189567f6355c71976f3ab37c Mon Sep 17 00:00:00 2001
From: Mathieu Loiseau <loizbek@lezinter.net>
Date: Wed, 14 Mar 2018 20:27:38 +0100
Subject: [PATCH] file upload

---
 R/class.R         | 56 +++++++++++++++++++++++++++++++++++++++++++----
 man/Mediawikir.Rd |  9 ++++++++
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/R/class.R b/R/class.R
index ffc9336..f5b294a 100644
--- a/R/class.R
+++ b/R/class.R
@@ -22,6 +22,7 @@ library(R6)
 #'  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")
 #'  mwHandler$postContent()
 #'@section Methods:
 #'\describe{
@@ -45,6 +46,12 @@ library(R6)
 #'    }}
 #'  }}
 #'  \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}}
+#'  \item{\code{uploadFile(file_path, file_name)}}{To upload a file to the server (will overwrite existing files)
+#'  \describe{
+#'    \item{\code{file_path}}{Path to the file to upload to the server}
+#'    \item{\code{file_name}}{Name under which to store it}
+#'    \item{Returns the title of the wiki page containing the file or \code{FALSE} on error}{}
+#'  }}
 #'}
 Mediawikir <- R6Class("Mediawikir",
  public = list(
@@ -54,6 +61,7 @@ Mediawikir <- R6Class("Mediawikir",
    page_name = NULL,
    content = "",
    connect = function(pass){
+     token <- 1
      "une super mĂŠthode"
      tryCatch(
        { #get login token
@@ -81,9 +89,6 @@ Mediawikir <- R6Class("Mediawikir",
          if(response$clientlogin$status!="PASS"){
            stop(paste(response$clientlogin,"\n"))
          }
-         else{
-           return(token)
-         }
        },
        error = function(e)
        {
@@ -134,7 +139,6 @@ Mediawikir <- R6Class("Mediawikir",
          query <- httr::POST(self$url,
                              body = list(action  = "query",
                                          prop    = "info|revisions",
-                                         #intoken = "edit",
                                          meta    = "tokens",
                                          rvprop  = "timestamp",
                                          titles  = self$page_name,
@@ -161,6 +165,50 @@ Mediawikir <- R6Class("Mediawikir",
          success <- FALSE
        })
      return(success)
+   },
+
+   ######
+   # uploadFile
+   ######
+   uploadFile = function(file_path, file_name){
+     success <- TRUE
+     tryCatch(
+       { #get edit token
+         query <- httr::POST(self$url,
+                             body = list(action  = "query",
+                                         prop    = "info|revisions",
+                                         meta    = "tokens",
+                                         rvprop  = "timestamp",
+                                         titles  = paste("file:",file_name,sep=""),
+                                         format  = "json"))
+         httr::stop_for_status(query)
+         response <- httr::content(query, "parsed", "application/json")
+         #perform edit
+         the_file <- upload_file(file_path)
+         query <- httr::POST(self$url,
+                             body = list(action   = "upload",
+                                         filename = file_name,
+                                         comment  = "R generated content",
+                                         format   = "json",
+                                         ignorewarnings = 1,
+                                         file     = the_file,
+                                         token    = response$query$tokens$csrftoken,
+                                         ignorewarnings = 1),
+                             encode = "multipart")
+         httr::stop_for_status(query)
+         response <- httr::content(query, "parsed", "application/json")
+         if(response$upload$result != "Success"){
+           stop(paste("Failed to upload ",file_path,"\n",response))
+         }
+         else{
+           success <- paste("File:",file_name,sep="")
+         }
+       },
+       error = function(e){
+         print(e$message)
+         success <- FALSE
+       })
+     return(success)
    }
  )
 )
diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd
index 73eaf90..2306265 100644
--- a/man/Mediawikir.Rd
+++ b/man/Mediawikir.Rd
@@ -24,6 +24,8 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
 \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}
+
+\item{\code{user_name}}{the user to credit for the changes}
 }}
 
 \section{Methods}{
@@ -49,6 +51,12 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
    }}
  }}
  \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}}
+ \item{\code{uploadFile(file_path, file_name)}}{To upload a file to the server (will overwrite existing files)
+ \describe{
+   \item{\code{file_path}}{Path to the file to upload to the server}
+   \item{\code{file_name}}{Name under which to store it}
+   \item{Returns the title of the wiki page containing the file or \code{FALSE} on error}{}
+ }}
 }
 }
 
@@ -57,6 +65,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
  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")
  mwHandler$postContent()
 }
 \keyword{mediawiki}
-- 
GitLab