-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking change - Replaced MBot with more robust PTask class.
Also included: Cleaned up CAction, refactor for sanity, doc updates.
- Loading branch information
Showing
6 changed files
with
178 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.