2 * Functions pertaining to rooms with a wiki view
4 * Copyright (c) 2009-2021 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(StrBuf *s)
23 StrBufSanitizeAscii(s, '_');
28 * Display a specific page from a wiki room
30 * "rev" may be set to an empty string to display the current version.
31 * "do_revert" may be set to nonzero to perform a reversion to the specified version.
33 void display_wiki_page_backend(StrBuf *pagename, char *rev, int do_revert)
39 if (WC->CurRoom.view != VIEW_WIKI) {
40 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WC->CurRoom.name) );
44 if (StrLength(pagename) == 0) {
45 StrBufPlain(pagename, HKEY("home"));
48 str_wiki_index(pagename); /* convert index name to lowercase and numeric only */
50 if ((rev != NULL) && (strlen(rev) > 0)) {
51 /* read an older revision */
52 serv_printf("WIKI rev|%s|%s|%s", ChrPtr(pagename), rev, (do_revert ? "revert" : "fetch") );
53 serv_getln(buf, sizeof buf);
55 msgnum = extract_long(&buf[4], 0);
59 /* read the current revision */
60 msgnum = locate_message_by_uid(ChrPtr(pagename));
64 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
67 putbstr("pagename", pagename);
68 do_template("wiki_empty");
73 * Display a specific page from a wiki room
75 void display_wiki_page(void)
81 output_headers(1, 1, 1, 0, 0, 0);
82 pagename = NewStrBufDup(sbstr("page"));
83 str_wiki_index(pagename);
84 safestrncpy(rev, bstr("rev"), sizeof rev);
85 do_revert = atoi(bstr("revert"));
86 display_wiki_page_backend(pagename, rev, do_revert);
92 * Display the revision history for a wiki page (template callback)
94 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
100 pagename = NewStrBufDup(sbstr("page"));
101 str_wiki_index(pagename);
103 serv_printf("WIKI history|%s", ChrPtr(pagename));
105 StrBuf_ServGetln(Buf);
106 if (GetServerStatus(Buf, NULL) == 1) {
109 char rev_date_displayed[64];
110 StrBuf *rev_uuid = NewStrBuf();
111 StrBuf *author = NewStrBuf();
112 StrBuf *node = NewStrBuf();
114 wc_printf("<table class=\"wiki_history_background\">");
116 wc_printf("<th>%s</th>", _("Date"));
117 wc_printf("<th>%s</th>", _("Author"));
119 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
121 rev_date = extract_long(ChrPtr(Buf), 1);
122 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
123 StrBufExtract_token(author, Buf, 2, '|');
125 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
126 wc_printf("<td>%s</td><td>", rev_date_displayed);
127 wc_printf("<a href=\"showuser?who=");
128 urlescputs(ChrPtr(author));
130 escputs(ChrPtr(author));
131 wc_printf("</a></td>");
134 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
135 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
136 wc_printf("\">%s</a></td>", _("(show)"));
137 wc_printf("<td>(%s)</td>", _("Current version"));
141 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
145 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
146 wc_printf("\">%s</a></td>", _("(show)"));
147 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
153 wc_printf("</tr>\n");
155 /* Extract all fields except the author and date after displaying the row. This
156 * is deliberate, because the timestamp reflects when the diff was written, not
157 * when the version which it reflects was written. Similarly, the name associated
158 * with each diff is the author who created the newer version of the page that
159 * made the diff happen.
161 StrBufExtract_token(rev_uuid, Buf, 0, '|');
162 StrBufExtract_token(node, Buf, 3, '|');
166 wc_printf("</table>\n");
169 FreeStrBuf(&rev_uuid);
172 wc_printf("%s", ChrPtr(Buf));
181 * Display the revision history for a wiki page
183 void display_wiki_history(void)
185 output_headers(1, 1, 1, 0, 0, 0);
186 do_template("wiki_history");
192 * Display a list of all pages in a Wiki room (template callback)
194 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
199 if (!IsEmptyStr(bstr("query"))) {
200 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
203 serv_printf("MSGS ALL|||4"); /* full list */
207 StrBuf_ServGetln(Buf);
208 if (GetServerStatus(Buf, NULL) == 1) {
209 StrBuf *pagetitle = NewStrBuf();
211 wc_printf("<table class=\"wiki_pagelist_background\">");
212 wc_printf("<th>%s</th>", _("Page title"));
214 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
215 StrBufExtract_token(pagetitle, Buf, 1, '|');
217 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
218 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
219 wc_printf("<td><a href=\"wiki?page=");
220 urlescputs(ChrPtr(pagetitle));
222 escputs(ChrPtr(pagetitle));
223 wc_printf("</a></td>");
224 wc_printf("</tr>\n");
228 wc_printf("</table>\n");
229 FreeStrBuf(&pagetitle);
237 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
239 void display_wiki_pagelist(void)
241 output_headers(1, 1, 1, 0, 0, 0);
242 do_template("wiki_pagelist");
247 int wiki_Cleanup(void **ViewSpecific)
250 pagename = NewStrBufDup(sbstr("page"));
251 display_wiki_page_backend(pagename, "", 0);
257 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
264 GetTemplateTokenString(Target, TP, 2, &pch, &len);
265 return strcasecmp(page, pch) == 0;
269 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
274 GetTemplateTokenString(Target, TP, 2, &pch, &len);
275 return bmstrcasestr((char *)ChrPtr(WC->Hdr->HR.ReqLine), pch) != NULL;
279 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
281 /* this function was intentionaly left empty. */
285 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
293 if (oper == do_search)
294 display_wiki_pagelist();
296 http_redirect("wiki?page=home");
306 RegisterReadLoopHandlerset(
308 wiki_GetParamsGetServerCall,
309 wiki_PrintHeaderPage,
318 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
319 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
320 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
321 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
322 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
323 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
324 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);