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

Prepare for batchator…

parent feb00446
No related branches found
No related tags found
No related merge requests found
Package: mediawikir
Type: Package
Title: Connects to a mediawiki instance and writes in it what you want
Version: 0.1.1
Version: 0.1.2
Author: Mathieu Loiseau
Maintainer: Mathieu Loiseau <loiseaum@univ-grenoble-alpes.fr>
Description: It can be used to post the output of an R script directly into a mediawiki instance
......
# Generated by roxygen2: do not edit by hand
export(Batchator)
export(Mediawikir)
importFrom(R6,R6Class)
importFrom(httr,POST)
......
library(R6)
library(httr)
#'Mediawikir: write R output to mediawiki
#'
......@@ -18,21 +19,26 @@ library(R6)
#'@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 <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword")
#' mwHandler$setPage("wikiPage")
#' mwHandler$addContent("This is an introduction to Mediawikir")
#' mwHandler$addContent("First step","h2")
#' mwHandler$addContent("First step of the first step","h3")
#' mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
#' fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
#' mwHandler$addContent(paste("[[",fileName,"]]",sep=""))
#' mwHandler$postContent()
#'@section Methods:
#'\describe{
#' \item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of:
#' \item{\code{initialize(instance_url, user, pass)}}{Creates a new Mediawikir object out of:
#' \describe{
#' \item{\code{instance_url}}{The base url to the mediawiki instance}
#' \item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki}
#' \item{\code{page}}{The wiki page which contains the changes}
#' \item{\code{pass}}{The user's password}
#' }}
#' \item{\code{setPage(pageName)}}{To set the page for the next post
#' \describe{
#' \item{\code{pageName}}{The name of the wiki page which contains the changes}
#' }}
#' \item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)}
#' \item{\code{addContent(theContent, type="p")}}{To add content that will later on be posted to mediawiki. This is where structuring elements are provided
#' \describe{
......@@ -58,7 +64,7 @@ Mediawikir <- R6Class("Mediawikir",
url = NULL,
user_name = NULL,
auth_token = NULL,
page_name = NULL,
page_name = "",
content = "",
connect = function(pass){
token <- 1
......@@ -102,11 +108,10 @@ Mediawikir <- R6Class("Mediawikir",
######
# initialize
######
initialize = function (instance_url, user, page, pass){
initialize = function (instance_url, user, pass){
tryCatch({
self$url <- paste(instance_url,"/","api.php",sep="")
self$user_name <- user
self$page_name <- page
self$auth_token <- self$connect(pass)
},
error = function(e){
......@@ -115,6 +120,13 @@ Mediawikir <- R6Class("Mediawikir",
})
},
######
# setPage
######
setPage = function(pageName){
self$page_name <- pageName
},
######
# addContent
######
......@@ -135,29 +147,35 @@ Mediawikir <- R6Class("Mediawikir",
postContent = function(){
success <- TRUE
tryCatch(
{ #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"))
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))
{ #Verify that a page has been selected
if(self$page_name == ""){
warning("No page to write to, use “setPage” method before calling “postContent”.")
}
else{
#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"))
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){
......@@ -212,3 +230,31 @@ Mediawikir <- R6Class("Mediawikir",
}
)
)
#'Batchator: Batch input to mediawiki based on a csv and a template
#'
#'@docType class
#'@export
#'@keywords mediawiki
#'@description Uses mediawikir to create a batch of pages
Batchator <- R6Class("Batchator",
public = list(
author = NULL,
wiki = NULL,
prefix ="",
######
# initialize
######
initialize = function (wiki,author,pass,prefix){
tryCatch({
self$wiki <- Mediawikir$new(wiki, author, pass)
self$prefix <- prefix
},
error = function(e){
print(e$message)
self <- FALSE
})
}
)
)
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class.R
\docType{class}
\name{Batchator}
\alias{Batchator}
\title{Batchator: Batch input to mediawiki based on a csv and a template}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
Batchator
}
\description{
Uses mediawikir to create a batch of pages
}
\keyword{mediawiki}
......@@ -31,13 +31,16 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\section{Methods}{
\describe{
\item{\code{initialize(instance_url, user, page, pass)}}{Creates a new Mediawikir object out of:
\item{\code{initialize(instance_url, user, pass)}}{Creates a new Mediawikir object out of:
\describe{
\item{\code{instance_url}}{The base url to the mediawiki instance}
\item{\code{user}}{The screen name of the user who will be credited for the changes in the wiki}
\item{\code{page}}{The wiki page which contains the changes}
\item{\code{pass}}{The user's password}
}}
\item{\code{setPage(pageName)}}{To set the page for the next post
\describe{
\item{\code{pageName}}{The name of the wiki page which contains the changes}
}}
\item{\code{connect(pass)}}{Should not be called directly (see initialize for detail)}
\item{\code{addContent(theContent, type="p")}}{To add content that will later on be posted to mediawiki. This is where structuring elements are provided
\describe{
......@@ -61,11 +64,13 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
}
\examples{
mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword", "wikiPage")
mwHandler <- Mediawikir$new("http://my.wiki.org", "reg_user", "mypassword")
mwHandler$setPage("wikiPage")
mwHandler$addContent("This is an introduction to Mediawikir")
mwHandler$addContent("First step","h2")
mwHandler$addContent("First step of the first step","h3")
mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
mwHandler$addContent(paste("[[",fileName,"]]",sep=""))
mwHandler$postContent()
}
\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