summaryrefslogtreecommitdiff
path: root/mulkcms.lisp
blob: e7f88198ac8f8e27fd8388f57f37a1534249399c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
(in-package #:mulkcms)

(defvar *requested-characteristics*)
(defvar *propagated-params*)
(defvar *use-ssl-p*)
(defvar *base-uri*)
(defvar *user-agent*)
(defvar *real-remote-addr*)
(defvar *request-method*)
(defvar *headers*)
(defvar *user-name*)
(defvar *password*)
(defvar *authorization-page-handler*)

(unless (member "html-human-date" *template-formatters* :key #'car :test #'equal)
  (setq *template-formatters*
        (list* (cons "html-human-date" 'format-human-date)
               (cons "html-short-human-date" 'format-short-human-date)
               (cons "html-iso-date"   'format-iso-date)
               (cons "article-html"    'format-article)
               (cons "comment-html"    'format-comment)
               *template-formatters*)))


(defparameter *month-abbreviations*
              '("Jan" "Feb" "Mar" "Apr" "May" "Jun"
                "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))


(defun make-uuid ()
  ;; Taken from Mulkblog.
  "Generate a version 4 UUID according to RFC 4122, section 4.4."
  (let ((*random-state* (make-random-state t)))
    (format nil "~(~8,'0x-~4,'0x-~4,'0x-~2,'0x~2,'0x-~12,'0x~)"
            (random #x100000000)                ;time_low
            (random #x10000)                    ;time_mid
            (logior #b0100000000000000
                    (logand #b0000111111111111
                            (random #x10000)))  ;time_hi_and_version
            (logior #b10000000
                    (logand #b00111111
                            (random #x100)))    ;clock_seq_hi_and_reserved
            (random #x100)                      ;clock_seq_low
            (random #x1000000000000))))


(defmacro dynamic-lambda ((&rest dynvars) lambda-list &body body)
  (let ((lexvars (mapcar (lambda (x) (declare (ignore x)) (gensym)) dynvars)))
    `(let ,(mapcar #'list lexvars dynvars)
       (lambda ,lambda-list
         (let ,(mapcar #'list dynvars lexvars)
           ,@body)))))


(defun hashcash-hash-validp (text)
  (let* ((stripped-text (ppcre:regex-replace-all "\\s+" text ""))
         (digest (ironclad:digest-sequence 'ironclad:sha256 (flexi-streams:string-to-octets stripped-text :external-format :utf8))))
    (every #'zerop (subseq digest 0 2))))


(defun akismet-login ()
  ;; Taken from Mulkblog.
  (drakma:http-request "http://rest.akismet.com/1.1/verify-key"
                       :protocol :http/1.0
                       :method :post
                       :user-agent "MulkCMS/0.1.0"
                       :parameters `(("key" . ,*wordpress-key*)
                                     ("blog" . ,*base-uri*))))


(defun akismet-check-comment (body author-name author-website user-agent submitter-ip)
  ;; Taken from Mulkblog.
  (drakma:http-request
   (format nil
           "http://~A.rest.akismet.com/1.1/comment-check"
           *wordpress-key*)
   :protocol :http/1.0
   :method :post
   :user-agent "Mulk Journal/0.0.1"
   :parameters `(("blog" . ,*base-uri*)
                 ("user_ip" . ,submitter-ip)
                 ("user_agent" . ,user-agent)
                 ("comment_type" . "comment")
                 ("comment_author" . ,author-name)
                 ("comment_author_url" . ,author-website)
                 ("comment_content" . ,body))))


(defun use-akismet-p ()
  (and (boundp '*wordpress-key*) *wordpress-key*))

(defun spamp/akismet (&rest comment-data)
  ;; Taken from Mulkblog.
  (cond
    ((use-akismet-p)
     (ignore-errors
       (akismet-login)
       (string= "true" (apply #'akismet-check-comment comment-data))))
    (*drop-unfiltered* t)
    (t nil)))


(defun parse-http-date (date-string)
  (or (when-let (groups
                 ;; Match rfc1123-date (the whitespace stuff),
                 ;; rfc850-date (the hyphen stuff)
                 (nth-value
                  1
                  (ppcre:scan-to-strings
                   "\\w+, (\\d+)[- ](\\w+)[- ](\\d+) (\\d+):(\\d+):(\\d+) GMT"
                   date-string)))
        (simple-date:encode-timestamp
         (let ((yr (parse-integer (elt groups 2))))
           ;; Well.  For the two-digit year version, I'm just going to
           ;; assume a year >= 2000.  If some clown client software
           ;; thinks it needs to ask my server whether the site's
           ;; content has changed since 1978 and does so using a
           ;; two-digit year code, it deserves all the problems that
           ;; this may cause it.
           (if (>= yr 100)
               yr
               (+ 2000 yr)))
         (1+ (position (elt groups 1) *month-abbreviations* :test #'equal))
         (parse-integer (elt groups 0))
         (parse-integer (elt groups 3))
         (parse-integer (elt groups 4))
         (parse-integer (elt groups 5))))
      (when-let (groups
                 ;; Match asctime-date.
                 (nth-value
                  1
                  (ppcre:scan-to-strings
                   "\\w+ (\\w+)\\s+(\\d+) (\\d+):(\\d+):(\\d+) (\\d+)"
                   date-string)))
        (simple-date:encode-timestamp
         (parse-integer (elt groups 5))
         (1+ (position (elt groups 1) *month-abbreviations* :test #'equal))
         (parse-integer (elt groups 0))
         (parse-integer (elt groups 2))
         (parse-integer (elt groups 3))
         (parse-integer (elt groups 4))))))


(defun call-with-cache (path last-update content-type characteristics full-p thunk)
  (when (eq *request-method* :post)
    (return-from call-with-cache
      (let ((result (funcall thunk)))
        (typecase result
          (cons
           (append (list :content-type content-type)
                   result))
          (t
           (list :content-type content-type
                 :body         result))))))
  (when-let (date-string (cdr (assoc :if-modified-since *headers*)))
    (when-let (if-modified-since (parse-http-date date-string))
      ;; We need to subtract 1 second, since LAST-UPDATE will probably
      ;; have a non-zero millisecond part, which will make the
      ;; comparison fail in the very common case that the
      ;; If-Modified-Since time is exactly what we previously sent as
      ;; the Last-Modified header (which didn't include milliseconds).
      (when (simple-date:time<= (simple-date:time-subtract
                                 last-update
                                 (simple-date:encode-interval :second 1))
                                if-modified-since)
        (return-from call-with-cache
          (list :return-code 304))   ;;304 Not Modified
        )))
  (let* ((chars       characteristics)
         (charstring  (format nil "~A; ssl=~A; full=~A"
                              (prin1-to-string chars)
                              *use-ssl-p*
                              full-p))
         (charbytes   (flexi-streams:string-to-octets
                       charstring
                       :external-format :utf-8))
         (charhash    (ironclad:digest-sequence 'ironclad:crc32 charbytes))
         (charhashnum (- #x80000000
                         (logior (ash (elt charhash 0) 24)
                                 (ash (elt charhash 1) 16)
                                 (ash (elt charhash 2)  8)
                                 (ash (elt charhash 3)  0))))
         (cached-data (query "SELECT content, date
                                FROM cached_pages
                               WHERE characteristic_hash = $1
                                 AND alias = $2"
                             charhashnum
                             path
                             :row)))
    (list :headers `((:last-modified .
                      ,(local-time:format-timestring
                        nil
                        (local-time:universal-to-timestamp
                         (simple-date:timestamp-to-universal-time last-update))
                        :format local-time:+rfc-1123-format+)))
          :content-type content-type
          :body
          (if (and cached-data (simple-date:time< last-update (second cached-data)))
              (first cached-data)
              (let ((generated-content (funcall thunk)))
                (query "DELETE FROM cached_pages
                              WHERE characteristic_hash = $1
                                AND alias = $2"
                       charhashnum
                       path
                       :none)
                (query "INSERT INTO cached_pages(characteristic_hash, alias, content)
                             VALUES ($1, $2, $3)"
                       charhashnum
                       path
                       generated-content
                       :none)
                generated-content)))))


(defmacro with-cache ((path last-update &optional content-type characteristics full-p)
                      &body body)
  `(call-with-cache ,path ,last-update ,content-type ,characteristics ,full-p
                    (lambda () ,@body)))


(defun find-canonical-article-alias (article)
  (query "SELECT alias FROM article_aliases WHERE article = $1 LIMIT 1"
         article
         :single))


(defun queryize-alist (alist)
  (format nil "~{~A~^&~}"
          (mapcar (lambda (x)
                    (destructuring-bind (name . value)
                        x
                      (format nil
                              "~A=~A"
                              (json-template::escape-for-uri name)
                              (json-template::escape-for-uri value))))
                  alist)))

(defun uri-with-query (string alist)
  (if alist
      (format nil "~A~A~A"
              string
              (if (position #\? string)
                  #\&
                  #\?)
              (queryize-alist alist))
      string))

(defun link-to (action &key comment-id article-id revision-id (absolute nil))
  ;; Taken from Mulkblog.
  (symbol-macrolet ((article-base (find-canonical-article-alias article-id)))
    (multiple-value-call
        #'(lambda (&rest args)
            (let ((path (apply #'format nil args)))
              (if absolute
                  (uri-with-query
                   (with-output-to-string (strout)
                     (puri:render-uri (puri:merge-uris path *base-uri*)
                                      strout))
                   *propagated-params*)
                  path)))
      (ecase action
        (:index "")
        (:journal-index "/journal")
        (:full-journal-index "/journal?full")
        (:view-atom-feed (values "/feed"))
        (:view (cond (comment-id (values "/~A#comment-~D" article-base comment-id))
                     (article-id (values "/~A" article-base))
                     (t "/")))
        (:view-comments (values "/~A#comments" article-base))
        ((:edit :preview) (cond (revision-id (values "/~A?edit&revision=~D" article-base revision-id))
                                (t           (values "/~A?edit" article-base))))
        (:post-comment (values "/~A" article-base))
        (:trackback (values "/~A?trackback" article-base))
        (:view-atom-entry (values "/~A?atom" article-base))
        (:save (values "/~A?save" article-base))
        (:moderation-page "/moderate")
        (:css "/journal.css")
        (:prettify.css "/prettify/prettify.css")
        (:prettify.js "/prettify/prettify.js")
        (:prettify-lisp.js "/prettify/lang-lisp.js")
        (:prettify-rust.js "/prettify/lang-rust.js")
        (:prettify-hs.js "/prettify/lang-hs.js")
        (:pingback "/rpc")))))

(defun call-with-db (thunk)
  (call-with-connection (list *database-name* *database-user* *database-password* *database-host*
                              :pooled-p t :use-ssl :no)
                        thunk))

(defmacro with-db (&body body)
  `(call-with-db (lambda () ,@body)))

(defun invalidate-cache ()
  (with-db (query "DELETE FROM cached_pages")))

(defun find-template (template-name)
  (first (directory (make-pathname :name template-name
                                   :type :wild
                                   :directory *templates*))))

(defun format-short-human-date (date)
  (multiple-value-bind (year month day hour minute second millisecond)
      (decode-timestamp date)
    (declare (ignore second millisecond))
    (format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
            year month day hour minute)))

(defun format-human-date (date)
  ;; FIXME
  (multiple-value-bind (year month day hour minute second millisecond)
      (decode-timestamp date)
    (declare (ignore second millisecond))
    (format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D"
            year month day hour minute)))

(defun format-iso-date (date)
  (multiple-value-bind (year month day hour minute second millisecond)
      (decode-timestamp date)
    (declare (ignore millisecond))
    (format nil "~4,'0D-~2,'0D-~2,'0DT~2,'0D:~2,'0D:~2,'0DZ"
            year month day hour minute second)))

(defun template (template-name)
  (parse-template-string (read-file-into-string (find-template template-name) :external-format :utf-8)))

(defun format-article (article-params)
  (let ((article-template (template "article")))
    (expand-template article-template article-params)))

(defun format-comment (comment-params)
  (let ((comment-template (template "comment")))
    (expand-template comment-template comment-params)))

(defun paramify-article-data (revision-data &optional (comments nil commentary-p))
  (destructuring-bind (rid article date title content author format status
                       global-id
                       &optional publishing-date comment-num
                       &rest args)
      revision-data
    (declare (ignore args format author))
    (list :publishing-date publishing-date
          :revision rid
          :last-updated-date date
          :title (and (not (equal "" title)) title)
          :body content
          :article-id article
          :global-id global-id
          :status status
          :link (link-to :view
                         :article-id article
                         :absolute t)
          :commentary (if commentary-p (list :comments comments) nil)
          :comment-submission (when commentary-p
                                (list :fields (list (list :field-id "name"
                                                          :field-label "Name")
                                                    (list :field-id "website"
                                                          :field-label "Website")
                                                    (list :field-id "email"
                                                          :field-label "E-Mail"))
                                      :require-js (and (not (use-akismet-p))
                                                       *drop-unfiltered*)
                                      :body-label "Message"
                                      :submit-button-label "Submit"
                                      :title "Submit a comment"
                                      :notes (format
                                              nil
                                              "<p><strong>Note:</strong>
                                              <span
                                              class='spam-detection-info'>~A</span>
                                              Comment format is plain
                                              text.  Use blank lines to
                                              separate paragraphs.</p>"
                                              (cond
                                               ((use-akismet-p)
                                                "This website uses <span
                                                 class='spam-detection-method'><a
                                                 href=\"http://akismet.com/\">Akismet</a></span>
                                                 for spam detection.
                                                 E-mail addresses are
                                                 never disclosed to
                                                 anyone (including
                                                 Akismet) other than the
                                                 site owner.")
                                                (*drop-unfiltered*
                                                 "This website uses a JavaScript-based
                                                  spam prevention system.  Please enable
                                                  JavaScript in your browser to post
                                                  comments.")
                                                (t "")))
                                      :action (link-to :post-comment
                                                       :article-id article
                                                       :absolute t)))
          :edit-link (link-to :edit :article-id article)
          :edit-button-label "Edit"
          :comments-label (case comment-num
                            (0 "no comments")
                            (1 "1 comment")
                            (otherwise (format nil "~D comments" comment-num)))
          :comments-link (link-to :view-comments
                                  :article-id article
                                  :absolute t)
          :comments-heading "Comments")))

(defun format-comment-content (text)
  ;; Taken from Mulkblog.
  (with-html-output-to-string (out)
    (loop for last-position = 0 then (cadr matches)
          for matches = (ppcre:all-matches "(\\n|\\r|\\r\\n)(\\n|\\r|\\r\\n)+" text)
            then (cddr matches)
          while (not (endp matches))
          do (htm (:p (esc (subseq text last-position (car matches)))))
          finally
            (htm (:p (esc (subseq text last-position)))))))

#-portable-mulkcms
(defun make-characteristic-lists (characteristics &aux (first1-p t) (first2-p t))
  (with-output-to-string (out)
    (format out "(ARRAY[")
    (dolist (characteristic-list characteristics)
      (if first1-p
          (setq first1-p nil)
          (format out ", "))
      (format out "ROW(ARRAY[")
      (dolist (ch characteristic-list)
        (if first2-p
            (setq first2-p nil)
            (format out ", "))
        (format out "ROW(~A, ~A)::characteristic"
                (sql-escape (car ch))
                (sql-escape (cdr ch))))
      (setq first2-p t)
      (format out "])"))
    (format out "]::characteristic_list[])")))

#-portable-mulkcms
(defun find-article-revisions (article characteristics)
  (query (format nil "SELECT article_revisions_for_characteristics($1, ~A)"
                 (make-characteristic-lists characteristics))
         article
         :column))

#-portable-mulkcms
(defun find-all-revisions (characteristics &optional constraints)
  (query
    (format nil
            "SELECT (most_recent_revision(r)).*, d.publishing_date, c.comment_count
               FROM (SELECT article_revisions_for_characteristics(a.id, ~A)
                            AS revision
                       FROM articles a)
                    AS mr
               JOIN article_revisions        r ON r.id = mr.revision
               JOIN article_publishing_dates d ON d.article = r.article
               JOIN article_comment_counts   c ON c.article = r.article
              GROUP BY r.article, d.publishing_date, c.comment_count
             HAVING ~A
              ORDER BY d.publishing_date DESC"
            (make-characteristic-lists characteristics)
            (or constraints "true"))))


#+portable-mulkcms
(defun find-article-revisions (article characteristics &optional fixed-characteristics)
  ;; CHARACTERISTICS --- a proper list.
  ;;
  ;; CHARACTERISTICS is a list of lists of (key . value) pairs.  Each component list
  ;; is a precedence list of characteristics that are to be considered fallbacks for
  ;; each other and that will be tried in order.
  (let* ((fixed-characteristics-conditions
          (mapcar (lambda (x)
                    (format nil "AND EXISTS
                                   (SELECT 1
                                      FROM article_revision_characteristics
                                     WHERE revision = article_revisions.id
                                       AND characteristic = ~A
                                       AND value = ~A)"
                            (sql-escape (car x))
                            (sql-escape (cdr x))))
                  fixed-characteristics))
         (query (format nil
                        "SELECT id
                           FROM article_revisions
                          WHERE article = $1~
                          ~{~&~A~}
                          ORDER BY date DESC"
                        fixed-characteristics-conditions)))
    (when-let ((revisions (query query article :column)))
      (if (consp characteristics)
          (progn
            (dolist (potential-fixed-characteristic (first characteristics))
              (when-let ((more-specific-revisions
                          (find-article-revisions article
                                                  (rest characteristics)
                                                  (cons potential-fixed-characteristic
                                                        fixed-characteristics))))
                (return-from find-article-revisions more-specific-revisions)))
            revisions)
          revisions))))

;;;; Alternative definition.
;;
;; #+portable-mulkcms
;; (defun find-article-revisions (article characteristics)
;;   ;; CHARACTERISTICS --- a proper list.
;;   ;;
;;   ;; CHARACTERISTICS is a list of lists of (key . value) pairs.  Each component list
;;   ;; is a precedence list of characteristics that are to be considered fallbacks for
;;   ;; each other and that will be tried in order.
;;   (labels ((find-more-specific-revisions (revisions characteristics)
;;              (let ((alternatives (first characteristics))
;;                    (rest (rest characteristics)))
;;                (dolist (alternative alternatives)
;;                  (when-let ((potential-revisions
;;                              (remove-if-not (lambda (x)
;;                                               (query "SELECT 1
;;                                                       FROM article_revision_characteristics
;;                                                       WHERE revision = $1
;;                                                         AND characteristic = $2                                                     AND value = $3"
;;                                                      x
;;                                                      (car alternative)
;;                                                      (cdr alternative)
;;                                                      :column))
;;                                             revisions)))
;;                    (if-let ((subresults (find-more-specific-revisions potential-revisions rest)))
;;                      (return-from find-more-specific-revisions subresults)
;;                      (return-from find-more-specific-revisions potential-revisions)))))))
;;     (let ((revisions (query "SELECT id FROM article_revisions
;;                               WHERE article = $1
;;                               ORDER BY date DESC"
;;                             article
;;                             :column)))
;;       (if characteristics
;;           (find-more-specific-revisions revisions characteristics)
;;           revisions))))

(defun paramify-comment (comment-revision-data)
  (destructuring-bind (crid comment date content author format status
                       article-revision submitter-ip user-agent
                       &rest args)
      comment-revision-data
    (declare (ignore args status format submitter-ip user-agent))
    (destructuring-bind (author-name author-website)
        (query "SELECT name, website FROM users WHERE id = $1"
               author
               :row)
      (let ((article (query "SELECT article
                               FROM article_revisions
                              WHERE id = $1"
                            article-revision
                            :single!)))
        (list :publishing-date date
              :revision-id crid
              :comment-id comment
              :article-revision article-revision
              :body (format-comment-content content)
              :author author-name
              ;;FIXME
              :author-link (if (and author-website (not (equal author-website "")))
                               author-website
                               nil)
              :link (link-to :view
                             :article-id article
                             :comment-id comment
                             :absolute t)
              :edit-link ""
              :edit-button-label "Edit"
              :generic-commenter-name "Someone")))))

(defprepared find-journal-articles
  "SELECT article
     FROM journal_entries
    WHERE journal = 0
    ORDER BY index DESC"
  :column)

(defun find-journal-archive-request-handler (path full-p
                                             &optional
                                             action
                                             (characteristics
                                              *requested-characteristics*))
  (declare (ignore action))
  (when (member path '("journal" "journal/"
                       "feed" "feed/"
                       "journal/feed" "journal/feed")
                :test #'string=)
    (dynamic-lambda (*propagated-params* *base-uri* *use-ssl-p* *request-method*
                     *headers* *user-name* *password*) ()
      (with-db
        (with-cache (path
                     (query "SELECT max(date) FROM article_revisions" :single)
                     (if (member path '("feed" "feed/"
                                        "journal/feed" "journal/feed")
                                 :test #'string=)
                         "application/atom+xml; charset=UTF-8"
                         "text/html; charset=UTF-8")
                     characteristics
                     full-p)
          (let* (#+portable-mulkcms
                 (articles (find-journal-articles))
                 #+portable-mulkcms
                 (revisions (remove-if #'null
                                       (mapcar (lambda (x)
                                                 (find-article-params x characteristics))
                                               articles)))
                 #-portable-mulkcms
                 (revisions
                  (mapcar #'paramify-article
                          (find-all-revisions characteristics
                                              "EXISTS (SELECT 1
                                                         FROM journal_entries je
                                                        WHERE journal = 0
                                                          AND r.article = je.article)")))
                 (displayed-revisions (if full-p
                                          revisions
                                          (subseq revisions
                                                  0
                                                  (min 5 (length revisions)))))
                 (minor-revisions (remove-if (lambda (x)
                                               (or (null (getf x :title))
                                                   (equal (getf x :title) "")))
                                             revisions)))
            (cond
              ((member path '("journal" "journal/") :test #'string=)
               (let* ((page-skeleton (template "page_skeleton"))
                      (page-template (template "journal_page"))
                      (template-params
                       (list :title *site-name*
                             :root *base-uri*
                             :site-name *site-name*
                             :site-subtitle ""
                             :link (link-to :journal-index
                                            :absolute t)
                             :full-archive-link (link-to :full-journal-index
                                                         :absolute t)
                             :full-archive-label "Full archive (slow!)"
                             :archive-title "Older posts"
                             :archive-table-caption "Posts by date"
                             :archive-title-label "Title"
                             :archive-date-label "Date"
                             :archive-comments-label "Comments"))
                      (head (expand-template
                             page-template
                             (list* :head t
                                    :articles displayed-revisions
                                    :minor-articles minor-revisions
                                    template-params)))
                      (body (expand-template
                             page-template
                             (list* :body t
                                    :articles displayed-revisions
                                    :minor-articles minor-revisions
                                    template-params))))
                 (expand-template page-skeleton (list :title *site-name*
                                                      :head head
                                                      :body body))))
              ((member path '("feed" "feed/" "journal/feed" "journal/feed/")
                       :test #'string=)
               (let* ((authors
                       (query "SELECT DISTINCT name
                               FROM users
                               JOIN article_revisions
                                 ON author = users.id"
                              :plists))
                      (last-updated
                       (query "SELECT max(date)
                               FROM article_revisions
                              WHERE status = 'syndicated'"
                              :single))
                      (revisions-with-global-ids
                       (mapcar (lambda (x)
                                 (let ((global-id
                                        ;; FIXME: May want to specify a
                                        ;; stopping condition in the
                                        ;; recursive query.
                                        (query
                                         "WITH RECURSIVE q(rid, rglobal_id) AS (
                                              SELECT id, global_id
                                                FROM article_revisions
                                               WHERE id = $1
                                            UNION
                                              SELECT id, global_id FROM q
                                                JOIN article_revision_parenthood
                                                  ON rid = child
                                                JOIN article_revisions
                                                  ON parent = id
                                          )
                                          SELECT rglobal_id
                                            FROM q
                                           WHERE rglobal_id IS NOT NULL"
                                         (getf x :revision)
                                         :single)))
                                   ;; NOTE: This shadows the existing
                                   ;; :GLOBAL-ID key in X.
                                   (list* :global-id global-id x)))
                               revisions))
                      (template-params
                       (list :title *site-name*
                             :last-updated-date last-updated
                             :base-uri *base-uri*
                             :subtitle ""
                             :global-id *feed-global-id*
                             :authors authors
                             :feed-uri (link-to :view-atom-feed :absolute t)
                             :articles revisions-with-global-ids)))
                 (expand-template (template "article_feed")
                                  template-params))))))))))

(defun paramify-article (revision-data &optional commentary-p comments)
  (let* ()
    (cond ((null revision-data)
           nil)
          (commentary-p
           (paramify-article-data revision-data comments))
          (t
           (paramify-article-data revision-data)))))

(defun find-article-params (article characteristics &optional commentary-p)
  (let* ((comment-data (if commentary-p
                           (query "SELECT id FROM comments WHERE article = $1"
                                  article
                                  :column)
                           (list)))
         (comment-revision-data
          (remove-if #'null
                     (mapcar (lambda (cid)
                               (first
                                (query "SELECT *
                                          FROM comment_revisions 
                                         WHERE comment = $1
                                           AND status IN ('approved', 'trusted')
                                         ORDER BY date DESC"
                                       cid
                                       :lists)))
                             comment-data)))
         (comments (mapcar #'paramify-comment comment-revision-data))
         (comment-num (length comments))  ;FIXME
         (revisions (find-article-revisions article characteristics))
         (revision (first revisions))
         (revision-data (query "SELECT *
                                  FROM article_revisions
                                 WHERE id = $1"
                               revision
                               :row))
         (publishing-date (query "SELECT min(date)
                                    FROM article_revisions
                                   WHERE article = $1
                                     AND status IN ('published', 'syndicated')"
                                 article
                                 :single)))
    (cond ((null revision-data)
           nil)
          (t
           (paramify-article (append revision-data (list publishing-date
                                                         comment-num))
                             commentary-p
                             comments)))))

(defun expand-page (page-template title template-params)
  (let ((page-skeleton (template "page_skeleton"))
        (head (expand-template page-template
                               (list* :head t
                                      template-params)))
        (body (expand-template page-template
                               (list* :body t
                                      template-params))))
    (expand-template page-skeleton
                     (list :title title
                           :head head
                           :body body))))

(defmacro with-authorization ((user-id-var &rest options) &body body)
  `(call-with-authorization (lambda (,user-id-var) ,@body)
                            ,@options))

(defun call-with-authorization (thunk &key require)
  (with-db
    (let ((user-id (query (format nil
                                  "SELECT id
                                     FROM users u
                                     JOIN passwords p ON u.id = p.user
                                    WHERE p.password = $2
                                      AND u.name = $1
                                      AND ~A"
                                  (ecase require
                                    ((nil) "true")
                                    ((:admin) "u.status = 'admin'")
                                    ((:trusted) "u.status IN ('trusted', 'admin')")
                                    ((:approved) "u.status IN ('approved', 'trusted', 'admin')")))
                          *user-name*
                          *password*
                          :single)))
      (if user-id
          (funcall thunk user-id)
          (funcall *authorization-page-handler*)))))

(defun parse-row-or-array (string)
  (let ((output (list)))
    (with-input-from-string (in string)
      (read-char in)
      (loop for char = (read-char in nil nil nil)
            while (and char (not (member char (list #\} #\)))))
            do (push (with-output-to-string (out)
                       (if (char= char #\")
                           (loop for c = (read-char in)
                                 if (char= c #\\)
                                   do (format out "~C" (read-char in))
                                 else if (member c (list #\"))
                                   do (if (char= (peek-char nil in) #\")
                                          (progn
                                            (format out "\"")
                                            (read-char in))
                                          (progn
                                            (read-char in)
                                            (return)))
                                 else 
                                   do (format out "~C" c))
                           (progn
                             (format out "~C" char)
                             (loop for c = (read-char in)
                                   until (member c (list #\) #\, #\}))
                                   do (format out "~C" c)))))
                     output)))
    (nreverse output)))

(defun parse-row (string)
  (assert (and (char= (char string 0)                    #\()
               (char= (char string (1- (length string))) #\))))
  (parse-row-or-array string))

(defun parse-array (string)
  (assert (and (char= (char string 0)                    #\{)
               (char= (char string (1- (length string))) #\})))
  (parse-row-or-array string))

(defun find-comment-moderation-handler (path params
                                        &optional
                                        action
                                        (characteristics
                                         *requested-characteristics*))
  (declare (ignore characteristics action))
  (when (string= path "admin/comments")
    (dynamic-lambda (*user-name* *password* *propagated-params* *base-uri*
                     *use-ssl-p*) ()
      (with-authorization (user-id :require :admin)
        (declare (ignore user-id))
        (with-db
          (when-let (status
                     (cond
                       ((assoc "mark-as-spam" params :test #'equal) "spam")
                       ((assoc "reject"       params :test #'equal) "rejected")
                       ((assoc "approve"      params :test #'equal) "approved")
                       (t nil)))
            (query "UPDATE comment_revisions
                       SET status = $2
                     WHERE id = $1"
                   (parse-integer (cdr (assoc "revision-id" params :test #'equal)))
                   status
                   :none))
          (let* ((comments (query "SELECT *
                                     FROM comment_revisions
                                    WHERE status IN ('pending')"
                                  :rows))
                 (comment-data (mapcar #'paramify-comment comments))
                 (comment-data-with-article-data
                  (mapcar (lambda (comment)
                            (let ((revision (query "SELECT *
                                                      FROM article_revisions
                                                     WHERE id = $1"
                                                   (getf comment :article-revision)
                                                   :row)))
                              (list* :article (paramify-article revision)
                                     comment)))
                          comment-data)))
            (expand-page (template "comment_moderation_page")
                         "Comments Awaiting Moderation"
                         (list :spam-label "Mark as spam"
                               :reject-label "Reject"
                               :approve-label "Approve"
                               :site-name *site-name*
                               :title "Comments Awaiting Moderation"
                               :comments comment-data-with-article-data))))))))


(defun find-article-summary-handler (path params
                                     &optional
                                     action
                                     (characteristics
                                      *requested-characteristics*))
  (declare (ignore characteristics action))
  (when (string= path "admin/articles")
    (dynamic-lambda (*user-name* *password* *propagated-params* *base-uri* *use-ssl-p*) ()
      (with-authorization (user-id :require :admin)
        (with-db
          (labels ((paramify-revision-row (row article-id)
                     (destructuring-bind (id title date characteristics)
                         row
                       (list :id id
                             :title title
                             :date date
                             :link (link-to :edit
                                            :article-id article-id
                                            :revision-id id
                                            :absolute t)
                             :characteristics (parse-array characteristics))))
                   (paramify-article-row (row)
                     (destructuring-bind (id revisions aliases)
                         row
                       (let ((revision-data (mapcar (lambda (x)
                                                      (paramify-revision-row x id))
                                                    (mapcar #'parse-row
                                                            (parse-array revisions)))))
                         (list :id id
                               :aliases (if (eq aliases :null)
                                            nil
                                            aliases)
                               :revisions revision-data
                               :revision-num (length revision-data))))))
            (when (assoc "add-alias" params :test #'equal)
              (with-transaction ()
                (query "INSERT INTO article_aliases(article, alias) VALUES ($1, $2)"
                       (parse-integer (cdr (assoc "article" params :test #'equal)))
                       (cdr (assoc "alias" params :test #'equal))
                       :none)))
            (when (assoc "create-article" params :test #'equal)
              (with-transaction ()
                (let ((article-id (query "INSERT INTO articles(type) VALUES (1) RETURNING id"
                                         :single!)))
                  (query "INSERT INTO article_revisions(article, title, content, author, format, status, global_id)
                               VALUES ($1, '', '', $2, 'html', 'draft', $3)"
                         article-id
                         user-id
                         (format nil "urn:uuid:~A" (make-uuid))))))
            (when (assoc "create-journal-entry" params :test #'equal)
              (with-transaction ()
                (let* ((article-id
                         (query "INSERT INTO articles(type) VALUES (1) RETURNING id"
                                :single!))
                       (article-revision-id
                         (query "INSERT INTO article_revisions(article, title, content, author, format, status, global_id)
                                      VALUES ($1, '', '', $2, 'html', 'draft', $3)
                                   RETURNING id"
                                article-id
                                user-id
                                (format nil "urn:uuid:~A" (make-uuid))
                                :single!))
                       (journal-index (query "INSERT INTO journal_entries(journal, index, article)
                                                   SELECT 0, 1+MAX(index), $1
                                                     FROM journal_entries
                                                    WHERE journal = 0
                                                RETURNING index"
                                article-id
                                :single!)))
                  (query "INSERT INTO article_aliases(alias, article)
                                 VALUES ('journal/' || ($1 :: TEXT), $2)"
                         journal-index
                         article-id)
                  (query "INSERT INTO article_revision_characteristics(revision, characteristic, value)
                               VALUES ($1, 'language', 'de')"
                         article-revision-id))))
            (let* ((articles (query "SELECT a.id,
                                            array_agg(DISTINCT
                                                      ROW(r.id,
                                                          r.title,
                                                          r.date,
                                                          r.characteristics)),
                                            array_agg(DISTINCT aa.alias)
                                       FROM articles a
                                       LEFT OUTER JOIN article_aliases aa
                                         ON aa.article = a.id
                                       LEFT OUTER JOIN article_branch_tips bt
                                         ON bt.article = a.id
                                       LEFT OUTER JOIN
                                         (SELECT r.id    AS id,
                                                 r.title AS title,
                                                 r.date  AS date,
                                                 array_agg(DISTINCT
                                                           ROW(ch.characteristic, 
                                                               ch.value))
                                                   AS characteristics
                                            FROM article_revisions r
                                            LEFT OUTER JOIN article_revision_characteristics ch
                                              ON ch.revision = r.id
                                           GROUP BY r.id, r.title, r.date)
                                         AS r
                                         ON bt.revision = r.id
                                       LEFT OUTER JOIN article_publishing_dates pd
                                         ON pd.article = a.id
                                      GROUP BY a.id, pd.publishing_date
                                      ORDER BY pd.publishing_date DESC, a.id DESC"
                                    :rows))
                   (article-data (mapcar #'paramify-article-row articles)))
              (expand-page (template "article_summary_page")
                           "Articles"
                           (list :id-label "Article"
                                 :aliases-label "Aliases"
                                 :branch-label "Branch"
                                 :branch-title-label "Title"
                                 :characteristics-label "Characteristics"
                                 :date-label "Date"
                                 :create-article-button-label "Add Article"
                                 :create-journal-entry-button-label "Add Journal Entry"
                                 :add-alias-label "+"
                                 :articles article-data)))))))))

(defun parse-characteristic-string (char-string)
  (let* ((delimiter   (position #\: char-string))
         (char        (subseq char-string 0 delimiter))
         (value       (subseq char-string (1+ delimiter))))
    (values char
            value)))

(defun find-article-request-handler (path params
                                     &optional
                                     action
                                     (characteristics
                                      *requested-characteristics*))
  (with-db
    (when-let ((article (query "SELECT article FROM article_aliases
                                 WHERE alias = $1"
                               path
                               :single)))
      (ecase action
        (:edit
         (dynamic-lambda (*user-name* *password* *propagated-params* *base-uri* *use-ssl-p*) ()
           (with-authorization (user-id :require :admin)
             (with-db
               (with-transaction ()
                 (let* ((revision (if (assoc "save" params :test #'equal)
                                      (query "INSERT INTO article_revisions(article, title, content, author, format, status)
                                                VALUES ($1, $2, $3, $4, $5, $6)
                                              RETURNING *"
                                             article
                                             (cdr (assoc "title" params :test #'equal))
                                             (cdr (assoc "content" params :test #'equal))
                                             user-id
                                             "html"
                                             (if (assoc "publish-p"
                                                        params
                                                        :test #'equal)
                                                 "syndicated"
                                                 "draft")
                                             :row)
                                      (query "SELECT * FROM article_revisions
                                               WHERE id = $1
                                                 AND article = $2"
                                             (parse-integer
                                              (cdr (assoc "revision"
                                                          params
                                                          :test #'equal)))
                                             article
                                             :row)))
                        (article-params (list* (paramify-article revision)))
                        (editor-template (template "edit_page")))
                   (assert (not (null revision)))
                   (when-let (del-char-command
                              (find-if (lambda (x)
                                         (starts-with-subseq "delete-char:"
                                                             (car x)))
                                       params))
                     (let* ((char-string (subseq (car del-char-command) (length "delete-char:"))))
                       (multiple-value-bind (name value)
                           (parse-characteristic-string char-string)
                         (query "DELETE FROM article_revision_characteristics
                                  WHERE revision = $1
                                    AND characteristic = $2
                                    AND value = $3"
                                (first revision)
                                name
                                value
                                :none))))
                   (when (assoc "add-char" params :test #'equal)
                     (let ((name  (cdr (assoc "new-char-name"  params
                                              :test #'equal)))
                           (value (cdr (assoc "new-char-value" params
                                              :test #'equal))))
                       (query "INSERT INTO article_revision_characteristics(
                                 revision, characteristic, value
                               ) VALUES ($1, $2, $3)"
                              (first revision)
                              name
                              value
                              :none)))
                   (when (assoc "save" params :test #'equal)
                     (query "INSERT INTO article_revision_parenthood(parent, child)
                                  VALUES ($1, $2)"
                            (parse-integer (cdr (assoc "revision"
                                                       params
                                                       :test #'equal)))
                            (first revision)
                            :none)
                     (query "INSERT INTO article_revision_characteristics(revision, characteristic, value)
                                  SELECT $2, characteristic, value
                                    FROM article_revision_characteristics
                                   WHERE revision = $1"
                            (parse-integer (cdr (assoc "revision"
                                                       params
                                                       :test #'equal)))
                            (first revision)
                            :none))
                   (setq characteristics
                         (query "SELECT characteristic, value
                                   FROM article_revision_characteristics
                                  WHERE revision = $1"
                                (first revision)
                                :plists))
                   (expand-page editor-template
                                (getf article-params :title)
                                (list :article article-params
                                      :characteristics characteristics
                                      :title (getf article-params :title)
                                      :root *base-uri*
                                      :site-name *site-name*
                                      :site-subtitle ""
                                      :link (link-to :edit
                                                     :article-id article
                                                     :absolute t)
                                      :save-button-label "Save"
                                      :publish-flag-label "Publish"
                                      :title-label "Title"
                                      :content-label "Content"
                                      :characteristics-label "Characteristics"))))))))
        (:view
         (dynamic-lambda (*propagated-params* *base-uri* *use-ssl-p* *headers*
                          *real-remote-addr* *user-agent* *request-method*
                          *user-name* *password*) ()
           (with-db
             (with-cache (path
                          (query "SELECT max(date)
                                    FROM article_revisions
                                   WHERE article = $1"
                                 article
                                 :single)
                          "text/html; charset=UTF-8"
                          characteristics)
               (let* ((page-template-name (query "SELECT page_template FROM articles
                                                  JOIN article_types
                                                    ON articles.type = article_types.id
                                                 WHERE articles.id = $1"
                                                 article
                                                 :single!))
                      (article-params (find-article-params article
                                                           characteristics
                                                           t))
                      (page-template (template page-template-name))
                      (template-params (list :title (getf article-params :title)
                                             :root *base-uri*
                                             :site-name *site-name*
                                             :site-subtitle ""
                                             :link ""))
                      (submission-notice nil))
                 (when (assoc "post-comment" params :test #'equal)
                   (let* ((name     (cdr (assoc "name"            params :test #'equal)))
                          (website  (cdr (assoc "website"         params :test #'equal)))
                          (email    (cdr (assoc "email"           params :test #'equal)))
                          (body     (cdr (assoc "body"            params :test #'equal)))
                          (article  (cdr (assoc "article"         params :test #'equal)))
                          (revision (cdr (assoc "revision"        params :test #'equal)))
                          (tkey     (cdr (assoc "transaction-key" params :test #'equal)))
                          (salt     (cdr (assoc "salt"            params :test #'equal)))
                          (spam-p   (if tkey
                                        (or (null salt)
                                            (not (hashcash-hash-validp
                                                  (format nil "~A:~A:~A" body tkey salt))))
                                        (spamp/akismet body name website
                                                       *real-remote-addr*
                                                       *user-agent*))))
                     (with-transaction ()
                       (let ((comment (query "INSERT INTO comments(article, global_id)
                                              VALUES ($1, $2)
                                            RETURNING id"
                                             article
                                             (format nil "urn:uuid:~A" (make-uuid))
                                             :single!))
                             (author  (query "INSERT INTO users(name, status, email, website)
                                              VALUES ($1, 'visitor', $2, $3)
                                            RETURNING id"
                                             name
                                             email
                                             website
                                             :single!)))
                         (when tkey
                           (query "INSERT INTO used_transaction_keys(key) VALUES ($1)"
                                  tkey
                                  :none))
                         (query "INSERT INTO comment_revisions(comment, content, author, format, status, article_revision, submitter_ip, submitter_user_agent)
                                 VALUES ($1, $2, $3, 'text', $4, $5, $6, $7)"
                                comment
                                body
                                author
                                (if spam-p
                                    "spam"
                                    "pending")
                                revision
                                *real-remote-addr*
                                *user-agent*
                                :none)
                         (setq submission-notice
                               (cond
                                 (spam-p
                                  (list
                                   :content "<p><strong>Warning:</strong></p>

                                           <p>Your message could not be
                                           verified as non-spam.  If
                                           JavaScript is enabled in your
                                           browser, it may be broken in
                                           some way.  In this case,
                                           please disable JavaScript
                                           support and try again.
                                           Otherwise, feel free to
                                           contact one of the site
                                           administrators, who will be
                                           able to manually approve your
                                           comment.</p>"
                                   :message-type "warning"))
                                 (t
                                  (list
                                   :content "<p><strong>Note:</strong></p>

                                           <p>Your message has been received and
                                           classified as non-spam.  It has thus
                                           been put into the moderation queue and
                                           is now awaiting approval by one of the
                                           site's administrators.</p>"
                                   :message-type "success-message"))))))))
                 (expand-page page-template
                              (getf article-params :title)
                              (list* :articles (list article-params)
                                     :link (getf article-params :link)

                                     :info-messages (if submission-notice
                                                        (list submission-notice)
                                                        nil)
                                     template-params)))))))))))


(defun find-transaction-key-handler (path)
  (when (string= path "RPC/generate-transaction-key")
    (dynamic-lambda (*user-name* *password* *propagated-params* *base-uri* *use-ssl-p*) ()
      (with-db
        (list :content-type "text/plain; charset=utf-8"
              :body         (format nil "~D"
                                    (query "SELECT nextval('transaction_key_seq')"
                                           :single!)))))))


(defun keywordify (thing)
  (intern (string-upcase (format nil "~A" thing)) "KEYWORD"))


(defun parse-accept-language-header (accept-lang)
  (let ((*read-eval* nil)
        languages)
    (ppcre:do-register-groups (lang quality)
        ("\\b(\\w+)(?:-\\w+)?(?:;q=([0-9.]+))?\\b" accept-lang)
      (push (cons lang (if quality
                           (read-from-string quality)
                           1.0))
            languages))
    (mapcar (lambda (x) (list (cons "language" (car x))))
            (stable-sort (reverse languages) #'> :key #'cdr))))


(defun find-request-handler (path params accept-lang)
  (let ((*requested-characteristics*
         (or (when-let (langstr (or (cdr (assoc "lang" params :test #'equal))
                                    (cdr (assoc "hl"   params :test #'equal))))
               (let ((langs (split-sequence #\| langstr)))
                 (mapcar (lambda (x) (list (cons "language" x)))
                         langs)))
             (append (parse-accept-language-header accept-lang)
                     *default-characteristics-precedence-list*)))
        (*propagated-params* (remove-if-not (lambda (x)
                                              (member (car x)
                                                      (list "lang" "hl")
                                                      :test #'equal))
                                            params))
        (*base-uri* (if *use-ssl-p*
                        *base-uri/ssl*
                        *base-uri/plain*)))
    (or (find-article-summary-handler path params)
        (find-comment-moderation-handler path params)
        (find-journal-archive-request-handler
         path
         (assoc "full" params :test #'equal)
         (cond ((assoc "feed" params :test #'equal) :view-feed)
               (t                                   :view)))
        (find-article-request-handler
         path
         params
         (cond ((assoc "edit"         params :test #'equal) :edit)
               ((assoc "atom"         params :test #'equal) :view-atom-entry)
               (t                                           :view)))
        (find-transaction-key-handler path))))