Jump to content

User:Haus/Hanzo/slps

From Wikipedia, the free encyclopedia

A simple load-process-save loop in BeanShell looks something like this:

//Get one page, process, and save it back

//================================================
// Startup, load everything
addClassPath(new URL("file:/C:/Program Files/jEdit/hanzo/"));
import net.psammead.mwjed.*;
import net.psammead.mwapi.*;
import net.psammead.mwapi.ui.*;
MwJedPlugin mw = MwJedPlugin.SELF;
reloadClasses("<unpackaged>");

//LOGGING IN FIRST
mwjed.getMediaWiki().login("wikipedia.en", 
                           "YOURNAME", 
                           "YOURPASSWORD", 
                           true);

//================================================
//Get the page and process
mw.openPage(view,"wikipedia:en:User:Haus/6",true);
//== this is where the callback would start
int sz = buffer.getLength();    // need to wait
//Get index of beginning of line #1
int offset = buffer.getLineStartOffset(1);
String orig = buffer.getText(offset,sz-offset);
String processed = ShipBox.parseString(orig);

//Start compound edit
buffer.beginCompoundEdit();

//Clean out buffer
int sz = buffer.getLength();
buffer.remove(offset,sz-offset);

//Put processed text back into the buffer
buffer.insert(offset,processed);

buffer.endCompoundEdit();

//================================================
// Prepare to save
int sz = buffer.getLength();    // need to wait
String curr = buffer.getText(0,sz);
Page p = mw.textToPage(curr);
mw.storeAndDisplay(view,
                   p,
                   "Migrating infobox with [[User:Haus/Hanzo|Hanzo]]",
                   false,false);