Modification of virtual page identifiers (since 10/02/2016)
The following script is necessary if you have rich texts that may contain links to ODF pages (training, courses, ELP).
Attention The following script can only be run once! Make a backup of the repository BEFORE running script. Then go to script for the first time, leaving the instruction session.save() commented. Check that this seems correct before replaying the script with uncommentary comments. session.save()
Script update links in rich texts
Oops!
Copy to clipboard failed. Open the code and copy it manually.
var qm = session.getWorkspace().getQueryManager();
// récupération de tous les textes riches existants
var query = qm.createQuery("//element(*, ametys:richText)[@jcr:data]", javax.jcr.query.Query.XPATH);
var nodes = query.execute().getNodes();
var nodeCount = 0;
var liveCount = 0;
while (nodes.hasNext())
{
var node = nodes.next();
try
{
// contenu du texte riche
var is = node.getProperty("jcr:data").getStream();
var builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
var doc = builder.parse(is);
is.close();
var needUpdate = false;
// parcours des liens contenus dans le texte riche
var links = org.apache.xpath.XPathAPI.selectNodeList(doc, "//link[@type='page']", doc.getDocumentElement());
for (var i = 0; i < links.getLength(); i++)
{
var linkNeedUpdate = false;
var link = links.item(i);
var href = link.getAttributes().getNamedItem("xlink:href").getNodeValue();
// traitement des liens vers une formation, parcours ou ELP
if (href.indexOf("program://") == 0)
{
// les anciennes url sont de la forme program://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://art-lettres-langue-ALL/master-lmd-XB?rootId=page://xxx&programId=program://yyy
var index = href.indexOf('?');
var oldPath = href.substring("program://".length, index);
// println(node.getUUID() + " - "+ href);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length == 2)
{
newPath = "_root";
linkNeedUpdate = true;
}
else if (pathSegment.length > 2)
{
linkNeedUpdate = true;
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
}
if (linkNeedUpdate)
{
// calcul du nouveau lien
// les nouvelles url sont de la forme program://master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://_root?rootId=page://xxx&programId=program://yyy
var newHref = "program://" + newPath + href.substring(index);
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
else if (href.indexOf("course://") == 0 && href.indexOf("&programId=") == -1)
{
var index = href.indexOf('?');
var oldPath = href.substring("course://".length, index);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length > 2)
{
// les anciennes url sont de la forme course://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
// course://licence-professionnelle-design-program-fruai3182988bprh7agzrd6?rootId=...
var programName = pathSegment[2].substring (pathSegment[2].indexOf('program-'));
query = qm.createQuery("//" + programName, javax.jcr.query.Query.XPATH);
nodes = query.execute().getNodes();
var program = nodes.hasNext() ? nodes.next() : null;
// calcul du nouveau lien
// les nouvelles url sont de la forme course://master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy&programId=zzz
var newHref = "course://" + newPath + href.substring(index);
if (program != null)
{
newHref += "&programId=program://" + program.getId();
}
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
}
if (needUpdate)
{
// mise à jour du contenu du texte riche avec les liens modifiés
var os = new java.io.ByteArrayOutputStream();
var result = new javax.xml.transform.stream.StreamResult(os);
var source = new javax.xml.transform.dom.DOMSource(doc);
var transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
var format = new java.util.Properties();
format.put(javax.xml.transform.OutputKeys.METHOD, "xml");
//format.put(javax.xml.transform.OutputKeys.INDENT, "yes");
format.put(javax.xml.transform.OutputKeys.ENCODING, "UTF-8");
//format.put(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
transformer.setOutputProperties(format);
transformer.transform(source, result);
var is = new java.io.ByteArrayInputStream(os.toByteArray());
node.setProperty("jcr:data", is);
is.close();
nodeCount++;
println("\n"+node.getUUID()+"\n");
var content = node.getParent();
// TO UNCOMMENT WHEN READY
//content.save();
if (content.hasProperty("ametys-internal:currentStepId") && content.getProperty("ametys-internal:currentStepId").getLong() == 3)
{
//TO UNCOMMENT WHEN READY
//content.checkin();
//content.checkout();
var versionName = content.getBaseVersion().getName();
content.getVersionHistory().addVersionLabel(versionName, "Live", true);
liveCount++;
}
}
}
catch (e) { println("ERROR: "+node.getPath());}
}
// TO UNCOMMENT WHEN READY
// session.save();
println (nodeCount + " rich text(s) were processed");
println (liveCount + " live rich text(s) were processed");
var qm = session.getWorkspace().getQueryManager();
// récupération de tous les textes riches existants
var query = qm.createQuery("//element(*, ametys:richText)[@jcr:data]", javax.jcr.query.Query.XPATH);
var nodes = query.execute().getNodes();
var nodeCount = 0;
var liveCount = 0;
while (nodes.hasNext())
{
var node = nodes.next();
try
{
// contenu du texte riche
var is = node.getProperty("jcr:data").getStream();
var builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
var doc = builder.parse(is);
is.close();
var needUpdate = false;
// parcours des liens contenus dans le texte riche
var links = org.apache.xpath.XPathAPI.selectNodeList(doc, "//link[@type='page']", doc.getDocumentElement());
for (var i = 0; i < links.getLength(); i++)
{
var linkNeedUpdate = false;
var link = links.item(i);
var href = link.getAttributes().getNamedItem("xlink:href").getNodeValue();
// traitement des liens vers une formation, parcours ou ELP
if (href.indexOf("program://") == 0)
{
// les anciennes url sont de la forme program://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://art-lettres-langue-ALL/master-lmd-XB?rootId=page://xxx&programId=program://yyy
var index = href.indexOf('?');
var oldPath = href.substring("program://".length, index);
// println(node.getUUID() + " - "+ href);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length == 2)
{
newPath = "_root";
linkNeedUpdate = true;
}
else if (pathSegment.length > 2)
{
linkNeedUpdate = true;
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
}
if (linkNeedUpdate)
{
// calcul du nouveau lien
// les nouvelles url sont de la forme program://master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://_root?rootId=page://xxx&programId=program://yyy
var newHref = "program://" + newPath + href.substring(index);
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
else if (href.indexOf("course://") == 0 && href.indexOf("&programId=") == -1)
{
var index = href.indexOf('?');
var oldPath = href.substring("course://".length, index);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length > 2)
{
// les anciennes url sont de la forme course://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
// course://licence-professionnelle-design-program-fruai3182988bprh7agzrd6?rootId=...
var programName = pathSegment[2].substring (pathSegment[2].indexOf('program-'));
query = qm.createQuery("//" + programName, javax.jcr.query.Query.XPATH);
nodes = query.execute().getNodes();
var program = nodes.hasNext() ? nodes.next() : null;
// calcul du nouveau lien
// les nouvelles url sont de la forme course://master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy&programId=zzz
var newHref = "course://" + newPath + href.substring(index);
if (program != null)
{
newHref += "&programId=program://" + program.getId();
}
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
}
if (needUpdate)
{
// mise à jour du contenu du texte riche avec les liens modifiés
var os = new java.io.ByteArrayOutputStream();
var result = new javax.xml.transform.stream.StreamResult(os);
var source = new javax.xml.transform.dom.DOMSource(doc);
var transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
var format = new java.util.Properties();
format.put(javax.xml.transform.OutputKeys.METHOD, "xml");
//format.put(javax.xml.transform.OutputKeys.INDENT, "yes");
format.put(javax.xml.transform.OutputKeys.ENCODING, "UTF-8");
//format.put(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
transformer.setOutputProperties(format);
transformer.transform(source, result);
var is = new java.io.ByteArrayInputStream(os.toByteArray());
node.setProperty("jcr:data", is);
is.close();
nodeCount++;
println("\n"+node.getUUID()+"\n");
var content = node.getParent();
// TO UNCOMMENT WHEN READY
//content.save();
if (content.hasProperty("ametys-internal:currentStepId") && content.getProperty("ametys-internal:currentStepId").getLong() == 3)
{
//TO UNCOMMENT WHEN READY
//content.checkin();
//content.checkout();
var versionName = content.getBaseVersion().getName();
content.getVersionHistory().addVersionLabel(versionName, "Live", true);
liveCount++;
}
}
}
catch (e) { println("ERROR: "+node.getPath());}
}
// TO UNCOMMENT WHEN READY
// session.save();
println (nodeCount + " rich text(s) were processed");
println (liveCount + " live rich text(s) were processed");
var qm = session.getWorkspace().getQueryManager();
// récupération de tous les textes riches existants
var query = qm.createQuery("//element(*, ametys:richText)[@jcr:data]", javax.jcr.query.Query.XPATH);
var nodes = query.execute().getNodes();
var nodeCount = 0;
var liveCount = 0;
while (nodes.hasNext())
{
var node = nodes.next();
try
{
// contenu du texte riche
var is = node.getProperty("jcr:data").getStream();
var builder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
var doc = builder.parse(is);
is.close();
var needUpdate = false;
// parcours des liens contenus dans le texte riche
var links = org.apache.xpath.XPathAPI.selectNodeList(doc, "//link[@type='page']", doc.getDocumentElement());
for (var i = 0; i < links.getLength(); i++)
{
var linkNeedUpdate = false;
var link = links.item(i);
var href = link.getAttributes().getNamedItem("xlink:href").getNodeValue();
// traitement des liens vers une formation, parcours ou ELP
if (href.indexOf("program://") == 0)
{
// les anciennes url sont de la forme program://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://art-lettres-langue-ALL/master-lmd-XB?rootId=page://xxx&programId=program://yyy
var index = href.indexOf('?');
var oldPath = href.substring("program://".length, index);
// println(node.getUUID() + " - "+ href);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length == 2)
{
newPath = "_root";
linkNeedUpdate = true;
}
else if (pathSegment.length > 2)
{
linkNeedUpdate = true;
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
}
if (linkNeedUpdate)
{
// calcul du nouveau lien
// les nouvelles url sont de la forme program://master-design-program-fruai3182988bprh7agzrd6?rootId=page://xxxx&programId=subProgram://yyyy
// ou program://_root?rootId=page://xxx&programId=program://yyy
var newHref = "program://" + newPath + href.substring(index);
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
else if (href.indexOf("course://") == 0 && href.indexOf("&programId=") == -1)
{
var index = href.indexOf('?');
var oldPath = href.substring("course://".length, index);
var pathSegment = oldPath.split("/");
var newPath;
if (pathSegment.length > 2)
{
// les anciennes url sont de la forme course://art-lettres-langue-ALL/master-lmd-XB/master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy
newPath = org.apache.commons.lang.StringUtils.join(pathSegment, '/', 2, pathSegment.length);
// course://licence-professionnelle-design-program-fruai3182988bprh7agzrd6?rootId=...
var programName = pathSegment[2].substring (pathSegment[2].indexOf('program-'));
query = qm.createQuery("//" + programName, javax.jcr.query.Query.XPATH);
nodes = query.execute().getNodes();
var program = nodes.hasNext() ? nodes.next() : null;
// calcul du nouveau lien
// les nouvelles url sont de la forme course://master-design-program-fruai3182988bprh7agzrd6?rootId=xxx&courseId=yyy&programId=zzz
var newHref = "course://" + newPath + href.substring(index);
if (program != null)
{
newHref += "&programId=program://" + program.getId();
}
println(href + " \n=>\n " + newHref + "'");
println("");
link.getAttributes().getNamedItem("xlink:href").setNodeValue(newHref);
needUpdate = true;
}
}
}
if (needUpdate)
{
// mise à jour du contenu du texte riche avec les liens modifiés
var os = new java.io.ByteArrayOutputStream();
var result = new javax.xml.transform.stream.StreamResult(os);
var source = new javax.xml.transform.dom.DOMSource(doc);
var transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
var format = new java.util.Properties();
format.put(javax.xml.transform.OutputKeys.METHOD, "xml");
//format.put(javax.xml.transform.OutputKeys.INDENT, "yes");
format.put(javax.xml.transform.OutputKeys.ENCODING, "UTF-8");
//format.put(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
transformer.setOutputProperties(format);
transformer.transform(source, result);
var is = new java.io.ByteArrayInputStream(os.toByteArray());
node.setProperty("jcr:data", is);
is.close();
nodeCount++;
println("\n"+node.getUUID()+"\n");
var content = node.getParent();
// TO UNCOMMENT WHEN READY
//content.save();
if (content.hasProperty("ametys-internal:currentStepId") && content.getProperty("ametys-internal:currentStepId").getLong() == 3)
{
//TO UNCOMMENT WHEN READY
//content.checkin();
//content.checkout();
var versionName = content.getBaseVersion().getName();
content.getVersionHistory().addVersionLabel(versionName, "Live", true);
liveCount++;
}
}
}
catch (e) { println("ERROR: "+node.getPath());}
}
// TO UNCOMMENT WHEN READY
// session.save();
println (nodeCount + " rich text(s) were processed");
println (liveCount + " live rich text(s) were processed");
Auto-completion on the training and UE search engine
Adding auto-completion to the "keywords" search field requires content to be re-indexed.