Skip to content

[MediaWiki] Recent changes and patrol

Chen edited this page Nov 9, 2017 · 5 revisions

Prerequisite

  • [MediaWiki] Generators

Recent changes and patrol

You can list pages using RecentChangesGenerator.EnumPagesAsync, just like other generators, while this generator has another powerful method RecentChangesGenerator.EnumRecentChangesAsync, which can generate a sequence of RecentChangesEntry, containing the detailed information of each recent change.

static async Task HelloRecentChanges()
{
    // Create a MediaWiki API client.
    var wikiClient = new WikiClient();
    // Create a MediaWiki Site instance.
    var site = new WikiSite(wikiClient, "https://en.wikipedia.org/w/api.php");
    await site.Initialization;
    var rcg = new RecentChangesGenerator(site)
    {
        TypeFilters = RecentChangesFilterTypes.Create,
        PaginationSize = 50, // We already know we're not going to fetch results as many as 500 or 5000
        // so this will help.
    };
    // List the 10 latest new pages
    var pages = await rcg.EnumPagesAsync().Take(10).ToList();
    Console.WriteLine("New pages");
    foreach (var p in pages)
        Console.WriteLine("{0, -30} {1, 8}B {2}", p, p.ContentLength, p.LastTouched);
    // List the 10 latest recent changes
    rcg.TypeFilters = RecentChangesFilterTypes.All;
    var rcs = await rcg.EnumItemsAsync().Take(10).ToList();
    Console.WriteLine();
    Console.WriteLine("Recent changes");
    foreach (var rc in rcs)
        Console.WriteLine(rc);
}

The code produces the following result

New pages
Bill Eigel                           43B 2016/8/27 AM 6:38:46
Cool woods                         3971B 2016/8/27 AM 6:46:52
Indian Institute Of Technical Computer Application       56B 2016/8/27 AM 6:59:20
Leslie Bieh Yhan                    371B 2016/8/27 AM 6:58:02
Maranathirkku Pinbu Helan           157B 2016/8/27 AM 6:46:17
Mitchell Bolewski                    27B 2016/8/27 AM 6:44:45
Palazzo Fani Mignanelli, Siena     1537B 2016/8/27 AM 6:39:52
Punjab Highway Department          1940B 2016/8/27 AM 6:58:32
Shiny waves                         968B 2016/8/27 AM 6:53:50
Thailand national football team results (2000-09)    28321B 2016/8/27 AM 6:58:00

Recent changes
855757198,2016/8/27 AM 6:59:39,Edit,None,Melbourne Football Club,update coach
855757197,2016/8/27 AM 6:59:39,Edit,None,The Ocean (Mike Perry song),Undid revision 734440122 by [[Special:Contributions/82.113.183.202|82.113.183.202]] ([[User talk:82.113.183.202|talk]]) Considering Shy Martin doesn't have an article, this gives a tidbit of info about her and shouldn't be removed.
855757193,2016/8/27 AM 6:59:35,Edit,None,User:Lesbyan,db-g3
855757192,2016/8/27 AM 6:59:35,Edit,None,Kashmiri phonology,[[WP:CHECKWIKI]] error fix. Broken bracket problem. Do [[Wikipedia:GENFIXES|general fixes]] and cleanup if needed. - using [[Project:AWB|AWB]] (12082)
855757191,2016/8/27 AM 6:59:34,Edit,Annonymous,ConBravo!,
855757188,2016/8/27 AM 6:59:32,Edit,None,User:Moxy,
855757187,2016/8/27 AM 6:59:31,Edit,Minor, Bot,Society for the Propagation of the Faith,Dating maintenance tags: {{Cn}}
855757186,2016/8/27 AM 6:59:31,Edit,None,1530 in India,
855757185,2016/8/27 AM 6:59:30,Edit,None,User talk:101.98.246.169,Warning [[Special:Contributions/101.98.246.169|101.98.246.169]] - #1
855757184,2016/8/27 AM 6:59:30,Edit,Minor,Bay of Plenty Region,Reverting possible vandalism by [[Special:Contribs/101.98.246.169|101.98.246.169]] to version by Villianifm. [[WP:CBFP|Report False Positive?]] Thanks, [[WP:CBNG|ClueBot NG]]. (2741830) (Bot)

Patrol

Once you've got an instance of RecentChangesEntry, you can patrol it, as long as you have the patrol or autopatrol right.

static async Task InteractivePatrol()
{
    // Patrol the last unpatrolled change.
    // Ususally a user should have the patrol right to perform such operation.

    // Create a MediaWiki API client.
    var wikiClient = new WikiClient();
    // Create a MediaWiki Site instance.
    var site = new WikiSite(wikiClient, Input("Wiki site API URL"));
    await site.Initialization;
    await site.LoginAsync(Input("User name"), Input("Password"));
    var rcg = new RecentChangesGenerator(site)
    {
        TypeFilters = RecentChangesFilterTypes.Create,
        PaginationSize = 5,
        PatrolledFilter = PropertyFilterOption.WithoutProperty
    };
    // List the first unpatrolled result.
    var rc = await rcg.EnumItemsAsync().FirstOrDefault();
    if (rc == null)
    {
        Console.WriteLine("Nothing to patrol.");
        return;
    }
    Console.WriteLine("Unpatrolled:");
    Console.WriteLine(rc);
    // Show the involved revisions.
    if (rc.OldRevisionId > 0 && rc.RevisionId > 0)
    {
        var rev = await Revision.FetchRevisionsAsync(site, rc.OldRevisionId, rc.RevisionId).ToList();
        // Maybe we'll use some 3rd party diff lib
        Console.WriteLine("Before, RevId={0}, {1}", rev[0].Id, rev[0].TimeStamp);
        Console.WriteLine(rev[0].Content);
        Console.WriteLine("After, RevId={0}, {1}", rev[1].Id, rev[1].TimeStamp);
        Console.WriteLine(rev[1].Content);
    }
    else if (rc.RevisionId > 0)
    {
        var rev = await Revision.FetchRevisionAsync(site, rc.RevisionId);
        Console.WriteLine("RevId={0}, {1}", rev.Id, rev.TimeStamp);
        Console.WriteLine(rev.Content);
    }
    if (Confirm("Mark as patrolled?"))
    {
        await rc.PatrolAsync();
        Console.WriteLine("The change {0} has been marked as patrolled.", (object) rc.Title ?? rc.Id);
    }
}