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)
52 if (WC->CurRoom.view != VIEW_WIKI) {
53 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WC->CurRoom.name) );
57 if (IsEmptyStr(pagename)) {
58 strcpy(pagename, "home");
61 str_wiki_index(pagename); /* convert index name to lowercase and numeric only */
63 if ((rev != NULL) && (strlen(rev) > 0)) {
64 /* read an older revision */
65 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
66 serv_getln(buf, sizeof buf);
68 msgnum = extract_long(&buf[4], 0);
72 /* read the current revision */
73 msgnum = locate_message_by_uid(pagename);
77 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
82 "<div align=\"center\">"
83 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
84 "<tr><td align=\"center\">"
87 wc_printf(_("There is no page called '%s' here."), pagename);
88 wc_printf("</b><br><br>");
89 wc_printf(_("Select the 'Edit this page' link in the room banner "
90 "if you would like to create this page."));
91 wc_printf("<br><br>");
92 wc_printf("</td></tr></table></div>\n");
97 * Display a specific page from a wiki room
99 void display_wiki_page(void)
105 output_headers(1, 1, 1, 0, 0, 0);
106 safestrncpy(pagename, bstr("page"), sizeof pagename);
107 str_wiki_index(pagename);
108 safestrncpy(rev, bstr("rev"), sizeof rev);
109 do_revert = atoi(bstr("revert"));
110 display_wiki_page_backend(pagename, rev, do_revert);
116 * Display the revision history for a wiki page (template callback)
118 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
124 safestrncpy(pagename, bstr("page"), sizeof pagename);
125 str_wiki_index(pagename);
127 serv_printf("WIKI history|%s", pagename);
129 StrBuf_ServGetln(Buf);
130 if (GetServerStatus(Buf, NULL) == 1) {
133 char rev_date_displayed[64];
134 StrBuf *rev_uuid = NewStrBuf();
135 StrBuf *author = NewStrBuf();
136 StrBuf *node = NewStrBuf();
138 wc_printf("<table class=\"wiki_history_background\">");
140 wc_printf("<th>%s</th>", _("Date"));
141 wc_printf("<th>%s</th>", _("Author"));
143 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
145 rev_date = extract_long(ChrPtr(Buf), 1);
146 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
147 StrBufExtract_token(author, Buf, 2, '|');
149 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
150 wc_printf("<td>%s</td><td>", rev_date_displayed);
151 if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
152 escputs(ChrPtr(author));
154 escputs(ChrPtr(node));
157 wc_printf("<a href=\"showuser?who=");
158 urlescputs(ChrPtr(author));
160 escputs(ChrPtr(author));
166 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
167 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
168 wc_printf("\">%s</a></td>", _("(show)"));
169 wc_printf("<td>(%s)</td>", _("Current version"));
173 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
177 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
178 wc_printf("\">%s</a></td>", _("(show)"));
179 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
185 wc_printf("</tr>\n");
187 /* Extract all fields except the author and date after displaying the row. This
188 * is deliberate, because the timestamp reflects when the diff was written, not
189 * when the version which it reflects was written. Similarly, the name associated
190 * with each diff is the author who created the newer version of the page that
191 * made the diff happen.
193 StrBufExtract_token(rev_uuid, Buf, 0, '|');
194 StrBufExtract_token(node, Buf, 3, '|');
198 wc_printf("</table>\n");
201 FreeStrBuf(&rev_uuid);
204 wc_printf("%s", ChrPtr(Buf));
213 * Display the revision history for a wiki page
215 void display_wiki_history(void)
217 output_headers(1, 1, 1, 0, 0, 0);
218 do_template("wiki_history");
224 * Display a list of all pages in a Wiki room (template callback)
226 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
231 if (!IsEmptyStr(bstr("query"))) {
232 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
235 serv_printf("MSGS ALL|||4"); /* full list */
239 StrBuf_ServGetln(Buf);
240 if (GetServerStatus(Buf, NULL) == 1) {
241 StrBuf *pagetitle = NewStrBuf();
243 wc_printf("<table class=\"wiki_pagelist_background\">");
244 wc_printf("<th>%s</th>", _("Page title"));
246 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
247 StrBufExtract_token(pagetitle, Buf, 1, '|');
249 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
250 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
251 wc_printf("<td><a href=\"wiki?page=");
252 urlescputs(ChrPtr(pagetitle));
254 escputs(ChrPtr(pagetitle));
255 wc_printf("</a></td>");
256 wc_printf("</tr>\n");
260 wc_printf("</table>\n");
261 FreeStrBuf(&pagetitle);
269 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
271 void display_wiki_pagelist(void)
273 output_headers(1, 1, 1, 0, 0, 0);
274 do_template("wiki_pagelist");
279 int wiki_Cleanup(void **ViewSpecific)
282 safestrncpy(pagename, "home", sizeof pagename);
283 display_wiki_page_backend(pagename, "", 0);
289 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
296 GetTemplateTokenString(Target, TP, 2, &pch, &len);
297 return strcasecmp(page, pch) == 0;
301 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
307 GetTemplateTokenString(Target, TP, 2, &pch, &len);
308 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
312 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
314 /* this function was intentionaly left empty. */
318 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat,
326 if (oper == do_search)
327 display_wiki_pagelist();
329 http_redirect("wiki?page=home");
339 RegisterReadLoopHandlerset(
341 wiki_GetParamsGetServerCall,
342 wiki_PrintHeaderPage,
350 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
351 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
352 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
353 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
354 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
355 RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
356 RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);