Search in sources :

Example 31 with Engine

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

the class DefaultPageRenamer method renamePage.

/**
 *  Renames a page.
 *
 *  @param context The current context.
 *  @param renameFrom The name from which to rename.
 *  @param renameTo The new name.
 *  @param changeReferrers If true, also changes all the referrers.
 *  @return The final new name (in case it had to be modified)
 *  @throws WikiException If the page cannot be renamed.
 */
@Override
public String renamePage(final Context context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
    // Sanity checks first
    if (renameFrom == null || renameFrom.isEmpty()) {
        throw new WikiException("From name may not be null or empty");
    }
    if (renameTo == null || renameTo.isEmpty()) {
        throw new WikiException("To name may not be null or empty");
    }
    // Clean up the "to" -name so that it does not contain anything illegal
    final String renameToClean = MarkupParser.cleanLink(renameTo.trim());
    if (renameToClean.equals(renameFrom)) {
        throw new WikiException("You cannot rename the page to itself");
    }
    // Preconditions: "from" page must exist, and "to" page must not yet exist.
    final Engine engine = context.getEngine();
    final Page fromPage = engine.getManager(PageManager.class).getPage(renameFrom);
    if (fromPage == null) {
        throw new WikiException("No such page " + renameFrom);
    }
    Page toPage = engine.getManager(PageManager.class).getPage(renameToClean);
    if (toPage != null) {
        throw new WikiException("Page already exists " + renameToClean);
    }
    final Set<String> referrers = getReferencesToChange(fromPage, engine);
    // Do the actual rename by changing from the frompage to the topage, including all the attachments
    // Remove references to attachments under old name
    final List<Attachment> attachmentsOldName = engine.getManager(AttachmentManager.class).listAttachments(fromPage);
    for (final Attachment att : attachmentsOldName) {
        final Page fromAttPage = engine.getManager(PageManager.class).getPage(att.getName());
        engine.getManager(ReferenceManager.class).pageRemoved(fromAttPage);
    }
    engine.getManager(PageManager.class).getProvider().movePage(renameFrom, renameToClean);
    if (engine.getManager(AttachmentManager.class).attachmentsEnabled()) {
        engine.getManager(AttachmentManager.class).getCurrentProvider().moveAttachmentsForPage(renameFrom, renameToClean);
    }
    // Add a comment to the page notifying what changed.  This adds a new revision to the repo with no actual change.
    toPage = engine.getManager(PageManager.class).getPage(renameToClean);
    if (toPage == null) {
        throw new ProviderException("Rename seems to have failed for some strange reason - please check logs!");
    }
    toPage.setAttribute(Page.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
    toPage.setAuthor(context.getCurrentUser().getName());
    engine.getManager(PageManager.class).putPageText(toPage, engine.getManager(PageManager.class).getPureText(toPage));
    // Update the references
    engine.getManager(ReferenceManager.class).pageRemoved(fromPage);
    engine.getManager(ReferenceManager.class).updateReferences(toPage);
    // Update referrers
    if (changeReferrers) {
        updateReferrers(context, fromPage, toPage, referrers);
    }
    // re-index the page including its attachments
    engine.getManager(SearchManager.class).reindexPage(toPage);
    final Collection<Attachment> attachmentsNewName = engine.getManager(AttachmentManager.class).listAttachments(toPage);
    for (final Attachment att : attachmentsNewName) {
        final Page toAttPage = engine.getManager(PageManager.class).getPage(att.getName());
        // add reference to attachment under new page name
        engine.getManager(ReferenceManager.class).updateReferences(toAttPage);
        engine.getManager(SearchManager.class).reindexPage(att);
    }
    firePageRenameEvent(renameFrom, renameToClean);
    // Done, return the new name.
    return renameToClean;
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) SearchManager(org.apache.wiki.search.SearchManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) ReferenceManager(org.apache.wiki.references.ReferenceManager) PageManager(org.apache.wiki.pages.PageManager) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Engine(org.apache.wiki.api.core.Engine)

Example 32 with Engine

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

the class SpamFilter method insertInputFields.

/**
 * This helper method adds all the input fields to your editor that the SpamFilter requires
 * to check for spam.  This <i>must</i> be in your editor form if you intend to use the SpamFilter.
 *
 * @param pageContext The PageContext
 * @return A HTML string which contains input fields for the SpamFilter.
 */
public static String insertInputFields(final PageContext pageContext) {
    final Context ctx = Context.findContext(pageContext);
    final Engine engine = ctx.getEngine();
    final StringBuilder sb = new StringBuilder();
    if (engine.getContentEncoding().equals(StandardCharsets.UTF_8)) {
        sb.append("<input name='encodingcheck' type='hidden' value='\u3041' />\n");
    }
    return sb.toString();
}
Also used : Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) Engine(org.apache.wiki.api.core.Engine)

Example 33 with Engine

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

the class SpamFilter method getChange.

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context page context
 *  @param newText added content
 *  @return Empty string, if there is no change.
 */
private static Change getChange(final Context context, final String newText) {
    final Page page = context.getPage();
    final StringBuffer change = new StringBuffer();
    final Engine engine = context.getEngine();
    // Get current page version
    final Change ch = new Change();
    try {
        final String oldText = engine.getManager(PageManager.class).getPureText(page.getName(), WikiProvider.LATEST_VERSION);
        final String[] first = Diff.stringToArray(oldText);
        final String[] second = Diff.stringToArray(newText);
        final Revision rev = Diff.diff(first, second, new MyersDiff());
        if (rev == null || rev.size() == 0) {
            return ch;
        }
        for (int i = 0; i < rev.size(); i++) {
            final Delta d = rev.getDelta(i);
            if (d instanceof AddDelta) {
                d.getRevised().toString(change, "", "\r\n");
                ch.m_adds++;
            } else if (d instanceof ChangeDelta) {
                d.getRevised().toString(change, "", "\r\n");
                ch.m_adds++;
            } else if (d instanceof DeleteDelta) {
                ch.m_removals++;
            }
        }
    } catch (final DifferentiationFailedException e) {
        log.error("Diff failed", e);
    }
    // Don't forget to include the change note, too
    final String changeNote = page.getAttribute(Page.CHANGENOTE);
    if (changeNote != null) {
        change.append("\r\n");
        change.append(changeNote);
    }
    // And author as well
    if (page.getAuthor() != null) {
        change.append("\r\n").append(page.getAuthor());
    }
    ch.m_change = change.toString();
    return ch;
}
Also used : ChangeDelta(org.suigeneris.jrcs.diff.delta.ChangeDelta) DifferentiationFailedException(org.suigeneris.jrcs.diff.DifferentiationFailedException) DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) MyersDiff(org.suigeneris.jrcs.diff.myers.MyersDiff) Page(org.apache.wiki.api.core.Page) PageManager(org.apache.wiki.pages.PageManager) Revision(org.suigeneris.jrcs.diff.Revision) DeleteDelta(org.suigeneris.jrcs.diff.delta.DeleteDelta) Delta(org.suigeneris.jrcs.diff.delta.Delta) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta) ChangeDelta(org.suigeneris.jrcs.diff.delta.ChangeDelta) AddDelta(org.suigeneris.jrcs.diff.delta.AddDelta) Engine(org.apache.wiki.api.core.Engine)

Example 34 with Engine

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

the class PluginTag method executePlugin.

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

Example 35 with Engine

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

the class UserProfileTag method doWikiStartTag.

@Override
public final int doWikiStartTag() throws IOException {
    final UserManager manager = m_wikiContext.getEngine().getManager(UserManager.class);
    final UserProfile profile = manager.getUserProfile(m_wikiContext.getWikiSession());
    String result = null;
    if (EXISTS.equals(m_prop) || NOT_NEW.equals(m_prop)) {
        return profile.isNew() ? SKIP_BODY : EVAL_BODY_INCLUDE;
    } else if (NEW.equals(m_prop) || NOT_EXISTS.equals(m_prop)) {
        return profile.isNew() ? EVAL_BODY_INCLUDE : SKIP_BODY;
    } else if (CREATED.equals(m_prop) && profile.getCreated() != null) {
        result = profile.getCreated().toString();
    } else if (EMAIL.equals(m_prop)) {
        result = profile.getEmail();
    } else if (FULLNAME.equals(m_prop)) {
        result = profile.getFullname();
    } else if (GROUPS.equals(m_prop)) {
        result = printGroups(m_wikiContext);
    } else if (LOGINNAME.equals(m_prop)) {
        result = profile.getLoginName();
    } else if (MODIFIED.equals(m_prop) && profile.getLastModified() != null) {
        result = profile.getLastModified().toString();
    } else if (ROLES.equals(m_prop)) {
        result = printRoles(m_wikiContext);
    } else if (WIKINAME.equals(m_prop)) {
        result = profile.getWikiName();
        if (result == null) {
            // 
            // Default back to the declared user name
            // 
            final Engine engine = this.m_wikiContext.getEngine();
            final Session wikiSession = Wiki.session().find(engine, (HttpServletRequest) pageContext.getRequest());
            final Principal user = wikiSession.getUserPrincipal();
            if (user != null) {
                result = user.getName();
            }
        }
    } else if (CHANGE_PASSWORD.equals(m_prop) || CHANGE_LOGIN_NAME.equals(m_prop)) {
        final AuthenticationManager authMgr = m_wikiContext.getEngine().getManager(AuthenticationManager.class);
        if (!authMgr.isContainerAuthenticated()) {
            return EVAL_BODY_INCLUDE;
        }
    } else if (NOT_CHANGE_PASSWORD.equals(m_prop) || NOT_CHANGE_LOGIN_NAME.equals(m_prop)) {
        final AuthenticationManager authMgr = m_wikiContext.getEngine().getManager(AuthenticationManager.class);
        if (authMgr.isContainerAuthenticated()) {
            return EVAL_BODY_INCLUDE;
        }
    }
    if (result != null) {
        pageContext.getOut().print(TextUtil.replaceEntities(result));
    }
    return SKIP_BODY;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationManager(org.apache.wiki.auth.AuthenticationManager) UserProfile(org.apache.wiki.auth.user.UserProfile) UserManager(org.apache.wiki.auth.UserManager) Engine(org.apache.wiki.api.core.Engine) Principal(java.security.Principal) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) Session(org.apache.wiki.api.core.Session)

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