Search in sources :

Example 51 with Context

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

the class MetaWeblogHandler method editPost.

/**
 *  Allows the user to edit a post.  It does not allow general editability of wiki pages, because of the limitations of the metaWeblog API.
 */
boolean editPost(final String postid, final String username, final String password, final Hashtable<String, Object> content, final boolean publish) throws XmlRpcException {
    final Engine engine = m_context.getEngine();
    log.info("metaWeblog.editPost(" + postid + ") called");
    // FIXME: Is postid correct?  Should we determine it from the page name?
    final Page page = engine.getManager(PageManager.class).getPage(postid);
    checkPermissions(page, username, password, "edit");
    try {
        final Page entryPage = page.clone();
        entryPage.setAuthor(username);
        final Context context = Wiki.context().create(engine, entryPage);
        final StringBuilder text = new StringBuilder();
        text.append("!").append(content.get("title"));
        text.append("\n\n");
        text.append(content.get("description"));
        log.debug("Updating entry: " + text);
        engine.getManager(PageManager.class).saveText(context, text.toString());
    } catch (final Exception e) {
        log.error("Failed to create weblog entry", e);
        throw new XmlRpcException(0, "Failed to update weblog entry: " + e.getMessage());
    }
    return true;
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) Engine(org.apache.wiki.api.core.Engine) XmlRpcException(org.apache.xmlrpc.XmlRpcException) WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 52 with Context

use of org.apache.wiki.api.core.Context 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(final Context context, final DateFormat entryFormat, final boolean hasComments, final StringBuilder buffer, final Page entry, final Map<String, String> params) {
    final Engine engine = context.getEngine();
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    buffer.append("<div class=\"weblogentry\">\n");
    // 
    // Heading
    // 
    buffer.append("<div class=\"weblogentryheading\">\n");
    final Date entryDate = entry.getLastModified();
    buffer.append(entryFormat != null ? entryFormat.format(entryDate) : entryDate);
    buffer.append("</div>\n");
    // 
    // Append the text of the latest version.  Reset the context to that page.
    // 
    final Context entryCtx = context.clone();
    entryCtx.setPage(entry);
    String html = engine.getManager(RenderingManager.class).getHTML(entryCtx, engine.getManager(PageManager.class).getPage(entry.getName()));
    // Extract the first h1/h2/h3 as title, and replace with null
    buffer.append("<div class=\"weblogentrytitle\">\n");
    final Matcher matcher = HEADINGPATTERN.matcher(html);
    if (matcher.find()) {
        final 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");
    final int preview = TextUtil.parseIntParameter(params.get(PARAM_PREVIEW), 0);
    if (preview > 0) {
        // 
        // We start with the first 'preview' number of characters from the text,
        // and then add characters to it until we get to a linebreak.
        // The idea is that cutting off at a linebreak is less likely
        // to disturb the HTML and leave us with garbled output.
        // 
        boolean hasBeenCutOff = false;
        int cutoff = Math.min(preview, html.length());
        while (cutoff < html.length()) {
            if (html.charAt(cutoff) == '\r' || html.charAt(cutoff) == '\n') {
                hasBeenCutOff = true;
                break;
            }
            cutoff++;
        }
        buffer.append(html, 0, cutoff);
        if (hasBeenCutOff) {
            buffer.append(" <a href=\"").append(entryCtx.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), entry.getName())).append("\">").append(rb.getString("weblogentryplugin.more")).append("</a>\n");
        }
    } else {
        buffer.append(html);
    }
    buffer.append("</div>\n");
    // 
    // Append footer
    // 
    buffer.append("<div class=\"weblogentryfooter\">\n");
    String author = entry.getAuthor();
    if (author != null) {
        if (engine.getManager(PageManager.class).wikiPageExists(author)) {
            author = "<a href=\"" + entryCtx.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), author) + "\">" + engine.getManager(RenderingManager.class).beautifyTitle(author) + "</a>";
        }
    } else {
        author = "AnonymousCoward";
    }
    buffer.append(MessageFormat.format(rb.getString("weblogentryplugin.postedby"), author));
    buffer.append("<a href=\"").append(entryCtx.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), entry.getName())).append("\">").append(rb.getString("weblogentryplugin.permalink")).append("</a>");
    final String commentPageName = TextUtil.replaceString(entry.getName(), "blogentry", "comments");
    if (hasComments) {
        final 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;");
        final String addcomment = rb.getString("weblogentryplugin.addcomment");
        buffer.append("<a href=\"").append(entryCtx.getURL(ContextEnum.PAGE_COMMENT.getRequestContext(), commentPageName, "nc=" + numComments)).append("\">").append(MessageFormat.format(addcomment, numComments)).append("</a>");
    }
    buffer.append("</div>\n");
    // Done, close
    buffer.append("</div>\n");
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) Matcher(java.util.regex.Matcher) ResourceBundle(java.util.ResourceBundle) Engine(org.apache.wiki.api.core.Engine) Date(java.util.Date)

Example 53 with Context

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

the class BugReportHandler method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final String title = params.get(PARAM_TITLE);
    String description = params.get(PARAM_DESCRIPTION);
    String version = params.get(PARAM_VERSION);
    String submitter = null;
    final SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATEFORMAT);
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    final Principal wup = context.getCurrentUser();
    if (wup != null) {
        submitter = wup.getName();
    }
    if (title == null) {
        throw new PluginException(rb.getString("bugreporthandler.titlerequired"));
    }
    if (title.isEmpty()) {
        return "";
    }
    if (description == null) {
        description = "";
    }
    if (version == null) {
        version = "unknown";
    }
    final Properties mappings = parseMappings(params.get(PARAM_MAPPINGS));
    // Start things
    try {
        final StringWriter str = new StringWriter();
        final PrintWriter out = new PrintWriter(str);
        final Date d = new Date();
        // Outputting of basic data
        out.println("|" + mappings.getProperty(PARAM_TITLE, "Title") + "|" + title);
        out.println("|" + mappings.getProperty("date", "Date") + "|" + format.format(d));
        out.println("|" + mappings.getProperty(PARAM_VERSION, "Version") + "|" + version);
        if (submitter != null) {
            out.println("|" + mappings.getProperty("submitter", "Submitter") + "|" + submitter);
        }
        // Outputting the other parameters added to this.
        for (final Map.Entry<String, String> entry : params.entrySet()) {
            if (!(entry.getKey().equals(PARAM_TITLE) || entry.getKey().equals(PARAM_DESCRIPTION) || entry.getKey().equals(PARAM_VERSION) || entry.getKey().equals(PARAM_MAPPINGS) || entry.getKey().equals(PARAM_PAGE) || entry.getKey().startsWith("_"))) {
                // If no mapping has been defined, just ignore it.
                final String head = mappings.getProperty(entry.getKey(), entry.getKey());
                if (!head.isEmpty()) {
                    out.println("|" + head + "|" + entry.getValue());
                }
            }
        }
        out.println();
        out.println(description);
        out.close();
        // Now create a new page for this bug report
        final String pageName = findNextPage(context, title, params.get(PARAM_PAGE));
        final Page newPage = Wiki.contents().page(context.getEngine(), pageName);
        final Context newContext = context.clone();
        newContext.setPage(newPage);
        context.getEngine().getManager(PageManager.class).saveText(newContext, str.toString());
        final MessageFormat formatter = new MessageFormat("");
        formatter.applyPattern(rb.getString("bugreporthandler.new"));
        final String[] args = { "<a href=\"" + context.getViewURL(pageName) + "\">" + pageName + "</a>" };
        return formatter.format(args);
    } catch (final RedirectException e) {
        log.info("Saving not allowed, reason: '" + e.getMessage() + "', can't redirect to " + e.getRedirect());
        throw new PluginException("Saving not allowed, reason: " + e.getMessage());
    } catch (final WikiException e) {
        log.error("Unable to save page!", e);
        return rb.getString("bugreporthandler.unable");
    }
}
Also used : Context(org.apache.wiki.api.core.Context) WikiException(org.apache.wiki.api.exceptions.WikiException) MessageFormat(java.text.MessageFormat) PluginException(org.apache.wiki.api.exceptions.PluginException) Page(org.apache.wiki.api.core.Page) Properties(java.util.Properties) Date(java.util.Date) PageManager(org.apache.wiki.pages.PageManager) StringWriter(java.io.StringWriter) ResourceBundle(java.util.ResourceBundle) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) Principal(java.security.Principal) PrintWriter(java.io.PrintWriter) RedirectException(org.apache.wiki.api.exceptions.RedirectException)

Example 54 with Context

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

the class CachingProvider method refreshMetadata.

// FIXME: Kludge: make sure that the page is also parsed and it gets all the necessary variables.
private void refreshMetadata(final Page page) {
    if (page != null && !page.hasMetadata()) {
        final RenderingManager mgr = m_engine.getManager(RenderingManager.class);
        try {
            final String data = m_provider.getPageText(page.getName(), page.getVersion());
            final Context ctx = Wiki.context().create(m_engine, page);
            final MarkupParser parser = mgr.getParser(ctx, data);
            parser.parse();
        } catch (final Exception ex) {
            LOG.debug("Failed to retrieve variables for wikipage {}", page);
        }
    }
}
Also used : Context(org.apache.wiki.api.core.Context) RenderingManager(org.apache.wiki.render.RenderingManager) NoSuchElementException(java.util.NoSuchElementException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) IOException(java.io.IOException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 55 with Context

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

the class VersioningFileProviderTest method testChangeNote.

@Test
public void testChangeNote() throws Exception {
    final Page p = Wiki.contents().page(engine, NAME1);
    p.setAttribute(Page.CHANGENOTE, "Test change");
    final Context context = Wiki.context().create(engine, p);
    engine.getManager(PageManager.class).saveText(context, "test");
    final Page p2 = engine.getManager(PageManager.class).getPage(NAME1);
    Assertions.assertEquals("Test change", p2.getAttribute(Page.CHANGENOTE));
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) WikiPage(org.apache.wiki.WikiPage) Page(org.apache.wiki.api.core.Page) Test(org.junit.jupiter.api.Test)

Aggregations

Context (org.apache.wiki.api.core.Context)81 Page (org.apache.wiki.api.core.Page)46 PageManager (org.apache.wiki.pages.PageManager)42 Test (org.junit.jupiter.api.Test)40 RenderingManager (org.apache.wiki.render.RenderingManager)15 PageContext (javax.servlet.jsp.PageContext)11 Engine (org.apache.wiki.api.core.Engine)9 ReferenceManager (org.apache.wiki.references.ReferenceManager)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 ProviderException (org.apache.wiki.api.exceptions.ProviderException)6 WikiContext (org.apache.wiki.WikiContext)5 StringReader (java.io.StringReader)4 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiSessionTest (org.apache.wiki.WikiSessionTest)4 Attachment (org.apache.wiki.api.core.Attachment)4 SearchResult (org.apache.wiki.api.search.SearchResult)4