REBOL [
    Title: "Wikipedia Library"
    Date: 2-Jan-2003/19:53:46+1:00
    Version: 1.0.0
    File: %wikilib.r
    Author: "Athymik"
    Purpose: "Provide tools in Rebol to interact with Wikipedia"
    Email: athymik@ifrance.com
    Category: [4 web util text tcp net lib]
]

WikiLib: make object! [
wublck: ["http://" ".wikipedia.org/w/wiki.phtml?title=" "&action=submit"]

url: func ["Create the url" wiki titre] [return to-url rejoin [wublck/1 wiki wublck/2 (url-encode titre) wublck/3]]

time: func ["Get the time in Wiki format" /local now_year now_month now_day ntsp wt] [
    now_year: to-string now/year
    now_month: to-string now/month
    now_day: to-string now/day
    if (length? now_month) = 1 [now_month: rejoin ["0" now_month]]
    if (length? now_day) = 1 [now_day: rejoin ["0" now_day]]
    ntsp: parse (to-string now/time) ":"
    ntsp/1: to-string ((to-integer ntsp/1) - time-lag)
    wt: rejoin [now_year now_month now_day rejoin ntsp]
    return wt
]

get-ts: func ["Get the timestamp of an article" wiki article /local raw_txt sraw_txt ts] [
    raw_txt: read to-url rejoin ["http://" wiki ".wikipedia.org/w/wiki.phtml?title=" (url-encode article) "&action=edit"]
    set [sraw_txt ts] ""
    parse/all raw_txt [
        thru "</textarea>"
        copy sraw_txt
        to "</form>"
    ]
    parse/all sraw_txt [
        thru {<input type="hidden" value="2}
        copy ts
        to {"}
    ]
    ts: rejoin ["2" ts]
    return ts]

post: func ["Post an article on wikipedia" wiki titre texte sommaire modif_mineure timestamp /extended user-agent /local wikiurl param z] [
    wikiurl: url wiki titre
    texte: url-encore texte
    params: rejoin [{&wpTextbox1=} texte {&wpSummary=} sommaire {&wpSave=} "" {&wpEdittime=} timestamp {&wpMinoredit=} modif_mineure]
    z: open/custom/no-wait/direct wikiurl reduce ['POST params]
    close z
]

parse-html-entities:  func [texte] [
    replace/all texte "&lt;" "<"
    replace/all texte "&gt;" ">"
    replace/all texte "&quot;" {"}
    replace/all texte "&amp;" {&}
    return texte
]

url-encode: func [
    {URL-encode a string}
    data "String to encode"
    /local new-data
][
    new-data: make string! ""
    normal-char: charset [
        #"A" - #"Z" #"a" - #"z"
        #"@" #"." #"*" #"-" #"_"
        #"0" - #"9"
    ]
    if not string? data [return new-data]
    forall data [
        append new-data either find normal-char first data [
            first data
        ][rejoin ["%" to-string skip tail (to-hex to-integer first data) -2]]
    ]
    return new-data
]

get: func ["Get an article from wikipedia" wiki titre /local pagedef] [
        pagedef: copy ""
        page: read url wiki (url-encode titre)
        parse page [thru "<textarea tabindex=1 name='wpTextbox1' rows=25 cols=80 wrap=virtual>" copy pagedef to "</textarea>"]
    pagedef: parse-html-entities pagedef
        return reduce [pagedef (get-ts wiki titre)]
]

append: func ["Append text to an article" wiki titre texte sommaire modif_mineure /local z] [
    z: wikilib/get wiki titre
    append z texte
    wikilib/post wiki titre z sommaire modif_mineure (get-ts wiki titre)
]

]