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