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());
}
}
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);
}
}
}
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;
}
Aggregations