Written a MediaWiki Client Library

This is a MediaWiki client library based on .NET PCL, targeting at .NET Framework 4.5, ASP.NET Core 1.0, Xamarin.iOS, and Xamarin.Android. The verified supported MediaWiki API version can be as low as MediaWiki 1.19 (Wikia). FYI, now Wikipedia is using MediaWiki 1.28.

Repository: https://github.com/CXuesong/WikiClientLibrary

Other MediaWiki client libraries: https://www.mediawiki.org/wiki/API:Client_code

Advertise for it: Frankly I haven’t take time to summarize, but it supports

  • common API for page query/creation/edit/deletion
  • generators.
  • file upload
  • query result pagination (encapsulated as IAsyncEnumerable )

Some demo (Okay in fact I just copied from README.md 😕 )

static async Task HelloWikiWorld()
{
    // Create a MediaWiki API client.
    var wikiClient = new WikiClient
    {
        // UA of Client Application. The UA of WikiClientLibrary will
        // be append to the end of this when sending requests.
        ClientUserAgent = "ConsoleTestApplication1/1.0",
    };
    // Create a MediaWiki Site instance with the URL of API endpoint.
    var site = await Site.CreateAsync(wikiClient, "https://test2.wikipedia.org/w/api.php");
    // Access site information via Site.SiteInfo
    Console.WriteLine("API version: {0}", site.SiteInfo.Generator);
    // Access user information via Site.UserInfo
    Console.WriteLine("Hello, {0}!", site.UserInfo.Name);
    Console.WriteLine("You're in the following groups: {0}.", string.Join(",", site.UserInfo.Groups));
    // Site login
    if (Confirm($"Do you want to login into {site.SiteInfo.SiteName}?"))
    {
        await site.LoginAsync(Input("Username"), Input("Password"));
        Console.WriteLine("You have successfully logged in as {0}.", site.UserInfo.Name);
    }
    // Find out more members in Site class, such as
    //  site.Namespaces
    //  site.InterwikiMap

    // Page Operations
    // Fetch information and content
    var page = new Page(site, site.SiteInfo.MainPage);
    Console.WriteLine("Retriving {0}...", page);
    await page.RefreshAsync(PageQueryOptions.FetchContent);

    Console.WriteLine("Last touched at {0}.", page.LastTouched);
    Console.WriteLine("Last revision {0} by {1} at {2}.", page.LastRevisionId,
        page.LastRevision.UserName, page.LastRevision.TimeStamp);
    Console.WriteLine("Content length: {0} bytes ----------", page.ContentLength);
    Console.WriteLine(page.Content);
    // Purge the page
    if (await page.PurgeAsync())
        Console.WriteLine("  The page has been purged successfully.");
    // Edit the page
    page = new Page(site, "Project:Sandbox");
    await page.RefreshAsync(PageQueryOptions.FetchContent);
    if (!page.Exists) Console.WriteLine("Warning: The page {0} doesn't exist.", page);
    page.Content += "\n\n'''Hello''' ''world''!";
    await page.UpdateContentAsync("Test edit from WikiClientLibrary.");
    Console.WriteLine("{0} has been saved. RevisionId = {1}.", page, page.LastRevisionId);
    // Find out more operations in Page class, such as
    //  page.MoveAsync()
    //  page.DeleteAsync()
    // Logout
    await site.LogoutAsync();
    Console.WriteLine("You have successfully logged out.");
}

 

3 Replies to “Written a MediaWiki Client Library”

  1. 你好,有个问题想请教下你。
    我在用VS2015打开你的mediawiki 0.5.0.5版本项目时候,提示WikiClientLibrary加载失败,显示该项目需要用户输入,有关更多信息,请重新加载该项目。错误提示显示为WikiClientLibrary.csproj : error : 项目的默认 XML 命名空间必须为 MSBuild XML 命名空间。如果项目是用 MSBuild 2003 格式创建的,请将 xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″ 添加到 元素中。如果项目是用旧的 1.0 或 1.2 格式创建的,请将其转换为 MSBuild 2003 格式。 我按照要求加入编码格式,然后又提示无法识别元素 中的特性“Version”。

    1. 哦,我最近把整个项目升级到了VS2017……所以……抱歉啦😭

      不过VS2015还是可以引用打包之后的NuGet程序包的。

      1. 多谢您的回复,因为想写个基于wiki的windows程序,所以看到你的项目想看下你的项目源码,学习下您的代码。再次感谢您的回复,希望以后有问题还能得到您的解答多谢。

Leave a Reply

Your email address will not be published. Required fields are marked *

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Content is available under CC BY-SA 3.0 unless otherwise noted.