* Noticed something that was technically correct but confusing: when viewing a wiki...
[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         char buf[256];
42
43         str_wiki_index(pagename);
44
45         if (StrLength(roomname) > 0) {
46
47                 /* If we're not in the correct room, try going there. */
48                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
49                         gotoroom(roomname);
50                 }
51         
52                 /* If we're still not in the correct room, it doesn't exist. */
53                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
54                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
55                         return;
56                 }
57
58         }
59
60         if (WC->wc_view != VIEW_WIKI) {
61                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
62                 return;
63         }
64
65         if (IsEmptyStr(pagename)) {
66                 strcpy(pagename, "home");
67         }
68
69         /* Found it!  Now read it. */
70
71         if ((rev != NULL) && (strlen(rev) > 0)) {
72                 /* read an older revision */
73                 serv_printf("WIKI rev|%s|%s|fetch", pagename, rev);
74                 serv_getln(buf, sizeof buf);
75                 if (buf[0] == '2') {
76                         msgnum = extract_long(&buf[4], 0);
77                 }
78         }
79         else {
80                 /* read the current revision? */
81                 msgnum = locate_message_by_uid(pagename);
82         }
83
84         if (msgnum >= 0L) {
85                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
86                 return;
87         }
88
89         wc_printf("<br /><br />"
90                 "<div align=\"center\">"
91                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
92                 "<tr><td align=\"center\">"
93         );
94         wc_printf("<br><b>");
95         wc_printf(_("There is no page called '%s' here."), pagename);
96         wc_printf("</b><br><br>");
97         wc_printf(_("Select the 'Edit this page' link in the room banner "
98                 "if you would like to create this page."));
99         wc_printf("<br><br>");
100         wc_printf("</td></tr></table></div>\n");
101 }
102
103
104 /*
105  * Display a specific page from a wiki room
106  */
107 void display_wiki_page(void)
108 {
109         const StrBuf *roomname;
110         char pagename[128];
111         char rev[128];
112
113         output_headers(1, 1, 1, 0, 0, 0);
114         roomname = sbstr("room");
115         safestrncpy(pagename, bstr("page"), sizeof pagename);
116         safestrncpy(rev, bstr("rev"), sizeof rev);
117         display_wiki_page_backend(roomname, pagename, rev);
118         wDumpContent(1);
119 }
120
121
122 /*
123  * Display the revision history for a wiki page (template callback)
124  */
125 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
126 {
127         const StrBuf *roomname;
128         char pagename[128];
129         StrBuf *Buf;
130         int row = 0;
131
132         roomname = sbstr("room");
133         safestrncpy(pagename, bstr("page"), sizeof pagename);
134         str_wiki_index(pagename);
135
136         if (StrLength(roomname) > 0) {
137
138                 /* If we're not in the correct room, try going there. */
139                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
140                         gotoroom(roomname);
141                 }
142         
143                 /* If we're still not in the correct room, it doesn't exist. */
144                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
145                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
146                         return;
147                 }
148
149         }
150
151         serv_printf("WIKI history|%s", pagename);
152         Buf = NewStrBuf();
153         StrBuf_ServGetln(Buf);
154         if (GetServerStatus(Buf, NULL) == 1) {
155
156                 time_t rev_date;
157                 char rev_date_displayed[64];
158                 StrBuf *rev_uuid = NewStrBuf();
159                 StrBuf *author = NewStrBuf();
160                 StrBuf *node = NewStrBuf();
161
162                 wc_printf("<div class=\"fix_scrollbar_bug\">"
163                         "<table class=\"wiki_history_background\">"
164                 );
165
166                 wc_printf("<th>%s</th>", _("Date"));
167                 wc_printf("<th>%s</th>", _("Author"));
168
169                 while(StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
170
171                         rev_date = extract_long(ChrPtr(Buf), 1);
172                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
173                         StrBufExtract_token(author, Buf, 2, '|');
174
175                         wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
176                         wc_printf("<td>%s</td><td>", rev_date_displayed);
177                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
178                                 escputs(ChrPtr(author));
179                                 wc_printf(" @ ");
180                                 escputs(ChrPtr(node));
181                         }
182                         else {
183                                 wc_printf("<a href=\"showuser?who=");
184                                 urlescputs(ChrPtr(author));
185                                 wc_printf("\">");
186                                 escputs(ChrPtr(author));
187                                 wc_printf("</a>");
188                         }
189                         wc_printf("</td>");
190
191                         if (row == 0) {
192                                 wc_printf("<td><a href=\"wiki?page=%s\">%s</a></td>",
193                                         bstr("page"),
194                                         _("(show)")
195                                 );
196                                 wc_printf("<td>(%s)</td>", _("Current version"));
197                         }
198
199                         else {
200                                 wc_printf("<td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
201                                         bstr("page"),
202                                         ChrPtr(rev_uuid),
203                                         _("(show)")
204                                 );
205                                 wc_printf("</td><td><a href=\"wiki_revert?page=%s?rev=%s\">%s</a></td>",
206                                         bstr("page"),
207                                         ChrPtr(rev_uuid),
208                                         _("(revert)")
209                                 );
210                         }
211                         wc_printf("</tr>\n");
212
213                         /* Extract all fields except the author and date after displaying the row.  This
214                          * is deliberate, because the timestamp reflects when the diff was written, not
215                          * when the version which it reflects was written.  Similarly, the name associated
216                          * with each diff is the author who created the newer version of the page that
217                          * made the diff happen.
218                          */
219                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
220                         StrBufExtract_token(node, Buf, 3, '|');
221                         ++row;
222                 }
223
224                 wc_printf("</table>\n");
225                 FreeStrBuf(&author);
226                 FreeStrBuf(&node);
227                 FreeStrBuf(&rev_uuid);
228         }
229         else {
230                 wc_printf("%s", ChrPtr(Buf));
231         }
232
233         FreeStrBuf(&Buf);
234 }
235
236
237
238 /*
239  * Display the revision history for a wiki page
240  */
241 void display_wiki_history(void)
242 {
243         output_headers(1, 1, 1, 0, 0, 0);
244         do_template("wiki_history", NULL);
245         wDumpContent(1);
246 }
247
248
249 int wiki_Cleanup(void **ViewSpecific)
250 {
251         char pagename[5];
252         safestrncpy(pagename, "home", sizeof pagename);
253         display_wiki_page_backend(WC->wc_roomname, pagename, "");
254         wDumpContent(1);
255         return 0;
256 }
257
258 void 
259 InitModule_WIKI
260 (void)
261 {
262         RegisterReadLoopHandlerset(
263                 VIEW_WIKI,
264                 NULL,
265                 NULL,
266                 NULL,
267                 NULL,
268                 wiki_Cleanup
269         );
270
271         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
272         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
273         /* WebcitAddUrlHandler(HKEY("wiki_revert"), "", 0, wiki_revert, 0); FIXME implement this */
274         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
275 }