#!/usr/bin/env ruby
$:.unshift('vendor/ruby-mediawiki/lib')
$:.unshift('vendor/htree')
require 'mediawiki/dotfile'
@wiki = MediaWiki.dotfile(nil,'entropia')
@putwhere = "Benutzer:Neinomat/"

def link_to (title)
  if /^(Category|Kategorie):/.match(title)
    link = "[[:" + title + "]]"
  else
    link = "[[" + title + "]]"
  end
  link
end

def check_page_against_category (page, category)
  body  = "== #{page} ==\n\n"
  body += "[[Category:FIXME]]\n\n"
  body += "Unter [[#{page}]] fehlen folgende Artikel aus [[:#{category}]]:\n\n"

  count = 0
  page_html = @wiki.article(page).xhtml.to_s
  @wiki.category(category).articles.each do |title|
    # mjaammm...
    re = Regexp.new("(" + Regexp.escape("title=") + "(\"|')" +
                          Regexp.escape(CGI.escapeHTML(title)) + "(\"|')" +
                    "|>" + Regexp.escape(title) + "<)")
    if ! re.match(page_html) && title != page && title != @putwhere + page
      body += "* " + link_to(title) + "\n"
      count += 1
    end
  end

  body += "\n#{count} Artikel.\n"

  a = @wiki.article(@putwhere + page)
  a.text = body
  a.submit("wiki ex machina")    
end

#----------------------------------------------------------------------------

check_page_against_category("Übersicht über Vorträge", "Kategorie:Vorträge")
check_page_against_category("Übersicht über Projekte", "Kategorie:Projekte")
check_page_against_category("Entropia:FIXME",          "Kategorie:FIXME")