* Added the code to let display_wiki_page_backend() know when it's expected to fetch...
[citadel.git] / webcit / wiki.c
1 /*
2  * $Id$
3  *
4  * Functions pertaining to rooms with a wiki view
5  */
6
7 #include "webcit.h"
8 #include "groupdav.h"
9
10 /* 
11  * Convert a string to something suitable as a wiki index
12  */
13 void str_wiki_index(char *s)
14 {
15         int i;
16
17         if (s == NULL) return;
18
19         /* First remove all non-alphanumeric characters */
20         for (i=0; i<strlen(s); ++i) {
21                 if (!isalnum(s[i])) {
22                         strcpy(&s[i], &s[i+1]);
23                 }
24         }
25
26         /* Then make everything lower case */
27         for (i=0; i<strlen(s); ++i) {
28                 s[i] = tolower(s[i]);
29         }
30 }
31
32 /*
33  * Display a specific page from a wiki room
34  *
35  * "rev" may be set to an empty string to display the current version.
36  */
37 void display_wiki_page_backend(const StrBuf *roomname, char *pagename, char *rev)
38 {
39         const StrBuf *Mime;
40         long msgnum = (-1L);
41
42         str_wiki_index(pagename);
43
44         if (StrLength(roomname) > 0) {
45
46                 /* If we're not in the correct room, try going there. */
47                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
48                         gotoroom(roomname);
49                 }
50         
51                 /* If we're still not in the correct room, it doesn't exist. */
52                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
53                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
54                         return;
55                 }
56
57         }
58
59         if (WC->wc_view != VIEW_WIKI) {
60                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
61                 return;
62         }
63
64         if (IsEmptyStr(pagename)) {
65                 strcpy(pagename, "home");
66         }
67
68         /* Found it!  Now read it.   FIXME this is where we have to add the ability to read an old rev */
69         msgnum = locate_message_by_uid(pagename);
70         if (msgnum >= 0L) {
71                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
72                 return;
73         }
74
75         wc_printf("<br /><br />"
76                 "<div align=\"center\">"
77                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
78                 "<tr><td align=\"center\">"
79         );
80         wc_printf("<br><b>");
81         wc_printf(_("There is no page called '%s' here."), pagename);
82         wc_printf("</b><br><br>");
83         wc_printf(_("Select the 'Edit this page' link in the room banner "
84                 "if you would like to create this page."));
85         wc_printf("<br><br>");
86         wc_printf("</td></tr></table></div>\n");
87 }
88
89
90 /*
91  * Display a specific page from a wiki room
92  */
93 void display_wiki_page(void)
94 {
95         const StrBuf *roomname;
96         char pagename[128];
97         char rev[128];
98
99         output_headers(1, 1, 1, 0, 0, 0);
100         roomname = sbstr("room");
101         safestrncpy(pagename, bstr("page"), sizeof pagename);
102         safestrncpy(rev, bstr("rev"), sizeof rev);
103         display_wiki_page_backend(roomname, pagename, rev);
104         wDumpContent(1);
105 }
106
107
108 /*
109  * Display the revision history for a wiki page (template callback)
110  */
111 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
112 {
113         const StrBuf *roomname;
114         char pagename[128];
115         StrBuf *Buf;
116         int row = 0;
117
118         roomname = sbstr("room");
119         safestrncpy(pagename, bstr("page"), sizeof pagename);
120         str_wiki_index(pagename);
121
122         if (StrLength(roomname) > 0) {
123
124                 /* If we're not in the correct room, try going there. */
125                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
126                         gotoroom(roomname);
127                 }
128         
129                 /* If we're still not in the correct room, it doesn't exist. */
130                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
131                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
132                         return;
133                 }
134
135         }
136
137         serv_printf("WIKI history|%s", pagename);
138         Buf = NewStrBuf();
139         StrBuf_ServGetln(Buf);
140         if (GetServerStatus(Buf, NULL) == 1) {
141
142                 time_t rev_date;
143                 char rev_date_displayed[64];
144                 StrBuf *rev_uuid = NewStrBuf();
145                 StrBuf *author = NewStrBuf();
146                 StrBuf *node = NewStrBuf();
147
148                 wc_printf("<div class=\"fix_scrollbar_bug\">"
149                         "<table class=\"wiki_history_background\">"
150                 );
151
152                 wc_printf("<th>%s</th>", _("Date"));
153                 wc_printf("<th>%s</th>", _("Author"));
154
155                 while(StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
156
157                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
158                         rev_date = extract_long(ChrPtr(Buf), 1);
159                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
160                         StrBufExtract_token(author, Buf, 2, '|');
161                         StrBufExtract_token(node, Buf, 3, '|');
162
163                         wc_printf("<tr bgcolor=\"%s\">", (row ? "#FFFFFF" : "#DDDDDD"));
164                         row = 1 - row;
165                         wc_printf("<td>%s</td><td>", rev_date_displayed);
166
167                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
168                                 escputs(ChrPtr(author));
169                                 wc_printf(" @ ");
170                                 escputs(ChrPtr(node));
171                         }
172                         else {
173                                 wc_printf("<a href=\"showuser?who=");
174                                 urlescputs(ChrPtr(author));
175                                 wc_printf("\">");
176                                 escputs(ChrPtr(author));
177                                 wc_printf("</a>");
178                         }
179
180                         wc_printf("</td><td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
181                                 bstr("page"),
182                                 ChrPtr(rev_uuid),
183                                 _("(show)")
184                         );
185                         wc_printf("</td><td><a href=\"wiki_revert?page=%s?rev=%s\">%s</a></td>",
186                                 bstr("page"),
187                                 ChrPtr(rev_uuid),
188                                 _("(revert)")
189                         );
190                         wc_printf("</tr>\n");
191                 }
192
193                 wc_printf("</table>\n");
194                 FreeStrBuf(&author);
195                 FreeStrBuf(&node);
196                 FreeStrBuf(&rev_uuid);
197         }
198         else {
199                 wc_printf("%s", ChrPtr(Buf));
200         }
201
202         FreeStrBuf(&Buf);
203 }
204
205
206
207 /*
208  * Display the revision history for a wiki page
209  */
210 void display_wiki_history(void)
211 {
212         output_headers(1, 1, 1, 0, 0, 0);
213         do_template("wiki_history", NULL);
214         wDumpContent(1);
215 }
216
217
218 int wiki_Cleanup(void **ViewSpecific)
219 {
220         char pagename[5];
221         safestrncpy(pagename, "home", sizeof pagename);
222         display_wiki_page_backend(WC->wc_roomname, pagename, "");
223         wDumpContent(1);
224         return 0;
225 }
226
227 void 
228 InitModule_WIKI
229 (void)
230 {
231         RegisterReadLoopHandlerset(
232                 VIEW_WIKI,
233                 NULL,
234                 NULL,
235                 NULL,
236                 NULL,
237                 wiki_Cleanup
238         );
239
240         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
241         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
242         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
243 }