001
014
015 package com.liferay.portlet.wiki.action;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.util.DiffResult;
020 import com.liferay.portal.kernel.util.DiffUtil;
021 import com.liferay.portal.kernel.util.HtmlUtil;
022 import com.liferay.portal.kernel.util.HttpUtil;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.struts.PortletAction;
025 import com.liferay.portal.theme.ThemeDisplay;
026 import com.liferay.portal.util.WebKeys;
027 import com.liferay.portlet.wiki.NoSuchPageException;
028 import com.liferay.portlet.wiki.model.WikiNode;
029 import com.liferay.portlet.wiki.model.WikiPage;
030 import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
031 import com.liferay.portlet.wiki.util.WikiUtil;
032
033 import java.util.List;
034
035 import javax.portlet.PortletConfig;
036 import javax.portlet.PortletURL;
037 import javax.portlet.RenderRequest;
038 import javax.portlet.RenderResponse;
039
040 import org.apache.struts.action.ActionForm;
041 import org.apache.struts.action.ActionForward;
042 import org.apache.struts.action.ActionMapping;
043
044
048 public class CompareVersionsAction extends PortletAction {
049
050 public ActionForward render(
051 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052 RenderRequest renderRequest, RenderResponse renderResponse)
053 throws Exception {
054
055 try {
056 ActionUtil.getNode(renderRequest);
057 ActionUtil.getPage(renderRequest);
058
059 compareVersions(renderRequest, renderResponse);
060 }
061 catch (Exception e) {
062 if (e instanceof NoSuchPageException) {
063
064 SessionErrors.add(renderRequest, e.getClass().getName());
065
066 return mapping.findForward("portlet.wiki.error");
067 }
068 else {
069 throw e;
070 }
071 }
072
073 return mapping.findForward("portlet.wiki.compare_versions");
074 }
075
076 protected void compareVersions(
077 RenderRequest renderRequest, RenderResponse renderResponse)
078 throws Exception {
079
080 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
081 WebKeys.THEME_DISPLAY);
082
083 long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
084
085 String title = ParamUtil.getString(renderRequest, "title");
086
087 double sourceVersion = ParamUtil.getDouble(
088 renderRequest, "sourceVersion");
089 double targetVersion = ParamUtil.getDouble(
090 renderRequest, "targetVersion");
091 String type = ParamUtil.getString(renderRequest, "type", "escape");
092
093 WikiPage sourcePage = WikiPageServiceUtil.getPage(
094 nodeId, title, sourceVersion);
095 WikiPage targetPage = WikiPageServiceUtil.getPage(
096 nodeId, title, targetVersion);
097
098 if (type.equals("html")) {
099 WikiNode sourceNode = sourcePage.getNode();
100
101 PortletURL viewPageURL = renderResponse.createRenderURL();
102
103 viewPageURL.setParameter("struts_action", "/wiki/view");
104 viewPageURL.setParameter("nodeName", sourceNode.getName());
105
106 PortletURL editPageURL = renderResponse.createRenderURL();
107
108 editPageURL.setParameter("struts_action", "/wiki/edit_page");
109 editPageURL.setParameter("nodeId", String.valueOf(nodeId));
110 editPageURL.setParameter("title", title);
111
112 String attachmentURLPrefix =
113 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
114 "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
115 "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
116
117 String htmlDiffResult = WikiUtil.diffHtml(
118 sourcePage, targetPage, viewPageURL, editPageURL,
119 attachmentURLPrefix);
120
121 renderRequest.setAttribute(
122 WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
123 }
124 else {
125 String sourceContent = sourcePage.getContent();
126 String targetContent = targetPage.getContent();
127
128 sourceContent = WikiUtil.processContent(sourceContent);
129 targetContent = WikiUtil.processContent(targetContent);
130
131 if (type.equals("escape")) {
132 sourceContent = HtmlUtil.escape(sourceContent);
133 targetContent = HtmlUtil.escape(targetContent);
134 }
135 else if (type.equals("strip")) {
136 sourceContent = HtmlUtil.extractText(sourceContent);
137 targetContent = HtmlUtil.extractText(targetContent);
138 }
139
140 List<DiffResult>[] diffResults = DiffUtil.diff(
141 new UnsyncStringReader(sourceContent),
142 new UnsyncStringReader(targetContent));
143
144 renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
145 }
146
147 renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
148 renderRequest.setAttribute(WebKeys.TITLE, title);
149 renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
150 renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
151 }
152
153 }