Writing a simple greeting bot for MediaWiki/Introduction to MediaWiki scripting access
On this page you will get an introductory overview over how MediaWiki works and in which ways it can be accessed from scripts.
Fundamentals
[edit | edit source]MediaWiki is a PHP web application based on HTTP. Because of that, scripts communicate with MediaWiki in a series of requests and responses. To get the contents of a page, for example, the script sends a request to the server hosting MediaWiki (e.g. http://en.wikiversity.org/). The server then responds with the page content plus some additional statistical information. Each script action, such as loading or saving a page, requires a new set of requests and responses.
Exercise 1 - A simple C# request
[edit | edit source]Note: the following code example uses C# and the .NET Framework. The use of an Integrated development environment (IDE) is strongly recommended. For Windows users, Microsoft provides a free C# IDE called Visual C# Express. For Linux and Mac users, there is a free IDE called MonoDevelop available.
In this exercise, we will send a request to retrieve the contents of Wikiversity's main page in a file:
using System;
using System.Net;
using System.IO;
using System.Text;
namespace BotCourse
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://en.wikiversity.org/wiki/Wikiversity:Main Page");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent = "Wikiversity bot course/1.0";
request.ContentType = "application/x-www-form-urlencoded";
using (WebResponse response = request.GetResponse())
using (StreamReader strmReader = new StreamReader(response.GetResponseStream()))
File.WriteAllText("Main page.txt", strmReader.ReadToEnd(), Encoding.UTF8);
}
}
}
Let's take a closer look at what this code does. We
- We create an
Uri
object to hold the address of the page, in this case Wikiversity:Main Page. - We create a new HTTP request object by calling WebRequest's Create page with the address of the page we want. If you are not familiar with HTTP at all, I suggest taking a quick peek at w:HTTP.
- We then set the user agent and content type headers typical for HTTP requests. Although innocent looking, these are actually quite important: MediaWiki refuses any request which does not provide these two headers.
- By calling
GetResponse
, the request is sent and a response retrieved. - We read the content of the response and save it to a local file called "Main page.txt".
If you open the file, you will see the same content that would be shown if you went to Wikiversity:Main Page with your browser and look at the source of that page (the menu option is commonly called View source oder Page source in graphic browsers).