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