mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Remove results of {{subst:unc}} when adding categories
Also contains a braindead Template remover. Doesn't even handle identified edge cases, much less unidentified ones.
This commit is contained in:
parent
d76541ceec
commit
125275548f
5 changed files with 104 additions and 9 deletions
|
|
@ -21,6 +21,7 @@ import org.wikimedia.commons.media.*;
|
||||||
import org.wikimedia.commons.modifications.CategoryModifier;
|
import org.wikimedia.commons.modifications.CategoryModifier;
|
||||||
import org.wikimedia.commons.modifications.ModificationsContentProvider;
|
import org.wikimedia.commons.modifications.ModificationsContentProvider;
|
||||||
import org.wikimedia.commons.modifications.ModifierSequence;
|
import org.wikimedia.commons.modifications.ModifierSequence;
|
||||||
|
import org.wikimedia.commons.modifications.TemplateRemoveModifier;
|
||||||
|
|
||||||
public class MultipleShareActivity
|
public class MultipleShareActivity
|
||||||
extends AuthenticatedActivity
|
extends AuthenticatedActivity
|
||||||
|
|
@ -85,12 +86,17 @@ public class MultipleShareActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCategoriesSave(ArrayList<String> categories) {
|
public void onCategoriesSave(ArrayList<String> categories) {
|
||||||
|
if(categories.size() > 0) {
|
||||||
ContentProviderClient client = getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY);
|
ContentProviderClient client = getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY);
|
||||||
for(Contribution contribution: photosList) {
|
for(Contribution contribution: photosList) {
|
||||||
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
||||||
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
|
||||||
categoriesSequence.setContentProviderClient(client);
|
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
||||||
categoriesSequence.save();
|
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
||||||
|
|
||||||
|
categoriesSequence.setContentProviderClient(client);
|
||||||
|
categoriesSequence.save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// FIXME: Make sure that the content provider is up
|
// FIXME: Make sure that the content provider is up
|
||||||
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import org.wikimedia.commons.auth.*;
|
||||||
import org.wikimedia.commons.modifications.CategoryModifier;
|
import org.wikimedia.commons.modifications.CategoryModifier;
|
||||||
import org.wikimedia.commons.modifications.ModificationsContentProvider;
|
import org.wikimedia.commons.modifications.ModificationsContentProvider;
|
||||||
import org.wikimedia.commons.modifications.ModifierSequence;
|
import org.wikimedia.commons.modifications.ModifierSequence;
|
||||||
|
import org.wikimedia.commons.modifications.TemplateRemoveModifier;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
@ -74,10 +75,14 @@ public class ShareActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onCategoriesSave(ArrayList<String> categories) {
|
public void onCategoriesSave(ArrayList<String> categories) {
|
||||||
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
if(categories.size() > 0) {
|
||||||
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
ModifierSequence categoriesSequence = new ModifierSequence(contribution.getContentUri());
|
||||||
categoriesSequence.setContentProviderClient(getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY));
|
|
||||||
categoriesSequence.save();
|
categoriesSequence.queueModifier(new CategoryModifier(categories.toArray(new String[]{})));
|
||||||
|
categoriesSequence.queueModifier(new TemplateRemoveModifier("Uncategorized"));
|
||||||
|
categoriesSequence.setContentProviderClient(getContentResolver().acquireContentProviderClient(ModificationsContentProvider.AUTHORITY));
|
||||||
|
categoriesSequence.save();
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: Make sure that the content provider is up
|
// FIXME: Make sure that the content provider is up
|
||||||
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
// This is the wrong place for it, but bleh - better than not having it turned on by default for people who don't go throughl ogin
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,10 @@ import java.io.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
public static Date parseMWDate(String mwDate) {
|
public static Date parseMWDate(String mwDate) {
|
||||||
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // Assuming MW always gives me UTC
|
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); // Assuming MW always gives me UTC
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,10 @@ public abstract class PageModifier {
|
||||||
String name = data.optString("name");
|
String name = data.optString("name");
|
||||||
if(name.equals(CategoryModifier.MODIFIER_NAME)) {
|
if(name.equals(CategoryModifier.MODIFIER_NAME)) {
|
||||||
return new CategoryModifier(data.optJSONObject("data"));
|
return new CategoryModifier(data.optJSONObject("data"));
|
||||||
|
} else if(name.equals(TemplateRemoveModifier.MODIFIER_NAME)) {
|
||||||
|
return new TemplateRemoveModifier(data.optJSONObject("data"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,79 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@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 = 2;
|
||||||
|
int startIndex = matcher.start();
|
||||||
|
int curIndex = matcher.end();
|
||||||
|
while(curIndex < pageContents.length()) {
|
||||||
|
if(pageContents.charAt(curIndex) == '{') {
|
||||||
|
braceCount++;
|
||||||
|
} else if(pageContents.charAt(curIndex) == '}') {
|
||||||
|
braceCount--;
|
||||||
|
}
|
||||||
|
curIndex++;
|
||||||
|
if(braceCount == 0) {
|
||||||
|
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) + ".";
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue