2 * Functions pertaining to rooms with a wiki view
4 * Copyright (c) 2009-2011 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 as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Convert a string to something suitable as a wiki index
27 void str_wiki_index(char *s)
31 if (s == NULL) return;
33 /* First remove all non-alphanumeric characters */
34 for (i=0; i<strlen(s); ++i) {
36 strcpy(&s[i], &s[i+1]);
40 /* Then make everything lower case */
41 for (i=0; i<strlen(s); ++i) {
47 * Display a specific page from a wiki room
49 * "rev" may be set to an empty string to display the current version.
50 * "do_revert" may be set to nonzero to perform a reversion to the specified version.
52 void display_wiki_page_backend(const StrBuf *roomname, char *pagename, char *rev, int do_revert)
58 str_wiki_index(pagename);
60 if (StrLength(roomname) > 0) {
62 /* If we're not in the correct room, try going there. */
63 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
67 /* If we're still not in the correct room, it doesn't exist. */
68 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
69 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
75 if (WC->CurRoom.view != VIEW_WIKI) {
76 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
80 if (IsEmptyStr(pagename)) {
81 strcpy(pagename, "home");
84 /* Found it! Now read it. */
86 if ((rev != NULL) && (strlen(rev) > 0)) {
87 /* read an older revision */
88 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
89 serv_getln(buf, sizeof buf);
91 msgnum = extract_long(&buf[4], 0);
95 /* read the current revision? */
96 msgnum = locate_message_by_uid(pagename);
100 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
105 "<div align=\"center\">"
106 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
107 "<tr><td align=\"center\">"
109 wc_printf("<br><b>");
110 wc_printf(_("There is no page called '%s' here."), pagename);
111 wc_printf("</b><br><br>");
112 wc_printf(_("Select the 'Edit this page' link in the room banner "
113 "if you would like to create this page."));
114 wc_printf("<br><br>");
115 wc_printf("</td></tr></table></div>\n");
120 * Display a specific page from a wiki room
122 void display_wiki_page(void)
124 const StrBuf *roomname;
129 output_headers(1, 1, 1, 0, 0, 0);
130 roomname = sbstr("room");
131 safestrncpy(pagename, bstr("page"), sizeof pagename);
132 safestrncpy(rev, bstr("rev"), sizeof rev);
133 do_revert = atoi(bstr("revert"));
134 display_wiki_page_backend(roomname, pagename, rev, do_revert);
140 * Display the revision history for a wiki page (template callback)
142 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
144 const StrBuf *roomname;
149 roomname = sbstr("room");
150 safestrncpy(pagename, bstr("page"), sizeof pagename);
151 str_wiki_index(pagename);
153 if (StrLength(roomname) > 0) {
155 /* If we're not in the correct room, try going there. */
156 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
160 /* If we're still not in the correct room, it doesn't exist. */
161 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
162 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
168 serv_printf("WIKI history|%s", pagename);
170 StrBuf_ServGetln(Buf);
171 if (GetServerStatus(Buf, NULL) == 1) {
174 char rev_date_displayed[64];
175 StrBuf *rev_uuid = NewStrBuf();
176 StrBuf *author = NewStrBuf();
177 StrBuf *node = NewStrBuf();
179 wc_printf("<table class=\"wiki_history_background\">");
181 wc_printf("<th>%s</th>", _("Date"));
182 wc_printf("<th>%s</th>", _("Author"));
184 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
186 rev_date = extract_long(ChrPtr(Buf), 1);
187 webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
188 StrBufExtract_token(author, Buf, 2, '|');
190 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
191 wc_printf("<td>%s</td><td>", rev_date_displayed);
192 if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
193 escputs(ChrPtr(author));
195 escputs(ChrPtr(node));
198 wc_printf("<a href=\"showuser?who=");
199 urlescputs(ChrPtr(author));
201 escputs(ChrPtr(author));
207 wc_printf("<td><a href=\"wiki?page=%s\">%s</a></td>",
211 wc_printf("<td>(%s)</td>", _("Current version"));
215 wc_printf("<td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
220 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
226 wc_printf("</tr>\n");
228 /* Extract all fields except the author and date after displaying the row. This
229 * is deliberate, because the timestamp reflects when the diff was written, not
230 * when the version which it reflects was written. Similarly, the name associated
231 * with each diff is the author who created the newer version of the page that
232 * made the diff happen.
234 StrBufExtract_token(rev_uuid, Buf, 0, '|');
235 StrBufExtract_token(node, Buf, 3, '|');
239 wc_printf("</table>\n");
242 FreeStrBuf(&rev_uuid);
245 wc_printf("%s", ChrPtr(Buf));
254 * Display the revision history for a wiki page
256 void display_wiki_history(void)
258 output_headers(1, 1, 1, 0, 0, 0);
259 do_template("wiki_history");
265 * Display a list of all pages in a Wiki room (template callback)
267 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
269 const StrBuf *roomname;
273 roomname = sbstr("room");
274 if (StrLength(roomname) > 0) {
275 /* If we're not in the correct room, try going there. */
276 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
280 /* If we're still not in the correct room, it doesn't exist. */
281 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
282 wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
287 if (!IsEmptyStr(bstr("query"))) {
288 serv_printf("MSGS SEARCH|%s||4", bstr("query")); /* search-reduced list */
291 serv_printf("MSGS ALL|||4"); /* full list */
295 StrBuf_ServGetln(Buf);
296 if (GetServerStatus(Buf, NULL) == 1) {
297 StrBuf *pagetitle = NewStrBuf();
299 wc_printf("<table class=\"wiki_pagelist_background\">");
300 wc_printf("<th>%s</th>", _("Page title"));
302 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
303 StrBufExtract_token(pagetitle, Buf, 1, '|');
305 if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) { /* no history pages */
306 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
307 wc_printf("<td><a href=\"wiki?page=");
308 urlescputs(ChrPtr(pagetitle));
310 escputs(ChrPtr(pagetitle));
311 wc_printf("</a></td>");
312 wc_printf("</tr>\n");
316 wc_printf("</table>\n");
317 FreeStrBuf(&pagetitle);
325 * Display a list of all pages in a Wiki room. Search requests in a Wiki room also go here.
327 void display_wiki_pagelist(void)
329 output_headers(1, 1, 1, 0, 0, 0);
330 do_template("wiki_pagelist");
335 int wiki_Cleanup(void **ViewSpecific)
338 safestrncpy(pagename, "home", sizeof pagename);
339 display_wiki_page_backend(WC->CurRoom.name, pagename, "", 0);
345 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
352 GetTemplateTokenString(Target, TP, 2, &pch, &len);
353 return strcasecmp(page, pch) == 0;
357 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
363 GetTemplateTokenString(Target, TP, 2, &pch, &len);
364 return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
372 RegisterReadLoopHandlerset(
382 WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
383 WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
384 WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
385 RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
386 RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
387 RegisterConditional(HKEY("COND:WIKI:PAGE"), 1, ConditionalHaveWikiPage, CTX_NONE);
388 RegisterConditional(HKEY("COND:WIKI:TYPE"), 1, ConditionalHavewikiType, CTX_NONE);