Search in sources :

Example 56 with WikiEngine

use of org.apache.wiki.WikiEngine in project jspwiki by apache.

the class InsertPage method execute.

/**
 *  {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    StringBuilder res = new StringBuilder();
    String clazz = params.get(PARAM_CLASS);
    String includedPage = params.get(PARAM_PAGENAME);
    String style = params.get(PARAM_STYLE);
    String defaultstr = params.get(PARAM_DEFAULT);
    int section = TextUtil.parseIntParameter(params.get(PARAM_SECTION), -1);
    int maxlen = TextUtil.parseIntParameter(params.get(PARAM_MAXLENGTH), -1);
    if (style == null)
        style = DEFAULT_STYLE;
    if (maxlen == -1)
        maxlen = Integer.MAX_VALUE;
    if (includedPage != null) {
        WikiPage page = null;
        try {
            String pageName = engine.getFinalPageName(includedPage);
            if (pageName != null) {
                page = engine.getPage(pageName);
            } else {
                page = engine.getPage(includedPage);
            }
        } catch (ProviderException e) {
            res.append("<span class=\"error\">Page could not be found by the page provider.</span>");
            return res.toString();
        }
        if (page != null) {
            // 
            // Check for recursivity
            // 
            List<String> previousIncludes = (List<String>) context.getVariable(ATTR_RECURSE);
            if (previousIncludes != null) {
                if (previousIncludes.contains(page.getName())) {
                    return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
                }
            } else {
                previousIncludes = new ArrayList<String>();
            }
            previousIncludes.add(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
            // 
            // Check for permissions
            // 
            AuthorizationManager mgr = engine.getAuthorizationManager();
            if (!mgr.checkPermission(context.getWikiSession(), PermissionFactory.getPagePermission(page, "view"))) {
                res.append("<span class=\"error\">You do not have permission to view this included page.</span>");
                return res.toString();
            }
            /**
             *  We want inclusion to occur within the context of
             *  its own page, because we need the links to be correct.
             */
            WikiContext includedContext = (WikiContext) context.clone();
            includedContext.setPage(page);
            String pageData = engine.getPureText(page);
            String moreLink = "";
            if (section != -1) {
                try {
                    pageData = TextUtil.getSection(pageData, section);
                } catch (IllegalArgumentException e) {
                    throw new PluginException(e.getMessage());
                }
            }
            if (pageData.length() > maxlen) {
                pageData = pageData.substring(0, maxlen) + " ...";
                moreLink = "<p><a href=\"" + context.getURL(WikiContext.VIEW, includedPage) + "\">More...</a></p>";
            }
            res.append("<div style=\"" + style + "\"" + (clazz != null ? " class=\"" + clazz + "\"" : "") + ">");
            res.append(engine.textToHTML(includedContext, pageData));
            res.append(moreLink);
            res.append("</div>");
            // 
            // Remove the name from the stack; we're now done with this.
            // 
            previousIncludes.remove(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
        } else {
            if (defaultstr != null) {
                res.append(defaultstr);
            } else {
                res.append("There is no page called '" + includedPage + "'.  Would you like to ");
                res.append("<a href=\"" + context.getURL(WikiContext.EDIT, includedPage) + "\">create it?</a>");
            }
        }
    } else {
        res.append("<span class=\"error\">");
        res.append("You have to define a page!");
        res.append("</span>");
    }
    return res.toString();
}
Also used : WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) List(java.util.List) ArrayList(java.util.ArrayList) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 57 with WikiEngine

use of org.apache.wiki.WikiEngine in project jspwiki by apache.

the class RecentChangesPlugin method execute.

/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    int since = TextUtil.parseIntParameter(params.get("since"), DEFAULT_DAYS);
    String spacing = "4";
    boolean showAuthor = true;
    boolean showChangenote = true;
    String tablewidth = "4";
    WikiEngine engine = context.getEngine();
    // 
    if ("compact".equals(params.get(PARAM_FORMAT))) {
        spacing = "0";
        showAuthor = false;
        showChangenote = false;
        tablewidth = "2";
    }
    Calendar sincedate = new GregorianCalendar();
    sincedate.add(Calendar.DAY_OF_MONTH, -since);
    log.debug("Calculating recent changes from " + sincedate.getTime());
    // FIXME: Should really have a since date on the getRecentChanges method.
    Collection<WikiPage> changes = engine.getRecentChanges();
    super.initialize(context, params);
    changes = super.filterCollection(changes);
    if (changes != null) {
        Date olddate = new Date(0);
        DateFormat fmt = getDateFormat(context, params);
        DateFormat tfmt = getTimeFormat(context, params);
        Element rt = XhtmlUtil.element(XHTML.table);
        rt.setAttribute(XHTML.ATTR_class, "recentchanges");
        rt.setAttribute(XHTML.ATTR_cellpadding, spacing);
        for (Iterator<WikiPage> i = changes.iterator(); i.hasNext(); ) {
            WikiPage pageref = i.next();
            Date lastmod = pageref.getLastModified();
            if (lastmod.before(sincedate.getTime())) {
                break;
            }
            if (!isSameDay(lastmod, olddate)) {
                Element row = XhtmlUtil.element(XHTML.tr);
                Element col = XhtmlUtil.element(XHTML.td);
                col.setAttribute(XHTML.ATTR_colspan, tablewidth);
                col.setAttribute(XHTML.ATTR_class, "date");
                col.addContent(XhtmlUtil.element(XHTML.b, fmt.format(lastmod)));
                rt.addContent(row);
                row.addContent(col);
                olddate = lastmod;
            }
            String href = context.getURL(pageref instanceof Attachment ? WikiContext.ATTACH : WikiContext.VIEW, pageref.getName());
            Element link = XhtmlUtil.link(href, engine.beautifyTitle(pageref.getName()));
            Element row = XhtmlUtil.element(XHTML.tr);
            Element col = XhtmlUtil.element(XHTML.td);
            col.setAttribute(XHTML.ATTR_width, "30%");
            col.addContent(link);
            // 
            if (pageref instanceof Attachment) {
                link = XhtmlUtil.link(context.getURL(WikiContext.INFO, pageref.getName()), null);
                link.setAttribute(XHTML.ATTR_class, "infolink");
                Element img = XhtmlUtil.img(context.getURL(WikiContext.NONE, "images/attachment_small.png"), null);
                link.addContent(img);
                col.addContent(link);
            }
            row.addContent(col);
            rt.addContent(row);
            if (pageref instanceof Attachment) {
                Element td = XhtmlUtil.element(XHTML.td, tfmt.format(lastmod));
                td.setAttribute(XHTML.ATTR_class, "lastchange");
                row.addContent(td);
            } else {
                Element infocol = XhtmlUtil.element(XHTML.td);
                infocol.setAttribute(XHTML.ATTR_class, "lastchange");
                infocol.addContent(XhtmlUtil.link(context.getURL(WikiContext.DIFF, pageref.getName(), "r1=-1"), tfmt.format(lastmod)));
                row.addContent(infocol);
            }
            if (showAuthor) {
                String author = pageref.getAuthor();
                Element authorinfo = XhtmlUtil.element(XHTML.td);
                authorinfo.setAttribute(XHTML.ATTR_class, "author");
                if (author != null) {
                    if (engine.pageExists(author)) {
                        authorinfo.addContent(XhtmlUtil.link(context.getURL(WikiContext.VIEW, author), author));
                    } else {
                        authorinfo.addContent(author);
                    }
                } else {
                    authorinfo.addContent(Preferences.getBundle(context, InternationalizationManager.CORE_BUNDLE).getString("common.unknownauthor"));
                }
                row.addContent(authorinfo);
            }
            // Change note
            if (showChangenote) {
                String changenote = (String) pageref.getAttribute(WikiPage.CHANGENOTE);
                Element td_changenote = XhtmlUtil.element(XHTML.td, changenote);
                td_changenote.setAttribute(XHTML.ATTR_class, "changenote");
                row.addContent(td_changenote);
            }
        // Revert note
        /*                
                if( context.hasAdminPermissions() )
                {
                    row.addElement( new td("Revert") );
                }
 */
        }
        return XhtmlUtil.serialize(rt, XhtmlUtil.EXPAND_EMPTY_NODES);
    }
    return "";
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) WikiPage(org.apache.wiki.WikiPage) Element(org.jdom2.Element) GregorianCalendar(java.util.GregorianCalendar) Attachment(org.apache.wiki.attachment.Attachment) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) WikiEngine(org.apache.wiki.WikiEngine)

Example 58 with WikiEngine

use of org.apache.wiki.WikiEngine in project jspwiki by apache.

the class AttachmentsIteratorTag method doStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doStartTag() {
    m_wikiContext = (WikiContext) pageContext.getAttribute(WikiTagBase.ATTR_CONTEXT, PageContext.REQUEST_SCOPE);
    WikiEngine engine = m_wikiContext.getEngine();
    AttachmentManager mgr = engine.getAttachmentManager();
    WikiPage page;
    page = m_wikiContext.getPage();
    if (!mgr.attachmentsEnabled()) {
        return SKIP_BODY;
    }
    try {
        if (page != null && engine.pageExists(page)) {
            Collection atts = mgr.listAttachments(page);
            if (atts == null) {
                log.debug("No attachments to display.");
                // There are no attachments included
                return SKIP_BODY;
            }
            m_iterator = atts.iterator();
            if (m_iterator.hasNext()) {
                Attachment att = (Attachment) m_iterator.next();
                WikiContext context = (WikiContext) m_wikiContext.clone();
                context.setPage(att);
                pageContext.setAttribute(WikiTagBase.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
                pageContext.setAttribute(getId(), att);
            } else {
                return SKIP_BODY;
            }
        } else {
            return SKIP_BODY;
        }
        return EVAL_BODY_BUFFERED;
    } catch (ProviderException e) {
        log.fatal("Provider failed while trying to iterator through history", e);
    // FIXME: THrow something.
    }
    return SKIP_BODY;
}
Also used : WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) Collection(java.util.Collection) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 59 with WikiEngine

use of org.apache.wiki.WikiEngine in project jspwiki by apache.

the class AuthorTag method doWikiStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    String author = page.getAuthor();
    if (author != null && author.length() > 0) {
        author = TextUtil.replaceEntities(author);
        if (engine.pageExists(author)) {
            // FIXME: It's very boring to have to do this.
            // Slow, too.
            RenderingManager mgr = engine.getRenderingManager();
            MarkupParser p = mgr.getParser(m_wikiContext, "[" + author + "|" + author + "]");
            WikiDocument d = p.parse();
            author = mgr.getHTML(m_wikiContext, d);
        }
        pageContext.getOut().print(author);
    } else {
        pageContext.getOut().print(Preferences.getBundle(m_wikiContext, InternationalizationManager.CORE_BUNDLE).getString("common.unknownauthor"));
    }
    return SKIP_BODY;
}
Also used : RenderingManager(org.apache.wiki.render.RenderingManager) WikiPage(org.apache.wiki.WikiPage) WikiDocument(org.apache.wiki.parser.WikiDocument) WikiEngine(org.apache.wiki.WikiEngine) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 60 with WikiEngine

use of org.apache.wiki.WikiEngine in project jspwiki by apache.

the class CalendarTag method getDayLink.

/**
 *  Returns a link to the given day.
 */
private String getDayLink(Calendar day) {
    WikiEngine engine = m_wikiContext.getEngine();
    String result = "";
    if (m_pageFormat != null) {
        String pagename = m_pageFormat.format(day.getTime());
        if (engine.pageExists(pagename)) {
            if (m_urlFormat != null) {
                String url = m_urlFormat.format(day.getTime());
                result = "<td class=\"link\"><a href=\"" + url + "\">" + day.get(Calendar.DATE) + "</a></td>";
            } else {
                result = "<td class=\"link\"><a href=\"" + m_wikiContext.getViewURL(pagename) + "\">" + day.get(Calendar.DATE) + "</a></td>";
            }
        } else {
            result = "<td class=\"days\">" + day.get(Calendar.DATE) + "</td>";
        }
    } else if (m_urlFormat != null) {
        String url = m_urlFormat.format(day.getTime());
        result = "<td><a href=\"" + url + "\">" + day.get(Calendar.DATE) + "</a></td>";
    } else {
        result = "<td class=\"days\">" + day.get(Calendar.DATE) + "</td>";
    }
    return format(result);
}
Also used : WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

WikiEngine (org.apache.wiki.WikiEngine)67 WikiPage (org.apache.wiki.WikiPage)29 ProviderException (org.apache.wiki.api.exceptions.ProviderException)15 Attachment (org.apache.wiki.attachment.Attachment)10 WikiContext (org.apache.wiki.WikiContext)9 Properties (java.util.Properties)8 JspWriter (javax.servlet.jsp.JspWriter)8 TestEngine (org.apache.wiki.TestEngine)8 Element (org.jdom2.Element)7 IOException (java.io.IOException)5 SimpleDateFormat (java.text.SimpleDateFormat)5 Calendar (java.util.Calendar)5 Date (java.util.Date)5 ResourceBundle (java.util.ResourceBundle)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 Before (org.junit.Before)5 Principal (java.security.Principal)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 InternalWikiException (org.apache.wiki.InternalWikiException)4 ArrayList (java.util.ArrayList)3