Jump to content

User:Haus/Hanzo/slpds

From Wikipedia, the free encyclopedia

A simple load-process-diff-save cycle in Beanshell looks something like this:

//Get one page, process, diff, 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.*;
import jdiff.*;
MwJedPlugin mw = MwJedPlugin.SELF;
reloadClasses("<unpackaged>");

//Log in
mwjed.getMediaWiki().login("wikipedia:en", 
                           "YOURNAME", 
                           "YOURPASSWORD", 
                           true);

//Define functions
public void setBufferText(Buffer b, String txt){
   int sz = b.getLength();
   b.remove(0,sz);
   b.insert(0,txt);
}

//Get the page and process
mw.openPage(view,"wikipedia:en:User:Haus/6",true);

//================================================
// Prep for diff
// Check if two panes to work with
v = view;
EditPane[] pns = v.getEditPanes();
int alen = java.lang.reflect.Array.getLength(pns);
if(alen<2){
     v.splitVertically();
}
if(!DualDiff.isEnabledFor(v)){
  DualDiff.toggleFor(v);
}

//================================================
// process the page
//== this is where the callback would start
//skip first line, mwjed uses it to store vms "uri"
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);


//================================================
// sort out the two panes and buffers
EditPane[] pns = v.getEditPanes();
EditPane pn0 = pns[0];
EditPane pn1 = pns[1];
if(pn0.getBuffer()==pn1.getBuffer()) {
   pn1.setBuffer(jEdit.newFile(view));
   print(true);
}
Buffer buf0=pn0.getBuffer();
Buffer buf1=pn1.getBuffer();


// put processed content into buffer
//Start compound edit
buf0.beginCompoundEdit();

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

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

//Put orig text into new buffer
setBufferText(buf1,orig);

DualDiff dualDiff = DualDiff.getDualDiffFor(v);
if(dualDiff!=null){
   DualDiff.refreshFor( v );
}

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