User:Pseudomonas/AWBPerlWrapperPlugin

Note: This plugin would require modification to work with AWB Version 4.0+ because of changes to the IAWBPlugin interface.

This is a plugin for AWB designed to allow all the processing to be done in the wonderfully friendly and terse language of perl. It's not actually perl-specific, though - it'll run any external program, write the page to its standard input, and read in anything emitted by the standard output or standard error. It ought to be easy enough to produce an equivalent template in any other language.

The first line of the standard output is the status - if it's "CHANGE" the page will be edited, otherwise it'll be skipped. The second line is the edit summary. All the rest is the page contents.

The script must exit with an exit code of 0 otherwise it is deemed to have failed and its standard error will be displayed in a message box.

An alteration in the perl does not require AWB to be restarted.

The C bit:

// uses code from AutoWikiBrowser/Plugins. // Seems to work OK, but very much alpha kludgeware as of sept 06. using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WikiFunctions; using System.Windows.Forms; using System.Text.RegularExpressions; using WikiFunctions.Plugin; using System.Diagnostics; using System.ComponentModel; namespace ExamplePlugin {   public class ExamplePlugin : IAWBPlugin {       public event PluginEventHandler Diff; public event PluginEventHandler Preview; public event PluginEventHandler Save; public event PluginEventHandler Skip; public event PluginEventHandler Start; public event PluginEventHandler Stop; ToolStripMenuItem menuitem; public void Initialise(WikiFunctions.Lists.ListMaker list, WikiFunctions.Browser.WebControl web, ToolStripMenuItem tsmi, ContextMenuStrip cms, TabControl tab, Form frm, TextBox txt) {           //Make our new menu item menuitem = new ToolStripMenuItem(Name); //Set its check state to true menuitem.Checked = true; //Make it change its checked state on click menuitem.CheckOnClick = true; //Add it to the menu tsmi.DropDownItems.Add(menuitem); }       public string Name {           get { return "Perl Wrapper Plugin"; } }         public string ProcessArticle(string ArticleText, string ArticleTitle, int Namespace, ref string Summary, ref bool Skip) {           //If menu item is not checked, then return if (!menuitem.Checked) return ArticleText; //If not mainspace article then return if (Namespace != 0) return ArticleText; string[] perlResults = runPerl(ArticleText); string editSummary = perlResults[1]; string editAction = perlResults[0]; string newArticle = perlResults[2]; if (editAction.Equals("CHANGE")){ // System.Windows.Forms.MessageBox.Show(editAction); // System.Windows.Forms.MessageBox.Show(newArticle); ArticleText = newArticle; Summary = editSummary; }           else{ Skip = true; }           return ArticleText; }       public void ReadXML(System.Xml.XmlTextReader Reader) {           if (Reader.MoveToAttribute("enabled")) menuitem.Checked = bool.Parse(Reader.Value); }       public void WriteXML(System.Xml.XmlTextWriter Writer) {           //Write the attributes and values Writer.WriteAttributeString("enabled", menuitem.Checked.ToString); Writer.WriteAttributeString("anothersetting", "value"); }       public void Reset {           //set default settings menuitem.Checked = true; }       public static StringBuilder errorString = new StringBuilder(""); public static StringBuilder perlOut = new StringBuilder(""); public static string[] runPerl(string PageInput) {           errorString.Remove(0, errorString.Length); perlOut.Remove(0, perlOut.Length); System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo; psi.FileName = @"C:\perl\bin\wperl.exe"; //or wherever your perl is. psi.WindowStyle = ProcessWindowStyle.Minimized; psi.Arguments = String.Format("\"c:\\program files\\AutoWikiBrowser\\process.pl\" "); // adjust as necessary for your script psi.RedirectStandardOutput = true; psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; psi.UseShellExecute = false; psi.StandardOutputEncoding = Encoding.UTF8; psi.StandardErrorEncoding = Encoding.UTF8; psi.RedirectStandardError = true; psi.RedirectStandardInput = true; System.Diagnostics.Process perlProcess; perlProcess = System.Diagnostics.Process.Start(psi); perlProcess.ErrorDataReceived += new DataReceivedEventHandler(AppendErrorDataReceived); perlProcess.OutputDataReceived += new DataReceivedEventHandler(AppendOutDataReceived); perlProcess.BeginOutputReadLine; perlProcess.BeginErrorReadLine; System.IO.StreamWriter myInput = new System.IO.StreamWriter(perlProcess.StandardInput.BaseStream,Encoding.UTF8); myInput.Write(PageInput); myInput.Flush; myInput.Close; perlProcess.WaitForExit; if (perlProcess.HasExited && perlProcess.ExitCode == 0) {               System.Windows.Forms.MessageBox.Show("OK"); string perloutputstring = perlOut.ToString; int firstnl = perloutputstring.IndexOf("\n", 0); int secondnl = perloutputstring.IndexOf("\n", firstnl + 1); string[] output = new string[]{ perloutputstring.Substring(0,firstnl-1), perloutputstring.Substring(firstnl+1,secondnl-firstnl), perloutputstring.Substring(secondnl,perlOut.Length - secondnl) };               return output; }           else { System.Windows.Forms.MessageBox.Show(errorString.ToString); return new string[] { "FAILED", "", "" }; }       }        private static void AppendErrorDataReceived(object sendingProcess,             DataReceivedEventArgs errLine){ errorString.AppendLine(errLine.Data); }       private static void AppendOutDataReceived(object sendingProcess,             DataReceivedEventArgs outLine){ perlOut.AppendLine(outLine.Data); }      } }

The perl script:

use strict; use utf8; binmode( STDOUT, ':utf8' ); binmode( STDIN, ':utf8' ); binmode( STDERR, ':utf8' ); my $page; while (){$page .= $_}; my $action = "CHANGE"; #if it's not "CHANGE" no edit will be made my $editsummary = "This is what I did"; $page =~ s/aeiou//g; # Disemvowel the page. Don't use this live! $action =~ s/\n//g; print "$action\n"; $editsummary =~ s/\n//g; print "$editsummary\n"; print $page; exit 0;
 * 1)  $editsummary is changeable, naturally
 * 1)  $editsummary is changeable, naturally
 * 1) The business bit goes here
 * 1) The business bit goes here
 * 1) End of business bit