* Completed the WebCit UI functions to view a historical version of a wiki page.
[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                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
172                         rev_date = extract_long(ChrPtr(Buf), 1);
173                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
174                         StrBufExtract_token(author, Buf, 2, '|');
175                         StrBufExtract_token(node, Buf, 3, '|');
176
177                         wc_printf("<tr bgcolor=\"%s\">", (row ? "#FFFFFF" : "#DDDDDD"));
178                         row = 1 - row;
179                         wc_printf("<td>%s</td><td>", rev_date_displayed);
180
181                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
182                                 escputs(ChrPtr(author));
183                                 wc_printf(" @ ");
184                                 escputs(ChrPtr(node));
185                         }
186                         else {
187                                 wc_printf("<a href=\"showuser?who=");
188                                 urlescputs(ChrPtr(author));
189                                 wc_printf("\">");
190                                 escputs(ChrPtr(author));
191                                 wc_printf("</a>");
192                         }
193
194                         wc_printf("</td><td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
195                                 bstr("page"),
196                                 ChrPtr(rev_uuid),
197                                 _("(show)")
198                         );
199                         wc_printf("</td><td><a href=\"wiki_revert?page=%s?rev=%s\">%s</a></td>",
200                                 bstr("page"),
201                                 ChrPtr(rev_uuid),
202                                 _("(revert)")
203                         );
204                         wc_printf("</tr>\n");
205                 }
206
207                 wc_printf("</table>\n");
208                 FreeStrBuf(&author);
209                 FreeStrBuf(&node);
210                 FreeStrBuf(&rev_uuid);
211         }
212         else {
213                 wc_printf("%s", ChrPtr(Buf));
214         }
215
216         FreeStrBuf(&Buf);
217 }
218
219
220
221 /*
222  * Display the revision history for a wiki page
223  */
224 void display_wiki_history(void)
225 {
226         output_headers(1, 1, 1, 0, 0, 0);
227         do_template("wiki_history", NULL);
228         wDumpContent(1);
229 }
230
231
232 int wiki_Cleanup(void **ViewSpecific)
233 {
234         char pagename[5];
235         safestrncpy(pagename, "home", sizeof pagename);
236         display_wiki_page_backend(WC->wc_roomname, pagename, "");
237         wDumpContent(1);
238         return 0;
239 }
240
241 void 
242 InitModule_WIKI
243 (void)
244 {
245         RegisterReadLoopHandlerset(
246                 VIEW_WIKI,
247                 NULL,
248                 NULL,
249                 NULL,
250                 NULL,
251                 wiki_Cleanup
252         );
253
254         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
255         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
256         /* WebcitAddUrlHandler(HKEY("wiki_revert"), "", 0, wiki_revert, 0); FIXME implement this */
257         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
258 }