User:LivingBot/Wikibot

From Wikipedia, the free encyclopedia
Wikibot
Original author(s)Jarry1250
Initial releaseFebruary 8, 2009; 15 years ago (2009-02-08)
Stable release
0.2.1 / February 14, 2009; 15 years ago (2009-02-14)
Written inPHP
LicenseGNU 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[edit]

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[edit]

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.

  1. Create a new folder on your web server.
  2. Password protect access that folder with .htaccess (or similar). All files should be uploaded into that folder.
  3. Copy and paste the main code (see above) into a new file called Wikibot.php5 and upload it to your server.
  4. Create a new text file, password.txt. It should only contain your / your bot's Wikipedia password - no extra linebreaks or anything. Upload it.
  5. 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[edit]

These are some examples for what to put inside your MyBot.php5 file.

The bare basics[edit]

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[edit]

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[edit]

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[edit]

Wikibot constructor[edit]

What it does: Creates a new Wikibot

Parameters:

  1. A username
  2. A password
  3. A Wiki identifier: most commonly a Wikipedia two-letter language code, e.g. "en". (Optional, defaults to "en".)
  4. A max number of edits per minute. (Optional, defaults to 5.)
  5. A max-lag (Optional, defaults to 5.)

Returns: A bot object ready for your use.

get_page[edit]

What it does: gets the contents of a given page

Parameters:

  1. The name of the page
  2. 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[edit]

What it does: fills an array with the names of all of the categories to which a given page belongs.

Parameters:

  1. The name of the page
  2. The wiki to use, if different to one you specified already. (Optional.)

Returns: An array filled with the names of categories.

create_page[edit]

What it does: creates a new page (won't touch the page if it already exists)

Parameters:

  1. The name of the page.
  2. The text, in wiki format, to place on the page.
  3. An edit summary.
  4. Use the minor flag? Either true or false. (Optional, defaults to false.)
  5. Use the bot flag? Either true or false. (Optional, defaults to true.)
  6. The wiki to use, if different to one you specified already. (Optional.)

Returns: true/false depending on result.

edit_page[edit]

What it does: edits a page (won't touch the page if it doesn't exist already).

Parameters:

  1. The name of the page.
  2. The text, in wiki format, to place on the page.
  3. An edit summary.
  4. Use the minor flag? Either true or false. (Optional, defaults to false.)
  5. Use the bot flag? Either true or false. (Optional, defaults to true.)
  6. The wiki to use, if different to one you specified already. (Optional.)

Returns: true/false depending on result.

category[edit]

What it does: lists the members of a category (non-recursive).

Parameters:

  1. The name of the category.
  2. 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.)
  3. A namespace to use. (Optional, defaults to "all".)
  4. The wiki to use, if different to one you specified already. (Optional.)
  5. 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[edit]

Does it work?[edit]

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?[edit]

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?[edit]

As of version 0.2.1, released February 14, 2009; 15 years ago (2009-02-14), yes. It is effectively compulsory for all bots using the framework.

Other questions? Problems?[edit]

If you have any questions of problems, just shout on my talk page.

Footnotes[edit]

  1. ^ 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.