// Search "To do" template in the talk page
String contents = talkPage.getContents();
if (contents == null) {
contents = "";
}
PageAnalysis talkAnalysis = talkPage.getAnalysis(contents, true);
PageElementTemplate templateTodo = null;
List<String> todoTemplates = configuration.getStringList(WPCConfigurationStringList.TODO_TEMPLATES);
if ((todoTemplates == null) ||
(todoTemplates.isEmpty())) {
return false;
}
for (String todoTemplate : todoTemplates) {
List<PageElementTemplate> templates = talkAnalysis.getTemplates(todoTemplate);
PageElementTemplate templateTmp = (templates != null) && (templates.size() > 0) ?
templates.get(0) : null;
if (templateTmp != null) {
if ((templateTodo == null) || (templateTmp.getBeginIndex() < templateTodo.getBeginIndex())) {
templateTodo = templateTmp;
}
}
}
// If "To do" template is missing, add it
if (templateTodo == null) {
if (!createWarning) {
return false;
}
// Search where to add "To do" template
PageElementTemplate templatePrevious = null;
List<String> warningAfterTemplates = configuration.getStringList(
WPCConfigurationStringList.WARNING_AFTER_TEMPLATES);
if (warningAfterTemplates != null) {
for (String previousTemplate : warningAfterTemplates) {
Collection<PageElementTemplate> templates = talkAnalysis.getTemplates(previousTemplate);
for (PageElementTemplate templateTmp : templates) {
if ((templatePrevious == null) ||
(templateTmp.getEndIndex() > templatePrevious.getEndIndex())) {
templatePrevious = templateTmp;
}
}
}
}
int indexStart = (templatePrevious != null) ? templatePrevious.getEndIndex() : 0;
if ((indexStart == 0) && (talkPage.isRedirect())) {
indexStart = contents.length();
}
// Add warning
setText(getMessageUpdateWarning(talkPage.getTitle()));
StringBuilder tmp = new StringBuilder();
if (indexStart > 0) {
tmp.append(contents.substring(0, indexStart));
if (tmp.charAt(tmp.length() - 1) != '\n') {
tmp.append("\n");
}
}
tmp.append("{{");
tmp.append(todoTemplates.get(0));
tmp.append("|* ");
addWarning(tmp, pageRevId, elements);
tmp.append("}}");
if (indexStart < contents.length()) {
if (contents.charAt(indexStart) != '\n') {
tmp.append("\n");
}
tmp.append(contents.substring(indexStart));
}
String comment = wiki.formatComment(
getWarningComment(elements),
automaticEdit);
updateTalkPage(talkPage, tmp.toString(), comment);
// Inform creator and modifiers of the page
informContributors(analysis, elements, creator, modifiers);
return true;
}
// Search warning in the "To do" parameter
String parameter = templateTodo.getParameterValue("1");
PageAnalysis parameterAnalysis = talkPage.getAnalysis(parameter, false);
PageElementTemplate templateWarning = getFirstWarningTemplate(parameterAnalysis);
if (templateWarning == null) {
StringBuilder tmp = new StringBuilder();
int indexStart = templateTodo.getBeginIndex();
if (indexStart > 0) {