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;
}
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(" ");
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");
}
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");
}
}
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);
}
}
}
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));
}
Aggregations