Search in sources :

Example 1 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class WeblogEntryPlugin method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    final Engine engine = context.getEngine();
    String weblogName = params.get(PARAM_BLOGNAME);
    if (weblogName == null) {
        weblogName = context.getPage().getName();
    }
    String entryText = TextUtil.replaceEntities(params.get(PARAM_ENTRYTEXT));
    if (entryText == null) {
        entryText = rb.getString("weblogentryplugin.newentry");
    }
    final String url = context.getURL(ContextEnum.PAGE_NONE.getRequestContext(), "NewBlogEntry.jsp", "page=" + engine.encodeName(weblogName));
    return "<a href=\"" + url + "\">" + entryText + "</a>";
}
Also used : ResourceBundle(java.util.ResourceBundle) Engine(org.apache.wiki.api.core.Engine)

Example 2 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class WeblogPlugin method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final Calendar startTime;
    final Calendar stopTime;
    int numDays = DEFAULT_DAYS;
    final Engine engine = context.getEngine();
    final AuthorizationManager mgr = engine.getManager(AuthorizationManager.class);
    // 
    // Parse parameters.
    // 
    String days;
    final DateFormat entryFormat;
    String startDay;
    boolean hasComments = false;
    int maxEntries;
    String weblogName;
    if ((weblogName = params.get(PARAM_PAGE)) == null) {
        weblogName = context.getPage().getName();
    }
    if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) {
        days = params.get(PARAM_DAYS);
    }
    if ((params.get(PARAM_ENTRYFORMAT)) == null) {
        entryFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
    } else {
        entryFormat = new SimpleDateFormat(params.get(PARAM_ENTRYFORMAT));
    }
    if (days != null) {
        if (days.equalsIgnoreCase("all")) {
            numDays = Integer.MAX_VALUE;
        } else {
            numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS);
        }
    }
    if ((startDay = params.get(PARAM_STARTDATE)) == null) {
        startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE);
    }
    if (TextUtil.isPositive(params.get(PARAM_ALLOWCOMMENTS))) {
        hasComments = true;
    }
    maxEntries = TextUtil.parseIntParameter(params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE);
    // 
    // Determine the date range which to include.
    // 
    startTime = Calendar.getInstance();
    stopTime = Calendar.getInstance();
    if (startDay != null) {
        final SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT);
        try {
            final Date d = fmt.parse(startDay);
            startTime.setTime(d);
            stopTime.setTime(d);
        } catch (final ParseException e) {
            return "Illegal time format: " + startDay;
        }
    }
    // 
    // Mark this to be a weblog
    // 
    context.getPage().setAttribute(ATTR_ISWEBLOG, "true");
    // 
    // We make a wild guess here that nobody can do millisecond accuracy here.
    // 
    startTime.add(Calendar.DAY_OF_MONTH, -numDays);
    startTime.set(Calendar.HOUR, 0);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.SECOND, 0);
    stopTime.set(Calendar.HOUR, 23);
    stopTime.set(Calendar.MINUTE, 59);
    stopTime.set(Calendar.SECOND, 59);
    final StringBuilder sb = new StringBuilder();
    final List<Page> blogEntries = findBlogEntries(engine, weblogName, startTime.getTime(), stopTime.getTime());
    blogEntries.sort(new PageDateComparator());
    sb.append("<div class=\"weblog\">\n");
    for (final Iterator<Page> i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) {
        final Page p = i.next();
        if (mgr.checkPermission(context.getWikiSession(), new PagePermission(p, PagePermission.VIEW_ACTION))) {
            addEntryHTML(context, entryFormat, hasComments, sb, p, params);
        }
    }
    sb.append("</div>\n");
    return sb.toString();
}
Also used : Calendar(java.util.Calendar) Page(org.apache.wiki.api.core.Page) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Engine(org.apache.wiki.api.core.Engine) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 3 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class RecentChangesPlugin method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final int since = TextUtil.parseIntParameter(params.get("since"), DEFAULT_DAYS);
    String spacing = "4";
    boolean showAuthor = true;
    boolean showChangenote = true;
    String tablewidth = "4";
    final Engine engine = context.getEngine();
    // 
    if ("compact".equals(params.get(PARAM_FORMAT))) {
        spacing = "0";
        showAuthor = false;
        showChangenote = false;
        tablewidth = "2";
    }
    final 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<Page> changes = engine.getManager(PageManager.class).getRecentChanges();
    super.initialize(context, params);
    changes = filterWikiPageCollection(changes);
    if (changes != null) {
        Date olddate = new Date(0);
        final DateFormat fmt = getDateFormat(context, params);
        final DateFormat tfmt = getTimeFormat(context, params);
        final Element rt = XhtmlUtil.element(XHTML.table);
        rt.setAttribute(XHTML.ATTR_class, "recentchanges");
        rt.setAttribute(XHTML.ATTR_cellpadding, spacing);
        for (final Page pageref : changes) {
            final Date lastmod = pageref.getLastModified();
            if (lastmod.before(sincedate.getTime())) {
                break;
            }
            if (!isSameDay(lastmod, olddate)) {
                final Element row = XhtmlUtil.element(XHTML.tr);
                final 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;
            }
            final String href = context.getURL(pageref instanceof Attachment ? ContextEnum.PAGE_ATTACH.getRequestContext() : ContextEnum.PAGE_VIEW.getRequestContext(), pageref.getName());
            Element link = XhtmlUtil.link(href, engine.getManager(RenderingManager.class).beautifyTitle(pageref.getName()));
            final Element row = XhtmlUtil.element(XHTML.tr);
            final 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");
                final 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) {
                final Element td = XhtmlUtil.element(XHTML.td, tfmt.format(lastmod));
                td.setAttribute(XHTML.ATTR_class, "lastchange");
                row.addContent(td);
            } else {
                final 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) {
                final String author = pageref.getAuthor();
                final Element authorinfo = XhtmlUtil.element(XHTML.td);
                authorinfo.setAttribute(XHTML.ATTR_class, "author");
                if (author != null) {
                    if (engine.getManager(PageManager.class).wikiPageExists(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) {
                final String changenote = pageref.getAttribute(Page.CHANGENOTE);
                final 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 : Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) Element(org.jdom2.Element) GregorianCalendar(java.util.GregorianCalendar) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.attachment.Attachment) Date(java.util.Date) PageManager(org.apache.wiki.pages.PageManager) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Engine(org.apache.wiki.api.core.Engine)

Example 4 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class AtomFeed method getItems.

private Collection<Element> getItems() {
    final ArrayList<Element> list = new ArrayList<>();
    final Engine engine = m_wikiContext.getEngine();
    ServletContext servletContext = null;
    if (m_wikiContext.getHttpRequest() != null) {
        servletContext = m_wikiContext.getHttpRequest().getSession().getServletContext();
    }
    for (final Entry e : m_entries) {
        final Page p = e.getPage();
        final Element entryEl = getElement("entry");
        // Mandatory elements
        entryEl.addContent(getElement("id").setText(getEntryID(e)));
        entryEl.addContent(getElement("title").setAttribute("type", "html").setText(e.getTitle()));
        entryEl.addContent(getElement("updated").setText(DateFormatUtils.formatUTC(p.getLastModified(), RFC3339FORMAT)));
        // Optional elements
        entryEl.addContent(getElement("author").addContent(getElement("name").setText(e.getAuthor())));
        entryEl.addContent(getElement("link").setAttribute("rel", "alternate").setAttribute("href", e.getURL()));
        entryEl.addContent(getElement("content").setAttribute("type", "html").setText(e.getContent()));
        // Check for enclosures
        if (engine.getManager(AttachmentManager.class).hasAttachments(p) && servletContext != null) {
            try {
                final List<Attachment> c = engine.getManager(AttachmentManager.class).listAttachments(p);
                for (final Attachment att : c) {
                    final Element attEl = getElement("link");
                    attEl.setAttribute("rel", "enclosure");
                    attEl.setAttribute("href", engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName(), null));
                    attEl.setAttribute("length", Long.toString(att.getSize()));
                    attEl.setAttribute("type", getMimeType(servletContext, att.getFileName()));
                    entryEl.addContent(attEl);
                }
            } catch (final ProviderException ex) {
            // FIXME: log.info("Can't get attachment data",ex);
            }
        }
        list.add(entryEl);
    }
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ServletContext(javax.servlet.ServletContext) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Engine(org.apache.wiki.api.core.Engine)

Example 5 with Engine

use of org.apache.wiki.api.core.Engine in project jspwiki by apache.

the class Feed method getSiteName.

/**
 * Figure out a site name for a feed.
 *
 * @param context the wiki context
 * @return the site name
 */
public static String getSiteName(final Context context) {
    final Engine engine = context.getEngine();
    String blogname = null;
    try {
        blogname = engine.getManager(VariableManager.class).getValue(context, VAR_BLOGNAME);
    } catch (final NoSuchVariableException e) {
    }
    if (blogname == null) {
        blogname = engine.getApplicationName() + ": " + context.getPage().getName();
    }
    return blogname;
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) Engine(org.apache.wiki.api.core.Engine)

Aggregations

Engine (org.apache.wiki.api.core.Engine)60 Page (org.apache.wiki.api.core.Page)30 PageManager (org.apache.wiki.pages.PageManager)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)11 Attachment (org.apache.wiki.api.core.Attachment)10 JspWriter (javax.servlet.jsp.JspWriter)9 Context (org.apache.wiki.api.core.Context)9 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)8 Element (org.jdom2.Element)7 IOException (java.io.IOException)6 Calendar (java.util.Calendar)6 ResourceBundle (java.util.ResourceBundle)6 SimpleDateFormat (java.text.SimpleDateFormat)5 Date (java.util.Date)5 PageContext (javax.servlet.jsp.PageContext)5 Principal (java.security.Principal)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 PluginException (org.apache.wiki.api.exceptions.PluginException)4 RenderingManager (org.apache.wiki.render.RenderingManager)4 ArrayList (java.util.ArrayList)3