2 * Functions pertaining to rooms with a wiki view
4 * Copyright (c) 2009-2018 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)
40 if ((WCC->CurRoom.view != VIEW_WIKI) &&
41 (WCC->CurRoom.view != VIEW_WIKIMD)) {
42 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WCC->CurRoom.name) );
46 if (StrLength(pagename) == 0) {
47 StrBufPlain(pagename, HKEY("home"));
50 str_wiki_index(pagename); /* convert index name to lowercase and numeric only */
52 if ((rev != NULL) && (strlen(rev) > 0)) {
53 /* read an older revision */
54 serv_printf("WIKI rev|%s|%s|%s", ChrPtr(pagename), rev, (do_revert ? "revert" : "fetch") );
55 serv_getln(buf, sizeof buf);
57 msgnum = extract_long(&buf[4], 0);
61 /* read the current revision */
62 msgnum = locate_message_by_uid(ChrPtr(pagename));
66 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
69 putbstr("pagename", pagename);
70 do_template("wiki_empty");
75 * Display a specific page from a wiki room
77 void display_wiki_page(void)
83 output_headers(1, 1, 1, 0, 0, 0);
84 pagename = NewStrBufDup(sbstr("page"));
85 str_wiki_index(pagename);
86 safestrncpy(rev, bstr("rev"), sizeof rev);
87 do_revert = atoi(bstr("revert"));
88 display_wiki_page_backend(pagename, rev, do_revert);
94 * Display the revision history for a wiki page (template callback)
96 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
102 pagename = NewStrBufDup(sbstr("page"));
103 str_wiki_index(pagename);
105 serv_printf("WIKI history|%s", ChrPtr(pagename));
107 StrBuf_ServGetln(Buf);
108 if (GetServerStatus(Buf, NULL) == 1) {
111 char rev_date_displayed[64];
112 StrBuf *rev_uuid = NewStrBuf();
113 StrBuf *author = NewStrBuf();
114 StrBuf *node = NewStrBuf();
116 wc_printf("<table class=\"wiki_history_background\">");
118 wc_printf("<th>%s</th>", _("Date"));
119 wc_printf("<th>%s</th>", _("Author"));
121 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
123 rev_date = extract_long(ChrPtr(Buf), 1);
124 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
125 StrBufExtract_token(author, Buf, 2, '|');
127 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
128 wc_printf("<td>%s</td><td>", rev_date_displayed);
129 wc_printf("<a href=\"showuser?who=");
130 urlescputs(ChrPtr(author));
132 escputs(ChrPtr(author));
133 wc_printf("</a></td>");
136 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
137 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
138 wc_printf("\">%s</a></td>", _("(show)"));
139 wc_printf("<td>(%s)</td>", _("Current version"));
143 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
147 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
148 wc_printf("\">%s</a></td>", _("(show)"));
149 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
155 wc_printf("</tr>\n");
157 /* Extract all fields except the author and date after displaying the row. This
158 * is deliberate, because the timestamp reflects when the diff was written, not
159 * when the version which it reflects was written. Similarly, the name associated
160 * with each diff is the author who created the newer version of the page that
161 * made the diff happen.
163 StrBufExtract_token(rev_uuid, Buf, 0, '|');
164 StrBufExtract_token(node, Buf, 3, '|');
168 wc_printf("</table>\n");
171 FreeStrBuf(&rev_uuid);
174 wc_printf("%s", ChrPtr(Buf));
183 * Display the revision history for a wiki page
185 void display_wiki_history(void)
187 output_headers(1, 1, 1, 0, 0, 0);
188 do_template("wiki_history");
194 * Display a list of all pages in a Wiki room (template callback)
196 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
201 if (!IsEmptyStr(bstr("query"))) {
202 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
205 serv_printf("MSGS ALL|||4"); /* full list */
209 StrBuf_ServGetln(Buf);
210 if (GetServerStatus(Buf, NULL) == 1) {
211 StrBuf *pagetitle = NewStrBuf();
213 wc_printf("<table class=\"wiki_pagelist_background\">");
214 wc_printf("<th>%s</th>", _("Page title"));
216 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
217 StrBufExtract_token(pagetitle, Buf, 1, '|');
219 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
220 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
221 wc_printf("<td><a href=\"wiki?page=");
222 urlescputs(ChrPtr(pagetitle));
224 escputs(ChrPtr(pagetitle));
225 wc_printf("</a></td>");
226 wc_printf("</tr>\n");
230 wc_printf("</table>\n");
231 FreeStrBuf(&pagetitle);
239 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
241 void display_wiki_pagelist(void)
243 output_headers(1, 1, 1, 0, 0, 0);
244 do_template("wiki_pagelist");
249 int wiki_Cleanup(void **ViewSpecific)
252 pagename = NewStrBufDup(sbstr("page"));
253 display_wiki_page_backend(pagename, "", 0);
259 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
266 GetTemplateTokenString(Target, TP, 2, &pch, &len);
267 return strcasecmp(page, pch) == 0;
271 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
277 GetTemplateTokenString(Target, TP, 2, &pch, &len);
278 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
282 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
284 /* this function was intentionaly left empty. */
288 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
296 if (oper == do_search)
297 display_wiki_pagelist();
299 http_redirect("wiki?page=home");
309 RegisterReadLoopHandlerset(
311 wiki_GetParamsGetServerCall,
312 wiki_PrintHeaderPage,
321 RegisterReadLoopHandlerset(
323 wiki_GetParamsGetServerCall,
324 wiki_PrintHeaderPage,
333 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
334 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
335 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
336 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
337 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
338 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
339 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);