Skip to content
Snippets Groups Projects
Commit fb6ebc54 authored by Thomas Mortagne's avatar Thomas Mortagne
Browse files

XWIKI-12920: Hiding WebHome in wiki link syntax

* optimizations (thanks to Enygma2002)
parent 7221e23b
No related branches found
No related tags found
No related merge requests found
......@@ -20,15 +20,19 @@
package org.xwiki.rendering.internal.resolver;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import org.xwiki.bridge.DocumentAccessBridge;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceProvider;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;
......@@ -52,6 +56,12 @@ public abstract class AbstractResourceReferenceEntityReferenceResolver
@Named("current")
protected Provider<DocumentReference> currentDocumentProvider;
@Inject
protected EntityReferenceProvider defaultReferenceProvider;
@Inject
protected DocumentAccessBridge documentAccessBridge;
protected ResourceType resourceType;
/**
......@@ -140,4 +150,26 @@ protected EntityReference convertReference(EntityReference entityReference, Enti
return entityReference;
}
protected DocumentReference resolveDocumentReference(DocumentReference reference, EntityReference baseReference)
{
DocumentReference finalReference = reference;
// If already a space home page, no fallback
// If same as current page, no fallback
String defaultDocumentName = this.defaultReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();
if (!reference.getName().equals(defaultDocumentName) && !Objects.equals(reference, baseReference)) {
if (!this.documentAccessBridge.exists(reference)) {
// It does not exist, make it a space home page. If the space does not exist, it will be
// a wanted link.
SpaceReference spaceReference =
new SpaceReference(reference.getName(), (SpaceReference) reference.getParent());
// Return a DocumentReference by default since a DOCUMENTresource reference was provided
finalReference = new DocumentReference(defaultDocumentName, spaceReference);
}
}
return finalReference;
}
}
......@@ -23,20 +23,14 @@
import javax.inject.Named;
import javax.inject.Singleton;
import org.xwiki.bridge.DocumentAccessBridge;
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.model.reference.EntityReferenceProvider;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;
import com.google.common.base.Objects;
/**
* Convert attachment resource reference into entity reference.
*
......@@ -52,12 +46,6 @@ public class AttachmentResourceReferenceEntityReferenceResolver extends Abstract
@Named("current")
private AttachmentReferenceResolver<String> currentAttachmentReferenceResolver;
@Inject
private EntityReferenceProvider defaultEntityReferenceProvider;
@Inject
private DocumentAccessBridge documentAccessBridge;
/**
* Default constructor.
*/
......@@ -74,24 +62,11 @@ protected EntityReference resolveTyped(ResourceReference resourceReference, Enti
// See if the resolved (terminal or WebHome) document exists and, if so, use it.
DocumentReference documentReference = attachmentReference.getDocumentReference();
if (!this.documentAccessBridge.exists(documentReference)) {
// Also consider explicit "WebHome" references (i.e. the ones ending in "WebHome").
String defaultDocumentName =
this.defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();
// If already a space home page, no fallback
// If same as current page, no fallback
if (!documentReference.getName().equals(defaultDocumentName)
&& !Objects.equal(documentReference, baseReference)) {
// It does not exist, make it an attachment located on a space home page
SpaceReference spaceReference =
new SpaceReference(documentReference.getName(), (SpaceReference) documentReference.getParent());
documentReference = new DocumentReference(defaultDocumentName, spaceReference);
// Otherwise, handle it as a space reference for both cases when it exists or when it doesn't exist.
attachmentReference = new AttachmentReference(attachmentReference.getName(), documentReference);
}
// Take care of fallback if needed
DocumentReference finalDocumentReference = resolveDocumentReference(documentReference, baseReference);
if (finalDocumentReference != documentReference) {
attachmentReference = new AttachmentReference(attachmentReference.getName(), finalDocumentReference);
}
return attachmentReference;
......
......@@ -24,19 +24,13 @@
import javax.inject.Singleton;
import org.apache.commons.lang3.StringUtils;
import org.xwiki.bridge.DocumentAccessBridge;
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.model.reference.EntityReferenceProvider;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;
import com.google.common.base.Objects;
/**
* Convert document resource reference into entity reference.
*
......@@ -48,12 +42,6 @@
@Singleton
public class DocumentResourceReferenceEntityReferenceResolver extends AbstractResourceReferenceEntityReferenceResolver
{
@Inject
private EntityReferenceProvider defaultReferenceProvider;
@Inject
private DocumentAccessBridge documentAccessBridge;
@Inject
@Named("current")
private DocumentReferenceResolver<String> currentDocumentReferenceResolver;
......@@ -84,23 +72,8 @@ protected EntityReference resolveUntyped(ResourceReference resourceReference, En
DocumentReference reference =
this.currentDocumentReferenceResolver.resolve(resourceReference.getReference(), baseReference);
// It can be a link to an existing terminal document
if (!this.documentAccessBridge.exists(reference)) {
String defaultDocumentName =
this.defaultReferenceProvider.getDefaultReference(EntityType.DOCUMENT).getName();
// If already a space home page, no fallback
// If same as current page, no fallback
if (!reference.getName().equals(defaultDocumentName) && !Objects.equal(reference, baseReference)) {
// It does not exist, make it a space home page. If the space does not exist, it will be
// a wanted link.
SpaceReference spaceReference =
new SpaceReference(reference.getName(), (SpaceReference) reference.getParent());
// Return a DocumentReference by default since a DOCUMENTresource reference was provided
reference = new DocumentReference(defaultDocumentName, spaceReference);
}
}
// Take care of fallback if needed
reference = resolveDocumentReference(reference, baseReference);
return reference;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment