diff --git a/DESCRIPTION b/DESCRIPTION index 123b2f7707c4d3420de0a5d48e63e74fd11cdd96..745e3e66c4cb40c104022ca08c59be05b76daa8f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ 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. diff --git a/R/class.R b/R/class.R index 2a65616a101c36d9b5f6740a6b3c06b516d13551..683cee570d43ef26848fae6f3fa377e71711d754 100644 --- a/R/class.R +++ b/R/class.R @@ -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")) + }) + } ) ) diff --git a/man/Batchator.Rd b/man/Batchator.Rd index f9fea6cf07e322bec5d4d6209f48082094b9f468..d180f3af52fbe35bb70f700bf6df7ce84c04994b 100644 --- a/man/Batchator.Rd +++ b/man/Batchator.Rd @@ -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} diff --git a/man/Mediawikir.Rd b/man/Mediawikir.Rd index f4d05c868e9c5a66adca5dc8bc2030ad7ef4624c..e25d1eab6968fd00fc47b6dc05f6cc1fe1cf38f9 100644 --- a/man/Mediawikir.Rd +++ b/man/Mediawikir.Rd @@ -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}