Menu

[r31]: / trunk / bgbot.php  Maximize  Restore  History

Download this file

508 lines (435 with data), 15.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<?php
# $Id$
#
# A Wikimedia bot - used for automated editing of pages on Wikipedia
# and its sister projects
#
# 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 <b.manolov at web dot de>
# http://purl.oclc.org/NET/borislav/
# Bot's home page: http://phpwikibot.sourceforge.net/
#############################################################################
require_once('browser.php');
require_once('utils.php');
require_once('wikis.php');
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.3)';
var $page_name;
var $page_is_redirect;
var $page_content;
var $page_timestamp;
var $log = 'bot.log';
var $echo_log = true;
var $localdb = 'local.txt';
var $special_nss; // translations of "Special" namespace
var $sites; // all wiki sites
function Bgbot($wiki='bg', $user='', $pass='') {
global $wiki_sites, $wiki_special_nss;
$this->wiki = $wiki;
$this->user = $user;
$this->pass = $pass;
$this->special_nss = & $wiki_special_nss;
$this->sites = & $wiki_sites;
$this->base_url = 'http://{{DOMAIN}}/w/wiki.phtml?title=';
$this->page_url = $this->base_url .'{{PAGE}}';
$this->special_url = $this->base_url .'{{SPECIAL}}';
$this->export_url = $this->special_url .':Export';
$this->fetch_url = $this->export_url .'/{{PAGE}}';
$this->submit_url = $this->page_url .'&action=submit';
$this->delete_url = $this->page_url .'&action=delete';
$this->history_url = $this->page_url .'&action=history';
$this->diff_url = $this->page_url .'&diff=0';
$this->move_url = $this->special_url .':Movepage&action=submit';
$this->login_url = $this->special_url .':Userlogin&action=submit';
$this->upload_url = $this->special_url .':Upload';
$params = array(
'agent' => $this->agent,
);
$this->browser = new Browser($params);
}
# fetch a page
# $page - page name (in proper encoding for the wiki or urlencoded)
# $wiki - a wiki code
# $get_time - whether to return a timestamp
function fetch($page, $wiki='', $get_time = true, $follow_redirect = true) {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}', '{{PAGE}}');
$r = array($this->sites[$wiki], $this->special_nss[$wiki], my_urlencode($page));
$url = str_replace($s, $r, $this->fetch_url);
if ( $this->browser->fetch($url) ) {
$text_content = getCDATA('text', $this->browser->content);
if ($follow_redirect) {
if ($this->is_redirect($text_content)) {
preg_match('/\[\[([^]]+)\]\]/', $text_content, $matches);
$new_page = $matches[1];
$this->log("$wiki: [[". urldecode($page) .
"]] redirects to [[$new_page]]");
$this->page_is_redirect = true;
return $this->fetch($new_page, $wiki, $get_time, false);
} else {
$this->page_is_redirect = false;
}
}
if ($get_time) {
$this->page_timestamp = getCDATA('timestamp', $this->browser->content);
$this->page_timestamp = preg_replace('/\D/', '', $this->page_timestamp);
}
$this->page_content = html_entity_decode($text_content);
$this->page_name = urldecode($page);
if ( empty($this->page_content) ) {
$this->page_timestamp = 0;
}
} else {
$this->log('Fetch error: '. $this->browser->error);
return false;
}
return true;
}
# return true if given page is redirect, false otherwise
function is_redirect($content) {
return $content{0} === '#';
}
# fetch a set of pages
# $pages - array with page names (in proper encoding for the wiki)
# $wiki - a wiki code
# $get_time - whether to return a timestamp
function fetch2local($pages, $wiki='', $get_time = true) {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->domains[$wiki], $this->specials[$wiki]);
$url = str_replace( $s, $r, $this->export_url );
$vars = array(
'curonly' => 'true',
'action' => 'submit',
);
foreach ($pages as $page) {
$vars['pages'] .= "$page\n";
}
if ( $this->browser->submit($url, $vars) ) {
$content = $this->browser->content;
if ($get_time) {
$timestamps = getCDATAs('timestamp', preg_replace('/\D/', '', $content));
}
$contents = getCDATAs('text', $content);
$contents = array_map('html_entity_decode', $contents);
$i = 0;
foreach ($pages as $page) {
$this->content_many[$page] = array($contents[$i], $timestamps[$i]);
$i++;
}
} else {
$this->log('Fetch_m error: '. $this->browser->error);
return false;
}
return true;
}
# submit a page
# $page - page name (in proper encoding for the wiki or urlencoded)
# $content - content to be submitted
# $summary - summary text
# $isminor - should the edit be marked as minor
# $wiki - wiki code
function submit($page, $content, $summary, $wiki='', $isminor = true) {
global $latin;
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{PAGE}}');
$r = array($this->sites[$wiki], my_urlencode($page));
$url = str_replace( $s, $r, $this->submit_url );
if ( in_array($wiki, $latin) ) {
$content = utf8_decode($content);
}
$vars = array(
'wpTextbox1' => $content,
'wpSummary' => $summary,
'wpEdittime' => $this->page_timestamp,
);
if ($isminor) { $vars['wpMinoredit'] = 1; }
if (!$this->is_logged()) { $this->login($wiki); }
$dpage = urldecode($page);
if ( $this->browser->submit($url, $vars) ) {
if (preg_match("/<h1.+$dpage.*<\/h1>/", $this->browser->content)) {
$this->log("$wiki: [[$dpage]] was submitted.");
} else {
$this->log("$wiki: [[$dpage]] was NOT submitted. Probably the server is down or there were some other problems.");
}
} else {
$this->log('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='') {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->sites[$wiki], $this->special_nss[$wiki]);
$url = str_replace( $s, $r, $this->upload_url );
$vars = array(
'wpUploadDescription' => $desc,
'wpUploadAffirm' => 1,
);
$file_field = array('wpUploadFile' => $file);
if (!$this->is_logged()) { $this->login($wiki); }
if ( $this->browser->submit($url, $vars, $file_field) ) {
$this->log("$wiki: $file was uploaded.");
} else {
$this->log('Upload error: '. $this->browser->error);
return false;
}
return true;
}
# move a page to other name on wiki
# $page - page which will be moved to
# $new_page - new page name (in proper encoding)
# $wiki - wiki code
function move($page, $new_page, $wiki='') {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->sites[$wiki], $this->special_nss[$wiki]);
$url = str_replace( $s, $r, $this->move_url );
$page = str_replace(' ', '_', $page);
$new_page = str_replace('_', ' ', $new_page);
$vars = array(
'wpOldTitle' => $page,
'wpNewTitle' => $new_page,
'wpMovetalk' => 1, // move the talk page (discussion) too
);
if (!$this->is_logged()) { $this->login($wiki); }
if ( $this->browser->submit($url, $vars) ) {
$this->log("$wiki: [[$page]] was moved to [[$new_page]].");
} else {
$this->log('Move error: '. $this->browser->error);
return false;
}
return true;
}
# delete a page
# $page - page to be deleted (in proper encoding for the wiki)
# $reason - reason for deletion (Unicode)
# $wiki - wiki code
function delete($page, $reason, $wiki='') {
global $latin;
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{PAGE}}');
$r = array($this->sites[$wiki], my_urlencode($page));
$url = str_replace( $s, $r, $this->delete_url );
$vars = array(
'wpReason' => $reason,
'wpConfirm' => 1, // confirm deletion
);
if (!$this->is_logged()) { $this->login($wiki); }
if ( $this->browser->submit($url, $vars) ) {
$this->log("$wiki: [[". urldecode($page) .']] was deleted.');
} else {
$this->log('Delete error: '. $this->browser->error);
return false;
}
return true;
}
# return true if bot is logged on $wiki, false otherwise
# $wiki - wiki code
# $user - user name
function is_logged($wiki = '', $user = '') {
if ( empty($wiki) ) { $wiki = $this->wiki; }
if ( empty($user) ) { $user = $this->user; }
$is_logged = false;
if ( isset($this->browser->cookies[$this->sites[$wiki]]) ) {
foreach ($this->browser->cookies[$this->sites[$wiki]]
as $cookie_name => $cookie_vals) {
if ($user == $cookie_vals[0]) {
$is_logged = true;
}
}
}
return $is_logged;
}
# login to wiki
# $wiki - wiki code
# $user - user name
# $pass - user password
function login($wiki = '', $user = '', $pass = '') {
if (empty($wiki)) { $wiki = $this->wiki; }
if (empty($user)) { $user = $this->user; }
if (empty($pass)) { $pass = $this->pass; }
if ( empty($wiki) || empty($user) || empty($pass) ) { return false; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->sites[$wiki], $this->special_nss[$wiki]);
$url = str_replace( $s, $r, $this->login_url );
$vars = array(
'wpName' => $user,
'wpPassword' => $pass,
'wpRemember' => 1,
);
if ( $this->browser->submit($url, $vars) ) {
$this->log('['. date('Y/m/d') .'] '. $user . " is logged on $wiki:");
} else {
$this->log("Login error: ". $this->browser->error);
return false;
}
return true;
}
# signup to wiki
# $wiki - wiki code
# $user - chosen user name
# $pass - chosen user password
function signup($wiki, $user='', $pass='') {
if ( empty($user) ) { $user = $this->user; }
if ( empty($pass) ) { $pass = $this->pass; }
$s = array('{{DOMAIN}}', '{{SPECIAL}}');
$r = array($this->sites[$wiki], $this->special_nss[$wiki]);
$url = str_replace( $s, $r, $this->login_url );
$vars = array(
'wpName' => $user,
'wpPassword' => $pass,
'wpRetype' => $pass,
'wpCreateaccount' => 1
);
if ( $this->browser->submit($url, $vars) ) {
$this->log('['. date('Y/m/d') .'] '. $user . " is signuped on $wiki:");
} else {
$this->log("Signup error: ". $this->browser->error);
return false;
}
return true;
}
function log($msg) {
$msg = '['. date('H:i:s') .'] '. $msg ."\n";
if ($this->echo_log) { echo $msg; }
my_fwrite($this->log, $msg);
}
# update interwikis in a page (add or delete)
# $page - page name
# $content - page content
# return string with all updated interwikis
function update_interwikis($page, $content) {
$langs[] = $this->wiki;
$tmp[$start] = $this->get_interwikis($content);
$wikis = 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_interwikis($content);
$langs[] = $lang2;
$wikis[$lang2] = $page;
}
}
}
ksort($wikis);
$str = "\n\n";
foreach ($wikis as $wiki => $page) {
$str .= "\n[[$wiki:$page]]";
}
return $str;
}
# extract interwikis from a page
# $content - content of a page
# return assoc array of interwikis (wikicode => pagename)
function get_interwikis($content) {
global $latin;
$count = preg_match_all('/\[\[(\w+):(.+)\]\]/U', $content, $matches);
$wikis = array();
for ($i = 0; $i < $count; $i++) {
$wiki = $matches[1][$i];
if ( array_key_exists( $wiki, $this->special_nss ) ) {
$page = str_replace(array(' ', '&amp;'), 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);
$wikis[$wiki] = $page;
}
}
ksort($wikis);
return $wikis;
}
# strip interwikis from a page
# $content - content of a page
# return content without interwikis
function strip_interwikis($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
# $replace - force replacement if the interwiki $wiki exists
# return content with the new interwiki
function add_interwiki($local_wiki, $content, $page, $wiki='', $replace = false) {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$page = trim($page);
$new_interwiki = "[[$wiki:$page]]";
$has_interwiki = preg_match("/\[\[$wiki:([^]]+)\]\]/", $content, $matches);
if ($has_interwiki) {
$cur_page = trim($matches[1]);
if ( $replace && $page != $cur_page ) {
$this->log("The interwiki [[$wiki:$cur_page]] already exists on $local_wiki:. Replacing it with [[$wiki:$page]].");
return preg_replace("/\[\[$wiki:[^]]+\]\]/", $new_interwiki, $content);
} else {
$this->log("The interwiki [[$wiki:$cur_page]] already exists on $local_wiki:.");
return $content;
}
}
$pos = 0;
reset($this->sites);
$found = false;
while ( list($wiki_code, ) = each($this->sites) ) {
if ( $wiki == $wiki_code ) { break; }
}
$is_added = false;
$one_line_interwikis = array('fr', 'hu', 'pl');
while ( list($wiki_code, ) = each($this->sites) ) {
$search = "[[$wiki_code:";
$pos = strpos($content, $search, $pos);
if ( $pos !== false ) {
if ( !in_array($local_wiki, $one_line_interwikis) ) {
$new_interwiki .= "\n";
}
$content = substr_replace($content, $new_interwiki, $pos, 0);
$is_added = true;
break;
}
}
if ( !$is_added ) { $content .= "\n\n$new_interwiki"; }
return $content;
}
# $page - page name (in proper encoding for the wiki)
# $wiki - wiki code
# return last diff url for page
function get_diff_url($page, $wiki='') {
if ( empty($wiki) ) { $wiki = $this->wiki; }
$s = array('{{DOMAIN}}', '{{PAGE}}');
$r = array($this->sites[$wiki], $page);
return str_replace( $s, $r, $this->diff_url );
}
} // end of class Bgbot
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.