2 * Functions pertaining to rooms with a wiki view
4 * Copyright (c) 2009-2012 by the citadel.org team
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.
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.
19 * Convert a string to something suitable as a wiki index
21 void str_wiki_index(char *s)
25 if (s == NULL) return;
27 /* First remove all non-alphanumeric characters */
28 for (i=0; i<strlen(s); ++i) {
30 strcpy(&s[i], &s[i+1]);
34 /* Then make everything lower case */
35 for (i=0; i<strlen(s); ++i) {
41 * Display a specific page from a wiki room
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.
46 void display_wiki_page_backend(const StrBuf *roomname, char *pagename, char *rev, int do_revert)
52 str_wiki_index(pagename);
54 if (StrLength(roomname) > 0) {
56 /* If we're not in the correct room, try going there. */
57 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
61 /* If we're still not in the correct room, it doesn't exist. */
62 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
63 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
69 if (WC->CurRoom.view != VIEW_WIKI) {
70 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
74 if (IsEmptyStr(pagename)) {
75 strcpy(pagename, "home");
78 /* Found it! Now read it. */
80 if ((rev != NULL) && (strlen(rev) > 0)) {
81 /* read an older revision */
82 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
83 serv_getln(buf, sizeof buf);
85 msgnum = extract_long(&buf[4], 0);
89 /* read the current revision? */
90 msgnum = locate_message_by_uid(pagename);
94 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
99 "<div align=\"center\">"
100 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
101 "<tr><td align=\"center\">"
103 wc_printf("<br><b>");
104 wc_printf(_("There is no page called '%s' here."), pagename);
105 wc_printf("</b><br><br>");
106 wc_printf(_("Select the 'Edit this page' link in the room banner "
107 "if you would like to create this page."));
108 wc_printf("<br><br>");
109 wc_printf("</td></tr></table></div>\n");
114 * Display a specific page from a wiki room
116 void display_wiki_page(void)
118 const StrBuf *roomname;
123 output_headers(1, 1, 1, 0, 0, 0);
124 roomname = sbstr("room");
125 safestrncpy(pagename, bstr("page"), sizeof pagename);
126 str_wiki_index(pagename);
127 safestrncpy(rev, bstr("rev"), sizeof rev);
128 do_revert = atoi(bstr("revert"));
129 display_wiki_page_backend(roomname, pagename, rev, do_revert);
135 * Display the revision history for a wiki page (template callback)
137 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
139 const StrBuf *roomname;
144 roomname = sbstr("room");
145 safestrncpy(pagename, bstr("page"), sizeof pagename);
146 str_wiki_index(pagename);
148 if (StrLength(roomname) > 0) {
150 /* If we're not in the correct room, try going there. */
151 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
155 /* If we're still not in the correct room, it doesn't exist. */
156 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
157 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
163 serv_printf("WIKI history|%s", pagename);
165 StrBuf_ServGetln(Buf);
166 if (GetServerStatus(Buf, NULL) == 1) {
169 char rev_date_displayed[64];
170 StrBuf *rev_uuid = NewStrBuf();
171 StrBuf *author = NewStrBuf();
172 StrBuf *node = NewStrBuf();
174 wc_printf("<table class=\"wiki_history_background\">");
176 wc_printf("<th>%s</th>", _("Date"));
177 wc_printf("<th>%s</th>", _("Author"));
179 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
181 rev_date = extract_long(ChrPtr(Buf), 1);
182 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
183 StrBufExtract_token(author, Buf, 2, '|');
185 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
186 wc_printf("<td>%s</td><td>", rev_date_displayed);
187 if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
188 escputs(ChrPtr(author));
190 escputs(ChrPtr(node));
193 wc_printf("<a href=\"showuser?who=");
194 urlescputs(ChrPtr(author));
196 escputs(ChrPtr(author));
202 wc_printf("<td><a href=\"wiki?page=%s\">%s</a></td>",
206 wc_printf("<td>(%s)</td>", _("Current version"));
210 wc_printf("<td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
215 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
221 wc_printf("</tr>\n");
223 /* Extract all fields except the author and date after displaying the row. This
224 * is deliberate, because the timestamp reflects when the diff was written, not
225 * when the version which it reflects was written. Similarly, the name associated
226 * with each diff is the author who created the newer version of the page that
227 * made the diff happen.
229 StrBufExtract_token(rev_uuid, Buf, 0, '|');
230 StrBufExtract_token(node, Buf, 3, '|');
234 wc_printf("</table>\n");
237 FreeStrBuf(&rev_uuid);
240 wc_printf("%s", ChrPtr(Buf));
249 * Display the revision history for a wiki page
251 void display_wiki_history(void)
253 output_headers(1, 1, 1, 0, 0, 0);
254 do_template("wiki_history");
260 * Display a list of all pages in a Wiki room (template callback)
262 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
264 const StrBuf *roomname;
268 roomname = sbstr("room");
269 if (StrLength(roomname) > 0) {
270 /* If we're not in the correct room, try going there. */
271 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
275 /* If we're still not in the correct room, it doesn't exist. */
276 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
277 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
282 if (!IsEmptyStr(bstr("query"))) {
283 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
286 serv_printf("MSGS ALL|||4"); /* full list */
290 StrBuf_ServGetln(Buf);
291 if (GetServerStatus(Buf, NULL) == 1) {
292 StrBuf *pagetitle = NewStrBuf();
294 wc_printf("<table class=\"wiki_pagelist_background\">");
295 wc_printf("<th>%s</th>", _("Page title"));
297 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
298 StrBufExtract_token(pagetitle, Buf, 1, '|');
300 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
301 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
302 wc_printf("<td><a href=\"wiki?page=");
303 urlescputs(ChrPtr(pagetitle));
305 escputs(ChrPtr(pagetitle));
306 wc_printf("</a></td>");
307 wc_printf("</tr>\n");
311 wc_printf("</table>\n");
312 FreeStrBuf(&pagetitle);
320 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
322 void display_wiki_pagelist(void)
324 output_headers(1, 1, 1, 0, 0, 0);
325 do_template("wiki_pagelist");
330 int wiki_Cleanup(void **ViewSpecific)
333 safestrncpy(pagename, "home", sizeof pagename);
334 display_wiki_page_backend(WC->CurRoom.name, pagename, "", 0);
340 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
347 GetTemplateTokenString(Target, TP, 2, &pch, &len);
348 return strcasecmp(page, pch) == 0;
352 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
358 GetTemplateTokenString(Target, TP, 2, &pch, &len);
359 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
363 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
365 /* this function was intentionaly left empty. */
369 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
377 if (oper == do_search)
378 display_wiki_pagelist();
380 http_redirect("wiki?page=home");
390 RegisterReadLoopHandlerset(
392 wiki_GetParamsGetServerCall,
393 wiki_PrintHeaderPage,
401 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
402 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
403 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
404 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
405 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
406 RegisterConditional(HKEY("COND:WIKI:PAGE"), 1, ConditionalHaveWikiPage, CTX_NONE);
407 RegisterConditional(HKEY("COND:WIKI:TYPE"), 1, ConditionalHavewikiType, CTX_NONE);