Skip to content

Commit

Permalink
Move archiveContext and archiveServerUrl into the mcx
Browse files Browse the repository at this point in the history
  • Loading branch information
csev committed Feb 25, 2025
1 parent bf2a51d commit bd06370
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,6 @@ private void addSupplementaryItemAttachments(Document doc, Element item, List<St
public String merge(String siteId, Element root, String archivePath, String fromSiteId,
MergeConfig mcx, Map<String, String> userIdTrans, Set<String> userListAllowImport) {

String archiveContext = "";
String archiveServerUrl = "";

Node parent = root.getParentNode();
if (parent.getNodeType() == Node.ELEMENT_NODE)
{
Element parentEl = (Element)parent;
archiveContext = parentEl.getAttribute("source");
archiveServerUrl = parentEl.getAttribute("serverurl");
}

final StringBuilder results = new StringBuilder();
results.append("begin merging ").append(getLabel()).append(" context ").append(siteId).append(LINE_SEPARATOR);
Expand All @@ -465,7 +455,7 @@ public String merge(String siteId, Element root, String archivePath, String from
for (Element assignmentElement : assignmentElements) {

try {
mergeAssignment(siteId, assignmentElement, results, assignmentTitles, mcx, archiveContext, archiveServerUrl);
mergeAssignment(siteId, assignmentElement, results, assignmentTitles, mcx);
assignmentsMerged++;
} catch (Exception e) {
final String error = "could not merge assignment with id: " + assignmentElement.getFirstChild().getFirstChild().getNodeValue();
Expand Down Expand Up @@ -1001,7 +991,7 @@ public String getTimeSpent(AssignmentSubmission submission) {
}

@Transactional
private Assignment mergeAssignment(final String siteId, final Element element, final StringBuilder results, Set<String> assignmentTitles, MergeConfig mcx, String archiveContext, String archiveServerUrl) throws PermissionException {
private Assignment mergeAssignment(final String siteId, final Element element, final StringBuilder results, Set<String> assignmentTitles, MergeConfig mcx) throws PermissionException {

if (!allowAddAssignment(siteId)) {
throw new PermissionException(sessionManager.getCurrentSessionUserId(), SECURE_ADD_ASSIGNMENT, AssignmentReferenceReckoner.reckoner().context(siteId).reckon().getReference());
Expand All @@ -1027,7 +1017,7 @@ private Assignment mergeAssignment(final String siteId, final Element element, f
if ( StringUtils.isNotEmpty(mcx.creatorId) ) assignmentFromXml.setAuthor(mcx.creatorId);

String newInstructions = ltiService.fixLtiLaunchUrls(assignmentFromXml.getInstructions(), siteId, mcx);
newInstructions = linkMigrationHelper.migrateLinksInMergedRTE(siteId, archiveContext, archiveServerUrl, newInstructions);
newInstructions = linkMigrationHelper.migrateLinksInMergedRTE(siteId, mcx, newInstructions);
assignmentFromXml.setInstructions(newInstructions);

Long contentKey = ltiService.mergeContentFromImport(element, siteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1495,17 +1495,6 @@ public String merge(String siteId, Element root, String archivePath, String from
MergeConfig mcx, Map<String, String> userIdTrans, Set<String> userListAllowImport)
{

String archiveContext = "";
String archiveServerUrl = "";

Node parent = root.getParentNode();
if (parent.getNodeType() == Node.ELEMENT_NODE)
{
Element parentEl = (Element)parent;
archiveContext = parentEl.getAttribute("source");
archiveServerUrl = parentEl.getAttribute("serverurl");
}

// prepare the buffer for the results log
StringBuilder results = new StringBuilder();

Expand Down Expand Up @@ -1648,7 +1637,7 @@ public String merge(String siteId, Element root, String archivePath, String from
}
String description = edit.getDescriptionFormatted();
description = ltiService.fixLtiLaunchUrls(description, siteId, mcx);
description = linkMigrationHelper.migrateLinksInMergedRTE(siteId, archiveContext, archiveServerUrl, description);
description = linkMigrationHelper.migrateLinksInMergedRTE(siteId, mcx, description);
edit.setDescriptionFormatted(description);

calendar.commitEvent(edit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,15 @@ else if (element.getTagName().equals(UserDirectoryService.APPLICATION_ID))
if (service != null) {
if ((system.equalsIgnoreCase(ArchiveService.FROM_SAKAI) || system.equalsIgnoreCase(ArchiveService.FROM_SAKAI_2_8))) {
if (checkSakaiService(filterSakaiService, filteredSakaiService, serviceName)) {
// checks passed so now we attempt to do the merge
log.debug("Merging archive data for {} ({}) to site {}", serviceName, fileName, siteId);
// checks passed so now we attempt to do the merge
Node parent = element.getParentNode();
if (parent.getNodeType() == Node.ELEMENT_NODE)
{
Element parentEl = (Element)parent;
mcx.archiveContext = parentEl.getAttribute("source");
mcx.archiveServerUrl = parentEl.getAttribute("serverurl");
}
log.debug("Merging archive data for {} ({}) to site {} archive from context {} and server {}", serviceName, fileName, siteId, mcx.archiveContext, mcx.archiveServerUrl);
msg = service.merge(siteId, element, fileName, fromSite, mcx, new HashMap() /* empty userIdTran map */, usersListAllowImport);
} else {
log.warn("Skipping merge archive data for "+serviceName+" ("+fileName+") to site "+siteId+", checked filter failed (filtersOn="+filterSakaiService+", filters="+Arrays.toString(filteredSakaiService)+")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ default String merge(String siteId, Element root, String archivePath, String fro
* @return A log of status messages from the merge.
*/
default String merge(String siteId, Element root, String archivePath, String fromSiteId,
MergeConfig mcx, Map<String, String> userIdTrans,
Set<String> userListAllowImport) {
MergeConfig mcx, Map<String, String> userIdTrans, Set<String> userListAllowImport) {
// By default call the old merge without creatorId for those impls that don't need the creatorId
return merge(siteId, root, archivePath, fromSiteId, mcx.attachmentNames, userIdTrans, userListAllowImport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public class MergeConfig {
public String creatorId;
public String archiveContext = "";
public String archiveServerUrl = "";
public Map<Long, Map<String, Object>> ltiContentItems = new HashMap();
public Map<String, String> attachmentNames = new HashMap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.util.Map.Entry;
import java.util.Set;

import org.sakaiproject.util.MergeConfig;

/**
* From SAK-22283:
Expand Down Expand Up @@ -71,11 +71,10 @@ public interface LinkMigrationHelper {
* Do several transformations to migrate the content links present in a zip import of RTE content
*
* @param siteId the site id (Must not be null or empty)
* @param fromContext the context of the original content (Can be null)
* @param fromServerUrl the server url of the original content (Can be null)
* @param mcx the merge config
* @param content the content to migrate
* @return the migrated content
*/
public String migrateLinksInMergedRTE(String siteId, String fromContext, String fromServerUrl, String content);
public String migrateLinksInMergedRTE(String siteId, MergeConfig mcx, String content);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;

import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.util.MergeConfig;

public class LinkMigrationHelper {

Expand Down Expand Up @@ -45,7 +46,7 @@ public static String migrateOneLink(String fromContextRef, String targetContextR
return getLinkMigrationHelper().migrateOneLink(fromContextRef, targetContextRef, msgBody);
}

public static String migrateLinksInMergedRTE(String siteId, String fromContext, String fromServerUrl, String content) {
return getLinkMigrationHelper().migrateLinksInMergedRTE(siteId, fromContext, fromServerUrl, content);
public static String migrateLinksInMergedRTE(String siteId, MergeConfig mcx, String content) {
return getLinkMigrationHelper().migrateLinksInMergedRTE(siteId, mcx, content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.util.api.LinkMigrationHelper;

import org.sakaiproject.util.MergeConfig;
@Slf4j
public class LinkMigrationHelperImpl implements LinkMigrationHelper {
private static final String ESCAPED_SPACE = "%"+"20";
Expand Down Expand Up @@ -137,45 +137,44 @@ public String migrateOneLink(String fromContextRef, String targetContextRef, Str
* Do several transformations to migrate the content links present in a zip import of RTE content
*
* @param siteId the site id (Must not be null or empty)
* @param fromContext the context of the original content (Can be null)
* @param fromServerUrl the server url of the original content (Can be null)
* @param mcx the merge config
* @param content the content to migrate
* @return the migrated content
*/
public String migrateLinksInMergedRTE(String siteId, String fromContext, String fromServerUrl, String content) {
public String migrateLinksInMergedRTE(String siteId, MergeConfig mcx, String content) {
String before;
String after = serverConfigurationService.getServerUrl() + ACCESS_CONTENT_GROUP + siteId + "/";

// Replace full match of the fromserverUrl and fromContext (ideal)
if ( StringUtils.isNotBlank(fromServerUrl) && StringUtils.isNotBlank(fromContext) ) {
before = fromServerUrl + ACCESS_CONTENT_GROUP + fromContext + "/";
if ( StringUtils.isNotBlank(mcx.archiveServerUrl) && StringUtils.isNotBlank(mcx.archiveContext) ) {
before = mcx.archiveServerUrl + ACCESS_CONTENT_GROUP + mcx.archiveContext + "/";
content = content.replace(before, after);
}

// If we don't know the fromServerUrl, but we know the fromContext, replace athe url prefix to get the links onto this server
if ( StringUtils.isBlank(fromServerUrl) && StringUtils.isNotBlank(fromContext) ) {
before = "https?://[^/]+/" + ACCESS_CONTENT_GROUP + fromContext + "/";
if ( StringUtils.isBlank(mcx.archiveServerUrl) && StringUtils.isNotBlank(mcx.archiveContext) ) {
before = "https?://[^/]+/" + ACCESS_CONTENT_GROUP + mcx.archiveContext + "/";
content = content.replaceAll(before, after);
}

// If we know the fromContext, we can replace urls that start with "/"
if ( StringUtils.isNotBlank(fromContext) ) {
before = "\"/" + ACCESS_CONTENT_GROUP + fromContext + "/";
if ( StringUtils.isNotBlank(mcx.archiveContext) ) {
before = "\"/" + ACCESS_CONTENT_GROUP + mcx.archiveContext + "/";
after = "\"/" + ACCESS_CONTENT_GROUP + siteId + "/";
content = content.replaceAll(before, after);
}

// If we know the fromServerUrl and not the fromContext, move old broken urls to this server at a minimum
if ( StringUtils.isNotBlank(fromServerUrl) ) {
before = fromServerUrl + ACCESS_CONTENT_GROUP;
if ( StringUtils.isNotBlank(mcx.archiveServerUrl) ) {
before = mcx.archiveServerUrl + ACCESS_CONTENT_GROUP;
after = serverConfigurationService.getServerUrl() + ACCESS_CONTENT_GROUP;
content = content.replace(before, after);
}

// [http://localhost:8080/direct/forum_topic/1]
// Migrate direct links to the new server if we have the name of the server
if ( StringUtils.isNotBlank(fromServerUrl) ) {
before = fromServerUrl + DIRECT_LINK;
if ( StringUtils.isNotBlank(mcx.archiveServerUrl) ) {
before = mcx.archiveServerUrl + DIRECT_LINK;
after = serverConfigurationService.getServerUrl() + DIRECT_LINK;
content = content.replace(before, after);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,58 +70,64 @@ public void testMigrateOneLink() {
@Test
public void testMigrateLinksInMergedRTE() {
// Test basic migration of oldId to newId
MergeConfig mcx = new MergeConfig();
mcx.archiveContext = "oldId";
mcx.archiveServerUrl = "http://www.zap.com";
String content = "this <a href='http://www.zap.com/access/content/group/oldId/ietf-postel-06.png'>text</a>";
String migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
String migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
String result = "this <a href='http://localhost:8080/access/content/group/newId/ietf-postel-06.png'>text</a>";
assertEquals(result, migrated);

// Test link with different site ID (should remain unchanged)
content = "this <a href='http://www.zap.com/access/content/group/weirdId/ietf-postel-06.png'>text</a>";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "this <a href='http://localhost:8080/access/content/group/weirdId/ietf-postel-06.png'>text</a>";
assertEquals(result, migrated);

// Test multiple links in the same content
content = "this <a href='http://www.zap.com/access/content/group/oldId/file1.pdf'>link1</a> and " +
"<a href='http://www.zap.com/access/content/group/oldId/file2.jpg'>link2</a>";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "this <a href='http://localhost:8080/access/content/group/newId/file1.pdf'>link1</a> and " +
"<a href='http://localhost:8080/access/content/group/newId/file2.jpg'>link2</a>";
assertEquals(result, migrated);

// Test with different source domain
content = "this <a href='https://other-domain.com/access/content/group/oldId/file.pdf'>text</a>";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "https://other-domain.com", content);
result = "this <a href='http://localhost:8080/access/content/group/newId/file.pdf'>text</a>";
assertEquals(result, migrated);


// Test with non-matching URL pattern (should remain unchanged)
content = "this <a href='http://www.zap.com/different/path/oldId/file.pdf'>text</a>";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "this <a href='http://www.zap.com/different/path/oldId/file.pdf'>text</a>";
assertEquals(result, migrated);

// Test direct link migration
content = "Check this discussion [http://www.zap.com/direct/forum_topic/123]";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "Check this discussion [http://localhost:8080/direct/forum_topic/123]";
assertEquals(result, migrated);

// Test multiple direct links in the same content
content = "First topic [http://www.zap.com/direct/forum_topic/123] and " +
"second topic [http://www.zap.com/direct/forum_topic/456]";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "First topic [http://localhost:8080/direct/forum_topic/123] and " +
"second topic [http://localhost:8080/direct/forum_topic/456]";
assertEquals(result, migrated);

// Test mix of direct and content links
content = "Resource <a href='http://www.zap.com/access/content/group/oldId/file.pdf'>here</a> " +
"and discussion [http://www.zap.com/direct/forum_topic/789]";
migrated = impl.migrateLinksInMergedRTE("newId", "oldId", "http://www.zap.com", content);
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "Resource <a href='http://localhost:8080/access/content/group/newId/file.pdf'>here</a> " +
"and discussion [http://localhost:8080/direct/forum_topic/789]";
assertEquals(result, migrated);

// Test with different source domain
mcx.archiveServerUrl = "https://other-domain.com";
content = "this <a href='https://other-domain.com/access/content/group/oldId/file.pdf'>text</a>";
migrated = impl.migrateLinksInMergedRTE("newId", mcx, content);
result = "this <a href='http://localhost:8080/access/content/group/newId/file.pdf'>text</a>";
assertEquals(result, migrated);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,7 @@ public String getLabel() {

// the pages are already made. this adds the elements
private boolean mergePage(Element element, String oldServer, String siteId, String fromSiteId, Map<Long,Long> pageMap,
Map<Long,Long> itemMap, Map<String,String> entityMap, MergeConfig mcx,
String archiveContext, String archiveServerUrl) {
Map<Long,Long> itemMap, Map<String,String> entityMap, MergeConfig mcx) {

String oldSiteId = element.getAttribute("siteid");
String oldPageIdString = element.getAttribute("pageid");
Expand Down Expand Up @@ -806,7 +805,7 @@ private boolean mergePage(Element element, String oldServer, String siteId, Stri
} else if (type == SimplePageItem.TEXT) {
String html = itemElement.getAttribute("html");
explanation = ltiService.fixLtiLaunchUrls(html, siteId, mcx);
explanation = linkMigrationHelper.migrateLinksInMergedRTE(siteId, archiveContext, archiveServerUrl, explanation);
explanation = linkMigrationHelper.migrateLinksInMergedRTE(siteId, mcx, explanation);
} else if (type == SimplePageItem.PAGE) {
// sakaiId should be the new page ID
Long newPageId = pageMap.get(Long.valueOf(sakaiId));
Expand Down Expand Up @@ -1141,17 +1140,9 @@ public String merge(String siteId, Element root, String archivePath, String from
public String mergeInternal(String siteId, Element root, String archivePath, String fromSiteId,
MergeConfig mcx, Map userIdTrans, Set userListAllowImport, Map<String, String> entityMap)
{
String archiveContext = "";
String archiveServerUrl = "";
Node parent = root.getParentNode();
if (parent.getNodeType() == Node.ELEMENT_NODE)
{
Element parentEl = (Element)parent;
archiveContext = parentEl.getAttribute("source");
archiveServerUrl = parentEl.getAttribute("serverurl");
}

log.debug("Lessons Merge siteId={} fromSiteId={} creatorId={} archiveContext={} archiveServerUrl={}",
siteId, fromSiteId, mcx.creatorId, archiveContext, archiveServerUrl);
siteId, fromSiteId, mcx.creatorId, mcx.archiveContext, mcx.archiveServerUrl);

StringBuilder results = new StringBuilder();
// map old to new page ids
Expand Down Expand Up @@ -1236,7 +1227,7 @@ public String mergeInternal(String siteId, Element root, String archivePath, Str
Long oldPageId = Long.valueOf(pageElement.getAttribute("pageid"));
pageElementMap.put(oldPageId, pageElement);

if (mergePage(pageElement, oldServer, siteId, fromSiteId, pageMap, itemMap, entityMap, mcx, archiveContext, archiveServerUrl))
if (mergePage(pageElement, oldServer, siteId, fromSiteId, pageMap, itemMap, entityMap, mcx))

needFix = true;
}
Expand Down
Loading

0 comments on commit bd06370

Please sign in to comment.