Search in sources :

Example 6 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class DefaultLinkRefactoring method updateRelativeLinks.

private void updateRelativeLinks(XWikiDocument document, DocumentReference oldDocumentReference) throws XWikiException {
    // We support only the syntaxes for which there is an available renderer.
    if (!this.contextComponentManagerProvider.get().hasComponent(BlockRenderer.class, document.getSyntax().toIdString())) {
        this.logger.warn("We can't update the relative links from [{}]" + " because there is no renderer available for its syntax [{}].", document.getDocumentReference(), document.getSyntax());
        return;
    }
    DocumentReference newDocumentReference = document.getDocumentReference();
    XDOM xdom = document.getXDOM();
    List<Block> blocks = linkedResourceHelper.getBlocks(xdom);
    boolean modified = false;
    for (Block block : blocks) {
        ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
        if (resourceReference == null || StringUtils.isEmpty(resourceReference.getReference())) {
            // 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 = this.resourceReferenceResolver.resolve(resourceReference, null, oldDocumentReference);
        // new link, use the new document's reference to fill in blanks.
        EntityReference newLinkReference = this.resourceReferenceResolver.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 = this.compactEntityReferenceSerializer.serialize(oldLinkReference, newDocumentReference);
            // Update the reference in the XDOM.
            linkedResourceHelper.setResourceReferenceString(block, serializedLinkReference);
        }
    }
    if (modified) {
        document.setContent(xdom);
        saveDocumentPreservingContentAuthor(document, "Updated the relative links.", true);
        this.logger.info("Updated the relative links from [{}].", document.getDocumentReference());
    } else {
        this.logger.info("No relative links to update in [{}].", document.getDocumentReference());
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) EntityReference(org.xwiki.model.reference.EntityReference) Block(org.xwiki.rendering.block.Block) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 7 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class DefaultDocumentSplitter method updateAnchors.

/**
 * @param document the document whose anchors to update
 * @param fragments see {@link #collectDocumentFragments(List)}
 */
private void updateAnchors(WikiDocument document, Map<String, String> fragments) {
    for (LinkBlock linkBlock : document.getXdom().<LinkBlock>getBlocks(new ClassBlockMatcher(LinkBlock.class), Axes.DESCENDANT)) {
        ResourceReference reference = linkBlock.getReference();
        ResourceType resoureceType = reference.getType();
        String fragment = null;
        if ((ResourceType.DOCUMENT.equals(resoureceType) || ResourceType.SPACE.equals(resoureceType)) && StringUtils.isEmpty(reference.getReference())) {
            fragment = reference.getParameter(ANCHOR_PARAMETER);
        } else if (StringUtils.startsWith(reference.getReference(), "#") && (ResourceType.PATH.equals(resoureceType) || ResourceType.URL.equals(resoureceType))) {
            fragment = reference.getReference().substring(1);
        }
        String targetDocument = fragments.get(fragment);
        if (targetDocument != null && !targetDocument.equals(document.getFullName())) {
            // The fragment has been moved so we need to update the link.
            reference.setType(ResourceType.DOCUMENT);
            reference.setReference(targetDocument);
            reference.setParameter(ANCHOR_PARAMETER, fragment);
        }
    }
}
Also used : LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference)

Example 8 with ResourceType

use of org.xwiki.rendering.listener.reference.ResourceType in project xwiki-platform by xwiki.

the class XWikiDocument method getUniqueLinkedPages.

/**
 * Extract all the unique static (i.e. not generated by macros) wiki links (pointing to wiki page) from this
 * document's content to others documents.
 *
 * @param context the XWiki context.
 * @return the document names for linked pages, if null an error append.
 * @since 1.9M2
 */
public Set<String> getUniqueLinkedPages(XWikiContext context) {
    Set<String> pageNames;
    XWikiDocument contextDoc = context.getDoc();
    String contextWiki = context.getWikiId();
    try {
        // Make sure the right document is used as context document
        context.setDoc(this);
        // Make sure the right wiki is used as context document
        context.setWikiId(getDatabase());
        if (is10Syntax()) {
            pageNames = getUniqueLinkedPages10(context);
        } else {
            XDOM dom = getXDOM();
            // TODO: Add support for macro as well.
            List<LinkBlock> linkBlocks = dom.getBlocks(new ClassBlockMatcher(LinkBlock.class), Block.Axes.DESCENDANT);
            pageNames = new LinkedHashSet<String>(linkBlocks.size());
            DocumentReference currentDocumentReference = getDocumentReference();
            for (LinkBlock linkBlock : linkBlocks) {
                ResourceReference reference = linkBlock.getReference();
                String referenceString = reference.getReference();
                ResourceType resourceType = reference.getType();
                // TODO: Add support for ATTACHMENT as well.
                if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
                    // We are only interested in Document or Space references.
                    continue;
                }
                // Optimisation: If the reference is empty, the link is an autolink and we don`t include it.
                if (StringUtils.isEmpty(referenceString)) {
                    continue;
                }
                // FIXME?: pass this.getDocumentReference as parameter to the resolve so that relative references
                // get resolved relative to this and not to the context document.
                EntityReference documentReference = getResourceReferenceEntityReferenceResolver().resolve(reference, EntityType.DOCUMENT);
                // Verify after resolving it that the link is not an autolink(i.e. a link to the current document)
                if (!documentReference.equals(currentDocumentReference)) {
                    // Since this method is used for saving backlinks and since backlinks must be
                    // saved with the space and page name but without the wiki part, we remove the wiki
                    // part before serializing.
                    // This is a bit of a hack since the default serializer should theoretically fail
                    // if it's passed an invalid reference.
                    pageNames.add(getCompactWikiEntityReferenceSerializer().serialize(documentReference));
                }
            }
        }
    } finally {
        context.setDoc(contextDoc);
        context.setWikiId(contextWiki);
    }
    return pageNames;
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) EntityReference(org.xwiki.model.reference.EntityReference) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) ToString(org.suigeneris.jrcs.util.ToString) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference)

Aggregations

ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 DocumentReference (org.xwiki.model.reference.DocumentReference)5 EntityReference (org.xwiki.model.reference.EntityReference)5 LinkBlock (org.xwiki.rendering.block.LinkBlock)4 XDOM (org.xwiki.rendering.block.XDOM)4 Block (org.xwiki.rendering.block.Block)3 ToString (org.suigeneris.jrcs.util.ToString)2 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 XWiki (com.xpn.xwiki.XWiki)1 LinkedResourceHelper (com.xpn.xwiki.internal.render.LinkedResourceHelper)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 SectionBlock (org.xwiki.rendering.block.SectionBlock)1 InlineFilterListener (org.xwiki.rendering.listener.InlineFilterListener)1