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 org.apache.log4j.Logger;
023: import com.ecyrd.jspwiki.*;
024: import com.ecyrd.jspwiki.auth.*;
025: import com.ecyrd.jspwiki.auth.permissions.*;
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: private static Logger log = Logger.getLogger(InsertPage.class);
037:
038: public static final String PARAM_PAGENAME = "page";
039: public static final String PARAM_STYLE = "style";
040: public static final String PARAM_MAXLENGTH = "maxlength";
041: public static final String PARAM_CLASS = "class";
042: public static final String PARAM_SECTION = "section";
043: public static final String PARAM_DEFAULT = "default";
044:
045: private static final String DEFAULT_STYLE = "";
046:
047: public static final String ATTR_RECURSE = "com.ecyrd.jspwiki.plugin.InsertPage.recurseCheck";
048:
049: public String execute(WikiContext context, Map params)
050: throws PluginException {
051: WikiEngine engine = context.getEngine();
052:
053: StringBuffer res = new StringBuffer();
054:
055: String clazz = (String) params.get(PARAM_CLASS);
056: String includedPage = (String) params.get(PARAM_PAGENAME);
057: String style = (String) params.get(PARAM_STYLE);
058: String defaultstr = (String) params.get(PARAM_DEFAULT);
059: int section = TextUtil.parseIntParameter((String) params
060: .get(PARAM_SECTION), -1);
061: int maxlen = TextUtil.parseIntParameter((String) params
062: .get(PARAM_MAXLENGTH), -1);
063:
064: if (style == null)
065: style = DEFAULT_STYLE;
066:
067: if (maxlen == -1)
068: maxlen = Integer.MAX_VALUE;
069:
070: if (includedPage != null) {
071: WikiPage page = engine.getPage(includedPage);
072:
073: if (page != null) {
074: AuthorizationManager mgr = engine
075: .getAuthorizationManager();
076: UserProfile currentUser = context.getCurrentUser();
077:
078: // Disabled, because this seems to fail when used
079: // to insert something from a weblog entry.
080: if (!mgr.checkPermission(page, currentUser,
081: WikiPermission.newInstance("view"))) {
082: res
083: .append("<span class=\"error\">"
084: + context
085: .translate("msg.no_permission_included_page")
086: + "</span>");
087: return res.toString();
088: }
089:
090: //
091: // Check for recursivity
092: //
093:
094: List previousIncludes = (List) context
095: .getVariable(ATTR_RECURSE);
096:
097: if (previousIncludes != null) {
098: if (previousIncludes.contains(page.getName())) {
099: return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!";
100: }
101: } else {
102: previousIncludes = new ArrayList();
103: }
104:
105: previousIncludes.add(page.getName());
106: context.setVariable(ATTR_RECURSE, previousIncludes);
107:
108: /**
109: * We want inclusion to occur within the context of
110: * its own page, because we need the links to be correct.
111: */
112:
113: WikiContext includedContext = (WikiContext) context
114: .clone();
115: includedContext.setPage(page);
116:
117: String pageData = engine.getPureText(page);
118: String moreLink = "";
119:
120: if (section != -1) {
121: try {
122: pageData = TextUtil.getSection(pageData,
123: section);
124: } catch (IllegalArgumentException e) {
125: throw new PluginException(e.getMessage());
126: }
127: }
128:
129: if (pageData.length() > maxlen) {
130: pageData = pageData.substring(0, maxlen) + " ...";
131: moreLink = "<p><a href=\""
132: + context.getURL(WikiContext.VIEW,
133: includedPage)
134: + "\">More...</a></p>";
135: }
136:
137: res.append("<div style=\""
138: + style
139: + "\""
140: + (clazz != null ? " class=\"" + clazz + "\""
141: : "") + ">");
142: res
143: .append(engine.textToHTML(includedContext,
144: pageData));
145: res.append(moreLink);
146: res.append("</div>");
147:
148: //
149: // Remove the name from the stack; we're now done with this.
150: //
151: previousIncludes.remove(page.getName());
152: context.setVariable(ATTR_RECURSE, previousIncludes);
153: } else {
154: if (defaultstr != null) {
155: res.append(defaultstr);
156: } else {
157: res.append("There is no page called '"
158: + includedPage + "'. Would you like to ");
159: res
160: .append("<a href=\""
161: + context.getURL(WikiContext.EDIT,
162: includedPage)
163: + "\">create it?</a>");
164: }
165: }
166: } else {
167: res.append("<span class=\"error\">");
168: res.append("You have to define a page!");
169: res.append("</span>");
170: }
171:
172: return res.toString();
173: }
174:
175: }
|