User:DustyBot/Library
Dusty Library provides a set of functions you can use to write your own bot in PHP. The benefits of including this library are its simplicity, functionality, and compatibility. Care has been taken to avoid dependencies on PHP extensions. The library also attempts to emulate the standard PHP function set, as opposed to creating a new framework. It is also intended to be compatible with PHP 4, though this has not been tested. Bots using this library have been run successfully on both EN and Commons.
To download Dusty Library, go to User:DustyBot/dustylib.php, view the page source, and then copy it to disk, avoiding the source tags.
Function reference
[edit]Function-level documentation is included in the source file.
Template parsing
[edit]- wp_parse_template($text)
- wptmpl_create($title)
- wptmpl_set_arg(&$tmpl, $param, $val)
- wptmpl_unset_arg(&$tmpl, $param)
- wptmpl_get_arg($tmpl, $param)
- wptmpl_has_arg($tmpl, $param)
- wptmpl_set_title(&$tmpl, $title)
- wp_build_template($tmpl)
- wp_find_template($title, $text, $ignore_case = false)
Page editing
[edit]- wp_page_allows_bot($text, $context = null, $messages = null)
- wp_get_edit_token($title, $context)
- wp_edit_page($title, $content, $summary, $edtoken, $context, $timestamp = null)
Query
[edit]- wp_get($title, $context = null, &$timestamp = null)
- wp_get_multiple($titles, $context = null, &$timestamps = null)
- wp_locate_files($files, $context)
- wp_get_category_members($category, $context = null)
Session management
[edit]- wp_create_context($maxlag = null, $bot = false, $api_url = null)
- wp_context_set_query_limit($limit, &$context)
- wp_login($username, $password, &$context)
- wp_logout($context)
Other
[edit]- wp_post($post, $context = null)
Example
[edit]This basic example overwrites your user page with a list of all the pages in Category:Space colonization. You'll have to supply your own username and password.
include "dustylib.php";
$context = wp_create_context(5);
/* Set the limit for the number of items listed in each query
* This is only an optimization, the # of pages from get_cat_members is unchanged */
wp_context_set_query_limit(50, $context);
wp_login($username, $password, $context);
$title = "User:".$username;
$last_timestamp = null;
/* Get the timestamp of the last revision to avoid edit conflicts */
$old_page = wp_get($title, $context, $last_timestamp);
/* Check for bot exclusion */
if (!wp_page_allows_bot($old_page, $context)) {
wp_logout($context);
exit();
}
/* Get the list of members */
$members = wp_get_category_members("Space colonization", $context);
/* Create the new page contents */
$page = "";
foreach ($members as $title)
$page .= "* [[".$title."]]\n";
/* Upload it */
$edtoken = wp_get_edit_token($title, $context);
wp_edit_page($title, $page, "List of interesting pages", $edtoken, $context, $last_timestamp);
wp_logout($context);