Jump to content

User:Teslaton/Test

From Wikipedia, the free encyclopedia

AWB custom module to allow external script filters with the following features:

  • summary files
  • command line template, to allow sending extra arguments to external script. Most useful are: article title (URL escaped, to allow safe transfer of UTF strings) and article namespace
  • custom control of child process creation details (e.g. minimized or no window at all)


        // Filter path and basename:
        public const string path = @"X:\Wikipedia\Bot\AWB projects\201503-astro";
        public const string name = "filter";

        public string contentIoFile = path + "\\" + name + ".io.txt";
        public string summaryIoFile = path + "\\" + name + ".io.summary.txt";
        public string scriptFile    = path + "\\" + name + ".cmd";
        public string paramsTemplate = "\"%%file%%\" \"%%summaryFile%%\" \"%%titleHex%%\" %%namespace%%";

        public string ProcessArticle(string articleText, string articleTitle, int wikiNamespace, out string summary, out bool skip)
        {
          string origText = articleText;
          skip = false;
          summary = "";
          // summary = "test";
          try {
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
            psi.WorkingDirectory = System.IO.Path.GetDirectoryName(scriptFile);
            psi.FileName = System.IO.Path.GetFileName(scriptFile);
            psi.Arguments = paramsTemplate
              .Replace("%%file%%", contentIoFile)
              .Replace("%%summaryFile%%", summaryIoFile)
              .Replace("%%titleHex%%", System.Web.HttpUtility.UrlEncode(articleTitle, System.Text.Encoding.UTF8).Replace("+", "%20").Replace("%", "\\x"))  // hex (C) slashed UTF-8 title
              .Replace("%%namespace%%", wikiNamespace.ToString());
            psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            if (contentIoFile.Contains("\\"))
              WikiFunctions.Tools.WriteTextFileAbsolutePath(articleText, contentIoFile, false);
            else
              WikiFunctions.Tools.WriteTextFile(articleText, contentIoFile, false);
            if (summaryIoFile != null && summaryIoFile != "")
              if (summaryIoFile.Contains("\\"))
                WikiFunctions.Tools.WriteTextFileAbsolutePath("", summaryIoFile, false);
              else
                WikiFunctions.Tools.WriteTextFile("", summaryIoFile, false);
            System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
            p.WaitForExit();
            if (System.IO.File.Exists(contentIoFile)) {
              using (System.IO.StreamReader reader = System.IO.File.OpenText(contentIoFile)) {
                articleText = reader.ReadToEnd();
                reader.Close();
              }
              // skip = (articleText == origText);
              System.IO.File.Delete(contentIoFile);
              if (summaryIoFile != null && summaryIoFile != "" && System.IO.File.Exists(summaryIoFile)) {
                using (System.IO.StreamReader reader = System.IO.File.OpenText(summaryIoFile)) {
                  summary = reader.ReadToEnd();
                  reader.Close();
                }
                // System.IO.File.Delete(summaryIoFile);
              }
            }
            return articleText;
          }
          catch (Exception ex) {
            // // Most, if not all exceptions here are related to user wrong user input
            // // or environment specifics, so ErrorHandler is not needed.
            // MessageBox.Show(ActiveForm, ex.Message, "External processing error",
            //     MessageBoxButtons.OK, MessageBoxIcon.Error);
            return origText;
          }
        }