Allow admins to disable certain formatters

If a formatter is found to be a security risk it must be possible to
disable it globally. Add a new configuration parameter to disable a
formatter:

  [formatter "ASCIIDOCTOR"]
    enabled = false

Before I5b095ab4c it was possible to disable formatters by not
specifying any mime type or file extension for a formatter, but since
I5b095ab4c projects can now override this. This is why we need an
explicit way to disable a formatter now.

Change-Id: I1ea11e58cf7a75d1befe3149acae0d44b0e550a6
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
index 57eb74b..c3bb2d9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocGlobalConfig.java
@@ -23,6 +23,7 @@
 public class XDocGlobalConfig {
   public static final String SECTION_FORMATTER = "formatter";
   public static final String KEY_ALLOW_HTML = "allowHtml";
+  public static final String KEY_ENABLED = "enabled";
   public static final String KEY_EXT = "ext";
   public static final String KEY_MIME_TYPE = "mimeType";
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
index 8d046ce..eacfd57 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/Formatters.java
@@ -14,6 +14,7 @@
 
 package com.googlesource.gerrit.plugins.xdocs.formatter;
 
+import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_ENABLED;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_EXT;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_MIME_TYPE;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.SECTION_FORMATTER;
@@ -70,11 +71,17 @@
   }
 
   public FormatterProvider get(ProjectState project, String fileName) {
+    XDocGlobalConfig globalCfg = new XDocGlobalConfig(
+        pluginCfgFactory.getGlobalPluginConfig(pluginName));
     MimeType mimeType = fileTypeRegistry.getMimeType(fileName, null);
     String extension = FilenameUtils.getExtension(fileName);
     for (String pluginName : formatters.plugins()) {
       for (Entry<String, Provider<Formatter>> e :
           formatters.byPlugin(pluginName).entrySet()) {
+        if (!globalCfg.getFormatterConfig(e.getKey())
+            .getBoolean(KEY_ENABLED, true)) {
+          continue;
+        }
         ConfigSection formatterCfg = getFormatterConfig(e.getKey(), project);
         for (String configuredMimeType :
           formatterCfg.getStringList(KEY_MIME_TYPE)) {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 86f3891..d4c4d3f 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -106,3 +106,14 @@
 	Supported for the following formatters: `MARKDOWN`
 
 	Default: `false`
+
+<a id="formatterEnabled">
+formatter.<formatter>.enabled
+:	Whether this formatter is enabled.
+
+	When a formatter is disabled the `xdocs-x_doc_resources` cache must
+	be flushed.
+
+	*CANNOT* be overridden on project-level.
+
+	Default: `true`