Search in sources :

Example 51 with ResourceReference

use of org.xwiki.rendering.listener.reference.ResourceReference 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

ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)51 DocumentReference (org.xwiki.model.reference.DocumentReference)27 Test (org.junit.Test)26 LinkBlock (org.xwiki.rendering.block.LinkBlock)20 XDOM (org.xwiki.rendering.block.XDOM)16 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)15 AttachmentResourceReference (org.xwiki.rendering.listener.reference.AttachmentResourceReference)13 Block (org.xwiki.rendering.block.Block)12 SpaceReference (org.xwiki.model.reference.SpaceReference)11 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)9 AttachmentReference (org.xwiki.model.reference.AttachmentReference)9 ImageBlock (org.xwiki.rendering.block.ImageBlock)8 MacroBlock (org.xwiki.rendering.block.MacroBlock)8 ResourceType (org.xwiki.rendering.listener.reference.ResourceType)8 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)7 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)7 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)7 EntityReference (org.xwiki.model.reference.EntityReference)6 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 GroupBlock (org.xwiki.rendering.block.GroupBlock)5