All Projects → svick → Linq To Wiki

svick / Linq To Wiki

Licence: mit
.Net library to access MediaWiki API

Projects that are alternatives of or similar to Linq To Wiki

Wikiteam
Tools for downloading and preserving wikis. We archive wikis, from Wikipedia to tiniest wikis. As of 2020, WikiTeam has preserved more than 250,000 wikis.
Stars: ✭ 404 (+334.41%)
Mutual labels:  wikipedia, wiki, mediawiki
discord-wiki-bot
Wiki-Bot is a bot with the purpose to easily search for and link to wiki pages. Wiki-Bot shows short descriptions and additional info about the pages and is able to resolve redirects and follow interwiki links.
Stars: ✭ 69 (-25.81%)
Mutual labels:  wiki, mediawiki, wikipedia
Apps Android Wikipedia
📱The official Wikipedia app for Android!
Stars: ✭ 1,350 (+1351.61%)
Mutual labels:  wikipedia, wiki, mediawiki
wikibot
Some MediaWiki bot examples including wikipedia, wikidata using MediaWiki module of CeJS library. 採用 CeJS MediaWiki 自動化作業用程式庫來製作 MediaWiki (維基百科/維基數據) 機器人的範例。
Stars: ✭ 26 (-72.04%)
Mutual labels:  wiki, mediawiki, wikipedia
wikiapi
JavaScript MediaWiki API for node.js
Stars: ✭ 28 (-69.89%)
Mutual labels:  wiki, mediawiki, wikipedia
Wikipedia Mirror
🌐 Guide and tools to run a full offline mirror of Wikipedia.org with three different approaches: Nginx caching proxy, Kimix + ZIM dump, and MediaWiki/XOWA + XML dump
Stars: ✭ 160 (+72.04%)
Mutual labels:  wikipedia, wiki, mediawiki
Mediawiki
🌻 The collaborative editing software that runs Wikipedia. Mirror from https://gerrit.wikimedia.org/g/mediawiki/core. See https://mediawiki.org/wiki/Developer_access for contributing.
Stars: ✭ 2,752 (+2859.14%)
Mutual labels:  wikipedia, wiki, mediawiki
pw
Best websites a Programmer should visit
Stars: ✭ 27 (-70.97%)
Mutual labels:  wiki, wikipedia
wikipedia for humans
No description or website provided.
Stars: ✭ 44 (-52.69%)
Mutual labels:  wiki, wikipedia
meza
Setup an enterprise MediaWiki server with simple commands
Stars: ✭ 38 (-59.14%)
Mutual labels:  wiki, mediawiki
wikiradio
A radio for Wikimedia Commons audio files
Stars: ✭ 14 (-84.95%)
Mutual labels:  wiki, wikipedia
DiscordWikiBot
Discord bot for Wikimedia projects and MediaWiki wiki sites
Stars: ✭ 30 (-67.74%)
Mutual labels:  mediawiki, wikipedia
copyvios
A copyright violation detector running on Wikimedia Cloud Services
Stars: ✭ 32 (-65.59%)
Mutual labels:  mediawiki, wikipedia
SemanticWikibase
Makes Wikibase data available in Semantic MediaWiki
Stars: ✭ 14 (-84.95%)
Mutual labels:  wiki, mediawiki
cassandra-GLAM-tools
Support GLAMs in monitoring and evaluating their cooperation with Wikimedia projects
Stars: ✭ 17 (-81.72%)
Mutual labels:  mediawiki, wikipedia
bitnami-docker-mediawiki
Bitnami Docker Image for MediaWiki
Stars: ✭ 89 (-4.3%)
Mutual labels:  wiki, mediawiki
Wptools
Wikipedia tools (for Humans): easily extract data from Wikipedia, Wikidata, and other MediaWikis
Stars: ✭ 371 (+298.92%)
Mutual labels:  wikipedia, mediawiki
Mwparserfromhell
A Python parser for MediaWiki wikicode
Stars: ✭ 440 (+373.12%)
Mutual labels:  wikipedia, mediawiki
Jwiki
📖 A library for effortlessly interacting with Wikipedia/MediaWiki
Stars: ✭ 69 (-25.81%)
Mutual labels:  wikipedia, mediawiki
Mwclient
Python client library to interface with the MediaWiki API
Stars: ✭ 221 (+137.63%)
Mutual labels:  wikipedia, mediawiki

LinqToWiki

LinqToWiki is a library for accessing sites running MediaWiki (including Wikipedia) through the MediaWiki 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.

Because the API can vary from wiki to wiki, it's necessary to configure the library thorough an automatically generated assembly.

Downloads

Usage

Simple example

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

var wiki = new Wiki("TheNameOfMyBot/1.0 (http://website, [email protected])", "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.

Queries

Where LINQ to Wiki really shines, though, are queries: If you wanted to get the names of all pages in Category:Mammals of Indonesia, you can do:

var pages = (from cm in wiki.Query.categorymembers()
             where cm.title == "Category:Mammals of Indonesia"
             select cm.title)
            .ToEnumerable();
List of mammals of Indonesia
Mammals of Borneo
Agile gibbon
Andrew's Hill Rat
Anoa
…

The call to ToEnumerable() (or, alternatively, ToList()) is necessary, so that LINQ to Wiki methods don't get mixed up with LINQ to Objects methods, but the result is now an ordinary IEnumerable<string>.

Well, actually, you want the list sorted backwards (maybe you want to know whether there are any Indonesiam mammals whose name starts with Z):

var pages = (from cm in wiki.Query.categorymembers()
              where cm.title == "Category:Mammals of Indonesia"
              orderby cm descending 
              select cm.title)
    .ToEnumerable();
Wild water buffalo
Wild boar
Whitish Dwarf Squirrel
Whitehead's Woolly Bat
White-thighed surili
…

Hmm, no luck with the Z. Okay, can I get the first section of those articles? This is where things start to get more comlicated. If you were using the API directly, you would have to use generators. LINQ to Wiki can handle that for you, but since generators are quite powerful, you have to do something like this:

var pages = (from cm in wiki.Query.categorymembers()
             where cm.title == "Category:Mammals of Indonesia"
             orderby cm descending
             select cm)
    .Pages
    .Select(
        page =>
        new
        {
            title = page.info.title,
            text = page.revisions()
                .Where(r => r.section == "0")
                .Select(r => r.value)
                .FirstOrDefault()
        })
    .ToEnumerable();
Wild water buffalo
{{About|the wild species|the domestic livestock varieties descended from it|water buffalo}}

{{Taxobox|…}}

The '''wild water buffalo''' (''Bubalus arnee''), also called '''Asian buffalo''' and '''Asiatic buffalo''',
is a large [[bovinae|bovine]] native to [[Southeast Asia]].  …

This deserves some explanation. When you use Pages to access more information about the pages in some list, you then call Select() to choose what exactly do you want to know. In that Select(), you can use info for basic information about the page, like its name, ID or whether you are watching it. Then there are several lists, including revisions(). You can again use LINQ methods to alter this part of the query. For example, I want only the first section (Where(r => r.section == "0")), I want to select the text of the revision (here called “value”, Select(r => r.value)) and only for the first (latest) revision (FirstOrDefault()).

For examples of almost all methods in LINQ to Wiki, have a look at the LinqToWiki.Samples project.

Generating configuration for a wiki

To generate a configuration assembly for a certain wiki, you can use the linqtowiki-codegen command-line application (see the LinqToWiki.Codegen.App project). If you run it without parameters, it will show you basic usage, along with some examples:

Usage:    linqtowiki-codegen url-to-api [namespace [output-name]] [-d output-directory] [-p props-file-path]
Examples: linqtowiki-codegen en.wikipedia.org LinqToWiki.Enwiki linqtowiki-enwiki -d C:\Temp -p props-defaults-sample.xml
          linqtowiki-codegen https://en.wikipedia.org/w/api.php

The application retrieves information about the API from the API itself, using the URL you gave as a first parameter. This requires information about properties of results of the API, that was not previously available from the API, and was added there because of this library. This was done quite recently (on 12 June 2012), so it's not available in the most recent public version of MediaWiki (1.19.1) but it is available in the version currently in use on Wikipedia (1.20wmf7).

If you don't have recent enough version of MediaWiki, you can use a workaround: get the necessary information from a file. The file looks almost the same as an API response in XML format that would contain the information. There is a sample of the file available, which will most likely work for you out of the box.

You don't have to generate a separate assembly for each wiki, if the methods you want to use look the same on all of them. In that case, don't forget to specify which wiki do you want to use in the constructor of the Wiki class.

If you want to access multiple wikis with different configuration assemblies from one program, you can, if you generate each of them into a different namespace (the default namespace is LinqToWiki.Generated).

If you want to do something more complicated regarding generating the configuration assemblies (for example, create a bunch of C# files that you can modify by hand and then compile into a configuration assembly), you can use the LinqToWiki.Codegen library directly from your own application.

Developer documentation

If you want to modify this code (patches are welcome) or just have a look at the implementation, here is a short overview of the projects (more details are in the project directories):

  • LinqToWiki.Core – The core of the library. This project is referenced by all other projects and contains types necessary for acessing the API, processing LINQ expressions, etc.
  • LinqToWiki.Codegen – Handles generating code using Roslyn.
  • LinqToWiki.Codegen.App – The linqtowiki-codegen command-line application, see above.
  • LinqToWiki.Samples – Samples of code that uses this library.
  • LinqToWiki.ManuallyGenerated – A manually written configuration assembly. You could use this as a template for your own configuration assembly, but otherwise it's mostly useless.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].