Skip to content
Snippets Groups Projects
Commit 34ea860c authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

file upload

parent 64af7977
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ library(R6) ...@@ -22,6 +22,7 @@ library(R6)
#' mwHandler$addContent("This is an introduction to Mediawikir") #' mwHandler$addContent("This is an introduction to Mediawikir")
#' mwHandler$addContent("First step","h2") #' mwHandler$addContent("First step","h2")
#' mwHandler$addContent("First step of the first step","h3") #' mwHandler$addContent("First step of the first step","h3")
#' mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
#' mwHandler$postContent() #' mwHandler$postContent()
#'@section Methods: #'@section Methods:
#'\describe{ #'\describe{
...@@ -45,6 +46,12 @@ library(R6) ...@@ -45,6 +46,12 @@ library(R6)
#' }} #' }}
#' }} #' }}
#' \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}} #' \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", Mediawikir <- R6Class("Mediawikir",
public = list( public = list(
...@@ -54,6 +61,7 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -54,6 +61,7 @@ Mediawikir <- R6Class("Mediawikir",
page_name = NULL, page_name = NULL,
content = "", content = "",
connect = function(pass){ connect = function(pass){
token <- 1
"une super méthode" "une super méthode"
tryCatch( tryCatch(
{ #get login token { #get login token
...@@ -81,9 +89,6 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -81,9 +89,6 @@ Mediawikir <- R6Class("Mediawikir",
if(response$clientlogin$status!="PASS"){ if(response$clientlogin$status!="PASS"){
stop(paste(response$clientlogin,"\n")) stop(paste(response$clientlogin,"\n"))
} }
else{
return(token)
}
}, },
error = function(e) error = function(e)
{ {
...@@ -134,7 +139,6 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -134,7 +139,6 @@ Mediawikir <- R6Class("Mediawikir",
query <- httr::POST(self$url, query <- httr::POST(self$url,
body = list(action = "query", body = list(action = "query",
prop = "info|revisions", prop = "info|revisions",
#intoken = "edit",
meta = "tokens", meta = "tokens",
rvprop = "timestamp", rvprop = "timestamp",
titles = self$page_name, titles = self$page_name,
...@@ -161,6 +165,50 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -161,6 +165,50 @@ Mediawikir <- R6Class("Mediawikir",
success <- FALSE success <- FALSE
}) })
return(success) 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)
} }
) )
) )
...@@ -24,6 +24,8 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use ...@@ -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{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{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}{ \section{Methods}{
...@@ -49,6 +51,12 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use ...@@ -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{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 ...@@ -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("This is an introduction to Mediawikir")
mwHandler$addContent("First step","h2") mwHandler$addContent("First step","h2")
mwHandler$addContent("First step of the first step","h3") mwHandler$addContent("First step of the first step","h3")
mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
mwHandler$postContent() mwHandler$postContent()
} }
\keyword{mediawiki} \keyword{mediawiki}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment