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

modifyWikiTemplateAttributes replaces empty field by creating it

parent 6b864561
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.11
Title: Connects to a mediawiki instance and writes in it what you want. Provides classes Mediawikir and Batchator.
Version: 0.1.12
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.
......
......@@ -42,7 +42,7 @@ library(stringr)
#' \item{\code{pass}}{The user's password}
#' \item{\code{dir}}{In case of \href{https://www.mediawiki.org/wiki/Manual:Short_URL}{short Url configuration},
#' the base url for mediawiki is not the actual directory containing the api and index.php files. In these cases,
#' the user should specify the installation directory of mediawiki from the base_url.}
#' the user should specify the installation directory of mediawiki from the base_url.}}}
#' \item{\code{setPage(pageNameOrId)}}{To set the page for the next post
#' \describe{
#' \item{\code{pageNameOrId}}{The name (or id) of the wiki page which contains the changes}
......@@ -75,11 +75,11 @@ library(stringr)
#' \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.
#' This method does not work with numbered parameters. If the sought field does not exist, adds it at the beginning of the template.
#'
#' 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.
#' Additionnally, this function is not made 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}
......@@ -296,15 +296,23 @@ Mediawikir <- R6Class("Mediawikir",
if(success != FALSE){
search <- paste("\\|", field, "\\=[^\\|\\}\\n]*", sep="")
replace <- paste("|", field, "=",newValue, sep="")
formerText = success
newText = str_replace(success, search, replace)
formerText <- success
newText <- str_replace(formerText, search, replace)
if(newText == formerText){
#The parameter does not exist, adding it as a first parameter
success <- paste("Could not find field “", field, "”. Adding it as a first parameter → ", sep="")
newText <- str_replace(formerText, "(\\{\\{[^\\|\\n]*)(\\s|\\n)*\\|", paste("\\1\n |", field, "=",newValue,"\n |",sep=""))
}
else{
success <- ""
}
if(newText != formerText){
self$resetContent()
self$addContent(newText, "raw")
success <- self$postContent(paste(field," → ", newValue, sep=""))
success <- paste(success,self$postContent(paste(field," → ", newValue, sep="")))
}
else{
success <- paste("No modification of ",self$page," while trying to set “",field,"” to “",newValue,"”.",sep="")
success <- paste(success,"No modification of ",self$page," while trying to set “",field,"” to “",newValue,"”.",sep="")
}
}
return(success)
......
......@@ -11,8 +11,60 @@ Object of \code{\link{R6Class}} with methods to interact with mediawiki instance
\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
}
\section{}{
NA
\section{Methods}{
\describe{
\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}
\item{\code{pass}}{The user's password}
\item{\code{dir}}{In case of \href{https://www.mediawiki.org/wiki/Manual:Short_URL}{short Url configuration},
the base url for mediawiki is not the actual directory containing the api and index.php files. In these cases,
the user should specify the installation directory of mediawiki from the base_url.}}}
\item{\code{setPage(pageNameOrId)}}{To set the page for the next post
\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 \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}
}}
\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{
\item{\code{theContent}}{Either text (using mediawiki syntax) or path to a file}
\item{\code{type}}{\describe{
\item{\code{p} → A plain paragraph.}{}
\item{\code{h2} → Level 2 title}{}
\item{\code{h3} → Level 3 title}{}
\item{\code{h4} → Level 4 title}{}
\item{\code{h5} → Level 5 title}{}
\item{\code{raw} → No editing of the content}{}
}}
}}
\item{\code{postContent(comment)}}{To replace the content of the \code{page} (attribute) with \code{content} (attribute).
Parameter \code{comment} specifies the description of the modification, default is \code{“R generated 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}
\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. If the sought field does not exist, adds it at the beginning of the template.
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 made 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}
}}
}
}
\examples{
......
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