1
22
23 package com.liferay.portlet.wiki.util;
24
25 import com.germinus.easyconf.Filter;
26
27 import com.liferay.portal.kernel.portlet.LiferayPortletURL;
28 import com.liferay.portal.kernel.util.GetterUtil;
29 import com.liferay.portal.kernel.util.HttpUtil;
30 import com.liferay.portal.kernel.util.InstancePool;
31 import com.liferay.portal.kernel.util.StringMaker;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.util.ContentUtil;
36 import com.liferay.portal.util.PropsUtil;
37 import com.liferay.portal.util.PropsValues;
38 import com.liferay.portlet.wiki.PageContentException;
39 import com.liferay.portlet.wiki.WikiFormatException;
40 import com.liferay.portlet.wiki.engines.WikiEngine;
41 import com.liferay.portlet.wiki.model.WikiPage;
42
43 import java.io.IOException;
44
45 import java.util.Collections;
46 import java.util.HashMap;
47 import java.util.Map;
48 import java.util.regex.Matcher;
49 import java.util.regex.Pattern;
50
51 import javax.portlet.PortletPreferences;
52 import javax.portlet.PortletURL;
53
54
61 public class WikiUtil {
62
63 public static final String POP_PORTLET_PREFIX = "wiki.";
64
65 public static String convert(
66 WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
67 String attachmentURLPrefix)
68 throws PageContentException, WikiFormatException {
69
70 return _instance._convert(
71 page, viewPageURL, editPageURL, attachmentURLPrefix);
72 }
73
74 public static String getEditPage(String format) {
75 return _instance._getEditPage(format);
76 }
77
78 public static String getEmailFromAddress(PortletPreferences prefs) {
79 String emailFromAddress = PropsUtil.get(
80 PropsUtil.WIKI_EMAIL_FROM_ADDRESS);
81
82 return prefs.getValue("email-from-address", emailFromAddress);
83 }
84
85 public static String getEmailFromName(PortletPreferences prefs) {
86 String emailFromName = PropsUtil.get(PropsUtil.WIKI_EMAIL_FROM_NAME);
87
88 return prefs.getValue("email-from-name", emailFromName);
89 }
90
91 public static boolean getEmailPageAddedEnabled(
92 PortletPreferences prefs) {
93
94 String emailPageAddedEnabled = prefs.getValue(
95 "email-page-added-enabled", StringPool.BLANK);
96
97 if (Validator.isNotNull(emailPageAddedEnabled)) {
98 return GetterUtil.getBoolean(emailPageAddedEnabled);
99 }
100 else {
101 return GetterUtil.getBoolean(PropsUtil.get(
102 PropsUtil.WIKI_EMAIL_PAGE_ADDED_ENABLED));
103 }
104 }
105
106 public static String getEmailPageAddedBody(PortletPreferences prefs)
107 throws IOException {
108
109 String emailPageAddedBody = prefs.getValue(
110 "email-page-added-body", StringPool.BLANK);
111
112 if (Validator.isNotNull(emailPageAddedBody)) {
113 return emailPageAddedBody;
114 }
115 else {
116 return ContentUtil.get(PropsUtil.get(
117 PropsUtil.WIKI_EMAIL_PAGE_ADDED_BODY));
118 }
119 }
120
121 public static String getEmailPageAddedSignature(PortletPreferences prefs)
122 throws IOException {
123
124 String emailPageAddedSignature = prefs.getValue(
125 "email-page-added-signature", StringPool.BLANK);
126
127 if (Validator.isNotNull(emailPageAddedSignature)) {
128 return emailPageAddedSignature;
129 }
130 else {
131 return ContentUtil.get(PropsUtil.get(
132 PropsUtil.WIKI_EMAIL_PAGE_ADDED_SIGNATURE));
133 }
134 }
135
136 public static String getEmailPageAddedSubjectPrefix(
137 PortletPreferences prefs)
138 throws IOException {
139
140 String emailPageAddedSubjectPrefix = prefs.getValue(
141 "email-page-added-subject-prefix", StringPool.BLANK);
142
143 if (Validator.isNotNull(emailPageAddedSubjectPrefix)) {
144 return emailPageAddedSubjectPrefix;
145 }
146 else {
147 return ContentUtil.get(PropsUtil.get(
148 PropsUtil.WIKI_EMAIL_PAGE_ADDED_SUBJECT_PREFIX));
149 }
150 }
151
152 public static boolean getEmailPageUpdatedEnabled(
153 PortletPreferences prefs) {
154
155 String emailPageUpdatedEnabled = prefs.getValue(
156 "email-page-updated-enabled", StringPool.BLANK);
157
158 if (Validator.isNotNull(emailPageUpdatedEnabled)) {
159 return GetterUtil.getBoolean(emailPageUpdatedEnabled);
160 }
161 else {
162 return GetterUtil.getBoolean(PropsUtil.get(
163 PropsUtil.WIKI_EMAIL_PAGE_UPDATED_ENABLED));
164 }
165 }
166
167 public static String getEmailPageUpdatedBody(PortletPreferences prefs)
168 throws IOException {
169
170 String emailPageUpdatedBody = prefs.getValue(
171 "email-page-updated-body", StringPool.BLANK);
172
173 if (Validator.isNotNull(emailPageUpdatedBody)) {
174 return emailPageUpdatedBody;
175 }
176 else {
177 return ContentUtil.get(PropsUtil.get(
178 PropsUtil.WIKI_EMAIL_PAGE_UPDATED_BODY));
179 }
180 }
181
182 public static String getEmailPageUpdatedSignature(
183 PortletPreferences prefs)
184 throws IOException {
185
186 String emailPageUpdatedSignature = prefs.getValue(
187 "email-page-updated-signature", StringPool.BLANK);
188
189 if (Validator.isNotNull(emailPageUpdatedSignature)) {
190 return emailPageUpdatedSignature;
191 }
192 else {
193 return ContentUtil.get(PropsUtil.get(
194 PropsUtil.WIKI_EMAIL_PAGE_UPDATED_SIGNATURE));
195 }
196 }
197
198 public static String getEmailPageUpdatedSubjectPrefix(
199 PortletPreferences prefs)
200 throws IOException {
201
202 String emailPageUpdatedSubject = prefs.getValue(
203 "email-page-updated-subject-prefix", StringPool.BLANK);
204
205 if (Validator.isNotNull(emailPageUpdatedSubject)) {
206 return emailPageUpdatedSubject;
207 }
208 else {
209 return ContentUtil.get(PropsUtil.get(
210 PropsUtil.WIKI_EMAIL_PAGE_UPDATED_SUBJECT_PREFIX));
211 }
212 }
213
214 public static String getHelpPage(String format) {
215 return _instance._getHelpPage(format);
216 }
217
218 public static String getHelpURL(String format) {
219 return _instance._getHelpURL(format);
220 }
221
222 public static Map<String, Boolean> getLinks(WikiPage page)
223 throws PageContentException, WikiFormatException {
224
225 return _instance._getLinks(page);
226 }
227
228 public static boolean isLinkedTo(WikiPage page, String title)
229 throws PageContentException, WikiFormatException {
230
231 return _instance._isLinkedTo(page, title);
232 }
233
234 public static String getMailId(String mx, long nodeId, long pageId) {
235 StringMaker sm = new StringMaker();
236
237 sm.append(StringPool.LESS_THAN);
238 sm.append(POP_PORTLET_PREFIX);
239 sm.append(nodeId);
240 sm.append(StringPool.PERIOD);
241 sm.append(pageId);
242 sm.append(StringPool.AT);
243 sm.append(PropsValues.POP_SERVER_SUBDOMAIN);
244 sm.append(StringPool.PERIOD);
245 sm.append(mx);
246 sm.append(StringPool.GREATER_THAN);
247
248 return sm.toString();
249 }
250
251 public static String processContent(String content) {
252 content = content.replaceAll("</p>", "</p>\n");
253 content = content.replaceAll("</br>", "</br>\n");
254 content = content.replaceAll("</div>", "</div>\n");
255
256 return content;
257 }
258
259 public static boolean validate(
260 long nodeId, String content, String format)
261 throws WikiFormatException {
262
263 return _instance._validate(nodeId, content, format);
264 }
265
266 private String _convert(
267 WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
268 String attachmentURLPrefix)
269 throws PageContentException, WikiFormatException {
270
271 LiferayPortletURL liferayViewPageURL = (LiferayPortletURL)viewPageURL;
272 LiferayPortletURL liferayEditPageURL = (LiferayPortletURL)editPageURL;
273
274 WikiEngine engine = _getEngine(page.getFormat());
275
276 String content = engine.convert(page, editPageURL);
277
278 liferayEditPageURL.setParameter("title", "__REPLACEMENT__", false);
279
280 String editPageURLString = editPageURL.toString();
281
282 editPageURLString = StringUtil.replace(
283 editPageURLString, "__REPLACEMENT__", "$1");
284
285 Matcher matcher = _EDIT_PAGE_URL_PATTERN.matcher(content);
286
287 content = matcher.replaceAll(editPageURLString);
288
289 liferayViewPageURL.setParameter("title", "__REPLACEMENT__", false);
290
291 String viewPageURLString = viewPageURL.toString();
292
293 viewPageURLString = StringUtil.replace(
294 viewPageURLString, "__REPLACEMENT__", "$1");
295
296 matcher = _VIEW_PAGE_URL_PATTERN.matcher(content);
297
298 content = matcher.replaceAll(viewPageURLString);
299
300 content = _replaceAttachments(
301 content, page.getTitle(), attachmentURLPrefix);
302
303 return content;
304 }
305
306 private String _getEditPage(String format) {
307 return PropsUtil.getComponentProperties().getString(
308 PropsUtil.WIKI_FORMATS_EDIT_PAGE, Filter.by(format));
309 }
310
311 private WikiEngine _getEngine(String format) throws WikiFormatException {
312 WikiEngine engine = _engines.get(format);
313
314 if (engine == null) {
315 try {
316 String engineClass =
317 PropsUtil.getComponentProperties().getString(
318 PropsUtil.WIKI_FORMATS_ENGINE, Filter.by(format));
319
320 if (engineClass != null) {
321 if (!InstancePool.contains(engineClass)) {
322 engine = (WikiEngine)InstancePool.get(engineClass);
323
324 engine.setMainConfiguration(
325 _readConfigurationFile(
326 PropsUtil.WIKI_FORMATS_CONFIGURATION_MAIN,
327 format));
328
329 engine.setInterWikiConfiguration(
330 _readConfigurationFile(
331 PropsUtil.WIKI_FORMATS_CONFIGURATION_INTERWIKI,
332 format));
333 }
334 else {
335 engine = (WikiEngine)InstancePool.get(engineClass);
336 }
337
338 _engines.put(format, engine);
339 }
340 }
341 catch (Exception e) {
342 throw new WikiFormatException(e);
343 }
344
345 if (engine == null) {
346 throw new WikiFormatException(format);
347 }
348 }
349
350 return engine;
351 }
352
353 private String _getHelpPage(String format) {
354 return PropsUtil.getComponentProperties().getString(
355 PropsUtil.WIKI_FORMATS_HELP_PAGE, Filter.by(format));
356 }
357
358 private String _getHelpURL(String format) {
359 return PropsUtil.getComponentProperties().getString(
360 PropsUtil.WIKI_FORMATS_HELP_URL, Filter.by(format));
361 }
362
363 private Map<String, Boolean> _getLinks(WikiPage page)
364 throws PageContentException, WikiFormatException{
365
366 try {
367 return _getEngine(page.getFormat()).getOutgoingLinks(page);
368 }
369 catch (WikiFormatException wfe) {
370 return Collections.EMPTY_MAP;
371 }
372 }
373
374 private boolean _isLinkedTo(WikiPage page, String targetTitle)
375 throws PageContentException, WikiFormatException {
376
377 try {
378 return _getEngine(page.getFormat()).isLinkedTo(page, targetTitle);
379 }
380 catch (WikiFormatException wfe) {
381 return false;
382 }
383 }
384
385 private String _readConfigurationFile(String propertyName, String format)
386 throws IOException {
387
388 ClassLoader classLoader = getClass().getClassLoader();
389
390 String configurationFile = PropsUtil.getComponentProperties().getString(
391 propertyName, Filter.by(format));
392
393 if (Validator.isNotNull(configurationFile)) {
394 return HttpUtil.URLtoString(
395 classLoader.getResource(configurationFile));
396 }
397 else {
398 return StringPool.BLANK;
399 }
400 }
401
402 private String _replaceAttachments(
403 String content, String title, String attachmentURLPrefix) {
404
405 content = StringUtil.replace(content, "[$WIKI_PAGE_NAME$]", title);
406
407 content = StringUtil.replace(
408 content, "[$ATTACHMENT_URL_PREFIX$]", attachmentURLPrefix);
409
410 return content;
411 }
412
413 private boolean _validate(long nodeId, String content, String format)
414 throws WikiFormatException {
415
416 return _getEngine(format).validate(nodeId, content);
417 }
418
419 private static final Pattern _EDIT_PAGE_URL_PATTERN = Pattern.compile(
420 "\\[\\$BEGIN_PAGE_TITLE_EDIT\\$\\](.*?)\\[\\$END_PAGE_TITLE_EDIT\\$\\]");
421
422 private static final Pattern _VIEW_PAGE_URL_PATTERN = Pattern.compile(
423 "\\[\\$BEGIN_PAGE_TITLE\\$\\](.*?)\\[\\$END_PAGE_TITLE\\$\\]");
424
425 private static WikiUtil _instance = new WikiUtil();
426
427 private Map<String, WikiEngine> _engines =
428 new HashMap<String, WikiEngine>();
429
430 }