diff --git a/DESCRIPTION b/DESCRIPTION index 3c10292ab22a26bc25f8dd92a57e8b11ec9e93ab..d854b0b5f2dba062d84194aa08046c764261539f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,12 +1,12 @@ -Package: mediawikir +Package: Mediawikir Type: Package Title: Connects to a mediawiki instance and writes in it what you want -Version: 0.1.6 +Version: 0.1.7 Author: Mathieu Loiseau Maintainer: Mathieu Loiseau <loiseaum@univ-grenoble-alpes.fr> Description: Mediawikir class can be used to post the output of an R script directly into a mediawiki instance. Batchator class allows batch page creation. License: MIT Encoding: UTF-8 LazyData: true -RoxygenNote: 6.0.1 +RoxygenNote: 6.1.1 Imports: httr,R6,stringr diff --git a/R/class.R b/R/class.R index 7bc564045a32cfad834eac56a07acc7a5b2fa5e8..c78499e77c1abb349b2b483facb6a2805a7635c5 100644 --- a/R/class.R +++ b/R/class.R @@ -18,8 +18,8 @@ library(stringr) #'@return Object of \code{\link{R6Class}} with methods to interact with mediawiki instance #'@field url the mediawiki instance base url #'@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 page the page (name or id) which will be modified (including its namespace, if the name is provided) +#'@field content the future content of \code{page}, 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") @@ -38,9 +38,9 @@ library(stringr) #' \item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki} #' \item{\code{pass}}{The user's password} #' }} -#' \item{\code{setPage(pageName)}}{To set the page for the next post +#' \item{\code{setPage(pageNameOrId)}}{To set the page for the next post #' \describe{ -#' \item{\code{pageName}}{The name of the wiki page which contains the changes} +#' \item{\code{pageNameOrId}}{The name (or id) of the wiki page which contains the changes} #' }} #' \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)} #' \item{\code{resetContent(baseContent="")}}{To reset the content of the page for the next post @@ -58,7 +58,7 @@ library(stringr) #' \item{\code{h5} → Level 5 title}{} #' }} #' }} -#' \item{\code{postContent()}}{To replace the content of \code{page_name} with \code{content}} +#' \item{\code{postContent()}}{To replace the content of \code{page} 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} @@ -71,7 +71,7 @@ Mediawikir <- R6Class("Mediawikir", url = NULL, user_name = NULL, auth_token = NULL, - page_name = "", + page = "", content = "", connect = function(pass){ token <- 1 @@ -130,8 +130,8 @@ Mediawikir <- R6Class("Mediawikir", ###### # setPage ###### - setPage = function(pageName){ - self$page_name <- pageName + setPage = function(pageNameOrId){ + self$page <- pageNameOrId }, ###### @@ -162,33 +162,56 @@ Mediawikir <- R6Class("Mediawikir", success <- TRUE tryCatch( { #Verify that a page has been selected - if(self$page_name == ""){ + if(self$page == ""){ warning("No page to write to, use “setPage†method before calling “postContentâ€.") } else{ + if(is.character(self$page)){ + fields <- list(action = "query", + prop = "info|revisions", + meta = "tokens", + rvprop = "timestamp", + titles = self$page, + format = "json") + } + else{ + fields <- list(action = "query", + prop = "info|revisions", + meta = "tokens", + rvprop = "timestamp", + pageids = self$page, + format = "json") + } #get edit token query <- httr::POST(self$url, - body = list(action = "query", - prop = "info|revisions", - meta = "tokens", - rvprop = "timestamp", - titles = self$page_name, - format = "json")) + body = fields) httr::stop_for_status(query) response <- httr::content(query, "parsed", "application/json") #perform edit + if(is.character(self$page)){ + fields <- list(action = "edit", + title = self$page, + #basetimestamp = "TODO", + summary = "R generated content", + format = "json", + text = self$content, + token = response$query$tokens$csrftoken) + } + else{ + fields <- list(action = "edit", + pageid = self$page, + #basetimestamp = "TODO", + summary = "R generated content", + format = "json", + text = self$content, + token = response$query$tokens$csrftoken) + } 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)) + body = fields) 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)) + stop(paste("Failed to edit ",self$page,"\n",response)) } } }, diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd index 24f3296c3b58df2313b5cf665a4ac8486dd8646a..872b02a5be381c0322f0de10eed539de734989c2 100644 --- a/man/Mediawikir.Rd +++ b/man/Mediawikir.Rd @@ -21,9 +21,9 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use \item{\code{auth_token}}{the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)} -\item{\code{page_name}}{the page which will be modified (including its namespace)} +\item{\code{page}}{the page (name or id) which will be modified (including its namespace, if the name is provided)} -\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}, which will be incrementally constructed using object methods} \item{\code{user_name}}{the user to credit for the changes} }} @@ -37,9 +37,9 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use \item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki} \item{\code{pass}}{The user's password} }} - \item{\code{setPage(pageName)}}{To set the page for the next post + \item{\code{setPage(pageNameOrId)}}{To set the page for the next post \describe{ - \item{\code{pageName}}{The name of the wiki page which contains the changes} + \item{\code{pageNameOrId}}{The name (or id) of the wiki page which contains the changes} }} \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)} \item{\code{resetContent(baseContent="")}}{To reset the content of the page for the next post @@ -57,7 +57,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}} + \item{\code{postContent()}}{To replace the content of \code{page} 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}