001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.engines.mediawiki;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portlet.wiki.PageContentException;
021    import com.liferay.portlet.wiki.engines.WikiEngine;
022    import com.liferay.portlet.wiki.engines.mediawiki.matchers.EditURLMatcher;
023    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ImageTagMatcher;
024    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ImageURLMatcher;
025    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ViewURLMatcher;
026    import com.liferay.portlet.wiki.model.WikiPage;
027    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    import javax.portlet.PortletURL;
033    
034    import org.apache.commons.lang.LocaleUtils;
035    
036    import org.jamwiki.model.WikiUser;
037    import org.jamwiki.parser.ParserException;
038    import org.jamwiki.parser.ParserInput;
039    import org.jamwiki.parser.ParserOutput;
040    import org.jamwiki.parser.ParserUtil;
041    import org.jamwiki.parser.TableOfContents;
042    
043    /**
044     * @author Jonathan Potter
045     */
046    public class MediaWikiEngine implements WikiEngine {
047    
048            public String convert(
049                            WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
050                            String attachmentURLPrefix)
051                    throws PageContentException {
052    
053                    String html = parsePage(page, new ParserOutput());
054    
055                    html = postParsePage(
056                            html, viewPageURL, editPageURL, attachmentURLPrefix);
057    
058                    return html;
059            }
060    
061            public Map<String, Boolean> getOutgoingLinks(WikiPage page)
062                    throws PageContentException {
063    
064                    ParserOutput parserOutput = getParserOutput(page);
065    
066                    Map<String, Boolean> outgoingLinks = new HashMap<String, Boolean>();
067    
068                    for (String title : parserOutput.getLinks()) {
069                            Boolean existsObj = outgoingLinks.get(title);
070    
071                            if (existsObj == null) {
072                                    int pagesCount = 0;
073    
074                                    try {
075                                            pagesCount = WikiPageLocalServiceUtil.getPagesCount(
076                                                    page.getNodeId(), title, true);
077                                    }
078                                    catch (SystemException se) {
079                                            throw new PageContentException(se);
080                                    }
081    
082                                    if (pagesCount > 0) {
083                                            existsObj = Boolean.TRUE;
084                                    }
085                                    else {
086                                            existsObj = Boolean.FALSE;
087    
088                                            // JAMWiki turns images into links. The postProcess method
089                                            // turns them back to images, but the getOutgoingLinks does
090                                            // not call postProcess, so we must manual process this
091                                            // case.
092    
093                                            if (StringUtil.startsWith(title, "image:")) {
094                                                    continue;
095                                            }
096                                    }
097    
098                                    outgoingLinks.put(title, existsObj);
099                            }
100                    }
101    
102                    return outgoingLinks;
103            }
104    
105            public void setInterWikiConfiguration(String interWikiConfiguration) {
106            }
107    
108            public void setMainConfiguration(String mainConfiguration) {
109            }
110    
111            public boolean validate(long nodeId, String content) {
112                    return true;
113            }
114    
115            protected ParserInput getParserInput(long nodeId, String topicName) {
116                    ParserInput parserInput = new ParserInput(
117                            "Special:Node:" + nodeId, topicName);
118    
119                    // Dummy values
120    
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                    // Useful values
127    
128                    parserInput.setAllowSectionEdit(false);
129    
130                    // Table of contents
131    
132                    TableOfContents tableOfContents = new TableOfContents();
133    
134                    tableOfContents.setForceTOC(true);
135    
136                    parserInput.setTableOfContents(tableOfContents);
137    
138                    return parserInput;
139            }
140    
141            protected ParserOutput getParserOutput(WikiPage page)
142                    throws PageContentException {
143    
144                    ParserInput parserInput = getParserInput(
145                            page.getNodeId(), page.getTitle());
146    
147                    ParserOutput parserOutput = null;
148    
149                    try {
150                            parserOutput = ParserUtil.parseMetadata(
151                                    parserInput, page.getContent());
152                    }
153                    catch (ParserException pe) {
154                            throw new PageContentException(pe);
155                    }
156    
157                    return parserOutput;
158            }
159    
160            protected String parsePage(WikiPage page, ParserOutput parserOutput)
161                    throws PageContentException {
162    
163                    ParserInput parserInput = getParserInput(
164                            page.getNodeId(), page.getTitle());
165    
166                    String content = StringPool.BLANK;
167    
168                    try {
169                            content = page.getContent();
170    
171                            ImageTagMatcher imageTagMatcher = new ImageTagMatcher();
172    
173                            content = ParserUtil.parse(
174                                    parserInput, parserOutput,
175                                    imageTagMatcher.replaceMatches(content));
176                    }
177                    catch (ParserException pe) {
178                            throw new PageContentException(pe);
179                    }
180    
181                    return content;
182            }
183    
184            protected String postParsePage(
185                    String content, PortletURL viewPageURL, PortletURL editPageURL,
186                    String attachmentURLPrefix) {
187    
188                    if (editPageURL != null) {
189                            EditURLMatcher editURLMatcher = new EditURLMatcher(editPageURL);
190    
191                            content = editURLMatcher.replaceMatches(content);
192                    }
193    
194                    if (attachmentURLPrefix != null) {
195                            ImageURLMatcher imageURLMatcher = new ImageURLMatcher(
196                                    attachmentURLPrefix);
197    
198                            content = imageURLMatcher.replaceMatches(content);
199                    }
200    
201                    if (viewPageURL != null) {
202                            ViewURLMatcher viewURLMatcher = new ViewURLMatcher(viewPageURL);
203    
204                            content = viewURLMatcher.replaceMatches(content);
205                    }
206    
207                    return content;
208            }
209    
210    }