use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class PageAnalysis method countLinks.
/**
* Count number of links in the page.
*
* @param links Links.
*/
public void countLinks(List<Page> links) {
if ((links == null) || (links.size() == 0)) {
return;
}
List<Page> interestingLinks = new ArrayList<>();
for (Page link : links) {
if (!linksCount.containsKey(link.getTitle())) {
interestingLinks.add(link);
}
}
if (interestingLinks.size() > 0) {
InternalLinkCounter counter = new InternalLinkCounter(linksCount, interestingLinks);
PageAnalysisUtils.findInternalLinks(this, interestingLinks, counter);
}
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class TextProviderImageDescription method getTexts.
/**
* @return Possible texts.
*/
@Override
public Collection<String> getTexts() {
Collection<String> result = new ArrayList<>();
if (image != null) {
try {
API api = APIFactory.getAPI();
// Retrieve image descriptions
Page imagePage = DataManager.createSimplePage(image.getWiki(), image.getNamespace() + ":" + image.getImage(), null, null, Namespace.IMAGE);
api.retrieveContents(image.getWiki(), Collections.singletonList(imagePage), false, false);
// Use image description on the wiki
if (Boolean.TRUE.equals(imagePage.isExisting())) {
PageAnalysis pageAnalysis = imagePage.getAnalysis(imagePage.getContents(), true);
for (PageElementTemplate template : pageAnalysis.getTemplates()) {
if (Page.areSameTitle("Information", template.getTemplateName())) {
String description = template.getParameterValue("Description");
if ((description != null) && (description.trim().length() > 0)) {
result.add(description.trim());
}
}
}
}
// Retrieve image description on Commons
Page commonsPage = DataManager.createSimplePage(EnumWikipedia.COMMONS, "File:" + image.getImage(), null, null, Namespace.IMAGE);
api.retrieveContents(EnumWikipedia.COMMONS, Collections.singletonList(commonsPage), false, false);
if (Boolean.TRUE.equals(commonsPage.isExisting())) {
PageAnalysis pageAnalysis = commonsPage.getAnalysis(commonsPage.getContents(), true);
for (PageElementTemplate template : pageAnalysis.getTemplates()) {
if (Page.areSameTitle("Information", template.getTemplateName())) {
String global = template.getParameterValue("Description");
if ((global != null) && (global.trim().length() > 0)) {
PageAnalysis descAnalysis = commonsPage.getAnalysis(global, true);
for (PageElementTemplate template2 : descAnalysis.getTemplates()) {
if (Page.areSameTitle(image.getWiki().getSettings().getCode(), template2.getTemplateName())) {
String description = template2.getParameterValue("1");
if ((description != null) && (description.trim().length() > 0)) {
result.add(description.trim());
}
}
}
}
}
}
}
} catch (APIException e) {
//
}
}
return result;
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class UpdateUnknownParameterWarningWorker method construct.
/* (non-Javadoc)
* @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
*/
@Override
public Object construct() {
long startTime = System.currentTimeMillis();
EnumWikipedia wiki = getWikipedia();
int lastCount = 0;
Stats stats = new Stats();
UpdateUnknownParameterWarningTools tools = new UpdateUnknownParameterWarningTools(wiki, this, true, automaticEdit);
tools.setUsePurge(false);
try {
if (!useList) {
listWarningPages(tools);
// Ask for confirmation
if (getWindow() != null) {
int answer = getWindow().displayYesNoWarning(GT._T("Analysis found {0} articles to check for unknown parameter errors.\n" + "Do you want to update the warnings ?", Integer.valueOf(warningPages.size()).toString()));
if (answer != JOptionPane.YES_OPTION) {
return Integer.valueOf(0);
}
}
// Sort the list of articles
Collections.sort(warningPages, PageComparator.getTitleFirstComparator());
if (warningPages.isEmpty()) {
return Integer.valueOf(0);
}
}
// Working with sublists
tools.setContentsAvailable(contentsAvailable);
tools.prepareErrorsMap();
if (simulation) {
tools.setSimulation(true);
}
String lastTitle = null;
while (!warningPages.isEmpty()) {
// Creating sublist
List<Page> sublist = tools.extractSublist(warningPages, 10, false);
if (sublist.isEmpty()) {
displayStats(stats, startTime);
return Integer.valueOf(stats.getUpdatedPagesCount());
}
// Update warning
boolean finish = false;
while (!finish) {
finish = true;
try {
lastTitle = sublist.get(sublist.size() - 1).getTitle();
tools.updateWarning(sublist, null, null, stats);
} catch (APIException e) {
if (getWindow() != null) {
int answer = getWindow().displayYesNoWarning(GT._T("An error occurred when updating unknown parameter warnings. Do you want to continue ?\n\n" + "Error: {0}", e.getMessage()));
if (answer != JOptionPane.YES_OPTION) {
return e;
}
finish = false;
}
}
if (shouldStop()) {
Configuration config = Configuration.getConfiguration();
config.setString(null, ConfigurationValueString.LAST_UNKNOWN_PARAMETER_WARNING, lastTitle);
displayStats(stats, startTime);
return Integer.valueOf(stats.getUpdatedPagesCount());
}
}
if (stats.getUpdatedPagesCount() > lastCount) {
lastCount = stats.getUpdatedPagesCount();
/*if (getWindow() != null) {
int answer = getWindow().displayYesNoWarning(
"This feature is currently under development, please check the modification.\n" +
"Do you want to continue ?");
if (answer != JOptionPane.YES_OPTION) {
return Integer.valueOf(lastCount);
}
} else {
return Integer.valueOf(lastCount);
}*/
}
}
if (warningPages.isEmpty()) {
Configuration config = Configuration.getConfiguration();
config.setString(null, ConfigurationValueString.LAST_UNKNOWN_PARAMETER_WARNING, (String) null);
}
} catch (APIException e) {
return e;
}
displayStats(stats, startTime);
return Integer.valueOf(stats.getUpdatedPagesCount());
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class UpdateWarningTools method manageNonEncyclopedicPages.
/**
* Manage talk pages present in the list.
*
* @param pages List of pages.
* @throws APIException Exception thrown by the API.
*/
private void manageNonEncyclopedicPages(List<Page> pages) throws APIException {
if (pages == null) {
return;
}
Iterator<Page> itPage = pages.iterator();
List<Integer> encyclopedicNamespaces = configuration.getEncyclopedicNamespaces();
while (itPage.hasNext()) {
Page page = itPage.next();
if (!page.isArticle() || !encyclopedicNamespaces.contains(page.getNamespace())) {
itPage.remove();
if (!simulation) {
PageAnalysis analysis = page.getAnalysis(page.getContents(), true);
Collection<String> elements = constructWarningElements(analysis, null, null);
if ((elements == null) || (elements.isEmpty())) {
purgePage(page);
}
}
}
}
}
use of org.wikipediacleaner.api.data.Page in project wpcleaner by WPCleaner.
the class UpdateWarningTools method addMessage.
/**
* Add a message on user talk page.
*
* @param analysis Page analysis.
* @param msgElements Message elements.
* @param user User to inform.
* @param titleParam Parameter for the title of the new section.
* @param templateParam Parameter for the template used to inform.
*/
private void addMessage(PageAnalysis analysis, Collection<String> msgElements, String user, WPCConfigurationString titleParam, WPCConfigurationString templateParam) {
if ((analysis == null) || (user == null)) {
return;
}
String article = analysis.getPage().getTitle();
WPCConfiguration wpcConfig = analysis.getWPCConfiguration();
// Prepare elements
String message = createMessage(article, msgElements, wpcConfig, templateParam);
if ((message == null) || (message.trim().length() == 0)) {
return;
}
String globalListTemplate = wpcConfig.getString(WPCConfigurationString.MSG_GLOBAL_LIST_TEMPLATE);
String globalTemplate = wpcConfig.getString(WPCConfigurationString.MSG_GLOBAL_TEMPLATE);
String globalTitle = wpcConfig.getString(WPCConfigurationString.MSG_GLOBAL_TITLE);
String title = wpcConfig.getString(titleParam);
if (title != null) {
try {
title = MessageFormat.format(title, article);
} catch (IllegalArgumentException e) {
log.warn("Parameter " + titleParam.getAttributeName() + " has an incorrect format");
}
}
Configuration config = Configuration.getConfiguration();
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.createSimplePage(analysis.getWikipedia(), userTalk, null, null, Namespace.USER_TALK);
// 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(TemplateBuilder.from(globalTemplate.trim()).toString());
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(TemplateBuilder.from(globalListTemplate.trim()).toString());
fullMessage.append("\n");
}
if (title != null) {
fullMessage.append("== ");
fullMessage.append(title);
fullMessage.append(" ==\n");
}
fullMessage.append(message);
api.addNewSection(wiki, userTalkPage, globalTitle, fullMessage.toString(), true, true, automaticEdit, 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(), true, true, automaticEdit, false);
} else {
System.err.println("Page " + userTalk + " has been modified between two requests");
}
}
} else {
if (title != null) {
api.addNewSection(wiki, userTalkPage, title, message, true, true, automaticEdit, false);
} else {
// TODO: No global title, no title => Should append the message at the end
log.warn("Should add " + message + " in " + userTalk);
}
}
} catch (APIException e) {
//
}
}
Aggregations