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 relative EntityResourceReference resolvers for
    ResourceReference and use those to determine if an entity reference
contains a wiki part or not
  * WIP: need to check / improve actual implem of the resolvers and add
    test / doc
  • Loading branch information
surli committed Nov 19, 2024
1 parent 4ada8a1 commit ca12053
Show file tree
Hide file tree
Showing 9 changed files with 419 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class ResourceReferenceRenamer
@Inject
private EntityReferenceResolver<ResourceReference> entityReferenceResolver;

@Inject
@Named("relative")
private EntityReferenceResolver<ResourceReference> relativeEntityReferenceResolver;

@Inject
@Named("compact")
private EntityReferenceSerializer<String> compactEntityReferenceSerializer;
Expand Down Expand Up @@ -206,9 +210,10 @@ private boolean updateAbsoluteResourceReference(ResourceReference resourceRefere
private String getNewTargetReference(ResourceReference resourceReference, EntityReference newTargetReference,
EntityReference currentReference)
{
EntityReference entityReference =
this.relativeEntityReferenceResolver.resolve(resourceReference, null, (Object) null);
// If the reference contains the wiki name, then we should keep the absolute serialization.
// TODO: This regex feels really fragile, I'm not sure how we should check presence of a wiki here.
if (resourceReference.getReference().matches("^\\w+:\\w.*$")) {
if (entityReference.extractReference(EntityType.WIKI) != null) {
return this.defaultEntityReferenceSerializer.serialize(newTargetReference, currentReference);
} else {
return this.compactEntityReferenceSerializer.serialize(newTargetReference, currentReference);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

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

import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.rendering.listener.reference.ResourceReference;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert document resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
extends AbstractResourceReferenceEntityReferenceResolver
{
@Inject
@Named("relative")
private EntityReferenceResolver<String> relativeReferenceResolver;

/**
* Default constructor.
*/
public AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
{
super(type);
}

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

public abstract EntityType getEntityType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert attachment resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative/attach")
@Singleton
public class RelativeAttachmentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
public RelativeAttachmentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.ATTACHMENT);
}

@Override
public EntityType getEntityType()
{
return EntityType.ATTACHMENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert document resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative/doc")
@Singleton
public class RelativeDocumentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
public RelativeDocumentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.DOCUMENT);
}

@Override
public EntityType getEntityType()
{
return EntityType.DOCUMENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert attachment resource reference into entity reference.
*
* @version $Id$
* @since 13.10.5
* @since 17.0.0RC1
*/
@Component
@Named("relative/pageAttach")
@Singleton
public class RelativePageAttachmentResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
public RelativePageAttachmentResourceReferenceEntityReferenceResolver()
{
super(ResourceType.PAGE_ATTACHMENT);
}

@Override
public EntityType getEntityType()
{
return EntityType.PAGE_ATTACHMENT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.model.EntityType;
import org.xwiki.rendering.listener.reference.ResourceType;

/**
* Convert page resource reference into entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative/page")
@Singleton
public class RelativePageResourceReferenceEntityReferenceResolver
extends AbstractRelativeResourceReferenceEntityReferenceResolver
{
public RelativePageResourceReferenceEntityReferenceResolver()
{
super(ResourceType.PAGE);
}

@Override
public EntityType getEntityType()
{
return EntityType.PAGE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.xwiki.rendering.internal.resolver;

import java.lang.reflect.ParameterizedType;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.component.manager.ComponentLookupException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.component.util.DefaultParameterizedType;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.EntityReferenceResolver;
import org.xwiki.rendering.listener.reference.ResourceReference;

/**
* Default entry point to convert a resource reference into an entity reference.
*
* @version $Id$
* @since 17.0.0RC1
*/
@Component
@Named("relative")
@Singleton
public class RelativeResourceReferenceEntityReferenceResolver implements EntityReferenceResolver<ResourceReference>
{
/**
* Type instance for EntityReferenceResolver<ResourceReference>.
*/
public static final ParameterizedType TYPE_RESOURCEREFERENCE =
new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class);

private static final String RELATIVE_HINT = "relative/%s";

@Inject
@Named("context")
private Provider<ComponentManager> componentManagerProvider;

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

if (this.componentManagerProvider.get().hasComponent(TYPE_RESOURCEREFERENCE,
getRelativeHint(resourceReference))) {
EntityReferenceResolver<ResourceReference> resolver;
try {
resolver = this.componentManagerProvider.get().getInstance(TYPE_RESOURCEREFERENCE,
getRelativeHint(resourceReference));
} catch (ComponentLookupException e) {
throw new RuntimeException(
String.format("Unknown error when trying to load resolver for reference [%s]", resourceReference),
e);
}

return resolver.resolve(resourceReference, type, parameters);
}

// Unsupported resource reference type
return null;
}

private String getRelativeHint(ResourceReference resourceReference)
{
return String.format(RELATIVE_HINT, resourceReference.getType().getScheme());
}
}
Loading

0 comments on commit ca12053

Please sign in to comment.