4f14137904ca34dd02172e3bf58535876b946c5e
[citadel.git] / webcit / wiki.c
1 /*
2  * Functions pertaining to rooms with a wiki view
3  *
4  * Copyright (c) 2009-2012 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  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "dav.h"
23
24 /* 
25  * Convert a string to something suitable as a wiki index
26  */
27 void str_wiki_index(char *s)
28 {
29         int i;
30
31         if (s == NULL) return;
32
33         /* First remove all non-alphanumeric characters */
34         for (i=0; i<strlen(s); ++i) {
35                 if (!isalnum(s[i])) {
36                         strcpy(&s[i], &s[i+1]);
37                 }
38         }
39
40         /* Then make everything lower case */
41         for (i=0; i<strlen(s); ++i) {
42                 s[i] = tolower(s[i]);
43         }
44 }
45
46 /*
47  * Display a specific page from a wiki room
48  *
49  * "rev" may be set to an empty string to display the current version.
50  * "do_revert" may be set to nonzero to perform a reversion to the specified version.
51  */
52 void display_wiki_page_backend(const StrBuf *roomname, char *pagename, char *rev, int do_revert)
53 {
54         const StrBuf *Mime;
55         long msgnum = (-1L);
56         char buf[256];
57
58         str_wiki_index(pagename);
59
60         if (StrLength(roomname) > 0) {
61
62                 /* If we're not in the correct room, try going there. */
63                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
64                         gotoroom(roomname);
65                 }
66         
67                 /* If we're still not in the correct room, it doesn't exist. */
68                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
69                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
70                         return;
71                 }
72
73         }
74
75         if (WC->CurRoom.view != VIEW_WIKI) {
76                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
77                 return;
78         }
79
80         if (IsEmptyStr(pagename)) {
81                 strcpy(pagename, "home");
82         }
83
84         /* Found it!  Now read it. */
85
86         if ((rev != NULL) && (strlen(rev) > 0)) {
87                 /* read an older revision */
88                 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
89                 serv_getln(buf, sizeof buf);
90                 if (buf[0] == '2') {
91                         msgnum = extract_long(&buf[4], 0);
92                 }
93         }
94         else {
95                 /* read the current revision? */
96                 msgnum = locate_message_by_uid(pagename);
97         }
98
99         if (msgnum >= 0L) {
100                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
101                 return;
102         }
103
104         wc_printf("<br><br>"
105                 "<div align=\"center\">"
106                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
107                 "<tr><td align=\"center\">"
108         );
109         wc_printf("<br><b>");
110         wc_printf(_("There is no page called '%s' here."), pagename);
111         wc_printf("</b><br><br>");
112         wc_printf(_("Select the 'Edit this page' link in the room banner "
113                 "if you would like to create this page."));
114         wc_printf("<br><br>");
115         wc_printf("</td></tr></table></div>\n");
116 }
117
118
119 /*
120  * Display a specific page from a wiki room
121  */
122 void display_wiki_page(void)
123 {
124         const StrBuf *roomname;
125         char pagename[128];
126         char rev[128];
127         int do_revert = 0;
128
129         output_headers(1, 1, 1, 0, 0, 0);
130         roomname = sbstr("room");
131         safestrncpy(pagename, bstr("page"), sizeof pagename);
132         safestrncpy(rev, bstr("rev"), sizeof rev);
133         do_revert = atoi(bstr("revert"));
134         display_wiki_page_backend(roomname, pagename, rev, do_revert);
135         wDumpContent(1);
136 }
137
138
139 /*
140  * Display the revision history for a wiki page (template callback)
141  */
142 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
143 {
144         const StrBuf *roomname;
145         char pagename[128];
146         StrBuf *Buf;
147         int row = 0;
148
149         roomname = sbstr("room");
150         safestrncpy(pagename, bstr("page"), sizeof pagename);
151         str_wiki_index(pagename);
152
153         if (StrLength(roomname) > 0) {
154
155                 /* If we're not in the correct room, try going there. */
156                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
157                         gotoroom(roomname);
158                 }
159         
160                 /* If we're still not in the correct room, it doesn't exist. */
161                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
162                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
163                         return;
164                 }
165
166         }
167
168         serv_printf("WIKI history|%s", pagename);
169         Buf = NewStrBuf();
170         StrBuf_ServGetln(Buf);
171         if (GetServerStatus(Buf, NULL) == 1) {
172
173                 time_t rev_date;
174                 char rev_date_displayed[64];
175                 StrBuf *rev_uuid = NewStrBuf();
176                 StrBuf *author = NewStrBuf();
177                 StrBuf *node = NewStrBuf();
178
179                 wc_printf("<table class=\"wiki_history_background\">");
180
181                 wc_printf("<th>%s</th>", _("Date"));
182                 wc_printf("<th>%s</th>", _("Author"));
183
184                 while((StrBuf_ServGetln(Buf) >= 0) &&  strcmp(ChrPtr(Buf), "000")) {
185
186                         rev_date = extract_long(ChrPtr(Buf), 1);
187                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
188                         StrBufExtract_token(author, Buf, 2, '|');
189
190                         wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
191                         wc_printf("<td>%s</td><td>", rev_date_displayed);
192                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
193                                 escputs(ChrPtr(author));
194                                 wc_printf(" @ ");
195                                 escputs(ChrPtr(node));
196                         }
197                         else {
198                                 wc_printf("<a href=\"showuser?who=");
199                                 urlescputs(ChrPtr(author));
200                                 wc_printf("\">");
201                                 escputs(ChrPtr(author));
202                                 wc_printf("</a>");
203                         }
204                         wc_printf("</td>");
205
206                         if (row == 0) {
207                                 wc_printf("<td><a href=\"wiki?page=%s\">%s</a></td>",
208                                         bstr("page"),
209                                         _("(show)")
210                                 );
211                                 wc_printf("<td>(%s)</td>", _("Current version"));
212                         }
213
214                         else {
215                                 wc_printf("<td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
216                                         bstr("page"),
217                                         ChrPtr(rev_uuid),
218                                         _("(show)")
219                                 );
220                                 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
221                                         bstr("page"),
222                                         ChrPtr(rev_uuid),
223                                         _("(revert)")
224                                 );
225                         }
226                         wc_printf("</tr>\n");
227
228                         /* Extract all fields except the author and date after displaying the row.  This
229                          * is deliberate, because the timestamp reflects when the diff was written, not
230                          * when the version which it reflects was written.  Similarly, the name associated
231                          * with each diff is the author who created the newer version of the page that
232                          * made the diff happen.
233                          */
234                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
235                         StrBufExtract_token(node, Buf, 3, '|');
236                         ++row;
237                 }
238
239                 wc_printf("</table>\n");
240                 FreeStrBuf(&author);
241                 FreeStrBuf(&node);
242                 FreeStrBuf(&rev_uuid);
243         }
244         else {
245                 wc_printf("%s", ChrPtr(Buf));
246         }
247
248         FreeStrBuf(&Buf);
249 }
250
251
252
253 /*
254  * Display the revision history for a wiki page
255  */
256 void display_wiki_history(void)
257 {
258         output_headers(1, 1, 1, 0, 0, 0);
259         do_template("wiki_history");
260         wDumpContent(1);
261 }
262
263
264 /*
265  * Display a list of all pages in a Wiki room (template callback)
266  */
267 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
268 {
269         const StrBuf *roomname;
270         StrBuf *Buf;
271         int row = 0;
272
273         roomname = sbstr("room");
274         if (StrLength(roomname) > 0) {
275                 /* If we're not in the correct room, try going there. */
276                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
277                         gotoroom(roomname);
278                 }
279         
280                 /* If we're still not in the correct room, it doesn't exist. */
281                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
282                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
283                         return;
284                 }
285         }
286
287         if (!IsEmptyStr(bstr("query"))) {
288                 serv_printf("MSGS SEARCH|%s||4", bstr("query"));        /* search-reduced list */
289         }
290         else {
291                 serv_printf("MSGS ALL|||4");                            /* full list */
292         }
293
294         Buf = NewStrBuf();
295         StrBuf_ServGetln(Buf);
296         if (GetServerStatus(Buf, NULL) == 1) {
297                 StrBuf *pagetitle = NewStrBuf();
298
299                 wc_printf("<table class=\"wiki_pagelist_background\">");
300                 wc_printf("<th>%s</th>", _("Page title"));
301
302                 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
303                         StrBufExtract_token(pagetitle, Buf, 1, '|');
304
305                         if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) {    /* no history pages */
306                                 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
307                                 wc_printf("<td><a href=\"wiki?page=");
308                                 urlescputs(ChrPtr(pagetitle));
309                                 wc_printf("\">");
310                                 escputs(ChrPtr(pagetitle));
311                                 wc_printf("</a></td>");
312                                 wc_printf("</tr>\n");
313                                 ++row;
314                         }
315                 }
316                 wc_printf("</table>\n");
317                 FreeStrBuf(&pagetitle);
318         }
319
320         FreeStrBuf(&Buf);
321 }
322
323
324 /*
325  * Display a list of all pages in a Wiki room.  Search requests in a Wiki room also go here.
326  */
327 void display_wiki_pagelist(void)
328 {
329         output_headers(1, 1, 1, 0, 0, 0);
330         do_template("wiki_pagelist");
331         wDumpContent(1);
332 }
333
334
335 int wiki_Cleanup(void **ViewSpecific)
336 {
337         char pagename[5];
338         safestrncpy(pagename, "home", sizeof pagename);
339         display_wiki_page_backend(WC->CurRoom.name, pagename, "", 0);
340         wDumpContent(1);
341         return 0;
342 }
343
344
345 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
346 {
347         const char *page;
348         const char *pch;
349         long len;
350
351         page = BSTR("page");
352         GetTemplateTokenString(Target, TP, 2, &pch, &len);
353         return strcasecmp(page, pch) == 0;
354 }
355
356
357 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
358 {
359         wcsession *WCC = WC;
360         const char *pch;
361         long len;
362
363         GetTemplateTokenString(Target, TP, 2, &pch, &len);
364         return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
365 }
366
367
368 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
369 {
370         /* this function was intentionaly left empty. */
371         return 0;
372 }
373
374 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat, 
375                                 void **ViewSpecific, 
376                                 long oper, 
377                                 char *cmd, 
378                                 long len,
379                                 char *filter,
380                                 long flen)
381 {
382         if (oper == do_search)
383                 display_wiki_pagelist();
384         else 
385                 http_redirect("wiki?page=home");
386
387         return 300;
388 }
389
390
391 void 
392 InitModule_WIKI
393 (void)
394 {
395         RegisterReadLoopHandlerset(
396                 VIEW_WIKI,
397                 wiki_GetParamsGetServerCall,
398                 wiki_PrintHeaderPage,
399                 NULL,
400                 NULL,
401                 NULL,
402                 NULL,
403                 wiki_Cleanup
404         );
405
406         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
407         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
408         WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
409         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
410         RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
411         RegisterConditional(HKEY("COND:WIKI:PAGE"), 1, ConditionalHaveWikiPage, CTX_NONE);
412         RegisterConditional(HKEY("COND:WIKI:TYPE"), 1, ConditionalHavewikiType, CTX_NONE);
413 }