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->CurRoom.view != VIEW_WIKIMD)) {
41 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WC->CurRoom.name) );
45 if (StrLength(pagename) == 0) {
46 StrBufPlain(pagename, HKEY("home"));
49 str_wiki_index(pagename); /* convert index name to lowercase and numeric only */
51 if ((rev != NULL) && (strlen(rev) > 0)) {
52 /* read an older revision */
53 serv_printf("WIKI rev|%s|%s|%s", ChrPtr(pagename), rev, (do_revert ? "revert" : "fetch") );
54 serv_getln(buf, sizeof buf);
56 msgnum = extract_long(&buf[4], 0);
60 /* read the current revision */
61 msgnum = locate_message_by_uid(ChrPtr(pagename));
65 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
68 putbstr("pagename", pagename);
69 do_template("wiki_empty");
74 * Display a specific page from a wiki room
76 void display_wiki_page(void)
82 output_headers(1, 1, 1, 0, 0, 0);
83 pagename = NewStrBufDup(sbstr("page"));
84 str_wiki_index(pagename);
85 safestrncpy(rev, bstr("rev"), sizeof rev);
86 do_revert = atoi(bstr("revert"));
87 display_wiki_page_backend(pagename, rev, do_revert);
93 * Display the revision history for a wiki page (template callback)
95 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
101 pagename = NewStrBufDup(sbstr("page"));
102 str_wiki_index(pagename);
104 serv_printf("WIKI history|%s", ChrPtr(pagename));
106 StrBuf_ServGetln(Buf);
107 if (GetServerStatus(Buf, NULL) == 1) {
110 char rev_date_displayed[64];
111 StrBuf *rev_uuid = NewStrBuf();
112 StrBuf *author = NewStrBuf();
113 StrBuf *node = NewStrBuf();
115 wc_printf("<table class=\"wiki_history_background\">");
117 wc_printf("<th>%s</th>", _("Date"));
118 wc_printf("<th>%s</th>", _("Author"));
120 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
122 rev_date = extract_long(ChrPtr(Buf), 1);
123 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
124 StrBufExtract_token(author, Buf, 2, '|');
126 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
127 wc_printf("<td>%s</td><td>", rev_date_displayed);
128 wc_printf("<a href=\"showuser?who=");
129 urlescputs(ChrPtr(author));
131 escputs(ChrPtr(author));
132 wc_printf("</a></td>");
135 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
136 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
137 wc_printf("\">%s</a></td>", _("(show)"));
138 wc_printf("<td>(%s)</td>", _("Current version"));
142 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
146 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
147 wc_printf("\">%s</a></td>", _("(show)"));
148 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
154 wc_printf("</tr>\n");
156 /* Extract all fields except the author and date after displaying the row. This
157 * is deliberate, because the timestamp reflects when the diff was written, not
158 * when the version which it reflects was written. Similarly, the name associated
159 * with each diff is the author who created the newer version of the page that
160 * made the diff happen.
162 StrBufExtract_token(rev_uuid, Buf, 0, '|');
163 StrBufExtract_token(node, Buf, 3, '|');
167 wc_printf("</table>\n");
170 FreeStrBuf(&rev_uuid);
173 wc_printf("%s", ChrPtr(Buf));
182 * Display the revision history for a wiki page
184 void display_wiki_history(void)
186 output_headers(1, 1, 1, 0, 0, 0);
187 do_template("wiki_history");
193 * Display a list of all pages in a Wiki room (template callback)
195 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
200 if (!IsEmptyStr(bstr("query"))) {
201 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
204 serv_printf("MSGS ALL|||4"); /* full list */
208 StrBuf_ServGetln(Buf);
209 if (GetServerStatus(Buf, NULL) == 1) {
210 StrBuf *pagetitle = NewStrBuf();
212 wc_printf("<table class=\"wiki_pagelist_background\">");
213 wc_printf("<th>%s</th>", _("Page title"));
215 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
216 StrBufExtract_token(pagetitle, Buf, 1, '|');
218 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
219 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
220 wc_printf("<td><a href=\"wiki?page=");
221 urlescputs(ChrPtr(pagetitle));
223 escputs(ChrPtr(pagetitle));
224 wc_printf("</a></td>");
225 wc_printf("</tr>\n");
229 wc_printf("</table>\n");
230 FreeStrBuf(&pagetitle);
238 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
240 void display_wiki_pagelist(void)
242 output_headers(1, 1, 1, 0, 0, 0);
243 do_template("wiki_pagelist");
248 int wiki_Cleanup(void **ViewSpecific)
251 pagename = NewStrBufDup(sbstr("page"));
252 display_wiki_page_backend(pagename, "", 0);
258 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
265 GetTemplateTokenString(Target, TP, 2, &pch, &len);
266 return strcasecmp(page, pch) == 0;
270 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
275 GetTemplateTokenString(Target, TP, 2, &pch, &len);
276 return bmstrcasestr((char *)ChrPtr(WC->Hdr->HR.ReqLine), pch) != NULL;
280 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
282 /* this function was intentionaly left empty. */
286 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
294 if (oper == do_search)
295 display_wiki_pagelist();
297 http_redirect("wiki?page=home");
307 RegisterReadLoopHandlerset(
309 wiki_GetParamsGetServerCall,
310 wiki_PrintHeaderPage,
319 RegisterReadLoopHandlerset(
321 wiki_GetParamsGetServerCall,
322 wiki_PrintHeaderPage,
331 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
332 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
333 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
334 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
335 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
336 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
337 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);