Search in sources :

Example 1 with LinkedResourceHelper

use of com.xpn.xwiki.internal.render.LinkedResourceHelper in project xwiki-platform by xwiki.

the class XWikiDocument method rename.

/**
 * Same as {@link #rename(DocumentReference, List, XWikiContext)} but the list of documents having the current
 * document as their parent is passed in parameter.
 *
 * @param newDocumentReference the new document reference
 * @param backlinkDocumentReferences the list of references of documents to parse and for which links will be
 *            modified to point to the new document reference
 * @param childDocumentReferences the list of references of document whose parent field will be set to the new
 *            document reference
 * @param context the ubiquitous XWiki Context
 * @throws XWikiException in case of an error
 * @since 2.2M2
 */
public void rename(DocumentReference newDocumentReference, List<DocumentReference> backlinkDocumentReferences, List<DocumentReference> childDocumentReferences, XWikiContext context) throws XWikiException {
    // If the user is trying to rename to the same name... In that case, simply exits for efficiency.
    if (isNew() || getDocumentReference().equals(newDocumentReference)) {
        return;
    }
    // Grab the xwiki object, it gets used a few times.
    XWiki xwiki = context.getWiki();
    // Step 1: Copy the document and all its translations under a new document with the new reference.
    xwiki.copyDocument(getDocumentReference(), newDocumentReference, false, context);
    // Step 2: For each child document, update its parent reference.
    if (childDocumentReferences != null) {
        for (DocumentReference childDocumentReference : childDocumentReferences) {
            XWikiDocument childDocument = xwiki.getDocument(childDocumentReference, context);
            String compactReference = getCompactEntityReferenceSerializer().serialize(newDocumentReference);
            childDocument.setParent(compactReference);
            String saveMessage = localizePlainOrKey("core.comment.renameParent", compactReference);
            childDocument.setAuthorReference(context.getUserReference());
            xwiki.saveDocument(childDocument, saveMessage, true, context);
        }
    }
    // Step 3: For each backlink to rename, parse the backlink document and replace the links with the new name.
    for (DocumentReference backlinkDocumentReference : backlinkDocumentReferences) {
        XWikiDocument backlinkRootDocument = xwiki.getDocument(backlinkDocumentReference, context);
        // Update default locale instance
        renameLinks(backlinkRootDocument, getDocumentReference(), newDocumentReference, context);
        // Update translations
        for (Locale locale : backlinkRootDocument.getTranslationLocales(context)) {
            XWikiDocument backlinkDocument = backlinkRootDocument.getTranslatedDocument(locale, context);
            renameLinks(backlinkDocument, getDocumentReference(), newDocumentReference, context);
        }
    }
    // Get new document
    XWikiDocument newDocument = xwiki.getDocument(newDocumentReference, context);
    // document's location.
    if (Utils.getContextComponentManager().hasComponent(BlockRenderer.class, getSyntax().toIdString())) {
        // Only support syntax for which a renderer is provided
        LinkedResourceHelper linkedResourceHelper = Utils.getComponent(LinkedResourceHelper.class);
        DocumentReference oldDocumentReference = getDocumentReference();
        XDOM newDocumentXDOM = newDocument.getXDOM();
        List<Block> blocks = linkedResourceHelper.getBlocks(newDocumentXDOM);
        // FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#updateRelativeLinks in
        // xwiki-platform-refactoring-default
        boolean modified = false;
        for (Block block : blocks) {
            ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
            if (resourceReference == null) {
                // Skip invalid blocks.
                continue;
            }
            ResourceType resourceType = resourceReference.getType();
            // TODO: support ATTACHMENT as well.
            if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
                // We are currently only interested in Document or Space references.
                continue;
            }
            // current link, use the old document's reference to fill in blanks.
            EntityReference oldLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, oldDocumentReference);
            // new link, use the new document's reference to fill in blanks.
            EntityReference newLinkReference = getResourceReferenceEntityReferenceResolver().resolve(resourceReference, null, newDocumentReference);
            // If the new and old link references don`t match, then we must update the relative link.
            if (!newLinkReference.equals(oldLinkReference)) {
                modified = true;
                // Serialize the old (original) link relative to the new document's location, in compact form.
                String serializedLinkReference = getCompactWikiEntityReferenceSerializer().serialize(oldLinkReference, newDocumentReference);
                // Update the reference in the XDOM.
                linkedResourceHelper.setResourceReferenceString(block, serializedLinkReference);
            }
        }
        // Set the new content and save document if needed
        if (modified) {
            newDocument.setContent(newDocumentXDOM);
            newDocument.setAuthorReference(context.getUserReference());
            xwiki.saveDocument(newDocument, context);
        }
    }
    // Step 5: Delete the old document
    xwiki.deleteDocument(this, context);
    // Step 6: The current document needs to point to the renamed document as otherwise it's pointing to an
    // invalid XWikiDocument object as it's been deleted...
    clone(newDocument);
}
Also used : Locale(java.util.Locale) XDOM(org.xwiki.rendering.block.XDOM) XWiki(com.xpn.xwiki.XWiki) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ToString(org.suigeneris.jrcs.util.ToString) LinkedResourceHelper(com.xpn.xwiki.internal.render.LinkedResourceHelper) EntityReference(org.xwiki.model.reference.EntityReference) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Aggregations

XWiki (com.xpn.xwiki.XWiki)1 LinkedResourceHelper (com.xpn.xwiki.internal.render.LinkedResourceHelper)1 Locale (java.util.Locale)1 ToString (org.suigeneris.jrcs.util.ToString)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 EntityReference (org.xwiki.model.reference.EntityReference)1 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)1 Block (org.xwiki.rendering.block.Block)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 LinkBlock (org.xwiki.rendering.block.LinkBlock)1 MacroBlock (org.xwiki.rendering.block.MacroBlock)1 SectionBlock (org.xwiki.rendering.block.SectionBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)1 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)1