Set subwiki preferences
Last modified by Vincent Massol on 2021/03/18 11:28
![]() | Sets a value in the XWiki.Preferences of a subwiki in a multi wiki setup |
Type | Snippet |
Category | |
Developed by | xwiki:XWiki.edgo |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
Description
This snippet may be used for setting a value in the XWiki.Preferences of a subwiki in a multi wiki setup. The Value to set is taken from a template wiki (xwiki by default).
{{groovy}}
def blacklistWikis = ['xwiki', 'template']
def templateWikiName = xcontext.mainWikiName // or use your 'template' wiki here ?
def list = xwiki.wikimanager.allWikis
def mainWiki = xcontext.mainWikiName
try {
// the field to inspect an update // default
def field = request.get('updateField')
if (field == null || field.trim().size() == 0) {
field = "documentBundles"
}
// getting the new value from template Wiki
xcontext.setDatabase(templateWikiName)
println "== Available Properties of template wiki **${templateWikiName}** == "
def prefsTemplate = xwiki.getDocument("XWiki.XWikiPreferences")
def templateProperties = prefsTemplate.getObject("XWiki.XWikiPreferences")
def propertyName = templateProperties.getPropertyNames()
propertyName.each {it ->
def updateParams = "updateField=${it}"
def updateUrl = "{{html}}<a href='${doc.getURL('view', updateParams)}'> ${it} </a>{{/html}}"
print updateUrl
}
println " "
println "== Value of **${field}** in template wiki **${templateWikiName}**== "
def valueTemplate = prefsTemplate.getValue(field)
println "The value of property ${field} (copied from) is **${valueTemplate}** "
// ****** update code ******//
def skippedWikis =[]
skippedWikis.addAll(blacklistWikis)
if ('update'.equals(request.get('action'))) {
def doTheUpdate = true
def wikiToUpdateList = []
if ('all'.equals(request.get('updateAllWiki'))) {
wikiToUpdateList.addAll(list.collect {it.wikiName})
}
else if (request.get('updateWiki')) {
wikiToUpdateList.add(request.get('updateWiki'))
}
else {
doTheUpdate = false
}
if (doTheUpdate && wikiToUpdateList.size() > 0) {
for (String wikiName: wikiToUpdateList) {
if (blacklistWikis.contains(wikiName)) {
continue
}
xcontext.setDatabase(wikiName)
def prefs2UpdateDoc = xwiki.getDocument("XWiki.XWikiPreferences")
def oldValue = prefs2UpdateDoc.getValue(field)
if(!request.get('forceUpdate')) {
if(valueTemplate.equals(oldValue) || oldValue ) {
skippedWikis.add(wikiName)
continue
}
}
prefs2UpdateDoc.set(field, valueTemplate)
prefs2UpdateDoc.save()
} // eof for
} // eof doTheUpdate
println "== Update Report == "
println " the following wikis are skipped:"
skippedWikis.each() {
print " ${it} "
}
println " "
}
// ****** end of update ******//
println "== overview == "
def allUpdateParams = "action=update&updateField=${field}&updateAllWiki=all"
def allUpdateUrl = "{{html}}<a href='${doc.getURL('view', allUpdateParams)}'>Update all Wikis </a>{{/html}}"
println "${allUpdateUrl}"
xcontext.setDatabase(mainWiki)
println "|=Wiki |=Current value of **${field}**|=action"
for (def wiki : list) {
if (blacklistWikis.contains (wiki.wikiName)) {
continue
}
xcontext.setDatabase (wiki.wikiName)
def prefs = xwiki.getDocument("XWiki.XWikiPreferences")
def prop = prefs.xWikiClass.get(field)
def updateParams = "action=update&updateField=${field}&updateWiki=${wiki.wikiName}"
def updateUrl = "{{html}}<a href='${doc.getURL('view', updateParams)}'>update</a>{{/html}}"
def updateForceParams = "action=update&updateField=${field}&updateWiki=${wiki.wikiName}&forceUpdate=on"
def updateForceUrl = "{{html}}<a href='${doc.getURL('view', updateForceParams)}'>force update </a>{{/html}}"
println "|${wiki.wikiName} | ${prefs.display(field)} | ${valueTemplate.equals(prefs.getValue(field)) ? '' : updateUrl + ' or ' + updateForceUrl}"
}
} finally {
xcontext.setDatabase (mainWiki)
}
{{/groovy}}
def blacklistWikis = ['xwiki', 'template']
def templateWikiName = xcontext.mainWikiName // or use your 'template' wiki here ?
def list = xwiki.wikimanager.allWikis
def mainWiki = xcontext.mainWikiName
try {
// the field to inspect an update // default
def field = request.get('updateField')
if (field == null || field.trim().size() == 0) {
field = "documentBundles"
}
// getting the new value from template Wiki
xcontext.setDatabase(templateWikiName)
println "== Available Properties of template wiki **${templateWikiName}** == "
def prefsTemplate = xwiki.getDocument("XWiki.XWikiPreferences")
def templateProperties = prefsTemplate.getObject("XWiki.XWikiPreferences")
def propertyName = templateProperties.getPropertyNames()
propertyName.each {it ->
def updateParams = "updateField=${it}"
def updateUrl = "{{html}}<a href='${doc.getURL('view', updateParams)}'> ${it} </a>{{/html}}"
print updateUrl
}
println " "
println "== Value of **${field}** in template wiki **${templateWikiName}**== "
def valueTemplate = prefsTemplate.getValue(field)
println "The value of property ${field} (copied from) is **${valueTemplate}** "
// ****** update code ******//
def skippedWikis =[]
skippedWikis.addAll(blacklistWikis)
if ('update'.equals(request.get('action'))) {
def doTheUpdate = true
def wikiToUpdateList = []
if ('all'.equals(request.get('updateAllWiki'))) {
wikiToUpdateList.addAll(list.collect {it.wikiName})
}
else if (request.get('updateWiki')) {
wikiToUpdateList.add(request.get('updateWiki'))
}
else {
doTheUpdate = false
}
if (doTheUpdate && wikiToUpdateList.size() > 0) {
for (String wikiName: wikiToUpdateList) {
if (blacklistWikis.contains(wikiName)) {
continue
}
xcontext.setDatabase(wikiName)
def prefs2UpdateDoc = xwiki.getDocument("XWiki.XWikiPreferences")
def oldValue = prefs2UpdateDoc.getValue(field)
if(!request.get('forceUpdate')) {
if(valueTemplate.equals(oldValue) || oldValue ) {
skippedWikis.add(wikiName)
continue
}
}
prefs2UpdateDoc.set(field, valueTemplate)
prefs2UpdateDoc.save()
} // eof for
} // eof doTheUpdate
println "== Update Report == "
println " the following wikis are skipped:"
skippedWikis.each() {
print " ${it} "
}
println " "
}
// ****** end of update ******//
println "== overview == "
def allUpdateParams = "action=update&updateField=${field}&updateAllWiki=all"
def allUpdateUrl = "{{html}}<a href='${doc.getURL('view', allUpdateParams)}'>Update all Wikis </a>{{/html}}"
println "${allUpdateUrl}"
xcontext.setDatabase(mainWiki)
println "|=Wiki |=Current value of **${field}**|=action"
for (def wiki : list) {
if (blacklistWikis.contains (wiki.wikiName)) {
continue
}
xcontext.setDatabase (wiki.wikiName)
def prefs = xwiki.getDocument("XWiki.XWikiPreferences")
def prop = prefs.xWikiClass.get(field)
def updateParams = "action=update&updateField=${field}&updateWiki=${wiki.wikiName}"
def updateUrl = "{{html}}<a href='${doc.getURL('view', updateParams)}'>update</a>{{/html}}"
def updateForceParams = "action=update&updateField=${field}&updateWiki=${wiki.wikiName}&forceUpdate=on"
def updateForceUrl = "{{html}}<a href='${doc.getURL('view', updateForceParams)}'>force update </a>{{/html}}"
println "|${wiki.wikiName} | ${prefs.display(field)} | ${valueTemplate.equals(prefs.getValue(field)) ? '' : updateUrl + ' or ' + updateForceUrl}"
}
} finally {
xcontext.setDatabase (mainWiki)
}
{{/groovy}}