use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class ActionInsertPredefinedText method actionAddTemplate.
/**
* Action called when a template is selected to be added.
*
* @param templateName Template name.
*/
public void actionAddTemplate(String templateName) {
if ((templateName == null) || (pageProvider == null)) {
return;
}
Page page = pageProvider.getPage();
if (page == null) {
return;
}
String contents = pane.getText();
PageAnalysis analysis = page.getAnalysis(contents, false);
// Check that the template isn't already applied
if (analysis.hasTemplate(templateName) != null) {
return;
}
// Find where to add the template
int crBefore = 0;
int crAfter = 2;
int index = contents.length();
List<PageElementTemplate> templates = analysis.getTemplates();
if ((templates != null) && (!templates.isEmpty())) {
index = templates.get(0).getBeginIndex();
crAfter = 1;
int indexNewLine = contents.indexOf('\n');
if ((indexNewLine > 0) && (indexNewLine > index)) {
crBefore = 2;
}
} else {
List<PageElementCategory> categories = analysis.getCategories();
if ((categories != null) && (!categories.isEmpty())) {
index = categories.get(0).getBeginIndex();
} else {
List<PageElementLanguageLink> langLinks = analysis.getLanguageLinks();
if ((langLinks != null) && (!langLinks.isEmpty())) {
index = langLinks.get(0).getBeginIndex();
} else {
int indexNewLine = contents.indexOf('\n');
if (indexNewLine > 0) {
index = indexNewLine;
}
crBefore = 2;
crAfter = 0;
}
}
}
// Add the template
StringBuilder newContents = new StringBuilder();
if (index > 0) {
newContents.append(contents.substring(0, index));
}
for (int i = 0; i < crBefore; i++) {
newContents.append("\n");
}
newContents.append(TemplateBuilder.from(templateName).toString());
for (int i = 0; i < crAfter; i++) {
newContents.append("\n");
}
if (index < contents.length()) {
newContents.append(contents.substring(index));
}
pane.changeText(newContents.toString());
if (listener != null) {
listener.templateInserted(templateName);
}
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class ActionUpdateWarning method actionDisambiguationWarning.
/**
* Update disambiguation warnings on talk page.
*/
public void actionDisambiguationWarning() {
// Check selection
if (wikiProvider == null) {
return;
}
EnumWikipedia wiki = wikiProvider.getWiki();
if (wiki == null) {
return;
}
List<Page> pages = getPages();
if ((pages == null) || (pages.isEmpty())) {
return;
}
// Check configuration
WPCConfiguration wpcConfig = wiki.getConfiguration();
String template = wpcConfig.getString(WPCConfigurationString.DAB_WARNING_TEMPLATE);
if ((template == null) || (template.trim().length() == 0)) {
Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.DAB_WARNING_TEMPLATE.getAttributeName());
return;
}
// Ask for confirmation
int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the disambiguation warning on talk page?"));
if (answer != JOptionPane.YES_OPTION) {
return;
}
// Update warning
UpdateDabWarningWorker worker = new UpdateDabWarningWorker(wiki, window, pages, false, false, false, false);
worker.start();
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class ActionUpdateWarning method actionDuplicateArgsWarning.
/**
* Update duplicate arguments warnings on talk page.
*/
public void actionDuplicateArgsWarning() {
// Check selection
if (wikiProvider == null) {
return;
}
EnumWikipedia wiki = wikiProvider.getWiki();
if (wiki == null) {
return;
}
List<Page> pages = getPages();
if ((pages == null) || (pages.isEmpty())) {
return;
}
// Check configuration
WPCConfiguration wpcConfig = wiki.getConfiguration();
String template = wpcConfig.getString(WPCConfigurationString.DUPLICATE_ARGS_WARNING_TEMPLATE);
if ((template == null) || (template.trim().length() == 0)) {
Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.DUPLICATE_ARGS_WARNING_TEMPLATE.getAttributeName());
return;
}
// Ask for confirmation
int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the duplicate arguments warning on talk page?"));
if (answer != JOptionPane.YES_OPTION) {
return;
}
// Update warning
UpdateDuplicateArgsWarningWorker worker = new UpdateDuplicateArgsWarningWorker(wiki, window, pages, false, false);
worker.start();
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class ActionUpdateWarning method actionISBNWarning.
/**
* Update ISBN warnings on talk page.
*/
public void actionISBNWarning() {
// Check selection
if (wikiProvider == null) {
return;
}
EnumWikipedia wiki = wikiProvider.getWiki();
if (wiki == null) {
return;
}
List<Page> pages = getPages();
if ((pages == null) || (pages.isEmpty())) {
return;
}
// Check configuration
WPCConfiguration wpcConfig = wiki.getConfiguration();
String template = wpcConfig.getString(WPCConfigurationString.ISBN_WARNING_TEMPLATE);
if ((template == null) || (template.trim().length() == 0)) {
Utilities.displayMessageForMissingConfiguration(parent, WPCConfigurationString.ISBN_WARNING_TEMPLATE.getAttributeName());
return;
}
// Ask for confirmation
int answer = Utilities.displayYesNoWarning(parent, GT._T("Do you want to update the ISBN warning on talk page?"));
if (answer != JOptionPane.YES_OPTION) {
return;
}
// Update warning
UpdateISBNWarningWorker worker = new UpdateISBNWarningWorker(wiki, window, pages, false, false);
worker.start();
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class CheckErrorAlgorithm034 method analyze.
/**
* Analyze a page to check if errors are present.
*
* @param analysis Page analysis.
* @param errors Errors found in the page.
* @param onlyAutomatic True if analysis could be restricted to errors automatically fixed.
* @return Flag indicating if the error was found.
*/
@Override
public boolean analyze(PageAnalysis analysis, Collection<CheckErrorResult> errors, boolean onlyAutomatic) {
if (analysis == null) {
return false;
}
if (analysis.isInNamespace(Namespace.TEMPLATE)) {
return false;
}
// Check every position
Page page = analysis.getPage();
String contents = analysis.getContents();
int maxLen = contents.length();
boolean result = false;
int currentIndex = 0;
while (currentIndex < maxLen) {
int nextIndex = currentIndex;
if (contents.startsWith("__", currentIndex)) {
nextIndex = currentIndex + 2;
PageElementMagicWord magicWord = analysis.isInMagicWord(currentIndex);
if ((magicWord != null) && (magicWord.getBeginIndex() == currentIndex)) {
nextIndex = magicWord.getEndIndex();
MagicWordType magicWordType = magicWord.getMagicWord().getType();
if (SimpleMagicWordType.FORCE_TOC.equals(magicWordType) || SimpleMagicWordType.INDEX.equals(magicWordType) || SimpleMagicWordType.NO_INDEX.equals(magicWordType) || SimpleMagicWordType.NO_NEW_SECTION_LINK.equals(magicWordType)) {
result = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, magicWord.getBeginIndex(), magicWord.getEndIndex());
errorResult.addReplacement("");
errors.add(errorResult);
}
}
} else if (contents.startsWith("{{", currentIndex)) {
boolean done = false;
// Check for templates beginning with '{{{' instead of '{{'
if (!done && contents.startsWith("{{{", currentIndex)) {
PageElementTemplate currentTemplate = analysis.isInTemplate(currentIndex);
PageElementTemplate nextTemplate = analysis.isInTemplate(currentIndex + 1);
if ((nextTemplate != null) && (currentIndex + 1 == nextTemplate.getBeginIndex()) && ((currentTemplate == null) || (currentTemplate.getBeginIndex() < currentIndex - 1))) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, currentIndex, currentIndex + 3);
errorResult.addReplacement("{{");
errors.add(errorResult);
nextIndex = currentIndex + 3;
}
}
// Check for parameters
if (!done) {
PageElementParameter parameter = analysis.isInParameter(currentIndex);
if ((parameter != null) && (parameter.getBeginIndex() == currentIndex)) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, parameter.getBeginIndex(), parameter.getEndIndex());
if (parameter.getParameterCount() == 1) {
String value = parameter.getParameterValue(0);
if (value != null) {
errorResult.addReplacement(value);
}
}
errors.add(errorResult);
nextIndex = parameter.getEndIndex();
}
}
// Check for functions
if (!done) {
PageElementFunction function = analysis.isInFunction(currentIndex);
if ((function != null) && (function.getBeginIndex() == currentIndex)) {
MagicWord magicWord = function.getMagicWord();
MagicWordType magicWordType = magicWord.getType();
boolean isOk = false;
ErrorLevel errorLevel = ErrorLevel.ERROR;
if (FunctionMagicWordType.DEFAULT_SORT.equals(magicWordType) || FunctionMagicWordType.FORMAT_NUM.equals(magicWordType) || FunctionMagicWordType.DISPLAY_TITLE.equals(magicWordType)) {
isOk = true;
}
if (!isOk && FunctionMagicWordType.TAG.equals(magicWordType) && (function.getParameterCount() > 0) && (WikiTagType.REF.isPossibleName(function.getParameterValue(0)))) {
isOk = true;
}
if (!isOk) {
if (FunctionMagicWordType.INVOKE.equals(magicWordType) || FunctionMagicWordType.SAFE_SUBST.equals(magicWordType) || FunctionMagicWordType.SUBST.equals(magicWordType)) {
errorLevel = ErrorLevel.WARNING;
}
}
if (!isOk) {
result = true;
done = true;
if (errors == null) {
return true;
}
CheckErrorResult errorResult = createCheckErrorResult(analysis, function.getBeginIndex(), function.getEndIndex(), errorLevel);
if (FunctionMagicWordType.PAGE_NAME.equals(magicWordType)) {
errorResult.addReplacement(page.getTitle());
}
if (FunctionMagicWordType.IF_EXPR.equals(magicWordType)) {
for (int param = 1; param < function.getParameterCount(); param++) {
errorResult.addReplacement(function.getParameterValue(param));
}
}
if ((analysis.isInTag(currentIndex, WikiTagType.GALLERY) == null) && (analysis.isInTag(currentIndex, WikiTagType.INCLUDEONLY) == null) && (analysis.isInTag(currentIndex, WikiTagType.REF) == null) && (analysis.isInTag(currentIndex, WikiTagType.TIMELINE) == null) && (!FunctionMagicWordType.INVOKE.equals(magicWordType)) && (!FunctionMagicWordType.SUBST.equals(magicWordType)) && (!FunctionMagicWordType.SAFE_SUBST.equals(magicWordType))) {
errorResult.addReplacement("{{subst:" + contents.substring(function.getBeginIndex() + 2, function.getEndIndex()));
}
errors.add(errorResult);
nextIndex = function.getEndIndex();
} else {
nextIndex = currentIndex + 2;
}
}
}
}
currentIndex = Math.max(nextIndex, currentIndex + 1);
}
return result;
}
Aggregations