Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

[Ruby] Script/bot to send MediaWiki Recent Changes over UDP to a Campfire room

Requires the Tinder gem, and PHP on the server running MediaWiki must be compiled with --enable-sockets. The computer running the bot will also have to be open to the web - it can't be behind a firewall.

First, add this code to your MediaWiki LocalSettings.php, replacing the IP address with the IP of the server/computer running the bot, and the port to whatever you want to use (probably something above 40,000, but it's up to you):
$wgRC2UDPAddress = '69.178.6.244';
$wgRC2UDPPort = '41895';
$wgRC2UDPPrefix = "";


Now, you can run the bot! This is extremely hackish, because the retarded collective-mailing-list-coding system of the MediaWiki repos has somehow managed to code IRC color codes directly into their exported strings (at least with MediaWiki 1.2alpha, which is what I'm running right now).
You'll need to replace the new tinder definition with your subdomain on Campfire, the username and password with ones for the bot account you've created on your campfire room, and the room name with the room you want the updates to be sent to.
require 'rubygems'
require 'tinder'
require 'socket'
puts 'Dependencies loaded...'

campfire = Tinder::Campfire.new 'subdomain'
campfire.login 'email@domain.com', 'password'
exit unless room = campfire.find_room_by_name('Case Sensitive Room Name')
puts 'Campfire logged in...'

server = UDPSocket.new
exit unless server.bind('0.0.0.0', 41895)
puts 'Socket listening...'

loop do
  msg = server.recv(2048).gsub(/\003[\d]{0,2}/,'').chomp
  print "sending to room: #{msg.inspect}..."
  exit unless room.speak msg.to_s
  puts ' sent!'
end

Converting Twiki formatting to MediaWiki

// It's not necessarily the cleanest, but it got the job done on an internal transfer we were doing. Your mileage may vary by quite a bit.

def convert_from_twiki_to_mediawiki(old_page_text)
  new_page_text = ""
  
  # shifting the tables over
  while (true)
    if (old_page_text.index(/\n\|/).nil?)
      new_page_text += old_page_text
      break
    end
        
    # table extraction
    table_index = old_page_text.index("\n|")
    first_chunk = old_page_text.slice!(0..table_index)
    table_chunk = ""
    old_page_text.each_line do |line|
      if line[0..0] == '|'
        table_chunk += line
      else
        break
      end
    end
    old_page_text.slice!(table_chunk)
    
    # table conversion
    table_chunk.gsub!(/\|/, "||")
    table_chunk.gsub!(/\|\|\s*$/, "\n|-")
    table_chunk.gsub!(/^\|\|/, "|")
    table_chunk.gsub!(/\|-\s*\Z/, "|}")
    table_chunk.gsub!(/\A\|/, "{|class=\"wikitable sortable\"\n|")
    
    # process new table looking for headings
    new_table_chunk = ""
    table_chunk.each_line do |line|
      if line.match(/\*\s.*\s\*/)
        line.gsub!(/\|\|/, "!!")
        line.gsub!(/^\|/, "!")
        line.gsub!("*", '')
      end
      
      new_table_chunk += line
    end
    
    new_page_text += first_chunk + new_table_chunk
  end
  
  # bold
  new_page_text.gsub!(/(^|[\s\(])\*([^ ].*?[^ ])\*([\s\)\.\,\:\;\!\?]|$)/, "\\1'''\\2'''\\3")
  # italic bold
  new_page_text.gsub!(/(^|[\s\(])\_\_([^ ].*?[^ ])\_\_([\s\)\.\,\:\;\!\?]|$)/, "\\1''<b>\\2<\/b>''\\3") 
  # italic
  new_page_text.gsub!(/(^|[\s\(])\_([^ ].*?[^ ])\_([\s\)\.\,\:\;\!\?]|$)/, "\\1''\\2''\\3")
  # monospaced bold
  new_page_text.gsub!(/(^|[\s\(])==([^ ].*?[^ ])==([\s\)\.\,\:\;\!\?]|$)/, "\\1'''<tt>\\2<\/tt>'''\\3")
  # monospaced
  new_page_text.gsub!(/(^|[\s\(])=([^ ].*?[^ ])=([\s\)\.\,\:\;\!\?]|$)/, "\\1<tt>\\2<\/tt>\\3")
  # H6
  new_page_text.gsub!(/(^|[\n\r])---\+\+\+\+\+\+([^\n\r]*)/, "\\1======\\2 ======")
  # H5
  new_page_text.gsub!(/(^|[\n\r])---\+\+\+\+\+([^\n\r]*)/, "\\1=====\\2 =====")
  # H4
  new_page_text.gsub!(/(^|[\n\r])---\+\+\+\+([^\n\r]*)/, "\\1====\\2 ====")
  # H3
  new_page_text.gsub!(/(^|[\n\r])---\+\+\+([^\n\r]*)/, "\\1===\\2 ===")
  # H2
  new_page_text.gsub!(/(^|[\n\r])---\+\+([^\n\r]*)/, "\\1==\\2 ==")
  # H1
  new_page_text.gsub!(/(^|[\n\r])---\+([^\n\r]*)/, "\\1=\\2 =")
  # external link
  new_page_text.gsub!(/\[\[(https?\:.*?)\]\[(.*?)\]\]/, "\[\\1 \\2\]")
  # internal link [[WikiWord][WikiWord]]
  new_page_text.gsub!(/\[\[([^\]]*)\]\]/, "\[\[\\1\|\\1\]\]")
  # internal lnk [[WikiWord][label]]
  new_page_text.gsub!(/\[\[([^\]]*)\]\[(.*?)\]\]/, "\[\[\\1\|\\2\]\]")
  # level 1 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{3}\* /, "\\1*")
  # level 2 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{6}\* /, "\\1**")
  # level 3 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{9}\* /, "\\1***")
  # level 4 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{12}\* /, "\\1****")
  # level 5 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{15}\* /, "\\1*****")
  # level 6 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{18}\* /, "\\1******")
  # level 7 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{21}\* /, "\\1*******")
  # level 8 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{24}\* /, "\\1********")
  # level 9 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{27}\* /, "\\1*********")
  # level 10 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{30}\* /, "\\1**********")
  # level 1 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{3}[0-9]\.? /, "\\1#")
  # level 2 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{6}[0-9]\.? /, "\\1##")
  # level 3 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{9}[0-9]\.? /, "\\1###")
  # level 4 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{12}[0-9]\.? /, "\\1####")
  # level 5 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{15}[0-9]\.? /, "\\1#####")
  # level 6 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{18}[0-9]\.? /, "\\1######")
  # level 7 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{21}[0-9]\.? /, "\\1#######")
  # level 8 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{24}[0-9]\.? /, "\\1########")
  # level 9 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{27}[0-9]\.? /, "\\1#########")
  # level 10 bullet
  new_page_text.gsub!(/(^|[\n\r])[ ]{30}[0-9]\.? /, "\\1##########")
    
  
  new_page_text
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS