Search in sources :

Example 36 with WikiEngine

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

the class PageRenamer 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.
 */
public String renamePage(final WikiContext context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
    // 
    if (renameFrom == null || renameFrom.length() == 0) {
        throw new WikiException("From name may not be null or empty");
    }
    if (renameTo == null || renameTo.length() == 0) {
        throw new WikiException("To name may not be null or empty");
    }
    // 
    // Clean up the "to" -name so that it does not contain anything illegal
    // 
    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.
    // 
    WikiEngine engine = context.getEngine();
    WikiPage fromPage = engine.getPage(renameFrom);
    if (fromPage == null) {
        throw new WikiException("No such page " + renameFrom);
    }
    WikiPage toPage = engine.getPage(renameToClean);
    if (toPage != null) {
        throw new WikiException("Page already exists " + renameToClean);
    }
    // 
    // Options
    // 
    m_camelCase = TextUtil.getBooleanProperty(engine.getWikiProperties(), JSPWikiMarkupParser.PROP_CAMELCASELINKS, m_camelCase);
    Set<String> referrers = getReferencesToChange(fromPage, engine);
    // 
    // Do the actual rename by changing from the frompage to the topage, including
    // all of the attachments
    // 
    // Remove references to attachments under old name
    @SuppressWarnings("unchecked") Collection<Attachment> attachmentsOldName = engine.getAttachmentManager().listAttachments(fromPage);
    for (Attachment att : attachmentsOldName) {
        WikiPage fromAttPage = engine.getPage(att.getName());
        engine.getReferenceManager().pageRemoved(fromAttPage);
    }
    engine.getPageManager().getProvider().movePage(renameFrom, renameToClean);
    if (engine.getAttachmentManager().attachmentsEnabled()) {
        engine.getAttachmentManager().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.getPage(renameToClean);
    if (toPage == null)
        throw new InternalWikiException("Rename seems to have failed for some strange reason - please check logs!");
    toPage.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
    toPage.setAuthor(context.getCurrentUser().getName());
    engine.getPageManager().putPageText(toPage, engine.getPureText(toPage));
    // 
    // Update the references
    // 
    engine.getReferenceManager().pageRemoved(fromPage);
    engine.updateReferences(toPage);
    // 
    if (changeReferrers) {
        updateReferrers(context, fromPage, toPage, referrers);
    }
    // 
    // re-index the page including its attachments
    // 
    engine.getSearchManager().reindexPage(toPage);
    @SuppressWarnings("unchecked") Collection<Attachment> attachmentsNewName = engine.getAttachmentManager().listAttachments(toPage);
    for (Attachment att : attachmentsNewName) {
        WikiPage toAttPage = engine.getPage(att.getName());
        // add reference to attachment under new page name
        engine.updateReferences(toAttPage);
        engine.getSearchManager().reindexPage(att);
    }
    // Currently not used internally by JSPWiki itself, but you can use it for something else.
    WikiEventManager.fireEvent(this, new WikiPageRenameEvent(this, renameFrom, renameToClean));
    // 
    return renameToClean;
}
Also used : InternalWikiException(org.apache.wiki.InternalWikiException) WikiException(org.apache.wiki.api.exceptions.WikiException) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine) InternalWikiException(org.apache.wiki.InternalWikiException) WikiPageRenameEvent(org.apache.wiki.event.WikiPageRenameEvent)

Example 37 with WikiEngine

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

the class PageRenamer method updateReferrers.

/**
 *  This method finds all the pages which have anything to do with the fromPage and
 *  change any referrers it can figure out in that page.
 *
 *  @param context WikiContext in which we operate
 *  @param fromPage The old page
 *  @param toPage The new page
 */
private void updateReferrers(WikiContext context, WikiPage fromPage, WikiPage toPage, Set<String> referrers) {
    WikiEngine engine = context.getEngine();
    // No referrers
    if (referrers.isEmpty())
        return;
    for (String pageName : referrers) {
        // small kludge.
        if (pageName.equals(fromPage.getName())) {
            pageName = toPage.getName();
        }
        WikiPage p = engine.getPage(pageName);
        String sourceText = engine.getPureText(p);
        String newText = replaceReferrerString(context, sourceText, fromPage.getName(), toPage.getName());
        if (m_camelCase)
            newText = replaceCCReferrerString(context, newText, fromPage.getName(), toPage.getName());
        if (!sourceText.equals(newText)) {
            p.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName());
            p.setAuthor(context.getCurrentUser().getName());
            try {
                engine.getPageManager().putPageText(p, newText);
                engine.updateReferences(p);
            } catch (ProviderException e) {
                // 
                // We fail with an error, but we will try to continue to rename
                // other referrers as well.
                // 
                log.error("Unable to perform rename.", e);
            }
        }
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) WikiEngine(org.apache.wiki.WikiEngine)

Example 38 with WikiEngine

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

the class CookieAuthenticationLoginModule method login.

/**
 * @see javax.security.auth.spi.LoginModule#login()
 *
 * {@inheritDoc}
 */
public boolean login() throws LoginException {
    // Otherwise, let's go and look for the cookie!
    HttpRequestCallback hcb = new HttpRequestCallback();
    // UserDatabaseCallback ucb = new UserDatabaseCallback();
    WikiEngineCallback wcb = new WikiEngineCallback();
    Callback[] callbacks = new Callback[] { hcb, wcb };
    try {
        m_handler.handle(callbacks);
        HttpServletRequest request = hcb.getRequest();
        String uid = getLoginCookie(request);
        if (uid != null) {
            WikiEngine engine = wcb.getEngine();
            File cookieFile = getCookieFile(engine, uid);
            if (cookieFile != null && cookieFile.exists() && cookieFile.canRead()) {
                Reader in = null;
                try {
                    in = new BufferedReader(new InputStreamReader(new FileInputStream(cookieFile), "UTF-8"));
                    String username = FileUtil.readContents(in);
                    if (log.isDebugEnabled()) {
                        log.debug("Logged in cookie authenticated name=" + username);
                    }
                    // If login succeeds, commit these principals/roles
                    m_principals.add(new WikiPrincipal(username, WikiPrincipal.LOGIN_NAME));
                    // 
                    // Tag the file so that we know that it has been accessed recently.
                    // 
                    cookieFile.setLastModified(System.currentTimeMillis());
                    return true;
                } catch (IOException e) {
                    return false;
                } finally {
                    if (in != null)
                        in.close();
                }
            }
        }
    } catch (IOException e) {
        String message = "IO exception; disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    } catch (UnsupportedCallbackException e) {
        String message = "Unable to handle callback; disallowing login.";
        log.error(message, e);
        throw new LoginException(message);
    }
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Callback(javax.security.auth.callback.Callback) WikiPrincipal(org.apache.wiki.auth.WikiPrincipal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) WikiEngine(org.apache.wiki.WikiEngine)

Example 39 with WikiEngine

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

the class SpamFilter method getChange.

/**
 *  Creates a simple text string describing the added content.
 *
 *  @param context
 *  @param newText
 *  @return Empty string, if there is no change.
 */
private static Change getChange(WikiContext context, String newText) {
    WikiPage page = context.getPage();
    StringBuffer change = new StringBuffer();
    WikiEngine engine = context.getEngine();
    // Get current page version
    Change ch = new Change();
    try {
        String oldText = engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION);
        String[] first = Diff.stringToArray(oldText);
        String[] second = Diff.stringToArray(newText);
        Revision rev = Diff.diff(first, second, new MyersDiff());
        if (rev == null || rev.size() == 0) {
            return ch;
        }
        for (int i = 0; i < rev.size(); i++) {
            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 (DifferentiationFailedException e) {
        log.error("Diff failed", e);
    }
    // 
    // Don't forget to include the change note, too
    // 
    String changeNote = (String) page.getAttribute(WikiPage.CHANGENOTE);
    if (changeNote != null) {
        change.append("\r\n");
        change.append(changeNote);
    }
    // 
    if (page.getAuthor() != null) {
        change.append("\r\n" + 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) WikiPage(org.apache.wiki.WikiPage) MyersDiff(org.suigeneris.jrcs.diff.myers.MyersDiff) 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) WikiEngine(org.apache.wiki.WikiEngine)

Example 40 with WikiEngine

use of org.apache.wiki.WikiEngine 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 final String insertInputFields(PageContext pageContext) {
    WikiContext ctx = WikiContext.findContext(pageContext);
    WikiEngine engine = ctx.getEngine();
    StringBuilder sb = new StringBuilder();
    if (engine.getContentEncoding().equals("UTF-8")) {
        sb.append("<input name='encodingcheck' type='hidden' value='\u3041' />\n");
    }
    return sb.toString();
}
Also used : WikiContext(org.apache.wiki.WikiContext) 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