User:JarektBot/AWB Modules

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Samples of C#2.0 source code used by JarektBot. The code is compiled as AWB Custom Modules. Code below is mostly not usable as-is and need to be modified each time for specific tasks.

Add date subcategory based on date found

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            String Name = "Eugen Nosko";
            Skip = true;
            Summary = "Add date category";
            Match m = Regex.Match(ArticleText, @"5064\s*=\s*(19\d\d)");
            if (m.Success) {
              Skip = false;
              String cat1 = @"\[\[Category:Photographs by " + Name + @"\]\]";
              String cat2 = "[[Category:Photographs by " + Name + " from " + m.Result("$1") + "]]";
              ArticleText = Regex.Replace(ArticleText, cat1, cat2);
            }
            return ArticleText;
        }

Add date subcategory based on date found and correct date format

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            String Name = "Roger and Renate Rössing";
            Skip = false;
            Summary = "Add date category";
            Match m = Regex.Match(ArticleText, @"8450_1_8494\s*=\s*(19\d\d)");
            if (m.Success) {
              Skip = false;
              String cat1 = @"\[\[Category:Photographs by " + Name + @"\]\]";
              String cat2 = "[[Category:Photographs by " + Name +  m.Result(" from $1]]");
              ArticleText = Regex.Replace(ArticleText, cat1, cat2);
             }
            m = Regex.Match(ArticleText, @"8450_1_8494\s*=\s*(19\d\d)\.(\d\d)\.(\d\d)-(19\d\d)\.(\d\d)\.(\d\d)");
            if (m.Success) {
              Skip = false;
              String cat2 = m.Result("date = {{other date|between|$1-$2-$3|$4-$5-$6}}");
              ArticleText = Regex.Replace(ArticleText, "date =", cat2);
             } else {
              m = Regex.Match(ArticleText, @"8450_1_8494\s*=\s*(19\d\d)\.(\d\d)\.(\d\d)");
              if (m.Success) {
                Skip = false;
                String cat2 =  m.Result("date = $1-$2-$3");
                ArticleText = Regex.Replace(ArticleText, "date =", cat2);
               }
            }
            return ArticleText;
        }

Add date subcategory based on date found and correct date format 2

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            String Name = "Richard Peter";
            Skip = false;
            Summary = "Add date category";
            Match m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*um (19\d\d)");
            if (m.Success) {
                String cat1 = @"\[\[Category:Photographs by " + Name + @"\]\]";
                String cat2 = "[[Category:Photographs by " + Name +  m.Result(" from $1]]");
                ArticleText = Regex.Replace(ArticleText, cat1, cat2);
            } else {
              m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*(19\d\d)");
              if (m.Success) {
                String cat1 = @"\[\[Category:Photographs by " + Name + @"\]\]";
                String cat2 = "[[Category:Photographs by " + Name +  m.Result(" from $1]]");
                ArticleText = Regex.Replace(ArticleText, cat1, cat2);
              }
            }
            m = Regex.Match(ArticleText, @"{{other date\|");
            if (!m.Success) {
            m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*(19\d\d)\.(\d\d)\.(\d\d)-(19\d\d)\.(\d\d)\.(\d\d)");
            if (m.Success) {
              String cat2 = m.Result("date = {{other date|between|$1-$2-$3|$4-$5-$6}}");
              ArticleText = Regex.Replace(ArticleText, "date =", cat2);
            } else {
              m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*nach (19\d\d)\.(\d\d)\.(\d\d)");
              if (m.Success) {
                String cat2 =  m.Result("date = {{other date|after|$1-$2-$3}}");
                ArticleText = Regex.Replace(ArticleText, "date =", cat2);
              } else {
                m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*um (19\d\d)");
                if (m.Success) {
                  String cat2 =  m.Result("date = {{other date|ca|$1}}");
                  ArticleText = Regex.Replace(ArticleText, "date =", cat2);
                } else {
                  m = Regex.Match(ArticleText, @"8450_\d_8494\s*=\s*(19\d\d)\.(\d\d)\.(\d\d)");
                  if (m.Success) {
                    String cat2 =  m.Result("date = $1-$2-$3");
                    ArticleText = Regex.Replace(ArticleText, "date =", cat2);
                  }
                }
              }
            }
            }
            return ArticleText;
        }

Change name format

[edit]

Replace:

  • Name = [[??:FirstName(s) Surname|Surname, FirstName(s)]]

Like

  • Name = [[w:FirstName(s) Surname|Surname, FirstName(s)]]
  • Name = [[:en:FirstName(s) Surname|Surname, FirstName(s)]]
  • Name = [[:Category:FirstName(s) Surname|Surname, FirstName(s)]]

With

  • Name = [[??:FirstName(s) Surname|FirstName(s) Surname]]

Also, replace

  • Name = [[FirstName(s) Surname|Surname, FirstName(s)]]
  • Name = [[FirstName(s) Surname|FirstName(s) Surname]]

With

  • Name = [[FirstName(s) Surname]]
       public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
       {
           Skip = false;
           Summary = "";
           String str1 = @"Name\s*=\s*\[\[([^\]|]+)\|([^\]|]+), ([^\]|]+)\]\]";
           Match m  = Regex.Match(ArticleText, str1);
           if (m.Success) {
             String str2 = m.Result("Name = [[$1|$3 $2]]");
             ArticleText = Regex.Replace(ArticleText, str1, str2);
           }
           str1 = @"Name\s*=\s*\[\[([^\]|]+)\|([^\]|]+)\]\]";
           m  = Regex.Match(ArticleText, str1);
           if (m.Success) {
             if (m.Result("$1")==m.Result("$2")) {
               String str2 = m.Result("Name = [[$1]]");
               ArticleText = Regex.Replace(ArticleText, str1, str2);
             }
           }

Add Creator template to a category

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            String str = "{{Creator:" + ArticleTitle.Replace("Category:","") + "}}";
            int m  = ArticleText.IndexOf(str);
            if (m<0) ArticleText = str + "\r\n" + ArticleText;           
            str = "[[Category:People by alphabet]]";
            m  = ArticleText.IndexOf(str);
            if (m<0) ArticleText = ArticleText + "\r\n" + str;           
            return ArticleText;
        }

Add categories extracted from creator name

[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
          String ArticleTextLow = ArticleText.ToLower();   // lower case version of the article
          ArticleTextLow = ArticleTextLow.Replace('_',' ');// _ to space
          String str = @"{{[Cc]reator:\s*([^}]+)}}";
          Match m  = Regex.Match(ArticleTextLow, str);     // search for creator template
          if (m.Success) {                                 // if found
            String cat = m.Result("[[category:$1");        // build category name
            int n  = ArticleTextLow.IndexOf(cat);          // look for the category in the article
            if (n<0) {                                     // if cat not found
              m   = Regex.Match(ArticleText, str);         // get case dependent creator name 
              if (m.Success) 
                ArticleText = ArticleText + "\r\n" + m.Result("<nowiki>[[Category:$1]]</nowiki>");  
            }
          }
        return ArticleText;
      }

Add creator templates based on the authors name

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
           Skip = false;
           bool SkipVal = true;
           Summary = "";
           String author;
           int n  = ArticleText.IndexOf("{{Painting");              // Painting or Information template
           if (n<0) author = "Author"; else author = "Artist";
           String str1 = "(" + author + @"\s*=\s*([^\r\n]+))";      
           Match m  = Regex.Match(ArticleText, str1);
           if (m.Success) {
             String name     = m.Result("$2");    // Save creator name
             String nameLine = m.Result("$1");    // Save the whole original line
             // Skip to the next image if Name contains one of the words: "Attributed", "Follower", "School", "After", 
             // "Circle", "Workshop", "Anonymous" and "Unknown" or is name is less than 5 characters long
             if (name.IndexOf("attributed")>=0 || name.IndexOf("Attributed")>=0 ||
                 name.IndexOf("follower")>=0   || name.IndexOf("Follower")>=0 ||
                 name.IndexOf("school")>=0     || name.IndexOf("School")>=0 ||
                 name.IndexOf("after") >=0     || name.IndexOf("After") >=0 ||
                 name.IndexOf("circle") >=0    || name.IndexOf("Circle") >=0 ||
                 name.IndexOf("workshop") >=0  || name.IndexOf("Workshop") >=0 ||
                 name.IndexOf("nonymous") >=0  || name.IndexOf("nknown")>=0 ||
                 name.Length<5 ) { Skip=SkipVal; return ArticleText; }
             m = Regex.Match(name, @"\[\[([^\]]+)\]\].*");
             if (m.Success) {                     // strip [[]] brackets
               name = m.Result("$1");
               n = name.LastIndexOf(":");         // trim :en:, w:, Category:, etc. in front of the name
               if (n>=0) name = name.Substring(n+1);
               n = name.IndexOf("|");             // trim string after | on the end of the name
               if (n>=0) name = name.Substring(0,n);
             }
             m = Regex.Match(name, "([^,]+), (.*)");  // reverse names like: SURNAME, NAME
             if (m.Success) name = m.Result("$2 $1"); // to NAME SURNAME
             n = name.IndexOfAny(new char[]{'{', '}', '|', '[', ']', '(', ')', '<', '>', '-', '\n'});
             if (n>=0) { Skip=SkipVal; return ArticleText; } // Skip to the next file if any brackets are still left in the name
             //ArticleText = ArticleText + "<!-- Name = " + name + "; " +n+ "-->" ;
             n = name.Length;
             if (n<5 || n>30) { Skip=SkipVal; return ArticleText; } // Skip to the next file if too long or too short
             String str2 = "{{subst:#ifexist: creator:" +name+ " |" +author+ " = {{creator:" +name+ "}} |" +nameLine+ "}}";
             ArticleText = Regex.Replace(ArticleText, str1, str2);
           }
           return ArticleText;
        }
[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
          int m1  = ArticleText.IndexOf("|Description=");
          int m2  = ArticleText.IndexOf("|Source=");
          int m3  = ArticleText.IndexOf("|Date=");
          int m4  = ArticleText.IndexOf("|Author");
          System.IO.StreamWriter sw = System.IO.File.AppendText("C:Projects/WikiGallery.txt");
          sw.WriteLine("File:{{PAGENAME}}|");
          sw.WriteLine(ArticleText.Substring(m3, m4-m3));
          sw.WriteLine(" - ");
          sw.WriteLine(ArticleText.Substring(m1, m2-m1));
          sw.Close();
          return ArticleText;
      }

Extract image names from polish wikipedia articles

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            String outStr = "";
            Match m = Regex.Match(ArticleText, @"[Pp]lik\:([^\|\]]+)\|");
            while (m.Success) {
              outStr = outStr + m.Result("$1") + "\r\n";
              m = m.NextMatch();
            }
            m = Regex.Match(ArticleText, @"[Ff]ile\:([^\|\]]+)\|");
            while (m.Success) {
              outStr = outStr + m.Result("$1") + "\r\n";
              m = m.NextMatch();
            }
            m = Regex.Match(ArticleText, @"[Gg]rafika\:([^\|\]]+)\|");
            while (m.Success) {
              outStr = outStr + m.Result("$1") + "\r\n";
              m = m.NextMatch();
            }
            m = Regex.Match(ArticleText, @"=\s*(.+\.[Jj][Pp][Ee]?[Gg])");
            while (m.Success) {
              outStr = outStr + m.Result("$1") + "\r\n";
              m = m.NextMatch();
            }
            if (outStr.Length>0) {
              System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/WikiPliks2.txt");
              sw.WriteLine(outStr);
              sw.Close();
           } 
           return ArticleText;
       }

Extract page number from file name and create description page

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";
            Match m = Regex.Match(ArticleText, @"Title=Słownik języka polskiego T.1 Cz.(\d)");
            if (m.Success) {
              string vol = m.Result("$1");
              string str = ArticleTitle.Substring(ArticleTitle.Length-7,3);
              int num; 
              bool res = int.TryParse(str, out num);
              if (res == false)
              {
                Skip = true;
              } else {
                int num1 = num-1;
                int num2 = num+1;
                ArticleText = "{{Linde Samuel Bogumił - Słownik języka polskiego|volume="+vol+"|prev="+num1.ToString("D3")+"|next="+num2.ToString("D3")+"}}\r\n";
                ArticleText = ArticleText + "{{PD-old-100}}\r\n";
                ArticleText = ArticleText + "[[Category:Linde Samuel Bogumił - Słownik języka polskiego T.1 Cz."+vol+"|"+num.ToString("D3")+"]]";
              }
            }
            return ArticleText;
        }
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";
            string str = ArticleTitle.Substring(ArticleTitle.Length-7,3);
            int num; 
            bool res = int.TryParse(str, out num);
            if (res == false)
            {
              Skip = true;
            } else {
              int num1 = num-1;
              int num2 = num+1;
              ArticleText = "{{Uwagi nad budownictwem w starożytności by Johann Joachim Winckelmann|"+num1.ToString("D3")+"|"+num2.ToString("D3")+"}}\r\n";
              ArticleText = ArticleText + "{{PD-old-100}}\r\n";
              ArticleText = ArticleText + "[[Category:Uwagi nad budownictwem w starożytności by Johann Joachim Winckelmann]]";
            }
            return ArticleText;
        }
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";
            string str = ArticleTitle.Substring(ArticleTitle.Length-7,3);
            int num; 
            bool res = int.TryParse(str, out num);
            if (res == false)
            {
              Skip = false;
            } else {
              int num1 = num-1;
              int num2 = num+1;
              ArticleText = "=={{int:filedesc}}==\r\n{{Русский энциклопедический словарь Березина|volume=4.2|prev="+num1.ToString("D3")+"|next="+num2.ToString("D3")+"}}\r\n\r\n";
              ArticleText = ArticleText + "=={{int:license-header}}==\r\n{{PD-anon-1923}}\r\n\r\n";
              ArticleText = ArticleText + "[[Category:Русский энциклопедический словарь Березина том 4.2]]";
            }
            return ArticleText;
        }
[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            String outStr = "";

            Match m = Regex.Match(ArticleText, @"\[\[en:([^\]]*)\]\]");
            while (m.Success) {
              outStr = outStr + m.Result("$1") + "\r\n";
              m = m.NextMatch();
            }
            if (outStr.Length>0) {
              System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/WikiPliks6.txt");
              sw.WriteLine(outStr);
              sw.Close();
           } 

            return ArticleText;
        }

Extract categories and save them to the file

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = true;
            Summary = "test";
            String outStr = ArticleTitle;
            bool found = false;
 
            Match m = Regex.Match(ArticleText, @"\[\[[Cc]ategory:([^\]\|]*)\]\]");
            while (m.Success) {
              outStr = outStr + m.Result("; $1");
              m = m.NextMatch();
              found = true;
            }
            if (found) {
              outStr = outStr.Replace("\r","");
              outStr = outStr.Replace("\n","");
              System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/WikiPliks11.txt");
              sw.WriteLine(outStr);
              sw.Close();
           } 
 
            return ArticleText;
        }

Populate template fields based on External CSV file

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            
            string CreatorText = System.IO.File.ReadAllText("C:/Projects/Creators.csv");
 
            Match m1 = Regex.Match(CreatorText, ArticleTitle+@",([^,]*),([^,]*),@");
            Match m2 = Regex.Match(ArticleText, @"Nationality\s*= *");
            if (m1.Success && m2.Success) {
              ArticleText = Regex.Replace(ArticleText, @"Nationality\s*= *", "Nationality       = "+m1.Result("$2"));
            }
 
            return ArticleText;
        }


        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            string found = " 0";
            string line = " ";
            
            string CreatorText = System.IO.File.ReadAllText("C:/Projects/Creators.csv",System.Text.Encoding.UTF7  );

            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes( ArticleTitle);
            byte[] asciiArray = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.ASCII, byteArray);
            string name = ascii.GetString(asciiArray);
            byteArray = System.Text.Encoding.UTF8.GetBytes( CreatorText);
            asciiArray = System.Text.Encoding.Convert(System.Text.Encoding.UTF8, System.Text.Encoding.ASCII, byteArray);
            string CreatorText2 = ascii.GetString(asciiArray);
            CreatorText2 = Regex.Replace(CreatorText2,@"\?",";");
            name = Regex.Replace(name,@"\?",";");

            Match m1 = Regex.Match(CreatorText2, name+@",([^,]*),([^,]*),@");
            if (m1.Success) {
              Match m2 = Regex.Match(ArticleText, @"Occupation\s*=[ \t\v\f]*[\r\n]");
              if (m2.Success) {
                ArticleText = Regex.Replace(ArticleText, @"Occupation\s*=[ \t\v\f]*", "Occupation        = "+m1.Result("$1"));
                found = " 1";
              }
              m2 = Regex.Match(ArticleText, @"Nationality\s*=[ \t\v\f]*[\r\n]");
              if (m2.Success) {
                ArticleText = Regex.Replace(ArticleText, @"Nationality\s*=[ \t\v\f]*", "Nationality       = "+m1.Result("$2"));
                found = " 1";
              }
              line = m1.Result("<!-- $1 $2 -->\n");
            }
            string str = line+"<!--" + name + found+"-->\n";

            //str = str+CreatorText2;  

            return ArticleText;
        }

Scrape creator data off from creator pages

[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
          
          String birthdate="!";      
          Match m  = Regex.Match(ArticleText, @"Birthdate\s*=\s*([^\r\n\|]+)");
          if (m.Success) birthdate = m.Result("!$1");

          String deathdate="!";      
          m  = Regex.Match(ArticleText, @"Deathdate\s*=\s*([^\r\n\|]+)");
          if (m.Success) deathdate = m.Result("!$1");

          String birthplace="!";      
          m  = Regex.Match(ArticleText, @"Birthplace\s*=\s*([^\r\n\|]+)");
          if (m.Success) birthplace = m.Result("!$1");

          String deathplace="!";      
          m  = Regex.Match(ArticleText, @"Deathplace\s*=\s*([^\r\n\|]+)");
          if (m.Success) deathplace = m.Result("!$1");

          String nationality="!";      
          m  = Regex.Match(ArticleText, @"Nationality\s*=\s*([^\r\n\|]+)");
          if (m.Success) nationality = m.Result("!$1");

          String name="!";      
          m  = Regex.Match(ArticleText, @"Name\s*=\s*([^\r\n\|]+)");
          if (m.Success) name = m.Result("!$1");

          String homecat="!";      
          m  = Regex.Match(ArticleText, @"Homecat\s*=\s*([^\r\n\|]+)");
          if (m.Success) homecat = m.Result("!Category$1");

          String VIAF="!";      
          m  = Regex.Match(ArticleText, @"VIAF\s*=\s*([^\|]+)");
          if (m.Success) VIAF = m.Result("!$1");

          String GND="!";      
          m  = Regex.Match(ArticleText, @"[PG]ND\s*=\s*([^\|]+)");
          if (m.Success) GND = m.Result("!$1");

          String LCCN="!";      
          m  = Regex.Match(ArticleText, @"LCCN\s*=\s*([^\|]+)");
          if (m.Success) LCCN = m.Result("!$1");

          String NDL="!";      
          m  = Regex.Match(ArticleText, @"NDL\s*=\s*([^\|]+)");
          if (m.Success) NDL = m.Result("!$1");

          String ULAN="!";      
          m  = Regex.Match(ArticleText, @"ULAN\s*=\s*([^\|]+)");
          if (m.Success) ULAN = m.Result("!$1");

          String BNF="!";      
          m  = Regex.Match(ArticleText, @"BNF\s*=\s*cb([^\|]+)");
          if (m.Success) BNF = m.Result("!$1");

          String ISNI="!";      
          m  = Regex.Match(ArticleText, @"ISNI\s*=\s*([^\|]+)");
          if (m.Success) ISNI = m.Result("!$1");

          System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/CreatorData2.txt");
          sw.WriteLine(ArticleTitle + name + birthdate + deathdate + 
                birthplace + deathplace + nationality+ VIAF+GND+LCCN+NDL+ULAN+BNF+ISNI);
          sw.Close();
          return ArticleText;
      }

Scrape item IDs from artwork files

[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
             
          Match m  = Regex.Match(ArticleText, @"ikidata\s*=\s*([^\r\n\|]+)");
          if (m.Success) {
            String q = m.Result("!$1");

            System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/ItemID.txt");
            sw.WriteLine(q);
            sw.Close();
          }
          return ArticleText;
      }

Scrape creator data off from EN Wikipedia articles

[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
          
          String image="!";      
          Match m  = Regex.Match(ArticleText, @"image\s*=\s*([^\r\n\|]+)");
          if (m.Success) image = m.Result("!$1");

          String birthdate="!";      
          m  = Regex.Match(ArticleText, @"birthdate\s*=\s*([^\r\n]+)");
          if (m.Success) birthdate = m.Result("!$1");

          String deathdate="!";      
          m  = Regex.Match(ArticleText, @"deathdate\s*=\s*([^\r\n]+)");
          if (m.Success) deathdate = m.Result("!$1");

          String birthplace="!";      
          m  = Regex.Match(ArticleText, @"birthplace\s*=\s*([^\r\n]+)");
          if (m.Success) birthplace = m.Result("!$1");

          String deathplace="!";      
          m  = Regex.Match(ArticleText, @"deathplace\s*=\s*([^\r\n]+)");
          if (m.Success) deathplace = m.Result("!$1");

          String birthdate2="!";      
          m  = Regex.Match(ArticleText, @"DATE OF BIRTH\s*=\s*([^\r\n\|]+)");
          if (m.Success) birthdate2 = m.Result("!$1");

          String deathdate2="!";      
          m  = Regex.Match(ArticleText, @"DATE OF DEATH\s*=\s*([^\r\n\|]+)");
          if (m.Success) deathdate2 = m.Result("!$1");

          String birthplace2="!";      
          m  = Regex.Match(ArticleText, @"PLACE OF BIRTH\s*=\s*([^\r\n\|]+)");
          if (m.Success) birthplace2 = m.Result("!$1");

          String deathplace2="!";      
          m  = Regex.Match(ArticleText, @"PLACE OF DEATH\s*=\s*([^\r\n\|]+)");
          if (m.Success) deathplace2 = m.Result("!$1");

          String nationality="!";      
          m  = Regex.Match(ArticleText, @"nationality\s*=\s*([^\r\n]+)");
          if (m.Success) nationality = m.Result("!$1");

          String description="!";      
          m  = Regex.Match(ArticleText, @"SHORT DESCRIPTION\s*=\s*([^\r\n\|]+)");
          if (m.Success) description = m.Result("!$1");

          String alternative="!";      
          m  = Regex.Match(ArticleText, @"ALTERNATIVE NAMES\s*=\s*([^\r\n\|]+)");
          if (m.Success) alternative = m.Result("!$1");

          String field="!";      
          m  = Regex.Match(ArticleText, @"field\s*=\s*([^\r\n]+)");
          if (m.Success) field = m.Result("!$1");

          String sortkey="!";      
          m  = Regex.Match(ArticleText, @"\{\{DEFAULTSORT:([^\}]+)\}\}");
          if (m.Success) sortkey = m.Result("!$1");

          String es="!";      
          m  = Regex.Match(ArticleText, @"\[\[es:([^\]]*)\]\]");
          if (m.Success) es = m.Result("!$1");

          String de="!";      
          m  = Regex.Match(ArticleText, @"\[\[de:([^\]]*)\]\]");
          if (m.Success) de = m.Result("!$1");

          String fr="!";      
          m  = Regex.Match(ArticleText, @"\[\[fr:([^\]]*)\]\]");
          if (m.Success) fr = m.Result("!$1");

          String pl="!";      
          m  = Regex.Match(ArticleText, @"\[\[pl:([^\]]*)\]\]");
          if (m.Success) pl = m.Result("!$1");

          String en="!";      
          m  = Regex.Match(ArticleTitle, @"([^\r\n]*)");
          if (m.Success) en = m.Result("!$1");

          System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/PersonData.txt");
          sw.WriteLine(ArticleTitle + image + birthdate + birthdate2 + deathdate + deathdate2 + 
                birthplace + birthplace2 + deathplace + deathplace2 + nationality+description+alternative+field+sortkey+
                de+es+fr+pl);
          sw.Close();
          return ArticleText;
      }

Scrape creator data off from Category:People by name

[edit]
      public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
      {
          Skip = false;
          Summary = "";
 
          String birthdate="!";      
          Match m  = Regex.Match(ArticleText, @"\[\[[Cc]ategory:(\d\d\d\d) births\]\]");
          if (m.Success) birthdate = m.Result("!$1");
 
          String deathdate="!";      
          m  = Regex.Match(ArticleText, @"\[\[[Cc]ategory:(\d\d\d\d) deaths\]\]");
          if (m.Success) deathdate = m.Result("!$1");
 
          String sortkey="!";      
          m  = Regex.Match(ArticleText, @"\{\{DEFAULTSORT:([^\}]+)\}\}");
          if (m.Success) sortkey = m.Result("!$1");
          String de="!";      
          m  = Regex.Match(ArticleText, @"\[\[de:([^\]]*)\]\]");
          if (m.Success) de = m.Result("!$1");
 
          String en="!";      
          m  = Regex.Match(ArticleText, @"\[\[en:([^\]]*)\]\]");
          if (m.Success) en = m.Result("!$1");
          String str = ArticleTitle + birthdate + deathdate + sortkey + de + en;
          str = str.Replace("\r","");
          str = str.Replace("\n","");
 
          System.IO.StreamWriter sw = System.IO.File.AppendText("C:/Projects/PersonData5.txt");
          sw.WriteLine(str);
          sw.Close();
          return ArticleText;
      }

Format categories in Category:Users by language

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        { 
            Skip = false;
            Summary = "";
            Match m = Regex.Match(ArticleTitle, @"User (.*)\-([N012345])");
            String code = "";
            String level = "";
            String lang = "";
            bool ok = true;
            if (m.Success) {
                code  = m.Result("$1");
                level = m.Result("$2");
            } else {
               m = Regex.Match(ArticleTitle, @"User (.*)");
               if (m.Success) {
                 code = m.Result("$1");
                 level = "*";
               } else {
                 ok=false;
               }
            }
            m = Regex.Match(ArticleText, @"language ([^\.]*)");
            if (m.Success) {
                lang = m.Result("$1");
            } else {
                ok=false;
            }
            if (ok) {
              ArticleText = "{{UsersSpeak|" + code + "|" + level + "|" + lang + "|link=}}";
            } else {
              //ArticleText = ArticleText+"{{UsersSpeak|" + code + "|" + level + "|" + lang + "|link=}}";
            }
            return ArticleText;
        }

Add taxon category based on image description

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = true;
            Summary = "";
            Match m = Regex.Match(ArticleText, @"Description\s*=\s*''([^']*)''");
            if (m.Success) {
              Skip = false;
              Summary = m.Result("Add scientific name category: [[Category:$1]]");
              String cat = "[[Category:Photos by Jason Hollinger (uncategorized)]]";
              String str = m.Result("{{subst:#ifexist:Category:$1|[[Category:$1]]|[[Category:Photos by Jason Hollinger (uncategorized)]]}}");
              ArticleText = ArticleText.Replace(cat,str);
            }
            return ArticleText;
        }
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "test";
            String outStr = "";
            String field = "";

            Match m = Regex.Match(ArticleText, @"(\|\s*([\w ]*)\s*=)");
            string[] fields = new string[50];
            for(int i=0; m.Success && i<50; i++) {
              fields[i] = m.Result("$2");
              field     = m.Result("$1");
              m = m.NextMatch();
              for (int j=0; j<i; j++) {
                if (fields[i]==fields[j]) {
                  ArticleText = ArticleText.Replace(field, field.ToUpper());
                }
              }
            }
            return ArticleText;
        }


Download description pages

[edit]
        public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
        {
            Skip = false;
            Summary = "";
            String fname = @"C:\\Projects\\InfoTemp\\"+RandomString(20)+".txt";
            System.IO.File.WriteAllText(fname, ArticleTitle+"\r\n"+ArticleText);
 
            return ArticleText;
        }
        public static string RandomString(int length)
        {
          const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
          var random = new Random();
          return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
        }

Adding a different value to each edited page

[edit]
  • Problem: given list of pages, where each page is associated with some property: add that property to some template on that page.
  • Example: multibeacon produced a list of pages and item q-codes where given page is a value of Commons category (P373) property. Now add the q-codes to templates on Commons pages.
  • Solution:
    • Create excel sheet with
  • A - page name
  • B - q-code
  • C - =CONCATENATE("|",A1,"=",B1)
  • D - =CONCATENATE("Category:",A1)
{{{{{|safesubst:}}}#switch:{{{1|}}}
|Bernhard Windscheid=Q62966
|Karl Matthäus Winter=Q18908122
|Eduard von Winterstein=Q85051
.........
}}
and fill it with code from column C.
    • Call the template from AWB and use {{subst:User:Jarekt/subst|{{subst:PAGENAME}}}} wherever q-code is needed.

New Creator Pages

[edit]
{{Creator
 | Wikidata  = {{subst:User:Jarekt/subst|{{subst:PAGENAME}}}}
 | Option    = {{{1|}}} <!-- Do not modify -->
}}

See Also

[edit]