diff --git a/R/class.R b/R/class.R index ffc93367ba71f5e5bdd7160973c1b96c9646f254..f5b294a4850e84d984ebaa5df7b213196b14d9ba 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 73eaf90d8484a56d5d9b180cf0965c72a6dc0fbe..2306265a67652650a9400f363de21c4579bfd745 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}