Jump to content

User:Svick/LinqToWiki

From Wikipedia, the free encyclopedia

LinqToWiki is a library for accessing MediaWiki wikis (including Wikipedia) through the API from .Net languages like C# and VB.NET.

It can be used to do almost anything that can be done from the web interface and more, including things like editing articles, listing articles in categories, listing all kinds of links on a page and much more. Querying the various lists available can be done using LINQ queries, which then get translated into efficient API requests.

The library is strongly-typed, which means it should be hard to make invalid requests and it also makes it easy to discover available methods and properties though IntelliSense.

[edit]

Downloads

[edit]

LinqToWiki is always customized for a specific wiki.

English Wikipedia

[edit]

Other wikis

[edit]

In this case, you have several options:

  1. Use the English Wikipedia build and hope you don't encounter any significant differences between the two wikis in your queries.
  2. Request a build for your wiki on the talk page.
  3. Create a custom build yourself.

Usage

[edit]

For example, to edit the Sandbox anonymously, you can use the following:

var wiki = new Wiki("TheNameOfMyBot/1.0 (http://website, myemail@site)", "en.wikipedia.org");

// get edit token, necessary to edit pages
var token = wiki.tokens(new[] { tokenstype.edit }).edittoken;

// create new section called "Hello" on the page "Wikipedia:Sandbox"
wiki.edit(
    token: token, title: "Wikipedia:Sandbox", section: "new", sectiontitle: "Hello", text: "Hello world!");

As you can see, in methods like this, you should use named parameters, because the edit() method has lots of them, and you probably don't need them all.

The code looks more convoluted than necessary (can't the library get the token for me?), but that's because it's all generated automatically.

For more complex examples, have a look at the documentation on the project's site.