package org.wikimedia.commons.modifications;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TemplateRemoveModifier extends PageModifier {

    public static final String MODIFIER_NAME = "TemplateRemoverModifier";

    public static final String PARAM_TEMPLATE_NAME = "template";

    public TemplateRemoveModifier(String templateName) {
        super(MODIFIER_NAME);
        try {
            params.putOpt(PARAM_TEMPLATE_NAME, templateName);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public TemplateRemoveModifier(JSONObject data) {
        super(MODIFIER_NAME);
        this.params = data;
    }

    // Never get into a land war in Asia.
    @Override
    public String doModification(String pageName, String pageContents) {
        String templateRawName = params.optString(PARAM_TEMPLATE_NAME);
        // Wikitext title normalizing rules. Spaces and _ equivalent
        // They also 'condense' - any number of them reduce to just one (just like HTML)
        String templateNormalized = templateRawName.trim().replaceAll("(\\s|_)+", "(\\s|_)+");

        // Not supporting {{ inside <nowiki> and HTML comments yet
        // (Thanks to marktraceur for reminding me of the HTML comments exception)
        Pattern templateStartPattern = Pattern.compile("\\{\\{" + templateNormalized, Pattern.CASE_INSENSITIVE);
        Matcher matcher = templateStartPattern.matcher(pageContents);

        while(matcher.find()) {
            int braceCount = 1;
            int startIndex = matcher.start();
            int curIndex = matcher.end();
            while(curIndex < pageContents.length()) {
                if(pageContents.substring(curIndex, curIndex + 2).equals("{{")) {
                    braceCount++;
                } else if(pageContents.substring(curIndex, curIndex + 2).equals("}}")) {
                    braceCount--;
                }
                curIndex++;
                if(braceCount == 0) {
                    curIndex++; // To account for the last brace in the closing }} pair
                    break;
                }
            }

            // Strip trailing whitespace
            while(curIndex < pageContents.length()) {
                if(pageContents.charAt(curIndex) == ' ' || pageContents.charAt(curIndex) == '\n') {
                    curIndex++;
                } else {
                    break;
                }
            }

            // I am so going to hell for this, sigh
            pageContents = pageContents.substring(0, startIndex) + pageContents.substring(curIndex);
            matcher = templateStartPattern.matcher(pageContents);
        }

        return pageContents;

    }

    @Override
    public String getEditSumary() {
        return "Removed template " + params.optString(PARAM_TEMPLATE_NAME) + ".";
    }
}

@@ -361,24 +361,193 @@
 template%22;%0A%0A
+    public static final Pattern PATTERN_TEMPLATE_OPEN = Pattern.compile(%22%5C%5C%7B%5C%5C%7B%22);%0A    public static final Pattern PATTERN_TEMPLATE_CLOSE = Pattern.compile(%22%5C%5C%7D%5C%5C%7D%22);%0A%0A%0A
     public T
@@ -915,50 +915,8 @@
  %7D%0A%0A
-    // Never get into a land war in Asia.%0A
     
@@ -1887,66 +1887,65 @@
  if(
-pageContents.substring(curIndex, curIndex + 2).equals(%22%7B%7B%22
+PATTERN_TEMPLATE_OPEN.matcher(pageContents).find(curIndex
 )) %7B
@@ -2001,24 +2001,55 @@
   %7D else if(
+PATTERN_TEMPLATE_CLOSE.matcher(
 pageContents
@@ -2044,34 +2044,30 @@
 pageContents
-.substring
+).find
 (curIndex, c
@@ -2067,35 +2067,8 @@
 ndex
-, curIndex + 2).equals(%22%7D%7D%22
 )) %7B
@@ -2190,96 +2190,8 @@
 ) %7B%0A
-                    curIndex++; // To account for the last brace in the closing %7D%7D pair%0A
     

Revert "Make TemplateRemoveer *actually* remove templates"