diff --git a/R/class.R b/R/class.R index 4761add57c81f8cfacfe964d17c71510623a225a..ffc93367ba71f5e5bdd7160973c1b96c9646f254 100644 --- a/R/class.R +++ b/R/class.R @@ -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 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 user_name the user to credit for the changes #'@examples #' mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage") #' mwHandler$addContent("This is an introduction to Mediawikir") #' mwHandler$addContent("First step","h2") #' mwHandler$addContent("First step of the first step","h3") +#' mwHandler$postContent() #'@section Methods: #'\describe{ #' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of: @@ -42,6 +44,7 @@ library(R6) #' \item{\code{h5} → Level 5 title}{} #' }} #' }} +#' \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}} #'} Mediawikir <- R6Class("Mediawikir", public = list( @@ -84,7 +87,7 @@ Mediawikir <- R6Class("Mediawikir", }, 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 } ) @@ -116,9 +119,48 @@ Mediawikir <- R6Class("Mediawikir", h3 = theContent <- paste("===", theContent, "==="), h4 = 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") + }, + + ###### + # 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) } ) ) diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd index 64d20d1a9bc0e99699c198631edd28140cfbd949..73eaf90d8484a56d5d9b180cf0965c72a6dc0fbe 100644 --- a/man/Mediawikir.Rd +++ b/man/Mediawikir.Rd @@ -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{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 mwHandler$addContent("This is an introduction to Mediawikir") mwHandler$addContent("First step","h2") mwHandler$addContent("First step of the first step","h3") + mwHandler$postContent() } \keyword{mediawiki}