Search in sources :

Example 1 with WikiPageRenameEvent

use of org.apache.wiki.event.WikiPageRenameEvent 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)

Aggregations

InternalWikiException (org.apache.wiki.InternalWikiException)1 WikiEngine (org.apache.wiki.WikiEngine)1 WikiPage (org.apache.wiki.WikiPage)1 WikiException (org.apache.wiki.api.exceptions.WikiException)1 Attachment (org.apache.wiki.attachment.Attachment)1 WikiPageRenameEvent (org.apache.wiki.event.WikiPageRenameEvent)1