User:Mutante/Eggdrop TCL script for mediawiki

From Meta, a Wikimedia project coordination wiki

This is a tcl script for eggdrop, enabling some mediawiki features on IRC channels.

# mediawiki.tcl v0.5
# A Mediawiki <-> IRC - Eggdrop TCL script
# 
# IRC features: 
# - search in page titles (.title <string>) 
# - search in fulltext (.content <string>)
# - display page content (.display <pagename>)
# - display popular pages (.popular)
# - display sysops (.sysops)
# - display statistics (.stats)
# - place/remove IP bans (.ban / .unban <IP>)
# - teach other bot (infobot) (.teach <pagename>)
#
# tested on mediawiki 1.4/1.5 and eggdrop 1.6.13/1.6.16/1.6.17
#
# Mediawiki: http://meta.wikimedia.org/wiki/Mediawiki | #mediawiki on Freenode
# Eggdrop: http://eggheads.org/ | http://egghelp.org/ | #egghelp on Efnet
#
# by mutante (http://s23.org/wiki/User:Mutante) (mutante@s23.org) of
# the S23 Cabal (http://s23.org/wiki/) | #wiki, #bots on Efnet
#
# v0.23 (for 1.4 mediawiki) pre-release by [Hyarion]'s request. 25.10.2005
# v0.50 updated mysql queries due to mediawiki 1.5 upgrade 05.01.2006
#
# licensed under "Attribution-NonCommercial-ShareAlike 2.0"
# http://creativecommons.org/licenses/by-nc-sa/2.0/
#
# for the .teach feature you need an infobot running in the same channel (http://infobot.org)
#
# this script uses "libmysqltcl" for the TCL<->Mysql connection
# you can get it from http://rufus.o-town.de/eggdrop/
#
set file /usr/local/lib/mysqltcl-2.40/libmysqltcl2.40
load ${file}[info sharedlibextension]
##

####
# CONFIG

# MYSQL
# database host
set myhost "localhost"
# database user
set myuser "wikiuser"
# database name
set mydb "wikidb"
# database password
set mypass "<password>"

# BASIC
# Name of your Wiki
set mywiki "S23-Wiki"
# (All Your Base..) URL (the part before the wiki page name)
set mybaseurl "http://s23.org/wiki"

# MISC

# number of recent changes displayed on .rc
set myrcnum "5"
# max number of results shown on .find and .content
set myfindlimit "5"
# number of days a spammer is blocked on .ban
set myspamdays "5"
# name of another bot (infobot) used on .teach
set myotherbot "padma"
# number of popular pages displayed on .popular
set mypopnum "5"
# the date your wiki was setup ,used on .stats
set mysetupdate "Feb,04 2005"
# number of characters shown of a wikipage content on .display
set mydisplaychars "180"
####

### the (public) triggers

# Recent changes
bind pub - .rc wikirc

# Search in titles
bind pub - .find findpage
bind pub - .title findpage

# Search fulltext
bind pub - .content findcontent
bind pub - .full findcontent

# Show popular pages
bind pub - .popular popular

# Display page content (first x chars)
bind pub - .display display

# Teach other (info)bot
bind pub - .teach teach

# Show Statistics
bind pub - .stats wikistats

# Show Sysops
bind pub - .sysops sysops

# add / remove IP bans (only for users with +W flag)

bind pub W .ban ban
bind pub W .unban unban

# help
bind pub - .help help


# the procs

proc help {nick host hand chan args} {
puthelp "privmsg $chan :  Mediawiki.tcl script by S23/mutante. Here are the triggers, $nick"
puthelp "privmsg $chan : .title <string> - SEARCHes for a string in page TITLEs (Alias .find) (max $::myfindlimit hits)"
puthelp "privmsg $chan : .content <string> - SEARCHes for a string in the pages CONTENT (Alias .full) (max $::myfindlimit hits)"
puthelp "privmsg $chan : .display <page> - SHOWs the CONTENT of a wiki page (first $::mydisplaychars chars)"
puthelp "privmsg $chan : .rc - SHOWs the last $::myrcnum CHANGES on the Wiki (Recent Changes)"
puthelp "privmsg $chan : .popular - SHOW the $::mypopnum most POPULAR wiki pages (hit count)"
puthelp "privmsg $chan : .sysops - SHOW the wiki SYSOPS"
puthelp "privmsg $chan : .stats - SHOW the wiki site STATISTICS"
puthelp "privmsg $chan : .ban <IP> - ADD an IP BAN from the Wiki (for $::myspamdays days with reason 'spammer')"
puthelp "privmsg $chan : .unban <page> - REMOVE an IP BAN from the Wiki."
puthelp "privmsg $chan : .teach <page> - Bonus Trigger: TEACH $::myotherbot about <page> ;)"
puthelp "privmsg $chan : .help - SHOW this HELP message"
puthelp "privmsg $chan : (K)2006 The S23-Cabal - visit us on http://s23.org/wiki/"
}

proc wikistats {nick host hand chan args} {
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]
mysqlsel $db {SELECT ss_total_views,ss_total_edits,ss_good_articles from site_stats}
putquick "privmsg $chan :$::mywiki Site-Stats"
mysqlmap $db {ss_total_views ss_total_edits ss_good_articles} {
putquick "privmsg $chan :Total Views: $ss_total_views Total Edits: $ss_total_edits Good Articles: $ss_good_articles (since mediawiki setup on $::mysetupdate)"
}
mysqlendquery $db

mysqlclose $db
}

proc sysops {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# mysqlsel $db {SELECT user_name,user_email FROM user WHERE user_rights LIKE "%sysop%"}
# changes due to mediawiki 1.5 upgrade

set query "select user_name,user_email from user_rights join user on user_id=ur_user where ur_rights!='' order by user_name"

mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki Sysops (alphabetical order)"
mysqlmap $db {user_name user_email} {
puthelp "privmsg $chan :$user_name - $user_email"
}
mysqlendquery $db
mysqlclose $db
}

proc display {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# set query "SELECT left(cur_text,120) as cur_sum FROM cur WHERE cur_title='$args'"
# changes due to mediawiki 1.5 upgrade

set query "select left(si_text,$::mydisplaychars) as si_sum from searchindex join page on page_id=si_page where page_title='$args'"

mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki - Displaying page '$args' (first $::mydisplaychars chars)"
mysqlmap $db {si_sum} {
puthelp "privmsg $chan :$si_sum ... continued on $::mybaseurl/$args"
}
mysqlendquery $db
mysqlclose $db
}
proc ban {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]
set query "INSERT into ipblocks (ipb_address,ipb_user,ipb_by,ipb_reason,ipb_timestamp,ipb_auto,ipb_expiry) values ('$args','0','74','spammer (banned via IRC)',NOW()+0,'0',ADDDATE(NOW(),INTERVAL 1 DAY)+0);"
mysqlsel $db $query
puthelp "privmsg $chan :Ok, $nick, blocked '$args' for $::myspamdays days with reason 'spammer'"
mysqlendquery $db
mysqlclose $db
}

proc unban {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]
set query "DELETE from ipblocks WHERE ipb_address='$args';"
mysqlsel $db $query
puthelp "privmsg $chan :Ok, $nick, removed block on '$args'"
mysqlendquery $db
mysqlclose $db
}

proc teach {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# set query "SELECT left(cur_text,120) as cur_sum FROM cur WHERE cur_title='$args'"
# changes due to mediawiki 1.5 upgrade

set query "select left(si_text,180) as si_sum from searchindex join page on page_id=si_page where page_title='$args'"

mysqlsel $db $query
mysqlmap $db {si_sum} {
puthelp "privmsg $chan :$::myotherbot, $args is $si_sum ... $::mybaseurl/$args"
}
mysqlendquery $db
mysqlclose $db
}
proc wikirc {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]
set query "select rc_id,rc_title,rc_comment,rc_user_text from recentchanges order by rc_id desc LIMIT 0,$::myrcnum"
mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki - Last $::myrcnum changes"
mysqlmap $db {rc_id rc_title rc_comment rc_user_text} {
puthelp "privmsg $chan :#$rc_id '$rc_title' $rc_comment by $rc_user_text ($::mybaseurl/$rc_title)"
}
mysqlendquery $db
mysqlclose $db
}

proc findpage {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# set query "SELECT cur_title FROM cur WHERE cur_title LIKE '%$args%' LIMIT 0,$::myfindlimit"
# changes due to mediawiki 1.5 upgrade

# set query "select page_id,page_title,page_counter from page join revision on rev_id = page_id where page_title LIKE '%$args%' limit 0,$::myfindlimit"

set query "select page_title from page where lcase(page_title) LIKE '%$args%' LIMIT 0,$::myfindlimit"

mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki - Searching for page titles containing '$args'"
mysqlmap $db {page_title} {
puthelp "privmsg $chan :Found page $page_title ($::mybaseurl/$page_title)"
}
mysqlendquery $db
mysqlclose $db
}
proc findcontent {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# set query "SELECT cur_title FROM cur WHERE cur_text LIKE '%$args%' LIMIT 0,$::myfindlimit"
# changes due to mediawiki 1.5 upgrade

set query "select page_title from searchindex join page on page_id=si_page where si_text LIKE '%$args%' LIMIT 0,$::myfindlimit"
mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki - Searching full text for pages containing $args"
mysqlmap $db {page_title} {
puthelp "privmsg $chan :Found page $page_title ($::mybaseurl/$page_title)"
}
mysqlendquery $db
mysqlclose $db
}


proc popular {nick host hand chan args} {
global myhost,myuser,mypass,mydb
set db [mysqlconnect -host $::myhost -user $::myuser -password $::mypass -db $::mydb]

# old query for 1.4 mediawiki
# set query "select cur_title,cur_counter from cur ORDER by cur_counter DESC LIMIT 1,$::mypopnum"
# changes due to mediawiki 1.5 upgrade
set query "select page_title,page_counter from page order by page_counter desc limit 1,$::mypopnum"

mysqlsel $db $query
puthelp "privmsg $chan :$::mywiki Top $::mypopnum popular pages"
mysqlmap $db {page_title page_counter} {
puthelp "privmsg $chan :$page_title ($page_counter hits) - ($::mybaseurl/$page_title)"
}
mysqlendquery $db
mysqlclose $db
}

putlog "Mediawiki.tcl 0.5 by S23 loaded. Fnord. http://s23.org/wiki/"
# fnord


Related Links: