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(char *pagename, char *rev, int do_revert)
53 if ((WCC->CurRoom.view != VIEW_WIKI) &&
54 (WCC->CurRoom.view != VIEW_WIKIMD)) {
55 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WCC->CurRoom.name) );
59 if (IsEmptyStr(pagename)) {
60 strcpy(pagename, "home");
63 str_wiki_index(pagename); /* convert index name to lowercase and numeric only */
65 if ((rev != NULL) && (strlen(rev) > 0)) {
66 /* read an older revision */
67 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
68 serv_getln(buf, sizeof buf);
70 msgnum = extract_long(&buf[4], 0);
74 /* read the current revision */
75 msgnum = locate_message_by_uid(pagename);
79 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
84 "<div align=\"center\">"
85 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
86 "<tr><td align=\"center\">"
89 wc_printf(_("There is no page called '%s' here."), pagename);
90 wc_printf("</b><br><br>");
91 wc_printf(_("Select the 'Edit this page' link in the room banner "
92 "if you would like to create this page."));
93 wc_printf("<br><br>");
94 wc_printf("</td></tr></table></div>\n");
99 * Display a specific page from a wiki room
101 void display_wiki_page(void)
107 output_headers(1, 1, 1, 0, 0, 0);
108 safestrncpy(pagename, bstr("page"), sizeof pagename);
109 str_wiki_index(pagename);
110 safestrncpy(rev, bstr("rev"), sizeof rev);
111 do_revert = atoi(bstr("revert"));
112 display_wiki_page_backend(pagename, rev, do_revert);
118 * Display the revision history for a wiki page (template callback)
120 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
126 safestrncpy(pagename, bstr("page"), sizeof pagename);
127 str_wiki_index(pagename);
129 serv_printf("WIKI history|%s", pagename);
131 StrBuf_ServGetln(Buf);
132 if (GetServerStatus(Buf, NULL) == 1) {
135 char rev_date_displayed[64];
136 StrBuf *rev_uuid = NewStrBuf();
137 StrBuf *author = NewStrBuf();
138 StrBuf *node = NewStrBuf();
140 wc_printf("<table class=\"wiki_history_background\">");
142 wc_printf("<th>%s</th>", _("Date"));
143 wc_printf("<th>%s</th>", _("Author"));
145 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
147 rev_date = extract_long(ChrPtr(Buf), 1);
148 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
149 StrBufExtract_token(author, Buf, 2, '|');
151 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
152 wc_printf("<td>%s</td><td>", rev_date_displayed);
153 if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
154 escputs(ChrPtr(author));
156 escputs(ChrPtr(node));
159 wc_printf("<a href=\"showuser?who=");
160 urlescputs(ChrPtr(author));
162 escputs(ChrPtr(author));
168 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
169 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
170 wc_printf("\">%s</a></td>", _("(show)"));
171 wc_printf("<td>(%s)</td>", _("Current version"));
175 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
179 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
180 wc_printf("\">%s</a></td>", _("(show)"));
181 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
187 wc_printf("</tr>\n");
189 /* Extract all fields except the author and date after displaying the row. This
190 * is deliberate, because the timestamp reflects when the diff was written, not
191 * when the version which it reflects was written. Similarly, the name associated
192 * with each diff is the author who created the newer version of the page that
193 * made the diff happen.
195 StrBufExtract_token(rev_uuid, Buf, 0, '|');
196 StrBufExtract_token(node, Buf, 3, '|');
200 wc_printf("</table>\n");
203 FreeStrBuf(&rev_uuid);
206 wc_printf("%s", ChrPtr(Buf));
215 * Display the revision history for a wiki page
217 void display_wiki_history(void)
219 output_headers(1, 1, 1, 0, 0, 0);
220 do_template("wiki_history");
226 * Display a list of all pages in a Wiki room (template callback)
228 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
233 if (!IsEmptyStr(bstr("query"))) {
234 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
237 serv_printf("MSGS ALL|||4"); /* full list */
241 StrBuf_ServGetln(Buf);
242 if (GetServerStatus(Buf, NULL) == 1) {
243 StrBuf *pagetitle = NewStrBuf();
245 wc_printf("<table class=\"wiki_pagelist_background\">");
246 wc_printf("<th>%s</th>", _("Page title"));
248 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
249 StrBufExtract_token(pagetitle, Buf, 1, '|');
251 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
252 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
253 wc_printf("<td><a href=\"wiki?page=");
254 urlescputs(ChrPtr(pagetitle));
256 escputs(ChrPtr(pagetitle));
257 wc_printf("</a></td>");
258 wc_printf("</tr>\n");
262 wc_printf("</table>\n");
263 FreeStrBuf(&pagetitle);
271 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
273 void display_wiki_pagelist(void)
275 output_headers(1, 1, 1, 0, 0, 0);
276 do_template("wiki_pagelist");
281 int wiki_Cleanup(void **ViewSpecific)
284 safestrncpy(pagename, "home", sizeof pagename);
285 display_wiki_page_backend(pagename, "", 0);
291 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
298 GetTemplateTokenString(Target, TP, 2, &pch, &len);
299 return strcasecmp(page, pch) == 0;
303 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
309 GetTemplateTokenString(Target, TP, 2, &pch, &len);
310 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
314 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
316 /* this function was intentionaly left empty. */
320 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
328 if (oper == do_search)
329 display_wiki_pagelist();
331 http_redirect("wiki?page=home");
341 RegisterReadLoopHandlerset(
343 wiki_GetParamsGetServerCall,
344 wiki_PrintHeaderPage,
352 RegisterReadLoopHandlerset(
354 wiki_GetParamsGetServerCall,
355 wiki_PrintHeaderPage,
363 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
364 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
365 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
366 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
367 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
368 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
369 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);