diff --git a/config/alfresco/templates/webscripts/org/alfresco/slingshot/wiki/version.get.js b/config/alfresco/templates/webscripts/org/alfresco/slingshot/wiki/version.get.js deleted file mode 100644 index d36bdcdbbf..0000000000 --- a/config/alfresco/templates/webscripts/org/alfresco/slingshot/wiki/version.get.js +++ /dev/null @@ -1,48 +0,0 @@ - - -function main() -{ - var params = getTemplateArgs(["siteId", "pageTitle", "versionId"]); - var content = ""; - // Get the site - var site = siteService.getSite(params.siteId); - if (site === null) - { - return ""; - } - - var wiki = getWikiContainer(site); - if (wiki === null) - { - return ""; - } - - var page = wiki.childByNamePath(params.pageTitle); - if (page === null) - { - return ""; - } - - var version; - var versions = page.versionHistory; - // NOTE: would it be possible to pass in the noderef and do a search for the specific - // version (directly) against the "lightWeightVersionStore"? This would depend on what - // indexing (if any) there is on the version store. - for (var i=0; i < versions.length; i++) - { - version = versions[i]; - versionNode = version.node; - - // If we don't create a string explicitly the comparison fails - if (String(versionNode.id) === params.versionId || - String(version.label) == params.versionId) - { - content = versionNode.content; - break; - } - } - - return content; -} - -model.content = main(); diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index b486d57005..816f9bea85 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1577,6 +1577,14 @@ parent="abstractWikiWebScript"> + + + + + + . + */ +package org.alfresco.repo.web.scripts.wiki; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.alfresco.model.ContentModel; +import org.alfresco.service.cmr.repository.AspectMissingException; +import org.alfresco.service.cmr.repository.ContentReader; +import org.alfresco.service.cmr.repository.ContentService; +import org.alfresco.service.cmr.site.SiteInfo; +import org.alfresco.service.cmr.version.Version; +import org.alfresco.service.cmr.version.VersionDoesNotExistException; +import org.alfresco.service.cmr.version.VersionHistory; +import org.alfresco.service.cmr.version.VersionService; +import org.alfresco.service.cmr.wiki.WikiPageInfo; +import org.json.JSONObject; +import org.springframework.extensions.webscripts.Cache; +import org.springframework.extensions.webscripts.Status; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; + +/** + * This class is the controller for the wiki page version fetching version.get webscript. + * + * @author Nick Burch + * @since 4.0 + */ +public class WikiPageVersionGet extends AbstractWikiWebScript +{ + // For spotting if a version string is an ID or a Label + private static final Pattern LABEL_PATTERN = Pattern.compile("\\d+\\.\\d+"); + + private static final String PARAM_CONTENT = "content"; + + private ContentService contentService; + private VersionService versionService; + public void setVersionService(VersionService versionService) + { + this.versionService = versionService; + } + public void setContentService(ContentService contentService) + { + this.contentService = contentService; + } + + + @Override + protected Map executeImpl(SiteInfo site, String pageName, + WebScriptRequest req, JSONObject json, Status status, Cache cache) { + Map model = new HashMap(); + + // Grab the version string + Map templateVars = req.getServiceMatch().getTemplateVars(); + String versionId = templateVars.get("versionId"); + if(versionId == null) + { + String error = "No versionId supplied"; + throw new WebScriptException(Status.STATUS_BAD_REQUEST, error); + } + + // Try to find the page + WikiPageInfo page = wikiService.getWikiPage(site.getShortName(), pageName); + if(page == null) + { + String message = "The Wiki Page could not be found"; + status.setCode(Status.STATUS_NOT_FOUND); + status.setMessage(message); + + // Return an empty string though + model.put(PARAM_CONTENT, ""); + return model; + } + + + // Fetch the version history for the node + VersionHistory versionHistory = null; + Version version = null; + try + { + versionHistory = versionService.getVersionHistory(page.getNodeRef()); + } + catch(AspectMissingException e) {} + + if(versionHistory == null) + { + // Not been versioned, return an empty string + model.put(PARAM_CONTENT, ""); + return model; + } + + + // Fetch the version by either ID or Label + Matcher m = LABEL_PATTERN.matcher(versionId); + if(m.matches()) + { + // It's a version label like 2.3 + try + { + version = versionHistory.getVersion(versionId); + } + catch(VersionDoesNotExistException e) {} + } + else + { + // It's a version ID like ed00bac1-f0da-4042-8598-45a0d39cb74d + // (The ID is usually part of the NodeRef of the frozen node, but we + // don't assume to be able to just generate the full NodeRef) + for(Version v : versionHistory.getAllVersions()) + { + if(v.getFrozenStateNodeRef().getId().equals(versionId)) + { + version = v; + } + } + } + + + // Did we find the right version in the end? + String contents; + if(version != null) + { + ContentReader reader = contentService.getReader(version.getFrozenStateNodeRef(), ContentModel.PROP_CONTENT); + if(reader != null) + { + contents = reader.getContentString(); + } + else + { + // No content was stored in the version history + contents = ""; + } + } + else + { + // No warning of the missing version, just return an empty string + contents = ""; + } + + // All done + model.put(PARAM_CONTENT, contents); + model.put("page", page); + model.put("site", site); + model.put("siteId", site.getShortName()); + return model; + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/wiki/WikiRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/wiki/WikiRestApiTest.java index becdebd2ac..b8a618085f 100644 --- a/source/java/org/alfresco/repo/web/scripts/wiki/WikiRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/wiki/WikiRestApiTest.java @@ -555,7 +555,7 @@ public class WikiRestApiTest extends BaseWebScriptTest // Fetch it at this version String content = getPageAtVersion(name, "1.0", Status.STATUS_OK); -// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing + assertEquals(PAGE_CONTENTS_ONE, content); // Upload a new copy without a version flag, denied @@ -581,7 +581,7 @@ public class WikiRestApiTest extends BaseWebScriptTest content = getPageAtVersion(name, "1.1", Status.STATUS_OK); assertEquals(PAGE_CONTENTS_CHANGED, content); content = getPageAtVersion(name, "1.0", Status.STATUS_OK); -// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing + assertEquals(PAGE_CONTENTS_ONE, content); // Upload a new copy with the force flag, allowed @@ -606,7 +606,7 @@ public class WikiRestApiTest extends BaseWebScriptTest content = getPageAtVersion(name, "1.1", Status.STATUS_OK); assertEquals(PAGE_CONTENTS_CHANGED, content); content = getPageAtVersion(name, "1.0", Status.STATUS_OK); -// assertEquals(PAGE_CONTENTS_ONE, content); // TODO Fix the initial version content storing + assertEquals(PAGE_CONTENTS_ONE, content); // You get an empty string back for invalid versions content = getPageAtVersion(name, "1.4", Status.STATUS_OK);