Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (54 sloc) 1.84 KB
package mediawiki.task;
import java.util.List;
import java.util.regex.Pattern;
import util.Util;
import mediawiki.MediaWikiConnection;
import mediawiki.MediaWikiTask;
import mediawiki.MediaWikiUtil;
import mediawiki.info.Article;
import mediawiki.request.ContentRequest;
import mediawiki.request.EditRequest;
import mediawiki.request.TemplateEmbeddedInRequest;
public class TemplateRemovalTask extends MediaWikiTask {
private String template;
private String summary;
private Integer[] namespaces;
private boolean exclusionCompliant = false;
public TemplateRemovalTask(MediaWikiConnection con, String template, String summary, Integer...namespaces) {
super(con);
this.template = template;
this.summary = summary;
this.namespaces = namespaces;
}
@Override
public void run() {
try{
TemplateEmbeddedInRequest teir = new TemplateEmbeddedInRequest("Template:"+template, namespaces);
List<Article> articles = getConnection().request(teir);
for(Article a : articles) {
System.out.println(a.getTitle());
try{
String content = getConnection().request(new ContentRequest(a));
if(isExclusionCompliant() && getConnection().isBot() && ! getConnection().allowsBots(content)) {
System.err.println("no bot edits allowed");
continue;
}
content = content.replaceAll("\\{\\{\\s*"+Pattern.quote(template)+"[^\\}\\}]*\\}\\}\\n?", "");
String thisSummary = summary;
thisSummary = thisSummary.replaceAll("%article", a.getTitle());
getConnection().request(new EditRequest(a, content, thisSummary));
}catch(Exception e){
e.printStackTrace();
}
}
}catch(Exception e){
e.printStackTrace();
}
}
public boolean isExclusionCompliant() {
return exclusionCompliant;
}
public void setExclusionCompliant(boolean exclusionCompliant) {
this.exclusionCompliant = exclusionCompliant;
}
}