Skip to content

Commit

Permalink
Breaking change - Replaced MBot with more robust PTask class.
Browse files Browse the repository at this point in the history
Also included: Cleaned up CAction, refactor for sanity, doc updates.
  • Loading branch information
fastily committed Jan 31, 2016
1 parent 5ac0490 commit 15952fa
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 285 deletions.
130 changes: 33 additions & 97 deletions src/jwiki/core/CAction.java
Original file line number Diff line number Diff line change
@@ -1,147 +1,83 @@
package jwiki.core;

import static jwiki.core.MBot.Task;

import java.util.ArrayList;

import jwiki.util.FError;
import jwiki.util.FL;
import jwiki.util.PTask;
import jwiki.util.Tuple;

/**
* Contains methods to perform concurrent actions on a wiki. Most methods return a set of values indicating whether we
* were successful or not in performing the requested action(s).
* Static methods for performing concurrent actions on a Wiki.
*
* @author Fastily
*
*/
public final class CAction
{

/**
* Hiding from javadoc
* Constructors disallowed
*/
private CAction()
{

}

/**
* Deletes pages. Maximum concurrent threads = 20.
* Deletes pages concurrently. The account performing this task requires administrator rights.
*
* @param wiki The wiki object to use
* @param reason The log summary to use
* @param titles The page(s) to delete.
* @return A list of pages we didn't delete.
* @param wiki The Wiki object to use.
* @param reason The deletion log summary to use.
* @param titles The titles to delete.
* @return Pages which were not successfully deleted.
*/
public static ArrayList<String> delete(Wiki wiki, String reason, ArrayList<String> titles)
{
ColorLog.fyi(wiki, "Preparing to delete pages");
ArrayList<Task> tl = new ArrayList<>();
for (String s : titles)
tl.add(new Task(s, null, reason) {
public boolean doJob(Wiki wiki)
{
return WAction.delete(wiki, title, reason);
}
});
return Task.toString(wiki.submit(tl, 20));
return PTask.execute(FL.toAL(titles.stream().map(t -> new Tuple<>(t, () -> WAction.delete(wiki, t, reason)))));
}

/**
* Undelete pages. Maximum concurrent threads = 5.
* Undelete pages concurrently. The account performing this task requires administrator rights.
*
* @param wiki The wiki object to use
* @param agressive Set to true to attempt restoration 10 times before giving up. MediaWiki isn't very good at
* restoration.
* @param wiki The Wiki object to use
* @param agressive Set True to attempt restoration 10 times before giving up.
* @param reason The log reason to use
* @param titles The page(s) to delete
* @return A list of pages we didn't delete.
* @param titles The titles to restore.
* @return Pages which were not successfully restored.
*/
public static ArrayList<String> undelete(Wiki wiki, boolean agressive, String reason, ArrayList<String> titles)
{
ColorLog.fyi(wiki, "Preparing to restore pages");
ArrayList<Task> tl = new ArrayList<>();
for (String s : titles)
tl.add(new Task(s, null, reason) {
public boolean doJob(Wiki wiki)
{
return WAction.undelete(wiki, title, reason, agressive);
}
});
return Task.toString(wiki.submit(tl, 5));
return PTask.execute(FL.toAL(
MQuery.exists(wiki, false, titles).stream().map(t -> new Tuple<>(t, () -> WAction.undelete(wiki, t, reason, agressive)))));
}

/**
* Adds text to a file
* Edits pages to append or prepend some text concurrently.
*
* @param wiki The wiki object to use
* @param append Set to true to append text, set to false to prepend text. Newlines not automatically inserted.
* @param wiki The Wiki object to use
* @param append Set True to append text or False to prepend text. WARNING: Newline characters will not be
* automatically inserted.
* @param add The text to add
* @param reason The edit summary to use
* @param titles Pages to edit
* @return A list of titles we couldn't process.
* @param titles The pages to edit
* @return Pages which were not successfully edited.
*/
public static ArrayList<String> addText(Wiki wiki, boolean append, String add, String reason, ArrayList<String> titles)
{
return edit(wiki, append, reason, add, null, null, titles);
}

/**
* Replaces text on a page.
*
* @param wiki The wiki object to use
* @param replace The text to replace, as a regex.
* @param replacement The replacement text for anything matching <code>replace</code>.
* @param reason The edit summary to use
* @param titles Pages to edit
* @return A list of titles we couldn't process.
*/
public static ArrayList<String> replace(Wiki wiki, String replace, String replacement, String reason,
ArrayList<String> titles)
{
return edit(wiki, false, reason, null, replace, replacement, titles);
return PTask.execute(FL.toAL(titles.stream().map(t -> new Tuple<>(t, () -> WAction.addText(wiki, t, add, reason, append)))));
}

/**
* Edits a title. Allows for both add and/or replacement. Disable add and/or replace with null; you cannot disable
* both - you will get an error.
* Concurrently performs text replacement on pages.
*
* @param wiki The wiki object to use
* @param append Set to true to append text, set to false to prepend text. Newlines not automatically inserted. Param
* is ignored if <code>add</code> is null.
* @param wiki The Wiki object to use
* @param replace A regex matching the text on each page to replace.
* @param replacement The text to replace any text matching <code>replace</code>.
* @param reason The edit summary to use
* @param add The text to add. Optional param: set to null to disable.
* @param replace The text to replace, as a regex. Optional param: set to null to disable
* @param replacement The replacement text for anything matching <code>replace</code>. Ignored if replace is null.
* @param titles Pages to edit
* @return A list of titles we couldn't process.
* @param titles The pages to edit
* @return Pages which were not successfully edited.
*/
public static ArrayList<String> edit(Wiki wiki, boolean append, String reason, String add, String replace,
String replacement, ArrayList<String> titles)
public static ArrayList<String> replace(Wiki wiki, String replace, String replacement, String reason, ArrayList<String> titles)
{
ColorLog.fyi(wiki, "Preparing edit pages");
ArrayList<Task> tl = new ArrayList<>();
for (String s : titles)
tl.add(new Task(s, null, reason) {
public boolean doJob(Wiki wiki)
{
if (replace == null && add == null)
return FError.printErrAndRet(String.format("CAction: Add and replace in '%s' are null!. Skip.", title),
false);
else if (replace == null)
return WAction.addText(wiki, title, add, reason, append);
else
{
if (replacement == null)
return FError.printErrAndRet(
String.format("CAction: Replace OP requested but replacement in '%s' is null!. Skip.", title),
false);
text = wiki.getPageText(title).replaceAll(replace, replacement);
if (add != null)
text = append ? text + add : add + text;
return WAction.edit(wiki, title, text, reason, false);
}
}
});
return Task.toString(wiki.submit(tl, 3));
return PTask.execute(FL.toAL(titles.stream().map(t -> new Tuple<>(t, () -> wiki.replaceText(t, replace, replacement, reason)))));
}
}
140 changes: 0 additions & 140 deletions src/jwiki/core/MBot.java

This file was deleted.

7 changes: 6 additions & 1 deletion src/jwiki/core/SQ.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class SQ
*/
private String strMax = "max";

/**
* The <code>action</code> param to use for this SQ.
*/
protected String action = "query";

/**
* Constructor, sets a limit String and the overall maximum number of results returned in a [continuation] query.
*
Expand Down Expand Up @@ -108,7 +113,7 @@ protected SQ(Wiki wiki, HashMap<String, String> pl)
*/
private URLBuilder makeUB(String... params)
{
URLBuilder ub = wiki.makeUB("query", params);
URLBuilder ub = wiki.makeUB(action, params);
ub.setParams(pl);

if (limString != null)
Expand Down
Loading

0 comments on commit 15952fa

Please sign in to comment.