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

Set page by title or id

parent 33c5b676
No related branches found
No related tags found
No related merge requests found
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
......@@ -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))
}
}
},
......
......@@ -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}
......
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