aboutsummaryrefslogtreecommitdiff
path: root/cgi-bin/dict.cgi.in
blob: 3369db5ed6911a814db4b736c0b74e9675ce2221 (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
#! =GUILE_BINDIR=/guile -s
=AUTOGENERATED=
!#
;;;; Greek Dictionary Web Engine
;;;; Copyright (C) 2004, 2005 Sergey Poznyakoff
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;;;

;;; Tailor this statement to your needs if necessary.
;=GUILE_COMMENT=;(set! %load-path (cons "=GUILE_SITE=" %load-path))

(use-modules (www cgi)
	     (gamma sql)
	     (gamma gettext)
	     (xmltools dict)
	     (ellinika xlat))
(cgi:init)

;;; User-definable variables
(define base-dir "=PREFIX=")
(define html-dir "=HTMLDIR=")
(define sysconf-dir "=SYSCONFDIR=")
(define locale-dir "=LOCALEDIR=")
(define ref-loc #f)
  
(define dict-cgi-path "cgi-bin/dict.cgi")        ;; Path to the cgi (relative
                                                 ;; to the Base HREF)

(define config-file-name "ellinika.conf")
(define template-file-name "dict.html")
(define target-language "el_GR")

(define word-forms-reference '())

(define sql-iface "mysql")       ;; SQL interface ("mysql" or "postgres")
(define sql-host "localhost")    ;; SQL server hostname or a path to the UNIX
                                 ;; socket
(define sql-port 3306)           ;; SQL port number (0 for sockaddr_un
                                 ;; connection)
(define sql-database "ellinika") ;; Name of the database
(define sql-username "gray")     ;; Database user name
(define sql-password "")         ;; Password for that user name

(define match-list-columns 4)    ;; Number of colums in fuzzy search output
;;; End of user-definable variables


;;; Load the site defaults
(let ((rc-file (string-append sysconf-dir "/" config-file-name)))
  (if (file-exists? rc-file)
    (load rc-file)))

(define (language-code lang)
  (cond
   ((string-index lang #\_) =>
    (lambda (len)
      (substring lang 0 len)))
   (else
    lang)))

(define (template-file lang)
  (string-append html-dir "/" (language-code lang) "/" template-file-name))

;;; Load the language-specific defaults
(cond
 ((cgi:value "LANG") =>
  (lambda (x)
    (if (and (file-exists? (template-file x))
	     (false-if-exception (setlocale LC_ALL x)))
	(set! target-language x)))))

;;; Initialize i18n
(setlocale LC_ALL target-language)
(bindtextdomain "=PACKAGE=" locale-dir)
(bind_textdomain_codeset "=PACKAGE=" "UTF-8")
(textdomain "=PACKAGE=")

(define (make-cgi-name . rest)
  (apply
   string-append
   (cons
    dict-cgi-path
    (let ((arglist (let ((lang (cgi:value "LANG")))
		     (do ((ilist (if lang
				     (cons "LANG" (cons lang rest))
				     rest) (cdr ilist))
			  (i 1 (1+ i))
			  (olist '()))
			 ((null? ilist) (if (null? olist)
					    olist
					    (reverse (cdr olist))))
		       (set! olist (cons (car ilist) olist))
		       (set! olist (cons 
				    (if (odd? i) "=" "&")
				    olist))))))
      (if (null? arglist)
	  arglist
	  (cons "?" arglist))))))
  
;; Τα μέρη του λογου
(define part-of-speech '())

(define (hbase num)
  (do ((i 0 (1+ i))
       (n num (ash n -1)))
      ((= n 0) (ash 1 (- i 1)))))

(define (load-pos)
  (let ((conn (sql-connect
	       sql-iface sql-host sql-port sql-database
	       sql-username sql-password)))
    (cond
     (conn
      (let ((plist (sql-query
		    conn
		    "SELECT name,id FROM pos WHERE canonical='Y' order by id")))
	(set! part-of-speech
	      (cons
	       (cons "κανένα μέρος του λόγου" #f)
	       (map
		(lambda (x)
		  (let* ((value (string->number (car (cdr x))))
			 (mask (hbase value)))
		    (cons (car x)
			  (number->string
			   (if (= value mask)
			       value
			       (logand
				value
				(lognot mask)))))))
		plist)))
	(sql-connect-close conn))))))
	    
;; Protect occurences of " in a string.
;; Usual backslash escapes do not work in INPUT widgets, so I
;; change all quotation marks to "
;; Possibly not the better solution, though...
(define (protect string)
  (list->string
   (apply append
	  (map
	   (lambda (x)
	     (if (eq? x #\")
		 (list #\& #\# #\3 #\4 #\;)
		 (list x)))
	   (string->list string)))))

(define (get-topic-list)
  (let ((categories #f))
    (letrec ((getcat
	      (lambda ()
		(let ((conn (sql-connect sql-iface sql-host sql-port sql-database
					 sql-username sql-password)))
		  (cond
		   (conn
		    (let ((categories (sql-query
				       conn
				       "SELECT category, title, description FROM category ORDER BY category")))
		      (let ((result (if (null? categories)
					'()
					(map
					 (lambda (category)
					   (let ((topics (sql-query
							  conn
							  (string-append 
							   "SELECT ident,title FROM topic WHERE category="
							   (car category)))))
					     (append category (if (null? topics)
								  '()
								  (list topics)))))
					 categories))))
			(sql-connect-close conn)
			result)))
		   (else
		    '()))))))
      (if (not categories)
	  (set! categories (getcat)))
      categories)))

(define (join-widget widget-id tabindex)
  (let* ((name (string-append "join" widget-id))
	 (selected-choice (or (let ((s (cgi:value name)))
				(if s
				    (string->number s)
				    #f))
			      0)))
    (display (string-append "<SELECT NAME=\""
			    name
			    "\" TABINDEX=\""
			    tabindex
			    "\">"))
    (display "<OPTION VALUE=\"0\"")
    (if (= selected-choice 0)
	(display " selected"))
    (display ">") (display (_"και")) (display "</OPTION>")
    (display "<OPTION VALUE=\"1\"")
    (if (= selected-choice 1)
	(display " selected"))
    (display ">") (display (_"ή")) (display "</OPTION>")
    (display "</SELECT>")))

(define (main-form)
  (load-pos)
  (display "<FORM ACTION=\"")
  (display (make-cgi-name))
  (display "\" METHOD=POST>
<TABLE BORDER=0>
<TR>
 <TD>")
  (display (_"Εισάγετε τη λέξη"))
  (display "
 </TD>
 <TD>
  <INPUT size=36 NAME=\"key\" TABINDEX=\"1\"")
  (let ((value (cgi:value "key")))
    (if value
	(begin
	  (display "VALUE=\"")
	  (display (protect value))
	  (display "\""))))
  (display ">
 </TD>
</TR>")

  (display "<TR><TD COLSPAN=\"3\" ALIGN=\"center\">")
  (display (_"Συμπληρωματικοί όροι"))
  (display "</TD></TR>")

  (display "
<TR>
 <TD>")
  (display (_"Επιλέξτε το μέρος του λόγου"))
  (display "</TD><TD>")
  
  (let ((selected-choice (or (let ((s (cgi:value "POS")))
			       (if s
				   (string->number s)
				   #f))
			     0))
	(index 0))
    
    (display "<SELECT NAME=\"POS\" TABINDEX=\"2\">")
    
    (for-each
     (lambda (x)
       (let ((name (car x)))
	 (display "<OPTION VALUE=")
	 (display index)
	 (if (= index selected-choice)
	     (display " selected"))
	 (display ">")
	 (display name)
	 (set! index (1+ index))))
     part-of-speech)
    (display "</SELECT>"))  

  (display "</TD><TD>")
  (join-widget "pos" "3")
  (display "</TD></TR>")

  (let ((tabindex 4))
    (for-each
     (lambda (category)
       (display "<TR><TD>")
       (display (list-ref category 1))
       (display "</TD><TD>")
       (let ((selected-choice (or (let ((s (cgi:value (list-ref category 0))))
				    (if s
					(string->number s)
					#f))
				  0)))

	 (display (string-append
		   "<SELECT NAME=\""
		   (list-ref category 0)
		   "\" TABINDEX=\""
		   (number->string tabindex)
		   "\">"))
	 (set! tabindex (1+ tabindex))
	 (display "<OPTION VALUE=0>---")
	 (for-each
	  (lambda (x)
	    (let ((id (car x))
		  (name (car (cdr x))))
	      (display "<OPTION VALUE=")
	      (display id)
	      (if (eq? (string->number id) selected-choice)
		  (display " selected"))
	      (display ">")
	      (display name)
	      (display "</OPTION>")))
	  (list-ref category 3))
	 (display "</SELECT>")
	 (display "</TD><TD>")
	 (join-widget (list-ref category 0) (number->string tabindex))
	 (display "</TD></TR>")
	 (set! tabindex (1+ tabindex))))
     (get-topic-list))
  
    (display "
<TR>
 <TD colspan=\"2\" align=center>
  <INPUT TYPE=\"submit\" NAME=\"search\" VALUE=\"")
    (display (_"Αναζήτηση"))
    (display "\" TABINDEX=\"")
    (display tabindex)
    (display "\">
 </TD>
</TR>
</TABLE>
</FORM>
<P>")))

;;
(define (replace-tilde word sentence)
  (apply
   string-append
   (let loop ((lst '())
	      (str sentence))
     (cond
      ((string-index str #\~) =>
       (lambda (x)
	 (loop
	  (append lst (list (substring str 0 x) word))
	  (substring str (1+ x)))))
      ((string-null? str)
       lst)
      (else
       (append lst (list str)))))))

;;
(define (display-results rlist)
  (let ((x (car rlist)))
    (display "<TABLE BORDER=0>")
    (display "<TR><TD>")
    (display (car x))
    (display "</TD>")
    (cond
     ((list-ref x 3)
      (display "<TD>")
      (let ((href (assoc (list-ref x 2) word-forms-reference)))
	(cond
	 (href
	  (display "<A HREF=\"")
	  (cond
	   (ref-loc
	    (display ref-loc)
	    (display "/")))
	  (display (language-code target-language))
	  (display "/")
	  (display (cdr href))
	  (display (dict:encode-string (car x)))
	  (display "\">")
	  (display (list-ref x 3))
	  (display "</A>"))
	 (else
	  (display (list-ref x 3)))))
      (display "</TD>")))
    (display "<TD>")
    (display (list-ref x 2))
    (display "</TD></TR>"))
  (for-each
   (lambda (x)
     (display "<TR><TD>")
     (display (1+ (string->number (list-ref x 4))))
     (display "</TD><TD>")
     (display (replace-tilde (car x) (list-ref x 5)))
     (display ";</TD></TR>"))
   rlist)
  (display "</TABLE>")
  (newline))

(define (display-cross-reference word)
  (display "<A HREF=\"")
  (display (make-cgi-name "IDENT" (dict:encode-string word)))
  (display "\">")
  (display word)
  (display "</A>"))

(define (display-xref rlist text)
  (display text)
  (let ((n 0))
    (for-each
     (lambda (x)
       (if (> n 0)
	   (display ", "))
       (set! n (1+ n))
       (display-cross-reference (car x)))
     rlist))
  (display ";"))

(define (sort-result input-list)
  (let ((output-list '())
	(current-element '()))
    (for-each
     (lambda (x)
       (cond
	((or (null? current-element)
	     (=  (string->number (cadr x))
		 (string->number (cadr (car current-element)))))
	 (set! current-element (cons x current-element)))
	(else
	 (set! output-list (cons (reverse current-element) output-list))
	 (set! current-element (list x)))))
     input-list)
    (cons (reverse current-element) output-list)))


(define (search-failure key)
  (display "<H2>")
  (format #t (_"Συγγνώμη, η λέξη \"~A\" δεν βρέθηκε στο λέξικο.") key)
  (display "</H2>"))

(define (fuzzy-search conn key theme pos)
  (let* ((query  (string-append
		   "SELECT DISTINCT dict.word FROM dict,topic_tab WHERE "

		   (if (not (null? theme))
		       " topic_tab.word_ident=dict.ident "
		       "")
		   
		   (cond
		    ((not (string-null? key))
		     (string-append
		      (if (not (null? theme))
			  "AND" "")
		      " dict.sound LIKE \""
		      (ellinika:sounds-like key)
		      "%\" "))
		    (else
		     " "))
		   
		   (cond
		    ((> (string->number pos) 0)
		     (let ((pos-entry
			    (list-ref part-of-speech (string->number pos))))
		       (string-append
			(if (or (not (string-null? key)) (not (null? theme)))
			    (if (string=? (cgi:value "joinpos") "0")
				"AND"
				"OR")
			    "")
			" dict.pos & "
			(cdr pos-entry)
			" ")))
		    (else
		     " "))
		   
		   (apply
		    string-append
		    (map
		     (lambda (x)
		       (cond
			((boolean? x)
			 (if x "AND " "OR "))
			(else
			 (string-append "topic_tab.topic_ident=" x " "))))
		     theme))
		   
		   " ORDER BY dict.word"))

	 (result (sql-query conn query)))

    (cond
     ((null? result)
      (search-failure key))
     (else
      (display "<TABLE WIDTH=\"100%\">")
      (let* ((result-length (length result))
	     (lim (1+ (inexact->exact (/ result-length match-list-columns)))))
	(do ((i 0 (1+ i)))
	    ((= i lim) #f)
	  (display "<TR>")	  
	  (do ((j i (+ j lim)))
	      ((>= j result-length) #f)
	    (display "<TD>")
	    (display-cross-reference (car (list-ref result j)))
	    (display "</TD>"))
	  (display "</TR>")))	  
      (display "</TABLE>")))))


(define (dict-search)
  (let ((keyval (if (cgi:value "IDENT")
		    (dict:decode-string (cgi:value "IDENT"))
		    (cgi:value "key")))
	(theme (do ((catlist (get-topic-list) (cdr catlist))
		    (ret '()))
		   ((null? catlist) ret)
		 (let ((name (caar catlist)))
		   (let ((v (cgi:value name)))
		     (if (and v (> (string->number v) 0))
			 (set! ret (append
				    ret
				    (list (= (string->number
					      (cgi:value (string-append "join" name))) 0)
					  v))))))))
	(pos (or (cgi:value "POS") "0")))

    (cond
     ((and keyval
	   (not (string-null? keyval))
	   (null? theme)
	   (= (string->number pos) 0))

      (let ((conn (sql-connect
		   sql-iface sql-host sql-port sql-database
		   sql-username sql-password)))
	(cond
	 ((not conn)
	  (format #t "<H1>~A</H1>\n"
		  "ΣΦΆΛΜΑ: σύνδεση με το λέξικο απέτυχε."))
	 (else
	  (display "<HR>")
	  (let* ((key (ellinika:translate-input keyval))
		 (result (sql-query
			  conn
			  (string-append
			   "SELECT dict.word,dict.ident,pos.abbr,dict.forms,articles.subindex,articles.meaning FROM dict,articles,pos WHERE dict.word=\""
			   key
			   "\" and dict.ident=articles.ident and dict.pos=pos.id and pos.canonical='Y' order by dict.ident, articles.subindex"))))
	    
	    (cond
	     ((null? result)
	      (fuzzy-search conn key theme pos))
	     (else
	      (for-each
	       (lambda (entry)
		 (display-results entry)
		 (let ((ant (sql-query
			     conn
			     (string-append
			      "SELECT dict.word FROM dict,links WHERE links.type='ANT' AND links.ident="
			      (cadr (car entry))
			      " AND dict.ident=links.xref ORDER BY word"))))
		   (if (and ant (not (null? ant)))
		       (display-xref ant
				     (if (= (length ant) 1)
					 (_"Антоним: ") (_"Антонимы: ")))))
		 (display "<P>")
		 (let ((x (sql-query
			   conn
			   (string-append
			    "SELECT dict.word FROM dict,links WHERE links.type='XREF' AND links.ident="
			    (cadr (car entry))
			    " AND dict.ident=links.xref ORDER BY word"))))
		   (if (and x (not (null? x)))
		       (display-xref x (_"См. также ")))))
	       (sort-result result))))
	    (sql-connect-close conn))))))
     ((or (not (null? theme)) (> (string->number pos) 0))
      (let ((conn (sql-connect
		   sql-iface sql-host sql-port sql-database
		   sql-username sql-password)))
	(cond
	 ((not conn)
	  (format #t "<H1></H1>\n"
		  (_"ΣΦΆΛΜΑ: σύνδεση με το λέξικο απέτυχε.")))
	 (else
	  (display "<HR>")
	  (fuzzy-search conn (ellinika:translate-input (or keyval "")) theme pos)
	  (sql-connect-close conn))))))))

;;;

(define (stat key)
  (let ((stat-data #f))
    (if (not stat-data)
	(set! stat-data (let ((conn (sql-connect
				     sql-iface sql-host sql-port sql-database
				     sql-username sql-password)))
			  (cond
			   (conn
			    (let ((x (sql-query conn
						"SELECT count,updated from stat")))
			    (sql-connect-close conn)
			    x))
			   (else
			    '())))))
    
    (if (null? stat-data)
	"<>"
	(case key
	 ((#:updated)
	   (list-ref (car stat-data) 1))
	 ((#:count)
	   (list-ref (car stat-data) 0))
	 (else
	  "<>")))))
  

;;;

(define (dict-html)
  (do ((line (read-line) (read-line)))
      ((eof-object? line) #f)
    (cond
     ((string=? line "@@dict@@")
      (main-form)
      (dict-search))
     ((string=? line "@@stat_updated@@")
      (display (stat #:updated)))
     ((string=? line "@@stat_count@@")
      (display
       (let ((s (stat #:count)))
	 (if (string=? s "<>")
	     s
	     (let ((n (string->number s)))
	       (string-append s " " (ngettext "λέξη" "λέξεις" n)))))))
     (else
      (display line)
      (newline)))))  
  
;;; Main
(display "Content-type: text/html; charset=utf-8\r\n\r\n")

(with-input-from-file
    (template-file target-language)
  dict-html)


;;;; Local variables:
;;;; mode: Scheme
;;;; buffer-file-coding-system: utf-8
;;;; End:

Return to:

Send suggestions and report system problems to the System administrator.