use of org.wikipediacleaner.utils.Configuration in project wpcleaner by WPCleaner.
the class MediaWiki method replaceText.
/**
* Replace text in a list of pages.
*
* @param pages List of pages.
* @param replacements List of text replacements
* Key: Additional comments used for the modification.
* Value: Text replacements.
* @param wiki Wiki.
* @param comment Comment used for the modification.
* @param report (Out) Report of changes made.
* @param automaticCW Lit of CW fixing that should be done.
* @param forceCW List of CW fixing that should be done even if no automatic replacement was done.
* @param save True if modification should be saved.
* @param updateDabWarning True to update disambiguation warning.
* @param minor True if the modification should be tagged as minor.
* @param pauseAfterEachEdit True to pause after each edit.
* @param botFix True to apply bot fixes.
* @param parent Parent window.
* @return Count of modified pages.
* @throws APIException Exception thrown by the API.
*/
public int replaceText(Page[] pages, Map<String, List<AutomaticFixing>> replacements, EnumWikipedia wiki, String comment, ModificationReport report, Collection<CheckErrorAlgorithm> automaticCW, Collection<CheckErrorAlgorithm> forceCW, boolean save, boolean updateDabWarning, boolean minor, boolean pauseAfterEachEdit, boolean botFix, Component parent) throws APIException {
if ((pages == null) || (replacements == null) || (replacements.size() == 0)) {
return 0;
}
// Initialize page loading
Configuration config = Configuration.getConfiguration();
int nThreads = Math.max(config.getInt(null, ConfigurationValueInteger.INTERROG_THREAD), 1);
int currentPage = 0;
while ((currentPage < nThreads) && (currentPage < pages.length)) {
// TODO: withRedirects=false ?
retrieveContents(wiki, pages[currentPage], false, true, false, true, false);
// To release memory
pages[currentPage] = null;
currentPage++;
}
// Analyze pages
UpdateDabWarningTools dabWarnings = new UpdateDabWarningTools(wiki, null, false, false);
int count = 0;
final API api = APIFactory.getAPI();
StringBuilder details = new StringBuilder();
StringBuilder fullComment = new StringBuilder();
ModificationReport.Modification modification = null;
boolean stopRequested = false;
while (hasRemainingTask() && !shouldStop() && !stopRequested) {
Object result = getNextResult();
if (currentPage < pages.length) {
// TODO: withRedirects=false ?
retrieveContents(wiki, pages[currentPage], false, true, false, true, false);
// To release memory
pages[currentPage] = null;
currentPage++;
}
if ((result != null) && (result instanceof Page)) {
List<String> replacementsDone = new ArrayList<>();
Page page = (Page) result;
String oldContents = page.getContents();
if (oldContents != null) {
String newContents = oldContents;
details.setLength(0);
fullComment.setLength(0);
if (report != null) {
modification = new ModificationReport.Modification(page.getTitle());
}
// Apply automatic fixing
for (Entry<String, List<AutomaticFixing>> replacement : replacements.entrySet()) {
replacementsDone.clear();
String tmpContents = AutomaticFixing.apply(replacement.getValue(), newContents, replacementsDone);
if (!newContents.equals(tmpContents)) {
newContents = tmpContents;
// Update description
if (modification != null) {
for (String replacementDone : replacementsDone) {
modification.addModification(replacementDone);
}
}
// Memorize replacement
if ((replacement.getKey() != null) && (replacement.getKey().length() > 0)) {
if (details.length() > 0) {
details.append(", ");
}
details.append(replacement.getKey());
}
}
}
fullComment.append(wiki.createUpdatePageComment(comment, details.toString()));
// Apply automatic CW fixing if needed
if (automaticCW != null) {
// Apply fixing
List<AlgorithmError.Progress> usedAlgorithms = new ArrayList<>();
String tmpContents = AutomaticFormatter.tidyArticle(page, newContents, automaticCW, botFix, usedAlgorithms);
// Decide if modifications should be kept
boolean shouldKeep = (!oldContents.equals(newContents));
if (forceCW != null) {
for (AlgorithmError.Progress progress : usedAlgorithms) {
if (forceCW.contains(progress.algorithm)) {
shouldKeep = true;
}
}
}
// Keep modifications
if (shouldKeep) {
newContents = tmpContents;
if (!usedAlgorithms.isEmpty()) {
fullComment.append(" / ");
fullComment.append(wiki.getCWConfiguration().getComment(usedAlgorithms));
if (modification != null) {
for (AlgorithmError.Progress progress : usedAlgorithms) {
CheckErrorAlgorithm algorithm = progress.algorithm;
modification.addModification(algorithm.getShortDescriptionReplaced());
}
}
}
}
}
// Page contents has been modified
if (!oldContents.equals(newContents)) {
if (report != null) {
report.addModification(modification);
}
// Save page
setText(GT._T("Updating page {0}", page.getTitle()));
count++;
if (save && !stopRequested) {
try {
api.updatePage(wiki, page, newContents, fullComment.toString(), true, minor, false, false);
if (updateDabWarning) {
List<Page> tmpList = new ArrayList<>(1);
tmpList.add(page);
dabWarnings.updateWarning(tmpList, null, null, null);
}
if (pauseAfterEachEdit) {
int answer = Utilities.displayYesNoAllWarning(parent, GT._T("The page {0} has been modified.", page.getTitle()) + "\n" + GT._T("Do you want to continue?"));
switch(answer) {
case JOptionPane.YES_OPTION:
break;
case Utilities.YES_ALL_OPTION:
pauseAfterEachEdit = false;
break;
default:
stopRequested = true;
}
}
} catch (APIException e) {
EnumQueryResult error = e.getQueryResult();
if (report != null) {
report.addError(new ModificationReport.Error(page.getTitle(), error));
}
if (EnumQueryResult.PROTECTED_PAGE.equals(error)) {
System.err.println("Page " + page.getTitle() + " is protected.");
} else {
throw e;
}
}
}
}
}
}
}
block(true);
return count;
}
use of org.wikipediacleaner.utils.Configuration in project wpcleaner by WPCleaner.
the class MediaWikiController method getStaticExecutor.
/**
* @return The executor.
*/
private static synchronized ExecutorService getStaticExecutor() {
if (staticExecutor == null) {
Configuration config = Configuration.getConfiguration();
int nThreads = config.getInt(null, ConfigurationValueInteger.INTERROG_THREAD);
staticExecutor = new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(Integer.MAX_VALUE), new BasicThreadFactory.Builder().namingPattern("MW-%d").build());
}
return staticExecutor;
}
use of org.wikipediacleaner.utils.Configuration in project wpcleaner by WPCleaner.
the class ModificationReport method getReport.
/**
* @param wiki Wiki.
* @return Textual description of the report.
*/
public String getReport(EnumWikipedia wiki) {
StringBuilder result = new StringBuilder();
Configuration config = Configuration.getConfiguration();
boolean secured = config.getBoolean(null, ConfigurationValueBoolean.SECURE_URL);
// Report modifications
for (Modification modification : modifications) {
// Page
String title = modification.getTitle();
String formattedTitle = CompleteTagBuilder.from(HtmlTagType.A, title).addAttribute("href", wiki.getSettings().getURL(title, false, secured)).toString();
result.append(GT._T("Page {0}:", formattedTitle));
result.append("\n");
result.append(HtmlTagType.UL.getOpenTag());
result.append("\n");
// List of modifications
for (String replacement : modification.getModifications()) {
result.append(CompleteTagBuilder.from(HtmlTagType.LI, replacement.replaceAll("\\&", "&").replaceAll("\\<", "<"))).toString();
}
result.append(HtmlTagType.UL.getCloseTag());
result.append("\n");
}
// Report errors
if (errors.size() > 0) {
result.append("\n\n");
result.append(CompleteTagBuilder.from(HtmlTagType.FONT, GT._T("The following errors have occurred:")).addAttribute("color", "red").toString());
result.append(HtmlTagType.UL.getOpenTag());
result.append("\n");
for (Error error : errors) {
result.append(HtmlTagType.LI.getOpenTag());
String title = error.getTitle();
String formattedTitle = CompleteTagBuilder.from(HtmlTagType.A, title).addAttribute("href", wiki.getSettings().getURL(title, false, secured)).toString();
result.append(GT._T("Page {0}:", formattedTitle));
result.append(" ");
result.append(error.getError().getCode());
result.append(HtmlTagType.LI.getCloseTag());
result.append("\n");
}
result.append(HtmlTagType.UL.getCloseTag());
result.append("\n");
}
return result.toString();
}
use of org.wikipediacleaner.utils.Configuration in project wpcleaner by WPCleaner.
the class Bot method executeSet.
/**
* Execute an action of type Set.
*
* @param actionConfig Parameters of the action.
* @return True if the action was executed.
*/
private boolean executeSet(Action actionConfig) {
String[] actionArgs = actionConfig.actionArgs;
if (actionArgs.length < 1) {
return false;
}
String parameter = actionArgs[0];
// Set AdditionalAlgorithms
if ("AdditionalAlgorithms".equalsIgnoreCase(parameter)) {
additionalAlgorithms.clear();
if (actionArgs.length > 1) {
extractAlgorithms(additionalAlgorithms, null, actionArgs, 1);
}
return true;
}
// Set Configuration
if ("Configuration".equalsIgnoreCase(parameter)) {
if (actionArgs.length > 2) {
Configuration config = Configuration.getConfiguration();
config.forceValue(actionArgs[1], actionArgs[2]);
for (CheckErrorAlgorithm algorithm : CheckErrorAlgorithms.getAlgorithms(wiki)) {
algorithm.setConfiguration(wiki.getWikiConfiguration(), wiki.getCWConfiguration(), wiki.getConfiguration());
}
}
return true;
}
// Set Namespaces
if ("Namespaces".equalsIgnoreCase(parameter)) {
namespaces = new HashSet<>();
int currentArg = 1;
while (currentArg < actionArgs.length) {
try {
namespaces.add(Integer.valueOf(actionArgs[currentArg]));
} catch (NumberFormatException e) {
log.warn("Incorrect namespace {}", actionArgs[currentArg]);
}
currentArg++;
}
return true;
}
// Set Prefix
if ("Prefix".equalsIgnoreCase(parameter)) {
if (actionArgs.length > 1) {
CommentManager.addExtraText(actionArgs[1].replaceAll("_", " "));
}
return true;
}
// Set Range
if ("RangeBegin".equalsIgnoreCase(parameter)) {
rangeBegin = null;
if (actionArgs.length > 1) {
rangeBegin = actionArgs[1];
}
return true;
}
if ("RangeEnd".equalsIgnoreCase(parameter)) {
rangeEnd = null;
if (actionArgs.length > 1) {
rangeEnd = actionArgs[1];
}
return true;
}
// Set Time limit
if ("TimeLimit".equalsIgnoreCase(parameter) && (actionArgs.length > 1)) {
try {
timeLimit = System.currentTimeMillis() + 1000 * Long.parseLong(actionArgs[1]);
} catch (NumberFormatException e) {
log.warn("Incorrect time limit {}", actionArgs[1]);
}
return true;
}
// Set Typo groups
if ("TypoGroups".equalsIgnoreCase(parameter) && (actionArgs.length > 1)) {
for (int numArg = 1; numArg < actionArgs.length; numArg++) {
typoGroups.add(actionArgs[numArg]);
}
return true;
}
return false;
}
use of org.wikipediacleaner.utils.Configuration in project wpcleaner by WPCleaner.
the class DumpDownloader method main.
/**
* @param args Command line arguments.
*/
public static void main(String[] args) {
log.info("Running dump downloader");
// Various configuration
Configuration config = Configuration.getConfiguration();
EnumLanguage language = config.getLanguage();
Locale.setDefault(language.getLocale());
GT.setCurrentLanguage(config.getLanguage());
// Analyze command line arguments
int currentArg = 0;
// Retrieve wiki code
String wikiCode = null;
if (args.length > currentArg) {
wikiCode = args[currentArg];
currentArg++;
}
// Retrieve HTTP server
String server = null;
if (args.length > currentArg) {
server = args[currentArg];
currentArg++;
}
// Retrieve base directory on server
String baseDir = null;
if (args.length > currentArg) {
baseDir = args[currentArg];
currentArg++;
}
// Retrieve local directory
String localDir = null;
if (args.length > currentArg) {
localDir = args[currentArg];
currentArg++;
}
// File to download
String fileType = "pages-articles.xml.bz2";
if (args.length > currentArg) {
fileType = args[currentArg];
currentArg++;
}
System.exit(downloadDump(wikiCode, server, baseDir, localDir, fileType));
}
Aggregations