String signature = config.getString(null, ConfigurationValueString.SIGNATURE);
// Retrieve user talk page name
Namespace userTalkNS = wiki.getWikiConfiguration().getNamespace(Namespace.USER_TALK);
String userTalk = userTalkNS.getTitle() + ":" + user;
Page userTalkPage = DataManager.getPage(analysis.getWikipedia(), userTalk, null, null, null);
// Add message
try {
if (globalTitle != null) {
// Check if global title already exists in the talk page
List<Section> sections = api.retrieveSections(wiki, userTalkPage);
Section section = null;
if (sections != null) {
for (Section tmpSection : sections) {
if (globalTitle.equals(tmpSection.getLine())) {
section = tmpSection;
}
}
}
if (section == null) {
// Add the title
StringBuilder fullMessage = new StringBuilder();
if ((globalTemplate != null) && (globalTemplate.trim().length() > 0)) {
fullMessage.append("{{");
fullMessage.append(globalTemplate.trim());
fullMessage.append("}}\n");
if ((signature != null) && (signature.trim().length() > 0)) {
fullMessage.append(signature.trim());
fullMessage.append("\n\n");
}
}
if ((globalListTemplate != null) && (globalListTemplate.trim().length() > 0)) {
fullMessage.append("{{");
fullMessage.append(globalListTemplate.trim());
fullMessage.append("}}\n");
}
if (title != null) {
fullMessage.append("== ");
fullMessage.append(title);
fullMessage.append(" ==\n");
}
fullMessage.append(message);
api.addNewSection(wiki, userTalkPage, globalTitle, fullMessage.toString(), false);
} else {
// Add the message in the existing title
Integer revisionId = userTalkPage.getRevisionId();
api.retrieveSectionContents(wiki, userTalkPage, section.getIndex());
if (revisionId.equals(userTalkPage.getRevisionId())) {
StringBuilder fullMessage = new StringBuilder();
fullMessage.append(userTalkPage.getContents());
if (fullMessage.charAt(fullMessage.length() - 1) != '\n') {
fullMessage.append("\n");
}
fullMessage.append(message);
api.updateSection(wiki, userTalkPage, globalTitle, section.getIndex(), fullMessage.toString(), false);