1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
;; Copyright 2007-2010, Matthew Welland.
;;
;; This program is made available under the GNU GPL version 2.0 or
;; greater. See the accompanying file COPYING for details.
;;
;; This program is distributed WITHOUT ANY WARRANTY; without even the
;; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.
;; twiki module
(require-extension sqlite3 regex posix md5 base64)
;; TODO
;;
;; * Inline tiddlers [inline[TiddlerName]]
;; * Pics [pic X Y[picname.jpg]]
;; * Move twiki parsing/expanding to mattsutils as loadable module
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
;; Copyright 2007-2010, Matthew Welland.
;;
;; This program is made available under the GNU GPL version 2.0 or
;; greater. See the accompanying file COPYING for details.
;;
;; This program is distributed WITHOUT ANY WARRANTY; without even the
;; implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.
;; twiki module
(require-extension sqlite3 regex posix md5 base64)
(import (prefix base64 base64:))
;; TODO
;;
;; * Inline tiddlers [inline[TiddlerName]]
;; * Pics [pic X Y[picname.jpg]]
;; * Move twiki parsing/expanding to mattsutils as loadable module
|
︙ | | | ︙ | |
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
;; This is the currently supported mechanism. Postgres will be added later -mrw- 7/26/2009
;;
(define (twiki:open-db key . create-not-ok)
;; (s:log "Got to twiki:open-db with key: " key)
(let* ((create-ok (if (null? create-not-ok) #t (car create-not-ok)))
(fdat (twiki:key->fname key))
(basepath (slot-ref s:session 'twikidir))
(fpath (car fdat))
(fname (cadr fdat))
(fullname (conc basepath "/" fpath "/" fname))
(fexists (file-exists? fullname))
(db (if fexists (dbi:open 'sqlite3 (list (cons 'dbname fullname))) #f)))
(if (and (not db)
(not create-ok))
(exit 100)
(begin
(if (not fexists)
(begin
;; (print "fullname: " fullname)
(twiki:register-wiki key fullname)
(system (conc "mkdir -p " fpath)) ;; create the path
(set! db (dbi:open 'sqlite3 (list (cons 'dbname fullname))))
(for-each
(lambda (sqry)
;; (print sqry)
(dbi:exec db sqry))
;; types: 0 text, 1 jpg, 2 png, 3 svg, 4 spreadsheet, 5 audio, 6 video :: better specs to come...
(list
|
|
>
>
>
|
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
;; This is the currently supported mechanism. Postgres will be added later -mrw- 7/26/2009
;;
(define (twiki:open-db key . create-not-ok)
;; (s:log "Got to twiki:open-db with key: " key)
(let* ((create-ok (if (null? create-not-ok) #t (car create-not-ok)))
(fdat (twiki:key->fname key))
(basepath (sdat-get-twikidir s:session))
(fpath (car fdat))
(fname (cadr fdat))
(fullname (conc basepath "/" fpath "/" fname))
(fexists (file-exists? fullname))
(db (if fexists (dbi:open 'sqlite3 (list (cons 'dbname fullname))) #f)))
(if (and (not db)
(not create-ok))
(exit 100)
(begin
(if (not fexists)
(begin
;; (print "fullname: " fullname)
(twiki:register-wiki key fullname)
(system (conc "mkdir -p " fpath)) ;; create the path
(if (file-exists? fpath)
(s:log "OK: dir " fpath " has been made")
(s:log "ERROR: Failed to make the path for the twiki"))
(set! db (dbi:open 'sqlite3 (list (cons 'dbname fullname))))
(for-each
(lambda (sqry)
;; (print sqry)
(dbi:exec db sqry))
;; types: 0 text, 1 jpg, 2 png, 3 svg, 4 spreadsheet, 5 audio, 6 video :: better specs to come...
(list
|
︙ | | | ︙ | |
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
"CREATE TABLE members (id INTEGER PRIMARY KEY,person_id INTEGER,group_id INTEGER);"
;; setup and configuration data
"CREATE TABLE meta (id INTEGER PRIMARY KEY,key TEXT,val TEXT);"
;; need to create an entry for *this* twiki
(conc "INSERT INTO wikis (id,name,created_on) VALUES (1,'main'," (current-seconds) ");")))
;; (conc "INSERT INTO tiddlers (wiki_id,name,created_on) VALUES(1,'MainMenu'," (current-seconds) ");")))))
(twiki:save-tiddler db "MainMenu" "[[FirstTiddler]]" "" 1 1)))
(sqlite3:set-busy-timeout!(dbi:db-conn db) 1000000)
db))))
;;======================================================================
;; twikis (db naming, sqlite vs postgresql, keys etc.
;;======================================================================
;; A wiki is specified by a list of keys, here we convert that list to a single string
|
|
|
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
"CREATE TABLE members (id INTEGER PRIMARY KEY,person_id INTEGER,group_id INTEGER);"
;; setup and configuration data
"CREATE TABLE meta (id INTEGER PRIMARY KEY,key TEXT,val TEXT);"
;; need to create an entry for *this* twiki
(conc "INSERT INTO wikis (id,name,created_on) VALUES (1,'main'," (current-seconds) ");")))
;; (conc "INSERT INTO tiddlers (wiki_id,name,created_on) VALUES(1,'MainMenu'," (current-seconds) ");")))))
(twiki:save-tiddler db "MainMenu" "[[FirstTiddler]]" "" 1 1)))
;; (sqlite3:set-busy-timeout!(dbi:db-conn db) 1000000)
db))))
;;======================================================================
;; twikis (db naming, sqlite vs postgresql, keys etc.
;;======================================================================
;; A wiki is specified by a list of keys, here we convert that list to a single string
|
︙ | | | ︙ | |
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
(p1 (substring keypath 0 delta)) ;; 0 8))
(p2 (substring keypath delta (* delta 2)));; 8 16))
(p3 (substring keypath (* delta 2) (* delta 3)))) ;; 16 24))
(list (string-intersperse (list "twikis" p1 p2 p3) "/") keypath)))
;; look up the wid based on the keys, this is used for sub wikis only. I.e. a wiki instantiated inside another wiki
;; giving a separate namespace to all the tiddlers
(define (twiki:name->wid db name) ;; (slot-ref s:session 'conn)
(let ((wid (dbi:get-one db "SELECT id FROM wikis WHERE name=?;" name)))
(if wid wid
(begin
(dbi:exec db "INSERT INTO wikis (name,created_on) VALUES(?,?);" name (current-seconds))
(twiki:name->wid db name)))))
;;======================================================================
|
|
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
(p1 (substring keypath 0 delta)) ;; 0 8))
(p2 (substring keypath delta (* delta 2)));; 8 16))
(p3 (substring keypath (* delta 2) (* delta 3)))) ;; 16 24))
(list (string-intersperse (list "twikis" p1 p2 p3) "/") keypath)))
;; look up the wid based on the keys, this is used for sub wikis only. I.e. a wiki instantiated inside another wiki
;; giving a separate namespace to all the tiddlers
(define (twiki:name->wid db name)
(let ((wid (dbi:get-one db "SELECT id FROM wikis WHERE name=?;" name)))
(if wid wid
(begin
(dbi:exec db "INSERT INTO wikis (name,created_on) VALUES(?,?);" name (current-seconds))
(twiki:name->wid db name)))))
;;======================================================================
|
︙ | | | ︙ | |
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
;;======================================================================
;; twiki registry
;;======================================================================
;; these can be overridden by end user (just create a new routine by the same name)
(define (twiki:open-registry)
(let* ((basepath (slot-ref s:session 'sroot))
(regfile (conc basepath "/twikis/registry.db"))
(regexists (file-exists? regfile))
(db (dbi:open 'sqlite3 (list (cons 'dbname regfile)))))
(if regexists
db
(begin
(for-each (lambda (stmt)(dbi:exec db stmt))
|
|
|
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
;;======================================================================
;; twiki registry
;;======================================================================
;; these can be overridden by end user (just create a new routine by the same name)
(define (twiki:open-registry)
(let* ((basepath (sdat-get-sroot s:session))
(regfile (conc basepath "/twikis/registry.db"))
(regexists (file-exists? regfile))
(db (dbi:open 'sqlite3 (list (cons 'dbname regfile)))))
(if regexists
db
(begin
(for-each (lambda (stmt)(dbi:exec db stmt))
|
︙ | | | ︙ | |
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
(s:a "delete" 'href (s:link-to (twiki:get-link-back-to-current) 'delete_tiddler tnum))(s:br)
(s:input-preserve 'type "text" 'name "twiki_title" 'size "58" 'maxlength "150")
(s:textarea-preserve 'type "textarea" 'name "twiki_body" 'rows "10" 'cols "65")
(s:p "Tags" (s:input-preserve 'type "text" 'name "twiki_tags" 'size "55" 'maxlength "150")))))
;; save a tiddler to the db for the twiki twik, getting data from the INPUT
(define (twiki:save-curr-tiddler tdb wid)
(formdat:printall (slot-ref s:session 'formdat) s:log)
(let* ((heading (s:get-input 'twiki_title))
(body (s:get-input 'twiki_body))
(tags (s:get-input 'twiki_tags))
(uid (twiki:get-id)))
;; (s:log "twiki:save-curr-tiddler heading: " heading " body: " body " tags: " tags)
(s:set! 'twiki_title heading)
(if body
|
|
|
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
(s:a "delete" 'href (s:link-to (twiki:get-link-back-to-current) 'delete_tiddler tnum))(s:br)
(s:input-preserve 'type "text" 'name "twiki_title" 'size "58" 'maxlength "150")
(s:textarea-preserve 'type "textarea" 'name "twiki_body" 'rows "10" 'cols "65")
(s:p "Tags" (s:input-preserve 'type "text" 'name "twiki_tags" 'size "55" 'maxlength "150")))))
;; save a tiddler to the db for the twiki twik, getting data from the INPUT
(define (twiki:save-curr-tiddler tdb wid)
(formdat:printall (sdat-get-formdat s:session) s:log)
(let* ((heading (s:get-input 'twiki_title))
(body (s:get-input 'twiki_body))
(tags (s:get-input 'twiki_tags))
(uid (twiki:get-id)))
;; (s:log "twiki:save-curr-tiddler heading: " heading " body: " body " tags: " tags)
(s:set! 'twiki_title heading)
(if body
|
︙ | | | ︙ | |
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
"INSERT INTO tiddlers (wiki_id,name,dat_id,created_on,owner_id) VALUES(?,?,?,?,?);"
wid heading dat-id (current-seconds) uid))
#t) ;; success
#f)) ;; non-success
;; text=0, jpg=1, png=2
(define (twiki:save-dat db dat type)
(let* ((md5sum (md5:digest dat))
(datid (twiki:dat-exists? db md5sum type))
(datblob (if (string? dat)
(string->blob dat)
dat)))
(if datid
datid
(begin
|
|
|
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
"INSERT INTO tiddlers (wiki_id,name,dat_id,created_on,owner_id) VALUES(?,?,?,?,?);"
wid heading dat-id (current-seconds) uid))
#t) ;; success
#f)) ;; non-success
;; text=0, jpg=1, png=2
(define (twiki:save-dat db dat type)
(let* ((md5sum (md5-digest dat))
(datid (twiki:dat-exists? db md5sum type))
(datblob (if (string? dat)
(string->blob dat)
dat)))
(if datid
datid
(begin
|
︙ | | | ︙ | |
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
(define (twiki:get-thumb-dat tdb wid pic-id)
(dbi:get-one tdb "SELECT dat FROM pics INNER JOIN dats ON pics.thumb_dat_id=dats.id WHERE pics.id=? AND wiki_id=?;" pic-id wid))
;; this one sets up the Content type, puts the data into page-dat and is done
(define (twiki:return-image-dat tdb wid pic-id)
(let ((dat (twiki:get-pic-dat tdb wid pic-id)))
(s:log "twiki:return-image-dat dat is: " dat " of size: " (if (blob? dat)(blob-size dat) "[not a blob]"))
(slot-set! s:session 'page-type 'image)
(slot-set! s:session 'content-type "image/jpeg")
(slot-set! s:session 'alt-page-dat dat)))
;; (session:alt-out s:session)))
;; this one sets up the Content type, puts the data into page-dat and is done
(define (twiki:return-thumb-dat tdb wid pic-id)
(let ((dat (twiki:get-thumb-dat tdb wid pic-id)))
(s:log "twiki:return-image-dat dat is: " dat " of size: " (if (blob? dat)(blob-size dat) "[not a blob]"))
(slot-set! s:session 'page-type 'image)
(slot-set! s:session 'content-type "image/jpeg")
(slot-set! s:session 'alt-page-dat dat)))
;; (session:alt-out s:session)))
(define (twiki:make-thumbnail tdb pic-id wid)
(let ((indat (twiki:get-pic-dat tdb wid pic-id)))
;; (outdat (open-output-string)))
(let-values (((inp oup pid)(process "convert" (list "-size" "500x180" "-" "-thumbnail" "250x90" "-unsharp" "0x.5" "-"))))
(write-string (blob->string indat) #f oup)
|
|
|
|
|
|
|
|
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
(define (twiki:get-thumb-dat tdb wid pic-id)
(dbi:get-one tdb "SELECT dat FROM pics INNER JOIN dats ON pics.thumb_dat_id=dats.id WHERE pics.id=? AND wiki_id=?;" pic-id wid))
;; this one sets up the Content type, puts the data into page-dat and is done
(define (twiki:return-image-dat tdb wid pic-id)
(let ((dat (twiki:get-pic-dat tdb wid pic-id)))
(s:log "twiki:return-image-dat dat is: " dat " of size: " (if (blob? dat)(blob-size dat) "[not a blob]"))
(sdat-set-page-type! s:session 'image)
(sdat-set-content-type! s:session "image/jpeg")
(sdat-set-alt-page-dat! s:session dat)))
;; (session:alt-out s:session)))
;; this one sets up the Content type, puts the data into page-dat and is done
(define (twiki:return-thumb-dat tdb wid pic-id)
(let ((dat (twiki:get-thumb-dat tdb wid pic-id)))
(s:log "twiki:return-image-dat dat is: " dat " of size: " (if (blob? dat)(blob-size dat) "[not a blob]"))
(sdat-set-page-type! s:session 'image)
(sdat-set-content-type! s:session "image/jpeg")
(sdat-set-alt-page-dat! s:session dat)))
;; (session:alt-out s:session)))
(define (twiki:make-thumbnail tdb pic-id wid)
(let ((indat (twiki:get-pic-dat tdb wid pic-id)))
;; (outdat (open-output-string)))
(let-values (((inp oup pid)(process "convert" (list "-size" "500x180" "-" "-thumbnail" "250x90" "-unsharp" "0x.5" "-"))))
(write-string (blob->string indat) #f oup)
|
︙ | | | ︙ | |
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
|
(dbi:close tdb)
result))
;; should do a single more efficient query but this is good enough
(define (twiki:get-tiddlers db wid tnames)
(apply twiki:get-tiddlers-by-name db wid tnames))
;; (let* ((tdlrs '())
;; ;; (conn (slot-ref s:session 'conn))
;; (namelst (conc "('" (string-intersperse (map conc tnames) "','") "')"))
;; (qry (conc twiki:tiddler-selector " WHERE t.wiki_id=? AND t.id IN " namelst ";")))
;; ;; (print qry)
;; (dbi:for-each-row
;; (lambda (row)
;; (set! tdlrs (cons row tdlrs)))
;; db qry wid)
;; (reverse tdlrs))) ;; !Twiki\
;; tlst is a list of tiddler nums
(define (twiki:get-tiddlers-by-num db wid tlst)
;; (s:log "Got to twiki:get-tiddlers with keys: " tlst " and wid: " wid)
;; select where created_on < somedate order by created_on desc limit 1
(let* ((tdlrs '())
(tlststr (string-intersperse (map number->string tlst) ","))
(already-got (make-hash-table))
(qry (conc twiki:tiddler-selector " WHERE t.wiki_id=? AND t.id IN (" tlststr ") ORDER BY created_on DESC;")))
;; (conn (slot-ref s:session 'conn))
;; (print "qry: " qry)
(dbi:for-each-row
(lambda (row)
(let ((tname (twiki:tiddler-get-name row)))
(if (not (hash-table-ref/default already-got tname #f))
(begin
(set! tdlrs (cons row tdlrs))
(hash-table-set! already-got tname #t)))))
|
|
<
<
|
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
(dbi:close tdb)
result))
;; should do a single more efficient query but this is good enough
(define (twiki:get-tiddlers db wid tnames)
(apply twiki:get-tiddlers-by-name db wid tnames))
;; (let* ((tdlrs '())
;; ;; (conn (sdat-get-conn s:session))
;; (namelst (conc "('" (string-intersperse (map conc tnames) "','") "')"))
;; (qry (conc twiki:tiddler-selector " WHERE t.wiki_id=? AND t.id IN " namelst ";")))
;; ;; (print qry)
;; (dbi:for-each-row
;; (lambda (row)
;; (set! tdlrs (cons row tdlrs)))
;; db qry wid)
;; (reverse tdlrs))) ;; !Twiki\
;; tlst is a list of tiddler nums
(define (twiki:get-tiddlers-by-num db wid tlst)
;; (s:log "Got to twiki:get-tiddlers with keys: " tlst " and wid: " wid)
;; select where created_on < somedate order by created_on desc limit 1
(let* ((tdlrs '())
(tlststr (string-intersperse (map number->string tlst) ","))
(already-got (make-hash-table))
(qry (conc twiki:tiddler-selector " WHERE t.wiki_id=? AND t.id IN (" tlststr ") ORDER BY created_on DESC;")))
(dbi:for-each-row
(lambda (row)
(let ((tname (twiki:tiddler-get-name row)))
(if (not (hash-table-ref/default already-got tname #f))
(begin
(set! tdlrs (cons row tdlrs))
(hash-table-set! already-got tname #t)))))
|
︙ | | | ︙ | |
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
|
(define twiki:u s:u)
(define twiki:td s:td)
(define twiki:tr s:tr)
(define twiki:table s:table)
(define twiki:div s:div)
(define (twiki:web64enc str)
(string-substitute "=" "_" (base64:encode str) #t))
(define (twiki:web64dec str)
(base64:decode (string-substitute "_" "=" str #t)))
(define (twiki:make-tlink text tiddlername)
(s:a text 'href (s:link-to (twiki:get-link-back-to-current) 'view_tiddler (twiki:web64enc tiddlername))))
(define (twiki:pic pic-name size wiki)
(let* ((tdb (twiki:wiki-get-dbh wiki))
(tkey (twiki:wiki-get-key wiki))
|
|
|
|
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
(define twiki:u s:u)
(define twiki:td s:td)
(define twiki:tr s:tr)
(define twiki:table s:table)
(define twiki:div s:div)
(define (twiki:web64enc str)
(string-substitute "=" "_" (base64:base64-encode str) #t))
(define (twiki:web64dec str)
(base64:base64-decode (string-substitute "_" "=" str #t)))
(define (twiki:make-tlink text tiddlername)
(s:a text 'href (s:link-to (twiki:get-link-back-to-current) 'view_tiddler (twiki:web64enc tiddlername))))
(define (twiki:pic pic-name size wiki)
(let* ((tdb (twiki:wiki-get-dbh wiki))
(tkey (twiki:wiki-get-key wiki))
|
︙ | | | ︙ | |