001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import com.ecyrd.jspwiki.*;
023: import com.ecyrd.jspwiki.auth.*;
024: import com.ecyrd.jspwiki.auth.permissions.PermissionFactory;
025:
026: import java.util.*;
027:
028: /**
029: * Inserts page contents. Muchos thanks to Scott Hurlbert for the initial code.
030: *
031: * @since 2.1.37
032: * @author Scott Hurlbert
033: * @author Janne Jalkanen
034: */
035: public class InsertPage implements WikiPlugin {
036: public static final String PARAM_PAGENAME = "page";
037: public static final String PARAM_STYLE = "style";
038: public static final String PARAM_MAXLENGTH = "maxlength";
039: public static final String PARAM_CLASS = "class";
040: public static final String PARAM_SECTION = "section";
041: public static final String PARAM_DEFAULT = "default";
042:
043: private static final String DEFAULT_STYLE = "";
044:
045: public static final String ATTR_RECURSE = "com.ecyrd.jspwiki.plugin.InsertPage.recurseCheck";
046:
047: public String execute(WikiContext context, Map params)
048: throws PluginException {
049: WikiEngine engine = context.getEngine();
050:
051: StringBuffer res = new StringBuffer();
052:
053: String clazz = (String) params.get(PARAM_CLASS);
054: String includedPage = (String) params.get(PARAM_PAGENAME);
055: String style = (String) params.get(PARAM_STYLE);
056: String defaultstr = (String) params.get(PARAM_DEFAULT);
057: int section = TextUtil.parseIntParameter((String) params
058: .get(PARAM_SECTION), -1);
059: int maxlen = TextUtil.parseIntParameter((String) params
060: .get(PARAM_MAXLENGTH), -1);
061:
062: if (style == null)
063: style = DEFAULT_STYLE;
064:
065: if (maxlen == -1)
066: maxlen = Integer.MAX_VALUE;
067:
068: if (includedPage != null) {
069: WikiPage page = engine.getPage(includedPage);
070:
071: if (page != null) {
072: //
073: // Check for recursivity
074: //
075:
076: List previousIncludes = (List) context
077: .getVariable(ATTR_RECURSE);
078:
079: if (previousIncludes != null) {
080: if (previousIncludes.contains(page.getName())) {
081: return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
082: }
083: } else {
084: previousIncludes = new ArrayList();
085: }
086:
087: previousIncludes.add(page.getName());
088: context.setVariable(ATTR_RECURSE, previousIncludes);
089:
090: //
091: // Check for permissions
092: //
093: AuthorizationManager mgr = engine
094: .getAuthorizationManager();
095:
096: if (!mgr.checkPermission(context.getWikiSession(),
097: PermissionFactory.getPagePermission(page,
098: "view"))) {
099: res
100: .append("<span class=\"error\">You do not have permission to view this included page.</span>");
101: return res.toString();
102: }
103:
104: /**
105: * We want inclusion to occur within the context of
106: * its own page, because we need the links to be correct.
107: */
108:
109: WikiContext includedContext = (WikiContext) context
110: .clone();
111: includedContext.setPage(page);
112:
113: String pageData = engine.getPureText(page);
114: String moreLink = "";
115:
116: if (section != -1) {
117: try {
118: pageData = TextUtil.getSection(pageData,
119: section);
120: } catch (IllegalArgumentException e) {
121: throw new PluginException(e.getMessage());
122: }
123: }
124:
125: if (pageData.length() > maxlen) {
126: pageData = pageData.substring(0, maxlen) + " ...";
127: moreLink = "<p><a href=\""
128: + context.getURL(WikiContext.VIEW,
129: includedPage)
130: + "\">More...</a></p>";
131: }
132:
133: res.append("<div style=\""
134: + style
135: + "\""
136: + (clazz != null ? " class=\"" + clazz + "\""
137: : "") + ">");
138: res
139: .append(engine.textToHTML(includedContext,
140: pageData));
141: res.append(moreLink);
142: res.append("</div>");
143:
144: //
145: // Remove the name from the stack; we're now done with this.
146: //
147: previousIncludes.remove(page.getName());
148: context.setVariable(ATTR_RECURSE, previousIncludes);
149: } else {
150: if (defaultstr != null) {
151: res.append(defaultstr);
152: } else {
153: res.append("There is no page called '"
154: + includedPage + "'. Would you like to ");
155: res
156: .append("<a href=\""
157: + context.getURL(WikiContext.EDIT,
158: includedPage)
159: + "\">create it?</a>");
160: }
161: }
162: } else {
163: res.append("<span class=\"error\">");
164: res.append("You have to define a page!");
165: res.append("</span>");
166: }
167:
168: return res.toString();
169: }
170:
171: }
|