cogSets a value in the XWiki.Preferences of a subwiki in a multi wiki setup
Developed by

Denis Gotthans

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

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).

Be Careful this Snippet is only tested for String values!
{{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}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Created by Denis Gotthans on 2010/04/08 16:39
     

Get Connected