User:LivingBot/Wikibot
Original author(s) | Jarry1250 |
---|---|
Initial release | February 8, 2009 |
Stable release | 0.2.1
/ February 14, 2009 |
Written in | PHP |
License | GNU General Public License |
Wikibot is a basic PHP framework (or rather, a single class) that simplifies the coding and deployment of PHP-based Mediawiki bots. It was originally based on Kaspo's phpwikibot, but is now substantially different.
Main code file
The main class code can be found here. It definitely works in PHP version 5, and on the English Wikipedia. Other versions of the PHP engine and other MediaWiki sites remain as yet untested. You might want to check fairly regularly to see if there has been a new release.
How to make your own bot
The Wikibot PHP framework greatly speeds up the process of coding and setting up a bot. However, actually coding or writing a bot is only one part of developing a bot. The first step on the road to creating a bot should involve you reading this guide. Before you make any proper edits[1], ensure that your bot follows Wikipedia's bot policy and has a proper BRFA approved by a member of the Bot Approvals Group. Failure to comply with the policy may lead to your bot failing to be approved or being blocked from editing Wikipedia.
As for utilising Wikibot to code your bot, here is a step-by-step process. Those steps not in italics are optional but highly advisable.
- Create a new folder on your web server.
- Password protect access that folder with .htaccess (or similar). All files should be uploaded into that folder.
- Copy and paste the main code (see above) into a new file called Wikibot.php5 and upload it to your server.
- Create a new text file, password.txt. It should only contain your / your bot's Wikipedia password - no extra linebreaks or anything. Upload it.
- Create a new .php5 file (e.g. MyBot.php5). Upload it. This will be the file you actually edit.
Examples for what to put in the file created during step 5 are shown below.
Example code for your bot
These are some examples for what to put inside your MyBot.php5 file.
The bare basics
This lot won't actually do anything, but it'll set you up just fine.
<?php
//grab class
include('Wikibot.php5');
//get password
$fhpw = fopen("password.txt", 'r') or die("Can't open password file");
$password = fgets($fhpw);
fclose($fhpw);
//create bot
$username = "MyBot"; //A registered username
$MyBot = new Wikibot($username, $password);
?>
Print a page
Load a page from Wikipedia, then print it.
<?php
//grab class
include('Wikibot.php5');
//get password
$fhpw = fopen("password.txt", 'r') or die("Can't open password file");
$password = fgets($fhpw);
fclose($fhpw);
//create bot
$username = "MyBot"; //A registered username
$MyBot = new Wikibot($username, $password);
$page = $MyBot->get_page("Sausages");
echo $page;
?>
Mimic another user's page
Load someone else's user page, then update yours to be the same.
<?php
//grab class
include('Wikibot.php5');
//get password
$fhpw = fopen("password.txt", 'r') or die("Can't open password file");
$password = fgets($fhpw);
fclose($fhpw);
//create bot
$username = "MyBot"; //A registered username
$MyBot = new Wikibot($username, $password);
$page = $MyBot->get_page("User:Cool");
$MyBot->edit_page("User:$username",$page,"Updating my user page");
?>
Individual functions explained
Wikibot constructor
What it does: Creates a new Wikibot
Parameters:
- A username
- A password
- A Wiki identifier: most commonly a Wikipedia two-letter language code, e.g. "en". (Optional, defaults to "en".)
- A max number of edits per minute. (Optional, defaults to 5.)
- A max-lag (Optional, defaults to 5.)
Returns: A bot object ready for your use.
get_page
What it does: gets the contents of a given page
Parameters:
- The name of the page
- The wiki to use, if different to one you specified already. (Optional.)
Returns: A string containing the entire contents of that page, in Wiki format.
get_cats_of_page
What it does: fills an array with the names of all of the categories to which a given page belongs.
Parameters:
- The name of the page
- The wiki to use, if different to one you specified already. (Optional.)
Returns: An array filled with the names of categories.
create_page
What it does: creates a new page (won't touch the page if it already exists)
Parameters:
- The name of the page.
- The text, in wiki format, to place on the page.
- An edit summary.
- Use the minor flag? Either true or false. (Optional, defaults to false.)
- Use the bot flag? Either true or false. (Optional, defaults to true.)
- The wiki to use, if different to one you specified already. (Optional.)
Returns: true/false depending on result.
edit_page
What it does: edits a page (won't touch the page if it doesn't exist already).
Parameters:
- The name of the page.
- The text, in wiki format, to place on the page.
- An edit summary.
- Use the minor flag? Either true or false. (Optional, defaults to false.)
- Use the bot flag? Either true or false. (Optional, defaults to true.)
- The wiki to use, if different to one you specified already. (Optional.)
Returns: true/false depending on result.
category
What it does: lists the members of a category (non-recursive).
Parameters:
- The name of the category.
- A limit on the number of the pages to retrieve. (Optional, defaults to 500 which is the maximum possible unless your user has the apihighlimits right (e.g. it's flagged or an adminbot), in which case it's 5000.)
- A namespace to use. (Optional, defaults to "all".)
- The wiki to use, if different to one you specified already. (Optional.)
- A starting point, usually the "next" value from a previous search. (Optional, defaults to "".)
Returns: An array with the names of the members of the category. An extra item is added to the end of the array: the "next" value, ready to be fed to the function next time.
Frequently asked questions
Does it work?
I haven't personally used it for a while now, but it should do: nothing's changed since.
Can I edit it? What licence is it released under?
It is everything you would expect Wikipedia to be: free (beer) and free (speech). You can edit the files yourself, or publish updates/extra functions straight to the file.
Does this create exclusion-compliant bots?
As of version 0.2.1, released February 14, 2009
, yes. It is effectively compulsory for all bots using the framework.Other questions? Problems?
If you have any questions of problems, just shout on my talk page.
Footnotes
- ^ As in, actually editing pages, rather than just reading them. Editing any pages without permission is at best considered bad form and at worst can result in a block.