Fix #2709 , patch by Zigger
[lhc/web/wiklou.git] / irc / rcdumper.php
1 <?php
2 /**
3 * A recentchanges IRC bot
4 *
5 * Example command-line where irc is the name of the ircII executable:
6 * php rcdumper.php | irc -d -c \#channel nick server
7 * The name of the IRC server should match $ircServer below.
8 *
9 * @package MediaWiki
10 * @subpackage IRC
11 */
12
13 $ircServer = "irc.freenode.net";
14
15 # Set the below if this is running on a non-Wikimedia site:
16 #$serverName="your.site.here";
17
18 ini_set( "display_errors", 1 );
19 $wgCommandLineMode = true;
20 $optionsWithArgs = array( 'm' );
21 require_once("../maintenance/commandLine.inc" );
22
23 if ( !empty( $options['m'] ) ) {
24 $channel = $options['m'];
25 } else {
26 $channel = false;
27 }
28
29 if ($lang == "commons") $site = "wikimedia";
30
31 if ($wgWikiFarm) {
32 $serverName="$lang.$site.org";
33 $newPageURLFirstPart="http://$serverName/wiki/";
34 $URLFirstPart="http://$serverName/w/wiki.phtml?title=";
35 } else {
36 $newPageURLFirstPart="http://$serverName$wgScript/";
37 $URLFirstPart="http://$serverName$wgScript?title=";
38 }
39
40 $wgTitle = Title::newFromText( "RC dumper" );
41 $wgCommandLineMode = true;
42 set_time_limit(0);
43
44 if ( empty($options['b']) ) {
45 $bots = "AND NOT(rc_bot)";
46 } else {
47 $bots = "";
48 }
49
50 if (isset($args[0]) && isset($args[1])) {
51 $lowest = $args[0];
52 $highest = $args[1];
53 #$what = $args[0][0];
54 #$highest = $args[0][1];
55 }
56 #sleep(30);
57
58 $res = $dbr->query( "SELECT rc_timestamp FROM $recentchanges ORDER BY rc_timestamp DESC LIMIT 1" );
59 $row = $dbr->fetchObject( $res );
60 $oldTimestamp = $row->rc_timestamp;
61 $serverCount = 0;
62
63 while (1) {
64 $res = $dbr->query( "SELECT * FROM $recentchanges WHERE rc_timestamp>'$oldTimestamp' ORDER BY rc_timestamp" );
65 $rowIndex = 0;
66 while ( $row = $dbr->fetchObject( $res ) ) {
67 if ( ++$serverCount % 20 == 0 ) {
68 print "/server $ircServer\n";
69 }
70 $ns = $wgLang->getNsText( $row->rc_namespace ) ;
71 if ( $ns ) {
72 $title = "$ns:{$row->rc_title}";
73 } else {
74 $title = $row->rc_title;
75 }
76 /*if ( strlen( $row->rc_comment ) > 50 ) {
77 $comment = substr( $row->rc_comment, 0, 50 );
78 } else {*/
79 $comment = $row->rc_comment;
80 // }
81 $bad = array("\n", "\r");
82 $empty = array("", "");
83 $comment = str_replace($bad, $empty, $comment);
84 $title = str_replace($bad, $empty, $title);
85 $a = $title[0];
86 if ($a < 'A' || $a > 'Z')
87 $a = 'Z';
88 #if ((isset($highest)) && (($what == "<" && $a > "$highest") || ($what == ">" && $a <= "$highest")))
89 if ((isset($highest) && ($a > $highest)) || (isset($lowest) && $a <= $lowest))
90 continue;
91 $user = str_replace($bad, $empty, $row->rc_user_text);
92 $lastid = IntVal($row->rc_last_oldid);
93 $flag = ($row->rc_minor ? "M" : "") . ($row->rc_new ? "N" : "");
94 $stupid_urlencode = array("%2F", "%3A");
95 $haha_take_that = array("/", ":");
96 if ( $row->rc_new ) {
97 $url = $newPageURLFirstPart . urlencode($title);
98 } else {
99 $url = $URLFirstPart . urlencode($title) .
100 "&diff=0&oldid=$lastid";
101 }
102 $url = str_replace($stupid_urlencode, $haha_take_that, $url);
103 $title = str_replace("_", " ", $title);
104 # see http://www.irssi.org/?page=docs&doc=formats for some colour codes. prefix is \003,
105 # no colour (\003) switches back to the term default
106 $comment = preg_replace("/\/\* (.*) \*\/(.*)/", "\00315\$1\003 - \00310\$2\003", $comment);
107 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
108 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 \00310$comment\003\n";
109 if ( $channel ) {
110 $fullString = "$channel\t$fullString";
111 }
112
113 print( $fullString );
114 $oldTimestamp = $row->rc_timestamp;
115 sleep(2);
116 }
117 sleep(5);
118 }
119
120 exit();
121
122 ?>