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

Post content to mediawiki

parent 0b0f5c01
No related branches found
No related tags found
No related merge requests found
...@@ -16,11 +16,13 @@ library(R6) ...@@ -16,11 +16,13 @@ library(R6)
#'@field auth_token the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token) #'@field auth_token the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)
#'@field page_name the page which will be modified (including its namespace) #'@field page_name the page which will be modified (including its namespace)
#'@field content the future content of \code{page_name}, which will be incrementally constructed using object methods #'@field content the future content of \code{page_name}, which will be incrementally constructed using object methods
#'@field user_name the user to credit for the changes
#'@examples #'@examples
#' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage") #' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage")
#' 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$postContent()
#'@section Methods: #'@section Methods:
#'\describe{ #'\describe{
#' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of: #' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of:
...@@ -42,6 +44,7 @@ library(R6) ...@@ -42,6 +44,7 @@ library(R6)
#' \item{\code{h5} → Level 5 title}{} #' \item{\code{h5} → Level 5 title}{}
#' }} #' }}
#' }} #' }}
#' \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}}
#'} #'}
Mediawikir <- R6Class("Mediawikir", Mediawikir <- R6Class("Mediawikir",
public = list( public = list(
...@@ -84,7 +87,7 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -84,7 +87,7 @@ Mediawikir <- R6Class("Mediawikir",
}, },
error = function(e) error = function(e)
{ {
print(paste("Could not login to", self$url, e$message)) print(paste("Could not login",self$user_name,"to", self$url, e$message))
token <- 0 token <- 0
} }
) )
...@@ -116,9 +119,48 @@ Mediawikir <- R6Class("Mediawikir", ...@@ -116,9 +119,48 @@ Mediawikir <- R6Class("Mediawikir",
h3 = theContent <- paste("===", theContent, "==="), h3 = theContent <- paste("===", theContent, "==="),
h4 = theContent <- paste("====", theContent, "===="), h4 = theContent <- paste("====", theContent, "===="),
h5 = theContent <- paste("=====", theContent, "====="), h5 = theContent <- paste("=====", theContent, "====="),
p = theContent <- paste(theContent, "\n", sep=""), p = theContent <- paste(theContent, "\n", sep="")
) )
self$content <- paste(self$content, theContent, sep="\n") self$content <- paste(self$content, theContent, sep="\n")
},
######
# postContent
######
postContent = function(){
success <- TRUE
tryCatch(
{ #get edit token
query <- httr::POST(self$url,
body = list(action = "query",
prop = "info|revisions",
#intoken = "edit",
meta = "tokens",
rvprop = "timestamp",
titles = self$page_name,
format = "json"))
httr::stop_for_status(query)
response <- httr::content(query, "parsed", "application/json")
#perform edit
query <- httr::POST(self$url,
body = list(action = "edit",
title = self$page_name,
#basetimestamp = "TODO",
summary = "R generated content",
format = "json",
text = self$content,
token = response$query$tokens$csrftoken))
httr::stop_for_status(query)
response <- httr::content(query, "parsed", "application/json")
if(response$edit$result != "Success"){
stop(paste("Failed to edit ",self$page_name,"\n",response))
}
},
error = function(e){
print(e$message)
success <- FALSE
})
return(success)
} }
) )
) )
...@@ -48,6 +48,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use ...@@ -48,6 +48,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\item{\code{h5} → Level 5 title}{} \item{\code{h5} → Level 5 title}{}
}} }}
}} }}
\item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}}
} }
} }
...@@ -56,5 +57,6 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use ...@@ -56,5 +57,6 @@ 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$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