more font size tweaks.
[citadel.git] / webcit / wiki.c
1 /*
2  * Functions pertaining to rooms with a wiki view
3  *
4  * Copyright (c) 2009-2021 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "dav.h"
17
18 /* 
19  * Convert a string to something suitable as a wiki index
20  */
21 void str_wiki_index(StrBuf *s)
22 {
23         StrBufSanitizeAscii(s, '_');
24         StrBufLowerCase(s);
25 }
26
27 /*
28  * Display a specific page from a wiki room
29  *
30  * "rev" may be set to an empty string to display the current version.
31  * "do_revert" may be set to nonzero to perform a reversion to the specified version.
32  */
33 void display_wiki_page_backend(StrBuf *pagename, char *rev, int do_revert)
34 {
35         const StrBuf *Mime;
36         long msgnum = (-1L);
37         char buf[256];
38
39         if (WC->CurRoom.view != VIEW_WIKI) {
40                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WC->CurRoom.name) );
41                 return;
42         }
43
44         if (StrLength(pagename) == 0) {
45                 StrBufPlain(pagename, HKEY("home"));
46         }
47
48         str_wiki_index(pagename);       /* convert index name to lowercase and numeric only */
49
50         if ((rev != NULL) && (strlen(rev) > 0)) {
51                 /* read an older revision */
52                 serv_printf("WIKI rev|%s|%s|%s", ChrPtr(pagename), rev, (do_revert ? "revert" : "fetch") );
53                 serv_getln(buf, sizeof buf);
54                 if (buf[0] == '2') {
55                         msgnum = extract_long(&buf[4], 0);
56                 }
57         }
58         else {
59                 /* read the current revision */
60                 msgnum = locate_message_by_uid(ChrPtr(pagename));
61         }
62
63         if (msgnum >= 0L) {
64                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
65                 return;
66         }
67         putbstr("pagename", pagename);
68         do_template("wiki_empty");
69 }
70
71
72 /*
73  * Display a specific page from a wiki room
74  */
75 void display_wiki_page(void)
76 {
77         StrBuf *pagename;
78         char rev[128];
79         int do_revert = 0;
80
81         output_headers(1, 1, 1, 0, 0, 0);
82         pagename = NewStrBufDup(sbstr("page"));
83         str_wiki_index(pagename);
84         safestrncpy(rev, bstr("rev"), sizeof rev);
85         do_revert = atoi(bstr("revert"));
86         display_wiki_page_backend(pagename, rev, do_revert);
87         wDumpContent(1);
88 }
89
90
91 /*
92  * Display the revision history for a wiki page (template callback)
93  */
94 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
95 {
96         StrBuf *pagename;
97         StrBuf *Buf;
98         int row = 0;
99
100         pagename = NewStrBufDup(sbstr("page"));
101         str_wiki_index(pagename);
102
103         serv_printf("WIKI history|%s", ChrPtr(pagename));
104         Buf = NewStrBuf();
105         StrBuf_ServGetln(Buf);
106         if (GetServerStatus(Buf, NULL) == 1) {
107
108                 time_t rev_date;
109                 char rev_date_displayed[64];
110                 StrBuf *rev_uuid = NewStrBuf();
111                 StrBuf *author = NewStrBuf();
112                 StrBuf *node = NewStrBuf();
113
114                 wc_printf("<table class=\"wiki_history_background\">");
115
116                 wc_printf("<th>%s</th>", _("Date"));
117                 wc_printf("<th>%s</th>", _("Author"));
118
119                 while((StrBuf_ServGetln(Buf) >= 0) &&  strcmp(ChrPtr(Buf), "000")) {
120
121                         rev_date = extract_long(ChrPtr(Buf), 1);
122                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
123                         StrBufExtract_token(author, Buf, 2, '|');
124
125                         wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
126                         wc_printf("<td>%s</td><td>", rev_date_displayed);
127                         wc_printf("<a href=\"showuser?who=");
128                         urlescputs(ChrPtr(author));
129                         wc_printf("\">");
130                         escputs(ChrPtr(author));
131                         wc_printf("</a></td>");
132
133                         if (row == 0) {
134                                 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
135                                 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
136                                 wc_printf("\">%s</a></td>", _("(show)"));
137                                 wc_printf("<td>(%s)</td>", _("Current version"));
138                         }
139
140                         else {
141                                 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
142                                         bstr("page"),
143                                         ChrPtr(rev_uuid)
144                                 );
145                                 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
146                                 wc_printf("\">%s</a></td>", _("(show)"));
147                                 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
148                                         bstr("page"),
149                                         ChrPtr(rev_uuid),
150                                         _("(revert)")
151                                 );
152                         }
153                         wc_printf("</tr>\n");
154
155                         /* Extract all fields except the author and date after displaying the row.  This
156                          * is deliberate, because the timestamp reflects when the diff was written, not
157                          * when the version which it reflects was written.  Similarly, the name associated
158                          * with each diff is the author who created the newer version of the page that
159                          * made the diff happen.
160                          */
161                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
162                         StrBufExtract_token(node, Buf, 3, '|');
163                         ++row;
164                 }
165
166                 wc_printf("</table>\n");
167                 FreeStrBuf(&author);
168                 FreeStrBuf(&node);
169                 FreeStrBuf(&rev_uuid);
170         }
171         else {
172                 wc_printf("%s", ChrPtr(Buf));
173         }
174
175         FreeStrBuf(&Buf);
176 }
177
178
179
180 /*
181  * Display the revision history for a wiki page
182  */
183 void display_wiki_history(void)
184 {
185         output_headers(1, 1, 1, 0, 0, 0);
186         do_template("wiki_history");
187         wDumpContent(1);
188 }
189
190
191 /*
192  * Display a list of all pages in a Wiki room (template callback)
193  */
194 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
195 {
196         StrBuf *Buf;
197         int row = 0;
198
199         if (!IsEmptyStr(bstr("query"))) {
200                 serv_printf("MSGS SEARCH|%s||4", bstr("query"));        /* search-reduced list */
201         }
202         else {
203                 serv_printf("MSGS ALL|||4");                            /* full list */
204         }
205
206         Buf = NewStrBuf();
207         StrBuf_ServGetln(Buf);
208         if (GetServerStatus(Buf, NULL) == 1) {
209                 StrBuf *pagetitle = NewStrBuf();
210
211                 wc_printf("<table class=\"wiki_pagelist_background\">");
212                 wc_printf("<th>%s</th>", _("Page title"));
213
214                 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
215                         StrBufExtract_token(pagetitle, Buf, 1, '|');
216
217                         if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) {    /* no history pages */
218                                 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
219                                 wc_printf("<td><a href=\"wiki?page=");
220                                 urlescputs(ChrPtr(pagetitle));
221                                 wc_printf("\">");
222                                 escputs(ChrPtr(pagetitle));
223                                 wc_printf("</a></td>");
224                                 wc_printf("</tr>\n");
225                                 ++row;
226                         }
227                 }
228                 wc_printf("</table>\n");
229                 FreeStrBuf(&pagetitle);
230         }
231
232         FreeStrBuf(&Buf);
233 }
234
235
236 /*
237  * Display a list of all pages in a Wiki room.  Search requests in a Wiki room also go here.
238  */
239 void display_wiki_pagelist(void)
240 {
241         output_headers(1, 1, 1, 0, 0, 0);
242         do_template("wiki_pagelist");
243         wDumpContent(1);
244 }
245
246
247 int wiki_Cleanup(void **ViewSpecific)
248 {
249         StrBuf *pagename;
250         pagename = NewStrBufDup(sbstr("page"));
251         display_wiki_page_backend(pagename, "", 0);
252         wDumpContent(1);
253         return 0;
254 }
255
256
257 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
258 {
259         const char *page;
260         const char *pch;
261         long len;
262
263         page = BSTR("page");
264         GetTemplateTokenString(Target, TP, 2, &pch, &len);
265         return strcasecmp(page, pch) == 0;
266 }
267
268
269 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
270 {
271         const char *pch;
272         long len;
273
274         GetTemplateTokenString(Target, TP, 2, &pch, &len);
275         return bmstrcasestr((char *)ChrPtr(WC->Hdr->HR.ReqLine), pch) != NULL;
276 }
277
278
279 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
280 {
281         /* this function was intentionaly left empty. */
282         return 0;
283 }
284
285 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat, 
286                                 void **ViewSpecific, 
287                                 long oper, 
288                                 char *cmd, 
289                                 long len,
290                                 char *filter,
291                                 long flen)
292 {
293         if (oper == do_search)
294                 display_wiki_pagelist();
295         else 
296                 http_redirect("wiki?page=home");
297
298         return 300;
299 }
300
301
302 void 
303 InitModule_WIKI
304 (void)
305 {
306         RegisterReadLoopHandlerset(
307                 VIEW_WIKI,
308                 wiki_GetParamsGetServerCall,
309                 wiki_PrintHeaderPage,
310                 NULL,
311                 NULL,
312                 NULL,
313                 NULL,
314                 wiki_Cleanup,
315                 NULL
316         );
317
318         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
319         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
320         WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
321         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
322         RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
323         RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
324         RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);
325 }