1# Wikit Utilities
2#
3# RecentChanges - return the Recent Changes special page
4# InfoProc - decode references from a page
5# graburl - grab the contents of a Url
6# AddLogEntry - create an external log entry for a page change
7
8package provide Wikit::Utils 1.0
9package require Wikit::Cache
10
11namespace eval Wikit {
12  namespace export InfoProc
13
14  # Special page: Recent Changes.
15  proc RecentChanges {{db wdb}} {
16    set count 0
17    set result ""
18    set lastDay 0
19    set threshold [expr {[clock seconds] - 7 * 86400}]
20
21    foreach id [mk::select $db.pages -rsort date] {
22      lassign [mk::get $db.pages!$id date name who] date name who
23
24      # these are fake pages, don't list them
25      if {$id == 2 || $id == 4} continue
26
27      # only report last change to a page on each day
28      set day [expr {$date/86400}]
29
30      #insert a header for each new date
31      incr count
32      if {$day != $lastDay} {
33        # only cut off on day changes and if over 7 days reported
34        if {$count > 100 && $date < $threshold} {
35          append result "''Older entries omitted...''"
36          break
37        }
38
39        set lastDay $day
40        append result "'''[clock format $date -gmt 1 -format {%B %e, %Y}]'''\n"
41      }
42
43      append result "   * \[$name\] . . . $who\n"
44    }
45
46    return $result
47  }
48
49  # InfoProc - get a page for the reference
50  # Used for rendering Wiki pages in HTML and as styled text in Tk
51  proc InfoProc {db ref} {
52    set id [::Wikit::LookupPage $ref $db]
53    pagevarsDB $db $id date name
54
55    if {$date == 0} {
56      append id @ ;# enter edit mode for missing links
57    } else {
58      #append id .html
59    }
60
61    return [list /$id $name $date]
62  }
63
64
65  # graburl - grab the contents of a URL
66  proc graburl {url} {
67    set result ""
68    set token [::http::geturl $url -binary 1 -timeout 10]
69    upvar #0 $token state
70    if {$state(status) == "ok"} {
71      #puts "http: $state(http)"
72      if {[string match *404* $state(http)]} {
73        #puts "not found: $url"
74        return ""
75      }
76      set result $state(body)
77    } else {
78      #puts "grab? $state(status)"
79      return ""
80    }
81    ::http::cleanup $token
82    return $result
83  }
84
85  # Enters an external log entry for a changed into the changelog view if an
86  # external $env(WIKI_HIST) directory is present.  The names of the files for
87  # the changed pages contain their id and also data and changing entity. The
88  # date is encoded as time_t value. Sorting the filenames alpahabetically will
89  # allow other applications to created reports and diffs between revisions.
90
91  proc AddLogEntry {id db} {
92    upvar #0 env(WIKIT_HIST) ewh
93    set fmt {%e %b %Y %H:%M:%S GMT}
94
95    pagevarsDB $db $id date page who name
96
97    if {[info exists ewh] && [file isdirectory $ewh]} {
98      set t [string trim $page]
99      if {$t != ""} {
100        set fd [open $ewh/$id-$date-$who w]
101	fconfigure $fd -encoding utf-8 ;# 2006-01-12 ticket #1
102        puts $fd "Title:\t$name"
103        puts $fd "Date:\t[clock format $date -gmt 1 -format $fmt]"
104        puts $fd "Site:\t$who"
105        puts $fd ""
106        puts $fd $t
107        close $fd
108      }
109      set fd [open $ewh/.index a]
110      fconfigure $fd -encoding utf-8 ;# 2006-01-12 ticket #1
111      puts $fd [list x $id $date $who $name]
112      close $fd
113    }
114
115    #mk::row append $wdb.archive id $id name $name date $date who $who
116  }
117}
118