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