more 'go=' links in wiki
[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",
174                                         bstr("page"),
175                                         ChrPtr(rev_uuid)
176                                 );
177                                 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
178                                 wc_printf("\">%s</a></td>", _("(show)"));
179                                 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
180                                         bstr("page"),
181                                         ChrPtr(rev_uuid),
182                                         _("(revert)")
183                                 );
184                         }
185                         wc_printf("</tr>\n");
186
187                         /* Extract all fields except the author and date after displaying the row.  This
188                          * is deliberate, because the timestamp reflects when the diff was written, not
189                          * when the version which it reflects was written.  Similarly, the name associated
190                          * with each diff is the author who created the newer version of the page that
191                          * made the diff happen.
192                          */
193                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
194                         StrBufExtract_token(node, Buf, 3, '|');
195                         ++row;
196                 }
197
198                 wc_printf("</table>\n");
199                 FreeStrBuf(&author);
200                 FreeStrBuf(&node);
201                 FreeStrBuf(&rev_uuid);
202         }
203         else {
204                 wc_printf("%s", ChrPtr(Buf));
205         }
206
207         FreeStrBuf(&Buf);
208 }
209
210
211
212 /*
213  * Display the revision history for a wiki page
214  */
215 void display_wiki_history(void)
216 {
217         output_headers(1, 1, 1, 0, 0, 0);
218         do_template("wiki_history");
219         wDumpContent(1);
220 }
221
222
223 /*
224  * Display a list of all pages in a Wiki room (template callback)
225  */
226 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
227 {
228         StrBuf *Buf;
229         int row = 0;
230
231         if (!IsEmptyStr(bstr("query"))) {
232                 serv_printf("MSGS SEARCH|%s||4", bstr("query"));        /* search-reduced list */
233         }
234         else {
235                 serv_printf("MSGS ALL|||4");                            /* full list */
236         }
237
238         Buf = NewStrBuf();
239         StrBuf_ServGetln(Buf);
240         if (GetServerStatus(Buf, NULL) == 1) {
241                 StrBuf *pagetitle = NewStrBuf();
242
243                 wc_printf("<table class=\"wiki_pagelist_background\">");
244                 wc_printf("<th>%s</th>", _("Page title"));
245
246                 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
247                         StrBufExtract_token(pagetitle, Buf, 1, '|');
248
249                         if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) {    /* no history pages */
250                                 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
251                                 wc_printf("<td><a href=\"wiki?page=");
252                                 urlescputs(ChrPtr(pagetitle));
253                                 wc_printf("\">");
254                                 escputs(ChrPtr(pagetitle));
255                                 wc_printf("</a></td>");
256                                 wc_printf("</tr>\n");
257                                 ++row;
258                         }
259                 }
260                 wc_printf("</table>\n");
261                 FreeStrBuf(&pagetitle);
262         }
263
264         FreeStrBuf(&Buf);
265 }
266
267
268 /*
269  * Display a list of all pages in a Wiki room.  Search requests in a Wiki room also go here.
270  */
271 void display_wiki_pagelist(void)
272 {
273         output_headers(1, 1, 1, 0, 0, 0);
274         do_template("wiki_pagelist");
275         wDumpContent(1);
276 }
277
278
279 int wiki_Cleanup(void **ViewSpecific)
280 {
281         char pagename[5];
282         safestrncpy(pagename, "home", sizeof pagename);
283         display_wiki_page_backend(pagename, "", 0);
284         wDumpContent(1);
285         return 0;
286 }
287
288
289 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
290 {
291         const char *page;
292         const char *pch;
293         long len;
294
295         page = BSTR("page");
296         GetTemplateTokenString(Target, TP, 2, &pch, &len);
297         return strcasecmp(page, pch) == 0;
298 }
299
300
301 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
302 {
303         wcsession *WCC = WC;
304         const char *pch;
305         long len;
306
307         GetTemplateTokenString(Target, TP, 2, &pch, &len);
308         return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
309 }
310
311
312 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
313 {
314         /* this function was intentionaly left empty. */
315         return 0;
316 }
317
318 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat, 
319                                 void **ViewSpecific, 
320                                 long oper, 
321                                 char *cmd, 
322                                 long len,
323                                 char *filter,
324                                 long flen)
325 {
326         if (oper == do_search)
327                 display_wiki_pagelist();
328         else 
329                 http_redirect("wiki?page=home");
330
331         return 300;
332 }
333
334
335 void 
336 InitModule_WIKI
337 (void)
338 {
339         RegisterReadLoopHandlerset(
340                 VIEW_WIKI,
341                 wiki_GetParamsGetServerCall,
342                 wiki_PrintHeaderPage,
343                 NULL,
344                 NULL,
345                 NULL,
346                 NULL,
347                 wiki_Cleanup
348         );
349
350         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
351         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
352         WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
353         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
354         RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
355         RegisterConditional(HKEY("COND:WIKI:PAGE"), 1, ConditionalHaveWikiPage, CTX_NONE);
356         RegisterConditional(HKEY("COND:WIKI:TYPE"), 1, ConditionalHavewikiType, CTX_NONE);
357 }