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(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 if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
130 escputs(ChrPtr(author));
132 escputs(ChrPtr(node));
135 wc_printf("<a href=\"showuser?who=");
136 urlescputs(ChrPtr(author));
138 escputs(ChrPtr(author));
144 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
145 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
146 wc_printf("\">%s</a></td>", _("(show)"));
147 wc_printf("<td>(%s)</td>", _("Current version"));
151 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
155 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
156 wc_printf("\">%s</a></td>", _("(show)"));
157 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
163 wc_printf("</tr>\n");
165 /* Extract all fields except the author and date after displaying the row. This
166 * is deliberate, because the timestamp reflects when the diff was written, not
167 * when the version which it reflects was written. Similarly, the name associated
168 * with each diff is the author who created the newer version of the page that
169 * made the diff happen.
171 StrBufExtract_token(rev_uuid, Buf, 0, '|');
172 StrBufExtract_token(node, Buf, 3, '|');
176 wc_printf("</table>\n");
179 FreeStrBuf(&rev_uuid);
182 wc_printf("%s", ChrPtr(Buf));
191 * Display the revision history for a wiki page
193 void display_wiki_history(void)
195 output_headers(1, 1, 1, 0, 0, 0);
196 do_template("wiki_history");
202 * Display a list of all pages in a Wiki room (template callback)
204 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
209 if (!IsEmptyStr(bstr("query"))) {
210 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
213 serv_printf("MSGS ALL|||4"); /* full list */
217 StrBuf_ServGetln(Buf);
218 if (GetServerStatus(Buf, NULL) == 1) {
219 StrBuf *pagetitle = NewStrBuf();
221 wc_printf("<table class=\"wiki_pagelist_background\">");
222 wc_printf("<th>%s</th>", _("Page title"));
224 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
225 StrBufExtract_token(pagetitle, Buf, 1, '|');
227 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
228 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
229 wc_printf("<td><a href=\"wiki?page=");
230 urlescputs(ChrPtr(pagetitle));
232 escputs(ChrPtr(pagetitle));
233 wc_printf("</a></td>");
234 wc_printf("</tr>\n");
238 wc_printf("</table>\n");
239 FreeStrBuf(&pagetitle);
247 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
249 void display_wiki_pagelist(void)
251 output_headers(1, 1, 1, 0, 0, 0);
252 do_template("wiki_pagelist");
257 int wiki_Cleanup(void **ViewSpecific)
260 pagename = NewStrBufDup(sbstr("page"));
261 display_wiki_page_backend(pagename, "", 0);
267 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
274 GetTemplateTokenString(Target, TP, 2, &pch, &len);
275 return strcasecmp(page, pch) == 0;
279 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
285 GetTemplateTokenString(Target, TP, 2, &pch, &len);
286 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
290 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
292 /* this function was intentionaly left empty. */
296 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
304 if (oper == do_search)
305 display_wiki_pagelist();
307 http_redirect("wiki?page=home");
317 RegisterReadLoopHandlerset(
319 wiki_GetParamsGetServerCall,
320 wiki_PrintHeaderPage,
329 RegisterReadLoopHandlerset(
331 wiki_GetParamsGetServerCall,
332 wiki_PrintHeaderPage,
341 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
342 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
343 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
344 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
345 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
346 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
347 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);