<?php
/*
* $Id$
*
* A Wikipedia bot
* used for automated editing of pages on wikipedia.org
*
* Copyright (C) 2004 Borislav Manolov
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* Author: Borislav Manolov
* E-mail: b.manolov@web.de
* Home page: http://purl.oclc.org/NET/manolov/
*/
include 'browser.php';
include 'utils.inc';
include 'wikies.inc';
include 'html2wiki-tables.inc';
class Bgbot {
var $wiki; // main wiki on which the bot will work
var $user; // user name
var $pass; // password for this user
var $browser; // browser object
var $agent = 'Mozilla/5.0 (Bgbot/0.2; +http://phpwikibot.sourceforge.net/)';
var $specials; // translations of "Special" namespace
var $domains; // all wiki domains
function Bgbot($wiki, $user, $pass) {
global $wiki_domains, $wiki_specials;
$this->wiki = $wiki;
$this->user = $user;
$this->pass = $pass;
$this->specials = & $wiki_specials;
$this->domains = & $wiki_domains;
if ( $wiki != 'local' ) {
$path1 = 'wiki';
$path2 = 'w/wiki.phtml';
} else {
// for local testing
$path1 = $path2 = 'wiki/index.php';
}
$this->fetch_url = 'http://{{DOMAIN}}/'.$path1.'/{{SPECIAL}}:Export/{{PAGE}}';
$this->upload_url = 'http://{{DOMAIN}}/'.$path1.'/{{SPECIAL}}:Upload';
$this->submit_url = 'http://{{DOMAIN}}/'.$path2.'?title={{PAGE}}&action=submit';
$this->login_url = 'http://{{DOMAIN}}/'.$path2.'?title={{SPECIAL}}:Userlogin&action=submit';
$params = array(
'agent' => $this->agent,
);
$this->browser = new Browser($params);
$this->login($wiki);
}
/*
* fetch a page
* $page - name of the page
* $wiki - a wiki code
* $get_time - whether to return a timestamp
*/
function fetch($page, $wiki, $get_time = true) {
$s = array('{{DOMAIN}}', '{{SPECIAL}}', '{{PAGE}}');
$r = array($this->domains[$wiki], $this->specials[$wiki], $page);
$url = str_replace( $s, $r, $this->fetch_url );
if ( $this->browser->fetch($url) ) {
$content = $this->browser->results;
if ($get_time) {
$timestamp = getCDATA('timestamp', $content);
$timestamp = preg_replace('/\D/', '', $timestamp);
$ret['timestamp'] = $timestamp;
}
$content = getCDATA('text', $content);
$ret['content'] = html_entity_decode($content);
if ( empty($content) ) {
return false;
}
} else {
echo "\n".'Fetch error: '. $this->browser->error;
return false;
}
return $ret;
}
/*
* submit the page
* $page - page name
* $content - content to be submitted
* $timestamp - timesptamp of last page edit
* $summary - summary text
* $wiki - wiki code
*/
function submit($page, $content, $timestamp, $summary, $wiki) {
$s = array('{{DOMAIN}}', '{{PAGE}}');
$r = array($this->domains[$wiki], $page);
$url = str_replace( $s, $r, $this->submit_url );
$vars = array(
'wpTextbox1' => $content,
'wpSummary' => $summary,
'wpEdittime' => $timestamp,
'wpMinoredit' => 1,
);
if ( $this->browser->submit($url, $vars) ) {
echo "\n". $url . ' - Done.';
} else {
echo "\n".'Submit error: '. $this->browser->error;
return false;
}
return true;
}
/*
* upload a file on wiki
* $file - file to be uploaded
* $desc - description for this file
* $wiki - wiki code
*/
function upload($file, $desc, $wiki) {
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->domains[$wiki], $this->specials[$wiki]);
$url = str_replace( $s, $r, $this->upload_url );
$vars = array(
'wpUploadDescription' => $desc,
'wpUploadAffirm' => 1,
);
$file_field = array('wpUploadFile' => $file);
if ( $this->browser->submit($url, $vars, $file_field) ) {
echo "\n". $url . ' - Done.';
} else {
echo "\n".'Upload error: '. $this->browser->error;
return false;
}
return true;
}
/*
* login to wiki
* $wiki - wiki code
*/
function login($wiki) {
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->domains[$wiki], $this->specials[$wiki]);
$url = str_replace( $s, $r, $this->login_url );
$vars = array(
'wpName' => $this->user,
'wpPassword' => $this->pass,
'wpRemember' => 1,
);
if ( !$this->browser->submit($url, $vars) ) {
echo "\n".'Login error: '. $this->browser->error;
return false;
} else echo $this->browser->results;
return true;
}
/*
* insert a text before some other text
* $content - main string
* $text2insert - text to be inserted
* $before - before this text will be added
* return main string with added text
*/
function insert($content, $text2insert, $before) {
$pos = strpos($content, $before);
$content = substr_replace($content, $text2insert, $pos, 0);
return html_entity_decode($content);
}
/*
* replace a text with some other text
* $content - main string
* $toreplace - text to be replaced
* $replacement - replacement text
* return main string with the replaced text
*/
function replace($content, $toreplace, $replacement) {
$content = str_replace($toreplace, $replacement, $content);
return html_entity_decode($content);
}
/*
* update interwikies in a page (add or delete)
* $page - page name
* $content - page content
* return string with all updated interwikies
*/
function update_interwikies($page, $content) {
$langs[] = $this->wiki;
$tmp[$start] = $this->get_interwikies($content);
$wikies = array();
for ($i = 0; $i < count($langs); $i++ ) {
$lang = $langs[$i];
foreach ($tmp[$lang] as $lang2 => $page) {
if ( in_array($lang2, $langs) ) {
continue;
}
$ret = $this->fetch($page, $lang2, false);
$content = $ret['content'];
if ( !empty($content) ) {
$tmp[$lang2] = $this->get_interwikies($content);
$langs[] = $lang2;
$wikies[$lang2] = $page;
}
}
}
ksort($wikies);
$str = "\n\n";
foreach ($wikies as $wiki => $page) {
$str .= "\n".'[[' . $wiki .':'. $page . ']]';
}
return $str;
}
/*
* extract interwikies from a page
* $content - content of a page
* return assoc array of interwikies (wikicode => pagename)
*/
function get_interwikies($content) {
global $latin1;
$count = preg_match_all('/\[\[(\w+):(.+)\]\]/U', $content, $matches);
$wikies = array();
for ($i = 0; $i < $count; $i++) {
$wiki = $matches[1][$i];
if ( array_key_exists( $wiki, $this->specials ) ) {
$page = str_replace(array(' ', '&'), array('_', '&'), $matches[2][$i]);
// replace html numeric entities with UTF-8
$callback = create_function('$matches', 'return unicode2utf8($matches[1]);');
$page = preg_replace_callback('/&#(\d+);/', $callback, $page);
$wikies[$wiki] = $page;
}
}
ksort($wikies);
return $wikies;
}
/*
* strip interwikies from a page
* $content - content of a page
* return content without interwikies
*/
function strip_interwikies($content) {
$content = preg_replace('/\[\[(\w+):(.+)\]\]/U', '', $content);
$content = trim($content);
return $content;
}
/*
* add an interwiki to a page
* $content - page content
* $page - page name to be added
* $wiki - wiki code
* return content with the new interwiki
*/
function add_interwiki($content, $page, $wiki = 'bg') {
$follows = array('bs', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'eo', 'es');
$new_interwiki = '[['. $wiki . ':'. $page . ']]'."\n";
$pos = 0;
foreach ( $follows as $follow ) {
$search = '[['. $follow . ':';
$pos = strpos($content, $search, $pos);
if ( $pos !== false ) {
$content = substr_replace($content, $new_interwiki, $pos, 0);
break;
}
}
return $content;
}
} // end class Bgbot
?>