Update CKEditor configuration on all wikis

Last modified by Vincent Massol on 2025/02/12 12:25

cogUpdate the CKEditor configuration on all wikis.
TypeSnippet
CategoryOther
Developed by

Guillaume Delhumeau

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

You can use the following Groovy script to update the CKEditor configuration on all wikis:

{{groovy}}
import org.xwiki.text.StringUtils;
import java.util.Arrays;

def DEFAULT_CONTENT = "// Define changes to default configuration here. For example:\n" +
        "// config.uiColor = '#AADC6E';\n" +
        "// config.linkShowTargetTab = true;";
def DEFAULT_CONTENT_2 = "// Define changes to default configuration here. For example:\n" +
        "// config.language = 'fr';\n" +
        "// config.uiColor = '#AADC6E';"
def DEFAULT_CONTENT_ARRAY = Arrays.asList(DEFAULT_CONTENT, DEFAULT_CONTENT_2);

for (def wiki : services.wiki.getAll()) {
    println("* Handling wiki [$wiki.id]");
    xcontext.getContext().setDatabase(wiki.getId());

    def configDocument = xwiki.getDocument('CKEditor.Config');
    boolean needSave = false;
    if (!configDocument.isHidden()) {
        needSave = true;
    }
    def configObject = configDocument.getObject('CKEditor.ConfigClass', true);
    def existingContent = configObject.getValue('advanced');

    if (!configDocument.isNew() && StringUtils.isNotBlank(existingContent)
            && !existingContent.contains('config.versionCheck')
            && !DEFAULT_CONTENT_ARRAY.contains(existingContent.replaceAll("\\r\\n", "\\n"))) {
        println ('ERROR: There is already a custom configuration.');
        // Print debug infos
        println ('{{code}}' + existingContent + '{{/code}}');
    } else {
        existingContent = StringUtils.isNotBlank(existingContent) ? existingContent + '\r\n' : '';
        if (!existingContent.contains('config.versionCheck')) {
            existingContent += 'config.versionCheck = false;'
            configObject.set('advanced', existingContent);
            needSave = true;
        } else {
            println('Configuration has already been updated!');
        }
    }
    if (needSave) {
        configDocument.setHidden(true);
        configDocument.setMinorEdit(true);
        configDocument.save();
        println('Configuration has been updated!');
    }
}
{{/groovy}}

Here is another approach in velocity. It's a little bit simpler and perhaps not that precise as the code above, but it's working good in most of the cases:

{{velocity}}
#set($wikiIds = $services.wiki.getAllIds())
#foreach($wikiId in $wikiIds)
  #set($configDoc = $xwiki.getDocument("${wikiId}:CKEditor.Config"))
  ## retrieve existing config object or create a new one
  #set($configObj = $configDoc.getObject('CKEditor.ConfigClass',true))
  $configDoc.use($configObj)
  #set($advanced = $configDoc.getValue('advanced'))
  ## If not already done, remove existing definitions and append the right one
  #if(!$advanced.contains("config.versionCheck = false;"))
    #if($advanced.contains("config.versionCheck"))
      #set($advanced = $advanced.replaceAll("config.versionCheck.*;",""))
    #end
    $configDoc.set('advanced',
    "${advanced}
config.versionCheck = false;")
    $configDoc.save("versionCheck in CKEditor.Config set to false")
  #end
#end
{{/velocity}}

Get Connected