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

Modify single wikipage use of a single line template attribute

parent 545013f4
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.7
Version: 0.1.8
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.
......
......@@ -7,4 +7,5 @@ importFrom(httr,POST)
importFrom(httr,content)
importFrom(httr,stop_for_status)
importFrom(httr,upload_file)
importFrom(stringr,str_replace)
importFrom(stringr,str_replace_all)
......@@ -11,13 +11,15 @@ library(stringr)
#'@importFrom httr stop_for_status
#'@importFrom httr upload_file
#'@importFrom httr content
#'@importFrom stringr str_replace
#'@export
#'@keywords mediawiki
#'@description A Mediawikir controller serves to connect to a mediawiki API as a registered user in order to edit automatically certain documents based on R scripts
#'@format An \code{\link{R6Class}} generator object
#'@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 base_url the mediawiki instance base url
#'@field api the mediawiki instance API base url
#'@field auth_token the current token used to edit the wiki (cf. \href{https://www.mediawiki.org/wiki/Manual:Edit_token}{Mediawiki API documentation})
#'@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
......@@ -32,7 +34,7 @@ library(stringr)
#' mwHandler$postContent()
#'@section Methods:
#'\describe{
#' \item{\code{initialize(instance_url, user, pass)}}{Creates a new Mediawikir object out of:
#' \item{\code{initialize(instance_url, user, pass)}}{Creates a new \pkg{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}
......@@ -42,7 +44,7 @@ library(stringr)
#' \describe{
#' \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{connect(pass)}}{Should not be called directly (see \code{initialize} for detail)}
#' \item{\code{resetContent(baseContent="")}}{To reset the content of the page for the next post
#' \describe{
#' \item{\code{baseContent}}{The wikitext base for the next post}
......@@ -65,10 +67,21 @@ library(stringr)
#' \item{\code{file_name}}{Name under which to store it}
#' \item{Returns the title of the wiki page containing the file or \code{FALSE} on error}{}
#' }}
#' \item{\code{getContent()}}{Returns the \emph{wikitext} of the selected page (cf. \code{setPage})}
#' \item{\code{replaceSingleLineTemplateField(template, field, newValue)}}{To replace a field of a template in the selected \code{page}.\\
#' This method does not work with numbered parameters.\\
#' Right now it works only for pages that only contain one occurence of the parameter (it does not take outer context into account)\\
#' Additionnally, this function is not meat for templates the value of which spans accross multiple lines.
#' \describe{
#' \item{\code{template}}{The template to look for}
#' \item{\code{field}}{The name of the field concerned}
#' \item{\code{newValue}}{The new value of the field}
#' }}
#'}
Mediawikir <- R6Class("Mediawikir",
public = list(
url = NULL,
base_url = NULL,
api = NULL,
user_name = NULL,
auth_token = NULL,
page = "",
......@@ -78,7 +91,7 @@ Mediawikir <- R6Class("Mediawikir",
"une super méthode"
tryCatch(
{ #get login token
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = list(action="query",
meta="tokens",
type="login",
......@@ -89,12 +102,12 @@ Mediawikir <- R6Class("Mediawikir",
response <- httr::content(query, "parsed", "application/json")
token <- response$query$tokens$logintoken
#login query
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = list(action="clientlogin",
username = self$user_name,
password = pass,
logintoken = token,
loginreturnurl="http://wiki.lezinter.net",
loginreturnurl = self$base_url,
format= "json"))
httr::stop_for_status(query)
#parse query response to make sure login worked
......@@ -105,7 +118,7 @@ Mediawikir <- R6Class("Mediawikir",
},
error = function(e)
{
print(paste("Could not login",self$user_name,"to", self$url, e$message))
print(paste("Could not login",self$user_name,"to", self$api, e$message))
token <- 0
}
)
......@@ -117,7 +130,8 @@ Mediawikir <- R6Class("Mediawikir",
######
initialize = function (instance_url, user, pass){
tryCatch({
self$url <- paste(instance_url,"/","api.php",sep="")
self$base_url <- instance_url
self$api <- paste(instance_url,"/","api.php",sep="")
self$user_name <- user
self$auth_token <- self$connect(pass)
},
......@@ -183,7 +197,7 @@ Mediawikir <- R6Class("Mediawikir",
format = "json")
}
#get edit token
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = fields)
httr::stop_for_status(query)
response <- httr::content(query, "parsed", "application/json")
......@@ -206,7 +220,7 @@ Mediawikir <- R6Class("Mediawikir",
text = self$content,
token = response$query$tokens$csrftoken)
}
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = fields)
httr::stop_for_status(query)
response <- httr::content(query, "parsed", "application/json")
......@@ -222,6 +236,55 @@ Mediawikir <- R6Class("Mediawikir",
return(success)
},
######
# getContent
######
getContent = function(){
success <- TRUE
tryCatch(
{ #Verify that a page has been selected
if(self$page == ""){
warning("No page to update to, use “setPage” method before calling “getContent”.")
}
else{
if(is.character(self$page)){
fields <- list(action = "raw",
title = self$page)
}
else{
fields <- list(action = "raw",
curid = self$page)
}
#get edit token
query <- httr::POST(paste(self$base_url,"index.php",sep="/"),
body = fields)
httr::stop_for_status(query)
response <- httr::content(query)
success <- response
}
},
error = function(e){
print(e$message)
success <- FALSE
})
return(success)
},
######
# replaceSingleLineTemplateField
######
replaceSingleLineTemplateField = function(field="", newValue="", template=""){
success <- self$getContent()
if(success != FALSE){
search <- paste("\\|", field, "\\=[^\\|\\}\\n]*", sep="")
replace <- paste("|", field, "=",newValue, sep="")
self$resetContent()
self$addContent(str_replace(success, search, replace))
success <- self$postContent()
}
return(success)
},
######
# uploadFile
######
......@@ -229,7 +292,7 @@ Mediawikir <- R6Class("Mediawikir",
success <- TRUE
tryCatch(
{ #get edit token
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = list(action = "query",
prop = "info|revisions",
meta = "tokens",
......@@ -240,7 +303,7 @@ Mediawikir <- R6Class("Mediawikir",
response <- httr::content(query, "parsed", "application/json")
#perform edit
the_file <- upload_file(file_path)
query <- httr::POST(self$url,
query <- httr::POST(self$api,
body = list(action = "upload",
filename = file_name,
comment = "R generated content",
......
......@@ -17,9 +17,11 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\section{Fields}{
\describe{
\item{\code{url}}{the mediawiki instance base url}
\item{\code{base_url}}{the mediawiki instance base url}
\item{\code{auth_token}}{the current token used to edit the wiki (cf. https://www.mediawiki.org/wiki/Manual:Edit_token)}
\item{\code{api}}{the mediawiki instance API base url}
\item{\code{auth_token}}{the current token used to edit the wiki (cf. \href{https://www.mediawiki.org/wiki/Manual:Edit_token}{Mediawiki API documentation})}
\item{\code{page}}{the page (name or id) which will be modified (including its namespace, if the name is provided)}
......@@ -31,7 +33,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\section{Methods}{
\describe{
\item{\code{initialize(instance_url, user, pass)}}{Creates a new Mediawikir object out of:
\item{\code{initialize(instance_url, user, pass)}}{Creates a new \pkg{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}
......@@ -41,7 +43,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\describe{
\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{connect(pass)}}{Should not be called directly (see \code{\link[MEDIAWIKIR]{initialize}} for detail)}
\item{\code{resetContent(baseContent="")}}{To reset the content of the page for the next post
\describe{
\item{\code{baseContent}}{The wikitext base for the next post}
......@@ -64,6 +66,16 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\item{\code{file_name}}{Name under which to store it}
\item{Returns the title of the wiki page containing the file or \code{FALSE} on error}{}
}}
\item{\code{getContent()}}{Returns the \emph{wikitext} of the selected page (cf. \code{setPage})}
\item{\code{replaceSingleLineTemplateField(template, field, newValue)}}{To replace a field of a template in the selected \code{page}.\\
This method does not work with numbered parameters.\\
Right now it works only for pages that only contain one occurence of the parameter (it does not take outer context into account)\\
Additionnally, this function is not meat for templates the value of which spans accross multiple lines.
\describe{
\item{\code{template}}{The template to look for}
\item{\code{field}}{The name of the field concerned}
\item{\code{newValue}}{The new value of the field}
}}
}
}
......
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