Skip to content

Commit

Permalink
XWIKI-22571: Backlinks update changes an absolute reference to the mo…
Browse files Browse the repository at this point in the history
…ved page into one relative to the current wiki

  * Provide implementations for  relative
    ResourceReferenceEntityReference resolvers and tests
  • Loading branch information
surli committed Nov 21, 2024
1 parent f3c97a6 commit 6edf7c1
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.junit5.mockito.ComponentTest;
import org.xwiki.test.junit5.mockito.InjectMockComponents;
Expand Down Expand Up @@ -70,5 +71,19 @@ public void resolveDocumentReferenceWithBaseReference()
assertNull(reference.extractReference(EntityType.WIKI));
assertEquals("space", reference.extractReference(EntityType.SPACE).getName());
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new EntityReference("wikiFoo", EntityType.WIKI));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));

reference =
this.resolver.resolve("", EntityType.DOCUMENT, new WikiReference("wikiFoo"));

assertEquals("wikiFoo", reference.extractReference(EntityType.WIKI).getName());
assertNull(reference.extractReference(EntityType.SPACE));
assertNull(reference.extractReference(EntityType.DOCUMENT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Unit tests for {@link WikiReference}.
Expand All @@ -49,4 +50,11 @@ public void testInvalidParent()
() -> new WikiReference(new EntityReference("wiki", EntityType.WIKI, badParent)));
assertEquals("Unexpected parent [" + badParent + "] in a wiki reference", expected.getMessage());
}

@Test
void instanceOf()
{
assertTrue(new WikiReference("foo") instanceof WikiReference);
assertTrue(new WikiReference("foo") instanceof EntityReference);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package org.xwiki.rendering.internal.resolver;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

Expand All @@ -29,7 +31,7 @@
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert document resource reference into entity reference.
* Abstract class for all relative resource reference resolvers.
*
* @version $Id$
* @since 17.0.0RC1
Expand All @@ -39,21 +41,84 @@ public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
{
@Inject
@Named("relative")
private EntityReferenceResolver<String> relativeReferenceResolver;
protected EntityReferenceResolver<String> relativeReferenceResolver;

/**
* Default constructor.
* @param type the resource type that this resolver will support
*/
public AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
protected AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
{
super(type);
}

@Override
public EntityReference resolve(ResourceReference resourceReference, EntityType entityType, Object... parameters)
{
if (resourceReference == null) {
return null;
}

if (this.resourceType != null && !resourceReference.getType().equals(this.resourceType)) {
throw new IllegalArgumentException(
String.format("You must pass a resource reference of type [%s]. [%s] was passed", this.resourceType,
resourceReference));
}

EntityReference entityReference;
EntityReference baseReference = getBaseReference(resourceReference, parameters);

if (resourceReference.isTyped()) {
entityReference = resolveTyped(resourceReference, baseReference);
} else {
entityReference = resolveUntyped(resourceReference, baseReference);
}

return entityReference;
}

@Override
protected EntityReference getBaseReference(ResourceReference resourceReference, Object... parameters)
{
EntityReference baseReference =
(parameters.length > 0 && parameters[0] instanceof EntityReference entityReference)
? entityReference : null;

if (!resourceReference.getBaseReferences().isEmpty()) {
// If the passed reference has a base reference, resolve it first with a relative resolver (it should
// normally be absolute but who knows what the API caller has specified...)
baseReference = resolveBaseReference(resourceReference.getBaseReferences(), baseReference);
}

return baseReference;
}

@Override
protected EntityReference resolveBaseReference(List<String> baseReferences, EntityReference defaultBaseReference)
{
EntityReference resolvedBaseReference = defaultBaseReference;
for (String baseReference : baseReferences) {
resolvedBaseReference =
this.relativeReferenceResolver.resolve(baseReference, EntityType.DOCUMENT, resolvedBaseReference);
}

return resolvedBaseReference;
}

@Override
protected EntityReference resolveUntyped(ResourceReference resourceReference, EntityReference baseReference)
{
return resolveTyped(resourceReference, baseReference);
}

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
{
return this.relativeReferenceResolver.resolve(resourceReference.getReference(), getEntityType());
return this.relativeReferenceResolver.resolve(resourceReference.getReference(), getEntityType(), baseReference);
}

public abstract EntityType getEntityType();
/**
*
* @return the entity type of the {@link EntityReference} this resolver produces.
*/
protected abstract EntityType getEntityType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@
*/
package org.xwiki.rendering.internal.resolver;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.AttachmentReference;
import org.xwiki.model.reference.AttachmentReferenceResolver;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
Expand All @@ -38,14 +45,55 @@
public class RelativeAttachmentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
@Inject
private AttachmentReferenceResolver<EntityReference> defaultReferenceAttachmentReferenceResolver;

/**
* Default constructor.
*/
public RelativeAttachmentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.ATTACHMENT);
}

@Override
public EntityType getEntityType()
protected EntityType getEntityType()
{
return EntityType.ATTACHMENT;
}

@Override
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
{
if (StringUtils.isEmpty(resourceReference.getReference())) {
return null;
}

// Get relative reference
EntityReference relativeReference =
this.relativeReferenceResolver
.resolve(resourceReference.getReference(), EntityType.ATTACHMENT, baseReference);

EntityReference result = relativeReference;
if (relativeReference.extractReference(EntityType.WIKI) != null) {
// Resolve full reference
AttachmentReference attachmentReference =
this.defaultReferenceAttachmentReferenceResolver.resolve(relativeReference, baseReference);

// See if the resolved (terminal or WebHome) document exists and, if so, use it.
DocumentReference documentReference = attachmentReference.getDocumentReference();

// Take care of fallback if needed
DocumentReference finalDocumentReference =
resolveDocumentReference(relativeReference.getParent(), documentReference, baseReference);
// Also use that resolution if the relative reference doesn't contain any document, even if it doesn't
// exists, to not produce incorrect references.
if (finalDocumentReference != documentReference
|| relativeReference.extractReference(EntityType.DOCUMENT) == null) {
result = new AttachmentReference(attachmentReference.getName(), finalDocumentReference);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
*/
package org.xwiki.rendering.internal.resolver;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.DocumentReferenceResolver;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
Expand All @@ -38,14 +44,47 @@
public class RelativeDocumentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
@Inject
private DocumentReferenceResolver<String> defaultStringDocumentReferenceResolver;

/**
* Default constructor.
*/
public RelativeDocumentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.DOCUMENT);
}

@Override
public EntityType getEntityType()
protected EntityType getEntityType()
{
return EntityType.DOCUMENT;
}

@Override
protected EntityReference resolveUntyped(ResourceReference resourceReference, EntityReference baseReference)
{
// If the reference is empty fallback on typed logic
if (StringUtils.isEmpty(resourceReference.getReference())) {
return resolveTyped(resourceReference, baseReference);
}

// Get relative reference
EntityReference relativeReference =
this.relativeReferenceResolver.resolve(resourceReference.getReference(), EntityType.DOCUMENT,
baseReference);

EntityReference result = relativeReference;
if (relativeReference.extractReference(EntityType.WIKI) != null) {
// Resolve the full document reference
// We don't start from the previously parsed relative reference to not loose "." prefixed reference meaning
DocumentReference reference =
this.defaultStringDocumentReferenceResolver.resolve(resourceReference.getReference(), baseReference);

// Take care of fallback if needed
result = resolveDocumentReference(relativeReference, reference, baseReference);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@
public class RelativePageAttachmentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
/**
* Default constructor.
*/
public RelativePageAttachmentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.PAGE_ATTACHMENT);
}

@Override
public EntityType getEntityType()
protected EntityType getEntityType()
{
return EntityType.PAGE_ATTACHMENT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
public class RelativePageResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
/**
* Default constructor.
*/
public RelativePageResourceReferenceEntityReferenceResolver()
{
super(ResourceType.PAGE);
}

@Override
public EntityType getEntityType()
protected EntityType getEntityType()
{
return EntityType.PAGE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
public class RelativeSpaceResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
/**
* Default constructor.
*/
public RelativeSpaceResourceReferenceEntityReferenceResolver()
{
super(ResourceType.SPACE);
}

@Override
public EntityType getEntityType()
protected EntityType getEntityType()
{
return EntityType.SPACE;
}
Expand Down
Loading

0 comments on commit 6edf7c1

Please sign in to comment.