use of org.apache.wiki.api.core.Engine in project jspwiki by apache.
the class SessionsPlugin method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
final Engine engine = context.getEngine();
final String prop = params.get(PARAM_PROP);
if ("users".equals(prop)) {
final Principal[] principals = SessionMonitor.getInstance(engine).userPrincipals();
final StringBuilder s = new StringBuilder();
for (final Principal principal : principals) {
s.append(principal.getName()).append(", ");
}
// remove the last comma and blank :
return TextUtil.replaceEntities(s.substring(0, s.length() - (s.length() > 2 ? 2 : 0)));
}
// show each user session only once (with a counter that indicates the number of sessions for each user)
if ("distinctUsers".equals(prop)) {
final Principal[] principals = SessionMonitor.getInstance(engine).userPrincipals();
// we do not assume that the principals are sorted, so first count them :
final HashMap<String, Integer> distinctPrincipals = new HashMap<>();
for (final Principal principal : principals) {
final String principalName = principal.getName();
if (distinctPrincipals.containsKey(principalName)) {
// we already have an entry, increase the counter:
int numSessions = distinctPrincipals.get(principalName);
// store the new value:
distinctPrincipals.put(principalName, ++numSessions);
} else {
// first time we see this entry, add entry to HashMap with value 1
distinctPrincipals.put(principalName, 1);
}
}
final StringBuilder s = new StringBuilder();
for (final Map.Entry<String, Integer> entry : distinctPrincipals.entrySet()) {
s.append(entry.getKey()).append("(").append(entry.getValue().toString()).append("), ");
}
// remove the last comma and blank :
return TextUtil.replaceEntities(s.substring(0, s.length() - (s.length() > 2 ? 2 : 0)));
}
return String.valueOf(SessionMonitor.getInstance(engine).sessions());
}
use of org.apache.wiki.api.core.Engine in project jspwiki by apache.
the class WeblogArchivePlugin method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
final Engine engine = context.getEngine();
// Parameters
String weblogName = params.get(PARAM_PAGE);
if (weblogName == null) {
weblogName = context.getPage().getName();
}
final String pttrn = "'" + context.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), weblogName, "weblog.startDate='ddMMyy'&weblog.days=%d") + "'";
m_monthUrlFormat = new SimpleDateFormat(pttrn);
final StringBuilder sb = new StringBuilder();
sb.append("<div class=\"weblogarchive\">\n");
// Collect months that have blog entries
final Collection<Calendar> months = collectMonths(engine, weblogName);
int year = 0;
// Output proper HTML.
sb.append("<ul>\n");
if (months.size() > 0) {
year = (months.iterator().next()).get(Calendar.YEAR);
sb.append("<li class=\"archiveyear\">").append(year).append("</li>\n");
}
for (final Calendar cal : months) {
if (cal.get(Calendar.YEAR) != year) {
year = cal.get(Calendar.YEAR);
sb.append("<li class=\"archiveyear\">").append(year).append("</li>\n");
}
sb.append(" <li>");
sb.append(getMonthLink(cal));
sb.append("</li>\n");
}
sb.append("</ul>\n");
sb.append("</div>\n");
return sb.toString();
}
use of org.apache.wiki.api.core.Engine 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.Engine in project jspwiki by apache.
the class PluginContent method invoke.
/**
*{@inheritDoc}
*/
@Override
public String invoke(final Context context) {
String result;
final Boolean wysiwygVariable = context.getVariable(Context.VAR_WYSIWYG_EDITOR_MODE);
boolean wysiwygEditorMode = false;
if (wysiwygVariable != null) {
wysiwygEditorMode = wysiwygVariable;
}
try {
// FIXME: The plugin name matching should not be done here, but in a per-editor resource
if (wysiwygEditorMode && !m_pluginName.matches(EMITTABLE_PLUGINS)) {
result = PLUGIN_START + m_pluginName + SPACE;
// convert newlines to <br> in case the plugin has a body.
final String cmdLine = m_params.get(CMDLINE).replaceAll(LINEBREAK, ELEMENT_BR);
result = result + cmdLine + PLUGIN_END;
} else {
final Boolean b = context.getVariable(Context.VAR_EXECUTE_PLUGINS);
if (b != null && !b) {
return BLANK;
}
final Engine engine = context.getEngine();
final Map<String, String> parsedParams = new HashMap<>();
// Parse any variable instances from the string
for (final Map.Entry<String, String> e : m_params.entrySet()) {
String val = e.getValue();
val = engine.getManager(VariableManager.class).expandVariables(context, val);
parsedParams.put(e.getKey(), val);
}
final PluginManager pm = engine.getManager(PluginManager.class);
result = pm.execute(context, m_pluginName, parsedParams);
}
} catch (final Exception e) {
if (wysiwygEditorMode) {
result = "";
} else {
// log.info("Failed to execute plugin",e);
final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
result = MarkupParser.makeError(MessageFormat.format(rb.getString("plugin.error.insertionfailed"), context.getRealPage().getWiki(), context.getRealPage().getName(), e.getMessage())).getText();
}
}
return result;
}
use of org.apache.wiki.api.core.Engine in project jspwiki by apache.
the class Groups method execute.
/**
* {@inheritDoc}
*/
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
// Retrieve groups, and sort by name
final Engine engine = context.getEngine();
final GroupManager groupMgr = engine.getManager(GroupManager.class);
final Principal[] groups = groupMgr.getRoles();
Arrays.sort(groups, COMPARATOR);
final StringBuilder s = new StringBuilder();
for (int i = 0; i < groups.length; i++) {
final String name = groups[i].getName();
// Make URL
final String url = engine.getManager(URLConstructor.class).makeURL(ContextEnum.GROUP_VIEW.getRequestContext(), name, null);
// Create hyperlink
s.append("<a href=\"");
s.append(url);
s.append("\">");
s.append(name);
s.append("</a>");
// If not the last one, add a comma and space
if (i < (groups.length - 1)) {
s.append(',');
s.append(' ');
}
}
return s.toString();
}
Aggregations