Search in sources :

Example 6 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class UpdateWarningWorker method retrieveCheckWikiPages.

/**
 * Retrieve pages for a given error number.
 *
 * @param errorNumber Error number.
 * @param pages Map of (title,page) to complete.
 * @param tools Update warning tools if the pages should be added as articles.
 */
protected void retrieveCheckWikiPages(int errorNumber, Map<String, Page> pages, UpdateWarningTools tools) {
    CheckWiki cw = APIFactory.getCheckWiki();
    EnumWikipedia wiki = getWikipedia();
    CheckErrorAlgorithm algorithm = CheckErrorAlgorithms.getAlgorithm(wiki, errorNumber);
    List<AlgorithmError> errors = new ArrayList<>();
    try {
        cw.retrievePages(algorithm, 100000, wiki, errors);
        for (AlgorithmError error : errors) {
            for (int pageNum = 0; pageNum < error.getPageCount(); pageNum++) {
                Page page = error.getPage(pageNum);
                addPage(page, pages);
                if (tools != null) {
                    tools.addArticle(page.getTitle());
                }
            }
        }
    } catch (APIException e) {
    // Nothing
    }
}
Also used : APIException(org.wikipediacleaner.api.APIException) CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) ArrayList(java.util.ArrayList) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) AlgorithmError(org.wikipediacleaner.api.algorithm.AlgorithmError) CheckWiki(org.wikipediacleaner.api.check.CheckWiki)

Example 7 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class TranslateWorker method construct.

/**
 * @return Translated text.
 * @see org.wikipediacleaner.gui.swing.basic.BasicWorker#construct()
 */
@Override
public Object construct() {
    String text = initialText;
    try {
        Configuration config = Configuration.getConfiguration();
        text = translateInternalLinks(text, config.getBoolean(null, ConfigurationValueBoolean.TRANSLATION_INTERNAL_LINK_TEXT), config.getBoolean(null, ConfigurationValueBoolean.TRANSLATION_INTERLANGUAGE));
        text = translateCategories(text, config.getBoolean(null, ConfigurationValueBoolean.TRANSLATION_CATEGORY));
        text = translateTemplates(text, config.getBoolean(null, ConfigurationValueBoolean.TRANSLATION_TEMPLATE_NAME), config.getBoolean(null, ConfigurationValueBoolean.TRANSLATION_TEMPLATE_NO_PARAM));
    } catch (APIException e) {
        return null;
    }
    return text;
}
Also used : APIException(org.wikipediacleaner.api.APIException) Configuration(org.wikipediacleaner.utils.Configuration)

Example 8 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class UpdateISSNWarningWorker method displayResult.

/**
 * Display results.
 *
 * @param stats Statistics.
 * @param startTime Start time.
 * @param errors Errors found.
 */
private void displayResult(Stats stats, long startTime, Map<String, List<String>> errors) {
    if (useList) {
        return;
    }
    // Errors
    if (errors != null) {
        // Configuration
        EnumWikipedia wiki = getWikipedia();
        // Compute synthesis
        StringBuilder buffer = new StringBuilder();
        List<String> keys = new ArrayList<>(errors.keySet());
        Collections.sort(keys);
        for (String key : keys) {
            List<String> values = errors.get(key);
            buffer.append("* ");
            if (values != null) {
                buffer.append(values.size());
                buffer.append(" x ");
            }
            buffer.append("ISSN ");
            buffer.append(key);
            buffer.append(" : ");
            if (values != null) {
                Collections.sort(values);
                int valueNum = 0;
                while (valueNum < values.size()) {
                    if (valueNum > 0) {
                        buffer.append(", ");
                    }
                    String value = values.get(valueNum);
                    int begin = valueNum;
                    while ((valueNum < values.size()) && (values.get(valueNum).equals(value))) {
                        valueNum++;
                    }
                    if (valueNum > begin + 1) {
                        buffer.append(valueNum - begin);
                        buffer.append(" x ");
                    }
                    buffer.append("[[");
                    buffer.append(value);
                    buffer.append("]]");
                }
            }
            buffer.append("\n");
        }
        // Update synthesis on dedicated page
        WPCConfiguration config = wiki.getConfiguration();
        String pageName = config.getString(WPCConfigurationString.ISSN_ERRORS_PAGE);
        boolean saved = false;
        if ((pageName != null) && (pageName.trim().length() > 0)) {
            boolean updatePage = false;
            if (simulation && (getWindow() != null)) {
                int answer = Utilities.displayYesNoWarning(getWindow().getParentComponent(), GT._T("Do you want to update {0}?", pageName));
                if (answer == JOptionPane.YES_OPTION) {
                    updatePage = true;
                }
            } else {
                updatePage = true;
            }
            if (updatePage) {
                try {
                    Page page = DataManager.createSimplePage(wiki, pageName, null, null, null);
                    API api = APIFactory.getAPI();
                    api.retrieveContents(wiki, Collections.singletonList(page), false, false);
                    String contents = page.getContents();
                    if (contents != null) {
                        int begin = -1;
                        int end = -1;
                        for (ContentsComment comment : page.getAnalysis(contents, true).comments().getAll()) {
                            String value = comment.getComment().trim();
                            if ("BOT BEGIN".equals(value)) {
                                if (begin < 0) {
                                    begin = comment.getEndIndex();
                                }
                            } else if ("BOT END".equals(value)) {
                                end = comment.getBeginIndex();
                            }
                        }
                        if ((begin >= 0) && (end > begin)) {
                            StringBuilder newText = new StringBuilder();
                            newText.append(contents.substring(0, begin));
                            newText.append("\n");
                            newText.append(buffer.toString());
                            newText.append(contents.substring(end));
                            api.updatePage(wiki, page, newText.toString(), config.getString(WPCConfigurationString.ISSN_ERRORS_PAGE_COMMENT), false, true, true, false);
                            saved = true;
                        }
                    }
                } catch (APIException e) {
                // Nothing
                }
            }
        }
        // Display synthesis
        if (!saved && (getWindow() != null)) {
            InformationWindow.createInformationWindow("ISSN", buffer.toString(), false, getWikipedia());
        }
    }
    // Statistics
    displayStats(stats, startTime);
}
Also used : ContentsComment(org.wikipediacleaner.api.data.contents.comment.ContentsComment) APIException(org.wikipediacleaner.api.APIException) ArrayList(java.util.ArrayList) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) Page(org.wikipediacleaner.api.data.Page) API(org.wikipediacleaner.api.API) WPCConfigurationString(org.wikipediacleaner.api.configuration.WPCConfigurationString) ConfigurationValueString(org.wikipediacleaner.utils.ConfigurationValueString) WPCConfiguration(org.wikipediacleaner.api.configuration.WPCConfiguration)

Example 9 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class CheckWikiProjectWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.utils.SwingWorker#construct()
   */
@Override
public Object construct() {
    // Retrieving errors
    boolean errorLoaded = false;
    APIException exception = null;
    if (selectedAlgorithms != null) {
        for (final CheckErrorAlgorithm algorithm : selectedAlgorithms) {
            try {
                if ((algorithm != null) && (algorithm.isAvailable()) && (algorithm.getPriority() != CWConfigurationError.PRIORITY_BOT_ONLY)) {
                    // Retrieving list of pages for the error number
                    setText(GT._T("Checking for errors n°{0}", Integer.toString(algorithm.getErrorNumber())) + " - " + algorithm.getShortDescriptionReplaced());
                    APIFactory.getCheckWiki().retrievePages(algorithm, errorLimit, getWikipedia(), errors);
                    errorLoaded = true;
                }
            } catch (APIException e) {
                exception = e;
            }
        }
    }
    if (!errorLoaded && (exception != null)) {
        return exception;
    }
    // Sorting errors by priority
    setText(GT._T("Sorting errors by priority"));
    Collections.sort(errors, new AlgorithmErrorComparator());
    return null;
}
Also used : APIException(org.wikipediacleaner.api.APIException) CheckErrorAlgorithm(org.wikipediacleaner.api.check.algorithm.CheckErrorAlgorithm) AlgorithmErrorComparator(org.wikipediacleaner.api.algorithm.AlgorithmErrorComparator)

Example 10 with APIException

use of org.wikipediacleaner.api.APIException in project wpcleaner by WPCleaner.

the class FullAnalysisWorker method construct.

/* (non-Javadoc)
   * @see org.wikipediacleaner.gui.swing.utils.SwingWorker#construct()
   */
@Override
public Object construct() {
    try {
        MediaWiki mw = MediaWiki.getMediaWikiAccess(this);
        final API api = APIFactory.getAPI();
        EnumWikipedia wiki = getWikipedia();
        mw.retrieveContents(wiki, page, false, false, false, true, false);
        api.retrieveLinks(wiki, page, Namespace.MAIN, knownPages, true, true);
        // Retrieve disambiguation information if not already retrieved
        List<Page> links = new ArrayList<>();
        for (Page link : page.getLinks()) {
            if (link.isDisambiguationPage() == null) {
                links.add(link);
            }
        }
        if (!links.isEmpty()) {
            mw.retrieveDisambiguationInformation(wiki, links, knownPages, true, false, true);
        }
        // Retrieve more information on disambiguation pages
        for (Page link : page.getLinks()) {
            if (Boolean.TRUE.equals(link.isDisambiguationPage())) {
                Iterator<Page> itLink = link.getRedirects().getIteratorWithPage();
                while (itLink.hasNext()) {
                    Page link2 = itLink.next();
                    if (!link2.getRedirects().isRedirect()) {
                        mw.retrieveAllLinks(wiki, link2, null, knownPages, false, false);
                    }
                    if (link.hasWiktionaryTemplate() && (link.getContents() == null)) {
                        mw.retrieveContents(wiki, link2, false, false, false, true, false);
                    }
                }
            }
        }
        if (CheckErrorAlgorithms.isAlgorithmActive(wiki, 508)) {
            mw.retrieveAllTemplates(wiki, page, false);
        }
        mw.block(true);
        if (Boolean.FALSE.equals(page.isExisting())) {
            mw.retrieveSimilarPages(wiki, page);
        }
        setText("Analyzing data");
        PageAnalysis analysis = page.getAnalysis(page.getContents(), true);
        AlgorithmError.analyzeErrors(algorithms, analysis, false);
    } catch (APIException e) {
        return e;
    }
    return null;
}
Also used : APIException(org.wikipediacleaner.api.APIException) ArrayList(java.util.ArrayList) EnumWikipedia(org.wikipediacleaner.api.constants.EnumWikipedia) PageAnalysis(org.wikipediacleaner.api.data.analysis.PageAnalysis) API(org.wikipediacleaner.api.API) Page(org.wikipediacleaner.api.data.Page) MediaWiki(org.wikipediacleaner.api.MediaWiki)

Aggregations

APIException (org.wikipediacleaner.api.APIException)128 Page (org.wikipediacleaner.api.data.Page)70 API (org.wikipediacleaner.api.API)42 Element (org.jdom2.Element)41 JDOMException (org.jdom2.JDOMException)37 ArrayList (java.util.ArrayList)36 EnumWikipedia (org.wikipediacleaner.api.constants.EnumWikipedia)15 IOException (java.io.IOException)12 WPCConfigurationString (org.wikipediacleaner.api.configuration.WPCConfigurationString)12 Configuration (org.wikipediacleaner.utils.Configuration)11 ConfigurationValueString (org.wikipediacleaner.utils.ConfigurationValueString)11 WPCConfiguration (org.wikipediacleaner.api.configuration.WPCConfiguration)10 MediaWiki (org.wikipediacleaner.api.MediaWiki)9 PageAnalysis (org.wikipediacleaner.api.data.analysis.PageAnalysis)9 HashMap (java.util.HashMap)8 EnumQueryPage (org.wikipediacleaner.api.constants.EnumQueryPage)7 BufferedInputStream (java.io.BufferedInputStream)6 InputStream (java.io.InputStream)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 Header (org.apache.commons.httpclient.Header)6