Skip to content

Commit ca12053

Browse files
committed
XWIKI-22571: Backlinks update changes an absolute reference to the moved 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
1 parent 4ada8a1 commit ca12053

File tree

9 files changed

+419
-2
lines changed

9 files changed

+419
-2
lines changed

xwiki-platform-core/xwiki-platform-refactoring/xwiki-platform-refactoring-default/src/main/java/org/xwiki/refactoring/internal/ResourceReferenceRenamer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public class ResourceReferenceRenamer
5959
@Inject
6060
private EntityReferenceResolver<ResourceReference> entityReferenceResolver;
6161

62+
@Inject
63+
@Named("relative")
64+
private EntityReferenceResolver<ResourceReference> relativeEntityReferenceResolver;
65+
6266
@Inject
6367
@Named("compact")
6468
private EntityReferenceSerializer<String> compactEntityReferenceSerializer;
@@ -206,9 +210,10 @@ private boolean updateAbsoluteResourceReference(ResourceReference resourceRefere
206210
private String getNewTargetReference(ResourceReference resourceReference, EntityReference newTargetReference,
207211
EntityReference currentReference)
208212
{
213+
EntityReference entityReference =
214+
this.relativeEntityReferenceResolver.resolve(resourceReference, null, (Object) null);
209215
// If the reference contains the wiki name, then we should keep the absolute serialization.
210-
// TODO: This regex feels really fragile, I'm not sure how we should check presence of a wiki here.
211-
if (resourceReference.getReference().matches("^\\w+:\\w.*$")) {
216+
if (entityReference.extractReference(EntityType.WIKI) != null) {
212217
return this.defaultEntityReferenceSerializer.serialize(newTargetReference, currentReference);
213218
} else {
214219
return this.compactEntityReferenceSerializer.serialize(newTargetReference, currentReference);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import javax.inject.Inject;
23+
import javax.inject.Named;
24+
25+
import org.xwiki.model.EntityType;
26+
import org.xwiki.model.reference.EntityReference;
27+
import org.xwiki.model.reference.EntityReferenceResolver;
28+
import org.xwiki.rendering.listener.reference.ResourceReference;
29+
import org.xwiki.rendering.listener.reference.ResourceType;
30+
31+
/**
32+
* Convert document resource reference into entity reference.
33+
*
34+
* @version $Id$
35+
* @since 17.0.0RC1
36+
*/
37+
public abstract class AbstractRelativeResourceReferenceEntityReferenceResolver
38+
extends AbstractResourceReferenceEntityReferenceResolver
39+
{
40+
@Inject
41+
@Named("relative")
42+
private EntityReferenceResolver<String> relativeReferenceResolver;
43+
44+
/**
45+
* Default constructor.
46+
*/
47+
public AbstractRelativeResourceReferenceEntityReferenceResolver(ResourceType type)
48+
{
49+
super(type);
50+
}
51+
52+
@Override
53+
protected EntityReference resolveTyped(ResourceReference resourceReference, EntityReference baseReference)
54+
{
55+
return this.relativeReferenceResolver.resolve(resourceReference.getReference(), getEntityType());
56+
}
57+
58+
public abstract EntityType getEntityType();
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import javax.inject.Named;
23+
import javax.inject.Singleton;
24+
25+
import org.xwiki.component.annotation.Component;
26+
import org.xwiki.model.EntityType;
27+
import org.xwiki.rendering.listener.reference.ResourceType;
28+
29+
/**
30+
* Convert attachment resource reference into entity reference.
31+
*
32+
* @version $Id$
33+
* @since 17.0.0RC1
34+
*/
35+
@Component
36+
@Named("relative/attach")
37+
@Singleton
38+
public class RelativeAttachmentResourceReferenceEntityReferenceResolver
39+
extends AbstractRelativeResourceReferenceEntityReferenceResolver
40+
{
41+
public RelativeAttachmentResourceReferenceEntityReferenceResolver()
42+
{
43+
super(ResourceType.ATTACHMENT);
44+
}
45+
46+
@Override
47+
public EntityType getEntityType()
48+
{
49+
return EntityType.ATTACHMENT;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import javax.inject.Named;
23+
import javax.inject.Singleton;
24+
25+
import org.xwiki.component.annotation.Component;
26+
import org.xwiki.model.EntityType;
27+
import org.xwiki.rendering.listener.reference.ResourceType;
28+
29+
/**
30+
* Convert document resource reference into entity reference.
31+
*
32+
* @version $Id$
33+
* @since 17.0.0RC1
34+
*/
35+
@Component
36+
@Named("relative/doc")
37+
@Singleton
38+
public class RelativeDocumentResourceReferenceEntityReferenceResolver
39+
extends AbstractRelativeResourceReferenceEntityReferenceResolver
40+
{
41+
public RelativeDocumentResourceReferenceEntityReferenceResolver()
42+
{
43+
super(ResourceType.DOCUMENT);
44+
}
45+
46+
@Override
47+
public EntityType getEntityType()
48+
{
49+
return EntityType.DOCUMENT;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import javax.inject.Named;
23+
import javax.inject.Singleton;
24+
25+
import org.xwiki.component.annotation.Component;
26+
import org.xwiki.model.EntityType;
27+
import org.xwiki.rendering.listener.reference.ResourceType;
28+
29+
/**
30+
* Convert attachment resource reference into entity reference.
31+
*
32+
* @version $Id$
33+
* @since 13.10.5
34+
* @since 17.0.0RC1
35+
*/
36+
@Component
37+
@Named("relative/pageAttach")
38+
@Singleton
39+
public class RelativePageAttachmentResourceReferenceEntityReferenceResolver
40+
extends AbstractRelativeResourceReferenceEntityReferenceResolver
41+
{
42+
public RelativePageAttachmentResourceReferenceEntityReferenceResolver()
43+
{
44+
super(ResourceType.PAGE_ATTACHMENT);
45+
}
46+
47+
@Override
48+
public EntityType getEntityType()
49+
{
50+
return EntityType.PAGE_ATTACHMENT;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import javax.inject.Named;
23+
import javax.inject.Singleton;
24+
25+
import org.xwiki.component.annotation.Component;
26+
import org.xwiki.model.EntityType;
27+
import org.xwiki.rendering.listener.reference.ResourceType;
28+
29+
/**
30+
* Convert page resource reference into entity reference.
31+
*
32+
* @version $Id$
33+
* @since 17.0.0RC1
34+
*/
35+
@Component
36+
@Named("relative/page")
37+
@Singleton
38+
public class RelativePageResourceReferenceEntityReferenceResolver
39+
extends AbstractRelativeResourceReferenceEntityReferenceResolver
40+
{
41+
public RelativePageResourceReferenceEntityReferenceResolver()
42+
{
43+
super(ResourceType.PAGE);
44+
}
45+
46+
@Override
47+
public EntityType getEntityType()
48+
{
49+
return EntityType.PAGE;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* See the NOTICE file distributed with this work for additional
3+
* information regarding copyright ownership.
4+
*
5+
* This is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU Lesser General Public License as
7+
* published by the Free Software Foundation; either version 2.1 of
8+
* the License, or (at your option) any later version.
9+
*
10+
* This software is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this software; if not, write to the Free
17+
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18+
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19+
*/
20+
package org.xwiki.rendering.internal.resolver;
21+
22+
import java.lang.reflect.ParameterizedType;
23+
24+
import javax.inject.Inject;
25+
import javax.inject.Named;
26+
import javax.inject.Provider;
27+
import javax.inject.Singleton;
28+
29+
import org.xwiki.component.annotation.Component;
30+
import org.xwiki.component.manager.ComponentLookupException;
31+
import org.xwiki.component.manager.ComponentManager;
32+
import org.xwiki.component.util.DefaultParameterizedType;
33+
import org.xwiki.model.EntityType;
34+
import org.xwiki.model.reference.EntityReference;
35+
import org.xwiki.model.reference.EntityReferenceResolver;
36+
import org.xwiki.rendering.listener.reference.ResourceReference;
37+
38+
/**
39+
* Default entry point to convert a resource reference into an entity reference.
40+
*
41+
* @version $Id$
42+
* @since 17.0.0RC1
43+
*/
44+
@Component
45+
@Named("relative")
46+
@Singleton
47+
public class RelativeResourceReferenceEntityReferenceResolver implements EntityReferenceResolver<ResourceReference>
48+
{
49+
/**
50+
* Type instance for EntityReferenceResolver<ResourceReference>.
51+
*/
52+
public static final ParameterizedType TYPE_RESOURCEREFERENCE =
53+
new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class);
54+
55+
private static final String RELATIVE_HINT = "relative/%s";
56+
57+
@Inject
58+
@Named("context")
59+
private Provider<ComponentManager> componentManagerProvider;
60+
61+
@Override
62+
public EntityReference resolve(ResourceReference resourceReference, EntityType type, Object... parameters)
63+
{
64+
if (resourceReference == null) {
65+
return null;
66+
}
67+
68+
if (this.componentManagerProvider.get().hasComponent(TYPE_RESOURCEREFERENCE,
69+
getRelativeHint(resourceReference))) {
70+
EntityReferenceResolver<ResourceReference> resolver;
71+
try {
72+
resolver = this.componentManagerProvider.get().getInstance(TYPE_RESOURCEREFERENCE,
73+
getRelativeHint(resourceReference));
74+
} catch (ComponentLookupException e) {
75+
throw new RuntimeException(
76+
String.format("Unknown error when trying to load resolver for reference [%s]", resourceReference),
77+
e);
78+
}
79+
80+
return resolver.resolve(resourceReference, type, parameters);
81+
}
82+
83+
// Unsupported resource reference type
84+
return null;
85+
}
86+
87+
private String getRelativeHint(ResourceReference resourceReference)
88+
{
89+
return String.format(RELATIVE_HINT, resourceReference.getType().getScheme());
90+
}
91+
}

0 commit comments

Comments
 (0)