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

template field replacement seems to work

parent 89daa7a9
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.8
Version: 0.1.9
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.
......
......@@ -32,6 +32,7 @@ library(stringr)
#' fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
#' mwHandler$addContent(paste("[[",fileName,"]]",sep=""))
#' mwHandler$postContent()
#' mwHandler$replaceSingleLineTemplateField("attribute", "newValue")
#'@section Methods:
#'\describe{
#' \item{\code{initialize(instance_url, user, pass)}}{Creates a new \pkg{Mediawikir} object out of:
......@@ -58,6 +59,7 @@ library(stringr)
#' \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()}}{To replace the content of \code{page} with \code{content}}
......@@ -68,9 +70,12 @@ library(stringr)
#' \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)\\
#' \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}
......@@ -166,7 +171,12 @@ Mediawikir <- R6Class("Mediawikir",
h5 = theContent <- paste("=====", theContent, "====="),
p = theContent <- paste(theContent, "\n", sep="")
)
self$content <- paste(self$content, theContent, sep="\n")
if(type != "raw"){
self$content <- paste(self$content, theContent, sep="\n")
}
else{
self$content <- paste(self$content, theContent, sep="")
}
},
######
......@@ -278,9 +288,16 @@ Mediawikir <- R6Class("Mediawikir",
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()
formerText = success
newText = str_replace(success, search, replace)
if(newText != formerText){
self$resetContent()
self$addContent(newText, "raw")
success <- self$postContent()
}
else{
success <- paste("No modification of ",self$page," due to setting “",field,"” to “",newValue,"”.",sep="")
}
}
return(success)
},
......@@ -366,6 +383,11 @@ Mediawikir <- R6Class("Mediawikir",
#' #"One","numbers","Awful Owen","Carlos"
#' #"One","numbers","Black Betty","Carla"
#' #3 pages are created in this example…
#' batchata$modifyWikiTemplateAttributes("list.csv")
#' #content of "list.csv"
#' #"Page","Att1","Att2"
#' #2204,"New value for att1","New value for att2"
#' #"Help:full/page/Name","New value for att1","New value for att2"
#'@section Methods:
#'\describe{
#' \item{\code{initialize(wiki,author,pass)}}{Creates a new Batchator object (and its Mediawikir object as an attribute)
......@@ -380,8 +402,12 @@ Mediawikir <- R6Class("Mediawikir",
#' The template should contain occurrences of the fields defined in the content file.}
#' \item{\code{loadContent(file_path)}}{Takes the path to a csv file that contains both the list of fields used in the templates (1st line)
#' AND the data for each of those fields (following lines).}
#' \item{\code{applyTemplate()}}{Applies the templates defined using loadTemplate, setPageNamePattern to the data loaded with loadContent.
#' \item{\code{applyTemplate()}}{Applies the templates defined using \code{loadTemplate}, \code{setPageNamePattern} to the data loaded with \code{loadContent}.
#' It should be noted that if the page exists it is replaced.}
#' \item{\code{modifyWikiTemplateAttributes(file_path)}}{Reads file_path and replaces in each page listed in the Page column the content
#' first occurence of each field with the corresponding value.
#'
#' \strong{Warning:} This function is dependant on \code{\link{replaceSingleLineTemplateField}}, which is very basic.}
#'}
#'@importFrom stringr str_replace_all
......@@ -442,7 +468,7 @@ Batchator <- R6Class("Batchator",
}
},
error = function(e){
print(paste(e$message, "No content loaded"))
print(paste(e$message, "Batchator:loadContent → No content loaded"))
})
},
......@@ -485,6 +511,43 @@ Batchator <- R6Class("Batchator",
error = function(e){
print(e$message)
})
},
######
# modifyWikiTemplateAttributes
######
modifyWikiTemplateAttributes = function(file_path){
tryCatch({
csvM <- read.csv(file=file_path, header = FALSE)
if(ncol(csvM) == 1){
stop(paste("'",file_path,"' only has one column.",sep=""))
}
else{
field_list <- as.vector(t(csvM[1,]))
if( (field_list[1] != "Page")
&& (field_list[1] != "page")){
stop(paste("1st column name of '",file_path,"' should be “Page” (",field_list[1],").",sep=""))
}
rows <- csvM[-1,]
for(i in 1:nrow(rows)){
pageId <- toString(rows[i,1])
pageIdType <- "page name"
if(grepl("^[0-9]+$", pageId, perl = T)){
pageId <- as.integer(pageId)
pageIdType <- "page ID"
}
self$wiki$setPage(pageId)
print(paste("processing page '",pageId,"' (",pageIdType,")", sep=""))#/**/
for(j in 2:length(field_list)){
success = self$wiki$replaceSingleLineTemplateField(toString(field_list[j]),toString(rows[i,j]))
print(paste("new value : |",field_list[j],"=",rows[i,j]," → ",success,sep=""))
}
}
}
},
error = function(e){
print(paste(e$message, "Batchator.modifyWikiTemplateAttributes → No content loaded"))
})
}
)
)
......@@ -45,8 +45,12 @@ Uses \link{Mediawikir} to create/edit a batch of mediawiki pages
The template should contain occurrences of the fields defined in the content file.}
\item{\code{loadContent(file_path)}}{Takes the path to a csv file that contains both the list of fields used in the templates (1st line)
AND the data for each of those fields (following lines).}
\item{\code{applyTemplate()}}{Applies the templates defined using loadTemplate, setPageNamePattern to the data loaded with loadContent.
\item{\code{applyTemplate()}}{Applies the templates defined using \code{loadTemplate}, \code{setPageNamePattern} to the data loaded with \code{loadContent}.
It should be noted that if the page exists it is replaced.}
\item{\code{modifyWikiTemplateAttributes(file_path)}}{Reads file_path and replaces in each page listed in the Page column the content
first occurence of each field with the corresponding value.
\strong{Warning:} This function is dependant on \code{\link{replaceSingleLineTemplateField}}, which is very basic.}
}
}
......@@ -71,5 +75,10 @@ Uses \link{Mediawikir} to create/edit a batch of mediawiki pages
#"One","numbers","Awful Owen","Carlos"
#"One","numbers","Black Betty","Carla"
#3 pages are created in this example…
batchata$modifyWikiTemplateAttributes("list.csv")
#content of "list.csv"
#"Page","Att1","Att2"
#2204,"New value for att1","New value for att2"
#"Help:full/page/Name","New value for att1","New value for att2"
}
\keyword{mediawiki}
......@@ -43,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 \code{\link[MEDIAWIKIR]{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}
......@@ -57,6 +57,7 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\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()}}{To replace the content of \code{page} with \code{content}}
......@@ -67,9 +68,12 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
\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)\\
\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}
......@@ -88,5 +92,6 @@ A Mediawikir controller serves to connect to a mediawiki API as a registered use
fileName <- mwHandler$uploadFile("path/toFile.png","My beautiful image.png")
mwHandler$addContent(paste("[[",fileName,"]]",sep=""))
mwHandler$postContent()
mwHandler$replaceSingleLineTemplateField("attribute", "newValue")
}
\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