Jump to content

User:MilHistBot/AutoClass.cs

From Wikipedia, the free encyclopedia
using System;
using System.Collections.Generic;
using System.Linq;
using NDesk.Options;

public class AutoClass
{
    private int  max     = 1;
    private bool debugs  = false;
    private bool force   = false;
    private bool help    = false;
    private bool verbose = false;

    private void autoClass (Page talk)
    {
        Page article = talk.Article;
        Console.WriteLine (article.Title);
        var taskForces = new TaskForces (article);
        if (taskForces.Any ())
        {
            talk.Load ();
            talk.MilHist.ProjectTemplate.Add (taskForces);
            Console.WriteLine ("   Added " + taskForces);
            if (force)
            {
                talk.Save ("AutoClass: Added task forces");
            }
        }
        else
        {
            Console.Error.WriteLine (article.Title + ": No task forces found");
        }
    }

    private static void showHelp (OptionSet optionSet)
    {
        Console.WriteLine ("Usage: mono AutoClass [OPTIONS]+ <article>");
        Console.WriteLine ("Add task forces to the MilHist template on the talk page.");
        Console.WriteLine ();
        Console.WriteLine ("Options:");
        optionSet.WriteOptionDescriptions (Console.Out);
        Environment.Exit (0);
    }

    List<string> options (string [] args)
    {
        var optionSet = new OptionSet () {
            { "d|debug",   "debugging",    v => debugs  = v != null },
            { "f|force",   "update page",  v => force   = v != null },
            { "h|?|help",  "display help", v => help    = v != null },
            { "n|max=",    "number of pages to process",  v => int.TryParse (v, out max) },
            { "v|verbose", "vebosity",     v => verbose = v != null },
        };

        List<string> extras = optionSet.Parse (args);

        if (debugs)
        {
            verbose = true;
            force = false;
        }

        if (help)
        {
            showHelp (optionSet);
        }

        return extras;
    }

    private AutoClass (string [] args)
    {
        var bot = new Bot ();
        var articles = options (args);
        bot.Cred.Showtime ("started");
        if (articles.Count > 0)
        {
            var articlePage = new Page (bot, articles[0]);
            autoClass (articlePage.Talk);
        }
        else
        {
            var query = new Query ("Military history articles with no associated task force", max);
            var talkPages = bot.Category (query);

            foreach (var talkPage in talkPages)
            {
                if (talkPage.Namespace.Equals("Talk"))
                {
                    autoClass (talkPage);
                }
            }
        }
        bot.Cred.Showtime ("done");
    }

    static public void Main (string [] args)
    {
        var autoclass = new AutoClass (args);
    }
}