This page must use the Rübÿ Spëëd Mëtäl Cödïng template!!!111

This commit is contained in:
AstRobot 2005-12-11 03:39:53 +00:00
parent 287122c1b9
commit 9bd28f0602
1 changed files with 2 additions and 53 deletions

View File

@ -1,11 +1,7 @@
[[Kategorie:Ruby]]
Der [[Date Determinator]] ist ein MediaWiki-Bot, welcher Terminfindungen vereinfachen sollen. Die Eingabe passiert als YAML, die Ausgabe kann vom Bot periodisch als hybsche Tabelle erstellt werden.
=Beispiele=
==CodingNight zu dritt==
'''Eingabe:'''
BEGIN DATA "Test"
Cool Hacker:
@ -18,7 +14,6 @@ Der [[Date Determinator]] ist ein MediaWiki-Bot, welcher Terminfindungen vereinf
24.5.: jo
23.5.: yezz!
END DATA
'''Ausgabe:'''
<!-- BEGIN TABLE "Test" -->
{| border="1" cellpadding="2" cellspacing="0" style="border-collapse:collapse;"
@ -49,11 +44,8 @@ Der [[Date Determinator]] ist ein MediaWiki-Bot, welcher Terminfindungen vereinf
|'''1'''
|}
<!-- END TABLE -->
==Realitätsabgleich 2==
''Hier sind die Daten in einem HTML-Kommentar. '''Artikel bearbeiten''' um zu schauen.''
<!-- BEGIN DATA "R2"
Astro:
16.6.: j
@ -133,9 +125,7 @@ toidinamai:
23.6.: j
24.6.: j
25.6.: j
END DATA -->
<!-- BEGIN TABLE "R2" -->
{| border="1" cellpadding="2" cellspacing="0" style="border-collapse:collapse;"
|-
@ -252,11 +242,8 @@ END DATA -->
|'''3'''
|}
<!-- END TABLE -->
=Source=
Open-source, yada-yada!
==date_determinator.rb==
<pre>
WIKI_USER = 'AstRobot'
@ -265,28 +252,21 @@ PAGE = 'Benutzer:Astro/Date_Determinator'
WIKI = 'http://wiki.c3d2.de/wikipgedia'
HTTP_USER = 'eris'
HTTP_PASSWORD = '...'
require 'yaml'
require 'mediawiki'
include MediaWiki
class DateUser
attr_reader :name
def initialize(name, hsh)
@name = name
@hsh = hsh
end
def dates
@hsh.keys
end
def day(d)
@hsh[d]
end
def day_class(d)
if day(d) == nil
nil
@ -298,7 +278,6 @@ class DateUser
""
end
end
def day_style(d)
case day_class(d)
when nil then ""
@ -308,7 +287,6 @@ class DateUser
end
end
end
class DateData
def initialize(yaml)
@users = []
@ -322,19 +300,16 @@ class DateData
@error = e.to_s
end
end
def table
return @error if @error
s = ''
##
# Collect dates
##
dates = []
dates_yes = {}
@users.each { |user| dates += user.dates }
##
# Sort dates
##
@ -358,7 +333,6 @@ class DateData
a <=> b
end
}
##
# Construct header
##
@ -367,7 +341,6 @@ class DateData
s += "!#{date}\n"
dates_yes[date] = 0
}
##
# Construct rows
##
@ -378,7 +351,6 @@ class DateData
dates_yes[date] += 1 if user.day_class(date) == true
}
}
##
# Build summary
##
@ -389,13 +361,9 @@ class DateData
s += "|}"
end
end
wiki = Wiki.new(WIKI, HTTP_USER, HTTP_PASSWORD)
wiki.login(WIKI_USER, WIKI_PASSWORD)
page = wiki.article(PAGE)
datasets = {}
current_data_name = nil
current_data_yaml = ''
@ -411,45 +379,34 @@ page.text.split(/\n/).each { |line|
current_data_yaml += "#{line}\n"
end
}
text_old = page.text.dup
signature = /(<!-- BEGIN TABLE ")(.+?)(" -->)(.+?)(<!-- END TABLE -->)/m
page.text.gsub!(signature) { |part|
begin1,name,begin2,obsolete,end1 = part.match(signature).to_a[1..-1]
table = datasets[name] ? datasets[name].table : "DATA #{name} not found!"
"#{begin1}#{name}#{begin2}\n#{table}\n#{end1}"
}
page.submit('Date Determinator run') if page.text != text_old
</pre>
==mediawiki.rb==
<pre>
require 'http-access2'
require 'cgi'
require 'rexml/document'
module MediaWiki
class Wiki
##
# HTTPAccess2 object, must be accessible by the Wiki's
# Article children
attr_accessor :http
def initialize(url, auth_name=nil, auth_password=nil)
@url = (url =~ /\/$/) ? url : "#{url}/"
@http = HTTPAccess2::Client.new(nil, 'Ruby-MediaWiki::Wiki/0.1')
add_auth(url, auth_name, auth_password) if auth_name and auth_password
end
def add_auth(uri, auth_name, auth_password)
@http.set_basic_auth(uri, auth_name, auth_password)
end
def login(username, password)
postdata = "wpName=#{CGI::escape(username)}&wpPassword=#{CGI::escape(password)}&wpLoginattempt="
result = @http.post_content(article_url('Special:Userlogin'), postdata)
@ -457,7 +414,6 @@ module MediaWiki
raise "Unable to authenticate as #{username}"
end
end
##
# Return a new article with the given name
# name:: [String] Article name
@ -465,13 +421,11 @@ module MediaWiki
def article(name)
Article.new(self, name)
end
##
# Return
def article_url(name)
"#{@url}index.php?title=#{CGI::escape(name)}"
end
##
# Return a hash of special pages:
# 'Link title' => 'Article name'
@ -483,30 +437,25 @@ module MediaWiki
result
end
end
class Article
attr_accessor :name, :text
def initialize(wiki, name, load_text=true)
@wiki = wiki
@name = name
@text = nil
@xhtml = nil
@xhtml_cached = false
@wp_edittoken = nil
@wp_edittime = nil
reload if load_text
end
def xhtml
unless @xhtml_cached
xhtml_reload
end
@xhtml
end
def xhtml_reload
html = @wiki.http.get_content("#{@wiki.article_url(@name)}")
html.scan(/<!-- start content -->(.+)<!-- end content -->/m) { |content,|
@ -515,7 +464,6 @@ module MediaWiki
@xhtml_cached = true
end
def reload
puts "Loading #{@wiki.article_url(@name)}&action=edit"
doc = REXML::Document.new(@wiki.http.get_content("#{@wiki.article_url(@name)}&action=edit")).root
@ -529,7 +477,6 @@ module MediaWiki
# wpEditToken might be missing, that's ok
end
end
##
# TODO: minor_edit, watch_this
def submit(summary, minor_edit=false, watch_this=false)
@ -541,3 +488,5 @@ module MediaWiki
end
end
</pre>
{{Rübÿ Spëëd Mëtäl Cödïng}}