Index: trunk/tools/wikibot/RPEDIRCBot.php |
— | — | @@ -0,0 +1,111 @@ |
| 2 | +<?php |
| 3 | +# RPEDIRC.php by Tisane, http://www.mediawiki.org/wiki/User:Tisane |
| 4 | +# |
| 5 | +# This script is free software that is available under the terms of the Creative Commons |
| 6 | +# Attribution 3.0 license and the current version of the GNU General Public License. |
| 7 | +# |
| 8 | +# The purpose of this script is to get the titles of all new, moved, deleted and restored |
| 9 | +# pages from #en.wikipedia and add/delete them from the rped_table. |
| 10 | + |
| 11 | +error_reporting( E_ALL | E_STRICT ); |
| 12 | + |
| 13 | +include 'wikibot.classes.php'; /* The wikipedia classes. */ |
| 14 | + |
| 15 | +$bot = $argv[1]; |
| 16 | +$wiki = $argv[2]; |
| 17 | +if ( !isset( $argv[3] ) || $argv[3] != "nodaemon" ){ |
| 18 | + $daemonize = true; |
| 19 | +} |
| 20 | + |
| 21 | +$wpapi = new wikipediaapi ( '', '', '', $bot, $wiki, true ); |
| 22 | +$wpq = new wikipediaquery ( '', '', '', $bot, $wiki, true ); |
| 23 | +$wpi = new wikipediaindex ( '', '', '', $bot, $wiki, true ); |
| 24 | +$user = getWikibotSetting( 'user', $bot, $wiki ); |
| 25 | +$pass = getWikibotSetting( 'pass', $bot, $wiki ); |
| 26 | +$host = getWikibotSetting( 'host', $bot, $wiki ); |
| 27 | +$port = getWikibotSetting( 'port', $bot, $wiki ); |
| 28 | +$nick = getWikibotSetting( 'nick', $bot, $wiki ); |
| 29 | +$ident = getWikibotSetting( 'ident', $bot, $wiki ); |
| 30 | +$realname = getWikibotSetting( 'realname', $bot, $wiki ); |
| 31 | +$chan = getWikibotSetting( 'chan', $bot, $wiki ); |
| 32 | +$deleteLine = getWikibotSetting( 'deleteline', $bot, $wiki ); |
| 33 | +$moveLine = getWikibotSetting( 'moveline', $bot, $wiki ); |
| 34 | +$deletedWord = getWikibotSetting( 'deletedword', $bot, $wiki ); |
| 35 | +$newCharacter = getWikibotSetting( 'thenewcharacter', $bot, $wiki ); |
| 36 | + |
| 37 | +if ( $wpapi->login( $user, $pass ) != 'true' ) { |
| 38 | + echo "Login failure\n"; |
| 39 | + die(); |
| 40 | +} |
| 41 | + |
| 42 | +$readbuffer = ""; |
| 43 | +$startSep = "[["; |
| 44 | +$endSep = "]]"; |
| 45 | + |
| 46 | +// open a socket connection to the IRC server |
| 47 | +$fp = fsockopen( $host, $port, $erno, $errstr, 30 ); |
| 48 | + |
| 49 | +// print the error if there is no connection |
| 50 | +if ( !$fp ) { |
| 51 | + echo $errstr . " (" . $errno . ")<br />\n"; |
| 52 | + die(); |
| 53 | +} |
| 54 | + |
| 55 | +// write data through the socket to join the channel |
| 56 | +fwrite( $fp, "NICK " . $nick . "\r\n" ); |
| 57 | +fwrite( $fp, "USER " . $ident . " " . $host . " bla :" . $realname . "\r\n" ); |
| 58 | +fwrite( $fp, "JOIN :" . $chan . "\r\n" ); |
| 59 | + |
| 60 | +# Launch daemon! |
| 61 | +if ( isset( $daemonize ) ) { |
| 62 | + |
| 63 | + $pid = pcntl_fork(); // fork |
| 64 | + if ( $pid < 0 ) { |
| 65 | + exit; |
| 66 | + } |
| 67 | + else if ( $pid ){ // parent |
| 68 | + exit; |
| 69 | + } |
| 70 | + // child |
| 71 | + $sid = posix_setsid(); |
| 72 | + if ( $sid < 0 ) { |
| 73 | + exit; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +while ( !feof( $fp ) ) { |
| 78 | + $line = fgets( $fp, 512 ); |
| 79 | + $pingLine = explode( ' ', $line ); |
| 80 | + if ( strtolower( $pingLine[0] ) == 'ping' ) { |
| 81 | + $response = "PONG " . $pingLine[1] . "\n"; |
| 82 | + fwrite( $fp, "PONG " . $response ); |
| 83 | + } |
| 84 | + usleep( 10 ); |
| 85 | + $startPos = strpos( $line, $startSep ); |
| 86 | + $endPos = strpos( $line, $endSep ); |
| 87 | + $subLine = substr( $line, $startPos + 5, $endPos -$startPos -8 ); |
| 88 | + if ( $subLine == $deleteLine ) { |
| 89 | + $delstartPos = strpos( $line, $startSep, $endPos ); |
| 90 | + $delendPos = strpos( $line, $endSep, $endPos + 1 ); |
| 91 | + $delLine = substr( $line, $delstartPos + 5, $delendPos -$delstartPos -8 ); |
| 92 | + $action = substr( $line, $delstartPos -9, 7 ); |
| 93 | + if ( $action == $deletedWord ) { |
| 94 | + $wpapi->rpedDelete( $delLine ); |
| 95 | + } else { |
| 96 | + $wpapi->rpedInsert( $delLine ); |
| 97 | + } |
| 98 | + } |
| 99 | + if ( $subLine == $moveLine ) { |
| 100 | + $delstartPos = strpos( $line, $startSep, $endPos ); |
| 101 | + $delendPos = strpos( $line, $endSep, $endPos + 1 ); |
| 102 | + $delstartPos = strpos( $line, $startSep, $delstartPos + 1 ); |
| 103 | + $delendPos = strpos( $line, $endSep, $delendPos + 1 ); |
| 104 | + $delLine = substr( $line, $delstartPos + 2, $delendPos -$delstartPos -2 ); |
| 105 | + $wpapi->rpedInsert( $delLine ); |
| 106 | + } |
| 107 | + if ( substr( $line, $endPos + 5, 1 ) == $newCharacter || substr( $line, $endPos + 6, 1 ) == $newCharacter ) { |
| 108 | + $wpapi->rpedInsert( $subLine ); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fclose( $fp ); |
\ No newline at end of file |