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