Search in sources :

Example 46 with WikiEngine

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

the class PluginTag method executePlugin.

private String executePlugin(String plugin, String args, String body) throws PluginException, IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    PluginManager pm = engine.getPluginManager();
    m_evaluated = true;
    Map<String, String> argmap = pm.parseArgs(args);
    if (body != null) {
        argmap.put("_body", body);
    }
    String result = pm.execute(m_wikiContext, plugin, argmap);
    return result;
}
Also used : PluginManager(org.apache.wiki.api.engine.PluginManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 47 with WikiEngine

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

the class WeblogPlugin method execute.

/**
 *  {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    Calendar startTime;
    Calendar stopTime;
    int numDays = DEFAULT_DAYS;
    WikiEngine engine = context.getEngine();
    AuthorizationManager mgr = engine.getAuthorizationManager();
    // 
    // Parse parameters.
    // 
    String days;
    DateFormat entryFormat;
    String startDay = null;
    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) {
        SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT);
        try {
            Date d = fmt.parse(startDay);
            startTime.setTime(d);
            stopTime.setTime(d);
        } catch (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);
    StringBuilder sb = new StringBuilder();
    try {
        List<WikiPage> blogEntries = findBlogEntries(engine, weblogName, startTime.getTime(), stopTime.getTime());
        Collections.sort(blogEntries, new PageDateComparator());
        sb.append("<div class=\"weblog\">\n");
        for (Iterator<WikiPage> i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) {
            WikiPage p = i.next();
            if (mgr.checkPermission(context.getWikiSession(), new PagePermission(p, PagePermission.VIEW_ACTION))) {
                addEntryHTML(context, entryFormat, hasComments, sb, p);
            }
        }
        sb.append("</div>\n");
    } catch (ProviderException e) {
        log.error("Could not locate blog entries", e);
        throw new PluginException("Could not locate blog entries: " + e.getMessage());
    }
    return sb.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Calendar(java.util.Calendar) WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(java.text.ParseException) WikiEngine(org.apache.wiki.WikiEngine) SimpleDateFormat(java.text.SimpleDateFormat) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 48 with WikiEngine

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

the class WeblogPlugin method addEntryHTML.

/**
 *  Generates HTML for an entry.
 *
 *  @param context
 *  @param entryFormat
 *  @param hasComments  True, if comments are enabled.
 *  @param buffer       The buffer to which we add.
 *  @param entry
 *  @throws ProviderException
 */
private void addEntryHTML(WikiContext context, DateFormat entryFormat, boolean hasComments, StringBuilder buffer, WikiPage entry) throws ProviderException {
    WikiEngine engine = context.getEngine();
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    buffer.append("<div class=\"weblogentry\">\n");
    // 
    // Heading
    // 
    buffer.append("<div class=\"weblogentryheading\">\n");
    Date entryDate = entry.getLastModified();
    buffer.append(entryFormat.format(entryDate));
    buffer.append("</div>\n");
    // 
    // Append the text of the latest version.  Reset the
    // context to that page.
    // 
    WikiContext entryCtx = (WikiContext) context.clone();
    entryCtx.setPage(entry);
    String html = engine.getHTML(entryCtx, engine.getPage(entry.getName()));
    // Extract the first h1/h2/h3 as title, and replace with null
    buffer.append("<div class=\"weblogentrytitle\">\n");
    Matcher matcher = HEADINGPATTERN.matcher(html);
    if (matcher.find()) {
        String title = matcher.group(2);
        html = matcher.replaceFirst("");
        buffer.append(title);
    } else {
        buffer.append(entry.getName());
    }
    buffer.append("</div>\n");
    buffer.append("<div class=\"weblogentrybody\">\n");
    buffer.append(html);
    buffer.append("</div>\n");
    // 
    // Append footer
    // 
    buffer.append("<div class=\"weblogentryfooter\">\n");
    String author = entry.getAuthor();
    if (author != null) {
        if (engine.pageExists(author)) {
            author = "<a href=\"" + entryCtx.getURL(WikiContext.VIEW, author) + "\">" + engine.beautifyTitle(author) + "</a>";
        }
    } else {
        author = "AnonymousCoward";
    }
    buffer.append(MessageFormat.format(rb.getString("weblogentryplugin.postedby"), author));
    buffer.append("<a href=\"" + entryCtx.getURL(WikiContext.VIEW, entry.getName()) + "\">" + rb.getString("weblogentryplugin.permalink") + "</a>");
    String commentPageName = TextUtil.replaceString(entry.getName(), "blogentry", "comments");
    if (hasComments) {
        int numComments = guessNumberOfComments(engine, commentPageName);
        // 
        // We add the number of comments to the URL so that
        // the user's browsers would realize that the page
        // has changed.
        // 
        buffer.append("&nbsp;&nbsp;");
        String addcomment = rb.getString("weblogentryplugin.addcomment");
        buffer.append("<a href=\"" + entryCtx.getURL(WikiContext.COMMENT, commentPageName, "nc=" + numComments) + "\">" + MessageFormat.format(addcomment, numComments) + "</a>");
    }
    buffer.append("</div>\n");
    // 
    // Done, close
    // 
    buffer.append("</div>\n");
}
Also used : WikiContext(org.apache.wiki.WikiContext) Matcher(java.util.regex.Matcher) ResourceBundle(java.util.ResourceBundle) WikiEngine(org.apache.wiki.WikiEngine) Date(java.util.Date)

Example 49 with WikiEngine

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

the class AtomFeed method getString.

/**
 *  {@inheritDoc}
 */
@Override
public String getString() {
    Element root = getElement("feed");
    WikiEngine engine = m_wikiContext.getEngine();
    Date lastModified = new Date(0L);
    for (Iterator i = m_entries.iterator(); i.hasNext(); ) {
        Entry e = (Entry) i.next();
        if (e.getPage().getLastModified().after(lastModified))
            lastModified = e.getPage().getLastModified();
    }
    // 
    // Mandatory parts
    // 
    root.addContent(getElement("title").setText(getChannelTitle()));
    root.addContent(getElement("id").setText(getFeedID()));
    root.addContent(getElement("updated").setText(DateFormatUtils.formatUTC(lastModified, RFC3339FORMAT)));
    // 
    // Optional
    // 
    // root.addContent( getElement("author").addContent(getElement("name").setText(format())))
    root.addContent(getElement("link").setAttribute("href", engine.getBaseURL()));
    root.addContent(getElement("generator").setText("JSPWiki " + Release.VERSTR));
    String rssFeedURL = engine.getURL(WikiContext.NONE, "rss.jsp", "page=" + engine.encodeName(m_wikiContext.getPage().getName()) + "&mode=" + m_mode + "&type=atom", true);
    Element self = getElement("link").setAttribute("rel", "self");
    self.setAttribute("href", rssFeedURL);
    root.addContent(self);
    // 
    // Items
    // 
    root.addContent(getItems());
    // 
    // aaand output
    // 
    XMLOutputter output = new XMLOutputter();
    output.setFormat(Format.getPrettyFormat());
    try {
        StringWriter res = new StringWriter();
        output.output(root, res);
        return res.toString();
    } catch (IOException e) {
        return null;
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) StringWriter(java.io.StringWriter) Element(org.jdom2.Element) Iterator(java.util.Iterator) IOException(java.io.IOException) WikiEngine(org.apache.wiki.WikiEngine) Date(java.util.Date)

Example 50 with WikiEngine

use of org.apache.wiki.WikiEngine 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(WikiContext context) {
    WikiEngine engine = context.getEngine();
    String blogname = null;
    try {
        blogname = engine.getVariableManager().getValue(context, VAR_BLOGNAME);
    } catch (NoSuchVariableException e) {
    }
    if (blogname == null) {
        blogname = engine.getApplicationName() + ": " + context.getPage().getName();
    }
    return blogname;
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) 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