Search in sources :

Example 1 with WPCConfigurationString

use of org.wikipediacleaner.api.configuration.WPCConfigurationString in project wpcleaner by WPCleaner.

the class UpdateWarningTools method createMessage.

/**
 * Create a message that should be added on user talk page.
 *
 * @param article Article.
 * @param msgElements Message elements.
 * @param wpcConfig Configuration.
 * @param templateParam Parameter for the template used to inform.
 */
private String createMessage(String article, Collection<String> msgElements, WPCConfiguration wpcConfig, WPCConfigurationString templateParam) {
    String[] templateElements = wpcConfig.getStringArray(templateParam);
    if ((templateElements == null) || (templateElements.length == 0) || (templateElements[0].trim().length() == 0)) {
        return null;
    }
    TemplateBuilder builder = TemplateBuilder.from(templateElements[0].trim());
    if ((templateElements.length > 1) && (templateElements[1].trim().length() > 0)) {
        builder.addParam(templateElements[1].trim(), article);
    }
    if ((templateElements.length > 2) && (templateElements[2].trim().length() > 0)) {
        String wpcUser = wpcConfig.getString(WPCConfigurationString.USER);
        if ((wpcUser != null) && (wpcUser.trim().length() > 0)) {
            builder.addParam(templateElements[2].trim(), wpcUser);
        }
    }
    if (msgElements != null) {
        for (String msgElement : msgElements) {
            builder.addParam(msgElement);
        }
    }
    return builder.toString();
}
Also used : TemplateBuilder(org.wikipediacleaner.api.data.contents.template.TemplateBuilder) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString)

Example 2 with WPCConfigurationString

use of org.wikipediacleaner.api.configuration.WPCConfigurationString in project wpcleaner by WPCleaner.

the class UpdateWarningWorker method retrieveArticlesWithWarning.

/**
 * Retrieve pages with a warning on their talk page.
 *
 * @param templateNameProperty Property for the name of the warning template.
 * @param pages Map of (title,page) to complete.
 * @throws APIException Exception thrown by the API.
 */
protected void retrieveArticlesWithWarning(WPCConfigurationString templateNameProperty, Map<String, Page> pages) throws APIException {
    EnumWikipedia wiki = getWikipedia();
    WPCConfiguration configuration = wiki.getConfiguration();
    WikiConfiguration wikiConfiguration = wiki.getWikiConfiguration();
    API api = APIFactory.getAPI();
    // Retrieve talk pages including a warning
    String warningTemplateName = configuration.getString(templateNameProperty);
    if (warningTemplateName != null) {
        setText(GT._T("Retrieving talk pages including {0}", TemplateBuilder.from(warningTemplateName).toString()));
        String templateTitle = wikiConfiguration.getPageTitle(Namespace.TEMPLATE, warningTemplateName);
        Page warningTemplate = DataManager.createSimplePage(wiki, templateTitle, null, null, null);
        api.retrieveEmbeddedIn(wiki, warningTemplate, configuration.getEncyclopedicTalkNamespaces(), false);
        // Convert them to article pages
        setText(GT._T("Constructing list of articles with warning"));
        List<Page> talkPages = warningTemplate.getRelatedPages(Page.RelatedPages.EMBEDDED_IN);
        if (talkPages != null) {
            for (Page talkPage : talkPages) {
                Page page = null;
                if (talkPage.isArticle()) {
                    page = talkPage;
                } else {
                    String title = talkPage.getTitle();
                    String todoSubpage = configuration.getString(WPCConfigurationString.TODO_SUBPAGE);
                    if (title.endsWith("/" + todoSubpage)) {
                        title = title.substring(0, title.length() - 1 - todoSubpage.length());
                    }
                    Integer namespace = talkPage.getNamespace();
                    if (namespace != null) {
                        Namespace namespaceTalk = wikiConfiguration.getNamespace(namespace.intValue());
                        if (namespaceTalk != null) {
                            int colonIndex = title.indexOf(':');
                            if (colonIndex >= 0) {
                                title = title.substring(colonIndex + 1);
                            }
                            if (namespace != Namespace.MAIN_TALK) {
                                title = wikiConfiguration.getPageTitle(namespace - 1, title);
                            }
                        }
                    }
                    page = DataManager.createSimplePage(wiki, title, null, null, null);
                }
                addPage(page, pages);
            }
        }
    }
}
Also used : WikiConfiguration(org.wikipediacleaner.api.configuration.WikiConfiguration) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) API(org.wikipediacleaner.api.API) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Namespace(org.wikipediacleaner.api.data.Namespace)

Example 3 with WPCConfigurationString

use of org.wikipediacleaner.api.configuration.WPCConfigurationString in project wpcleaner by WPCleaner.

the class UpdateWarningWorker method retrieveInternalLinks.

/**
 * Retrieve internal links in a page.
 *
 * @param pageNameProperty Property for the name of the page.
 * @param pages Map of (title,page) to complete.
 * @throws APIException Exception thrown by the API.
 */
protected void retrieveInternalLinks(WPCConfigurationString pageNameProperty, Map<String, Page> pages) throws APIException {
    EnumWikipedia wiki = getWikipedia();
    WPCConfiguration configuration = wiki.getConfiguration();
    API api = APIFactory.getAPI();
    String pageName = configuration.getString(pageNameProperty);
    if (pageName != null) {
        Page page = DataManager.createSimplePage(wiki, pageName, null, null, null);
        api.retrieveLinks(wiki, page, Namespace.MAIN, null, false, false);
        List<Page> links = page.getLinks();
        if (links != null) {
            for (Page link : links) {
                addPage(link, pages);
            }
        }
    }
}
Also used : EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) API(org.wikipediacleaner.api.API) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration)

Example 4 with WPCConfigurationString

use of org.wikipediacleaner.api.configuration.WPCConfigurationString in project wpcleaner by WPCleaner.

the class HelpButton method actionHelp.

/**
 * Action called when Help button is pressed.
 */
public void actionHelp() {
    WPCConfigurationString attributeHelpURL = WPCConfigurationString.HELP_URL;
    String url = EnumWikipedia.EN.getConfiguration().getString(attributeHelpURL);
    if (wikiProvider != null) {
        EnumWikipedia wiki = wikiProvider.getWiki();
        if ((wiki != null) && (wiki.getConfiguration() != null) && (wiki.getConfiguration().getString(attributeHelpURL) != null)) {
            url = wiki.getConfiguration().getString(attributeHelpURL);
        }
    }
    if (Utilities.isDesktopSupported()) {
        Utilities.browseURL(url);
    } else {
        Utilities.displayUrlMessage(parentComponent, GT._T("You can read the help on {0} at the following URL:", Version.PROGRAM), url);
    }
}
Also used : EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString)

Example 5 with WPCConfigurationString

use of org.wikipediacleaner.api.configuration.WPCConfigurationString 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) {
    // 
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Configuration(org.wikipediacleaner.utils.Configuration) Page(org.wikipediacleaner.api.data.Page) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) Section(org.wikipediacleaner.api.data.Section) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration) Namespace(org.wikipediacleaner.api.data.Namespace)

Aggregations

WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)5 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)3 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)3 Page (org.wikipediacleaner.api.data.Page)3 API (org.wikipediacleaner.api.API)2 Namespace (org.wikipediacleaner.api.data.Namespace)2 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)2 APIException (org.wikipediacleaner.api.APIException)1 WikiConfiguration (org.wikipediacleaner.api.configuration.WikiConfiguration)1 Section (org.wikipediacleaner.api.data.Section)1 TemplateBuilder (org.wikipediacleaner.api.data.contents.template.TemplateBuilder)1 Configuration (org.wikipediacleaner.utils.Configuration)1