1
14
15 package com.liferay.portlet.wiki.engines.mediawiki;
16
17 import com.liferay.portal.SystemException;
18 import com.liferay.portal.kernel.util.StringPool;
19 import com.liferay.portal.kernel.util.StringUtil;
20 import com.liferay.portlet.wiki.PageContentException;
21 import com.liferay.portlet.wiki.engines.WikiEngine;
22 import com.liferay.portlet.wiki.engines.mediawiki.matchers.EditURLMatcher;
23 import com.liferay.portlet.wiki.engines.mediawiki.matchers.ImageURLMatcher;
24 import com.liferay.portlet.wiki.engines.mediawiki.matchers.ViewURLMatcher;
25 import com.liferay.portlet.wiki.model.WikiPage;
26 import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import javax.portlet.PortletURL;
32
33 import org.apache.commons.lang.LocaleUtils;
34
35 import org.jamwiki.model.WikiUser;
36 import org.jamwiki.parser.ParserException;
37 import org.jamwiki.parser.ParserInput;
38 import org.jamwiki.parser.ParserOutput;
39 import org.jamwiki.parser.ParserUtil;
40 import org.jamwiki.parser.TableOfContents;
41
42
47 public class MediaWikiEngine implements WikiEngine {
48
49 public String convert(
50 WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
51 String attachmentURLPrefix)
52 throws PageContentException {
53
54 String html = parsePage(page, new ParserOutput());
55
56 html = postParsePage(
57 html, viewPageURL, editPageURL, attachmentURLPrefix);
58
59 return html;
60 }
61
62 public Map<String, Boolean> getOutgoingLinks(WikiPage page)
63 throws PageContentException {
64
65 ParserOutput parserOutput = getParserOutput(page);
66
67 Map<String, Boolean> outgoingLinks = new HashMap<String, Boolean>();
68
69 for (String title : parserOutput.getLinks()) {
70 Boolean existsObj = outgoingLinks.get(title);
71
72 if (existsObj == null) {
73 int pagesCount = 0;
74
75 try {
76 pagesCount = WikiPageLocalServiceUtil.getPagesCount(
77 page.getNodeId(), title, true);
78 }
79 catch (SystemException se) {
80 throw new PageContentException(se);
81 }
82
83 if (pagesCount > 0) {
84 existsObj = Boolean.TRUE;
85 }
86 else {
87 existsObj = Boolean.FALSE;
88
89
94 if (StringUtil.startsWith(title, "image:")) {
95 continue;
96 }
97 }
98
99 outgoingLinks.put(title.toLowerCase(), existsObj);
100 }
101 }
102
103 return outgoingLinks;
104 }
105
106 public void setInterWikiConfiguration(String interWikiConfiguration) {
107 }
108
109 public void setMainConfiguration(String mainConfiguration) {
110 }
111
112 public boolean validate(long nodeId, String content) {
113 return true;
114 }
115
116 protected ParserInput getParserInput(long nodeId, String topicName) {
117 ParserInput parserInput = new ParserInput();
118
119
121 parserInput.setContext("/wiki");
122 parserInput.setLocale(LocaleUtils.toLocale("en_US"));
123 parserInput.setUserDisplay("0.0.0.0");
124 parserInput.setWikiUser(new WikiUser("DummyUser"));
125
126
128 parserInput.setAllowSectionEdit(false);
129 parserInput.setTopicName(topicName);
130
131
133 parserInput.setVirtualWiki("Special:Node:" + nodeId);
134
135
137 TableOfContents tableOfContents = new TableOfContents();
138
139 tableOfContents.setForceTOC(true);
140
141 parserInput.setTableOfContents(tableOfContents);
142
143 return parserInput;
144 }
145
146 protected ParserOutput getParserOutput(WikiPage page)
147 throws PageContentException {
148
149 ParserInput parserInput = getParserInput(
150 page.getNodeId(), page.getTitle());
151
152 ParserOutput parserOutput = null;
153
154 try {
155 parserOutput = ParserUtil.parseMetadata(
156 parserInput, page.getContent());
157 }
158 catch (ParserException pe) {
159 throw new PageContentException(pe);
160 }
161
162 return parserOutput;
163 }
164
165 protected String parsePage(WikiPage page, ParserOutput parserOutput)
166 throws PageContentException {
167
168 ParserInput parserInput = getParserInput(
169 page.getNodeId(), page.getTitle());
170
171 String html = StringPool.BLANK;
172
173 try {
174 html = ParserUtil.parse(
175 parserInput, parserOutput, page.getContent());
176 }
177 catch (ParserException pe) {
178 throw new PageContentException(pe);
179 }
180
181 return html;
182 }
183
184 protected String postParsePage(
185 String content, PortletURL viewPageURL, PortletURL editPageURL,
186 String attachmentURLPrefix) {
187
188 EditURLMatcher editURLMatcher = new EditURLMatcher(editPageURL);
189
190 content = editURLMatcher.replaceMatches(content);
191
192 ImageURLMatcher imageURLMatcher = new ImageURLMatcher(
193 attachmentURLPrefix);
194
195 content = imageURLMatcher.replaceMatches(content);
196
197 ViewURLMatcher viewURLMatcher = new ViewURLMatcher(viewPageURL);
198
199 content = viewURLMatcher.replaceMatches(content);
200
201 return content;
202 }
203
204 }