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.LocaleUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portlet.wiki.PageContentException;
022    import com.liferay.portlet.wiki.engines.WikiEngine;
023    import com.liferay.portlet.wiki.engines.mediawiki.matchers.DirectURLMatcher;
024    import com.liferay.portlet.wiki.engines.mediawiki.matchers.EditURLMatcher;
025    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ImageTagMatcher;
026    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ImageURLMatcher;
027    import com.liferay.portlet.wiki.engines.mediawiki.matchers.ViewURLMatcher;
028    import com.liferay.portlet.wiki.model.WikiPage;
029    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    import javax.portlet.PortletURL;
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                    return parsePage(
054                            page, new ParserOutput(), viewPageURL, editPageURL,
055                            attachmentURLPrefix);
056            }
057    
058            public Map<String, Boolean> getOutgoingLinks(WikiPage page)
059                    throws PageContentException {
060    
061                    ParserOutput parserOutput = getParserOutput(page);
062    
063                    Map<String, Boolean> outgoingLinks = new HashMap<String, Boolean>();
064    
065                    for (String title : parserOutput.getLinks()) {
066                            Boolean existsObj = outgoingLinks.get(title);
067    
068                            if (existsObj == null) {
069                                    int pagesCount = 0;
070    
071                                    try {
072                                            pagesCount = WikiPageLocalServiceUtil.getPagesCount(
073                                                    page.getNodeId(), title, true);
074                                    }
075                                    catch (SystemException se) {
076                                            throw new PageContentException(se);
077                                    }
078    
079                                    if (pagesCount > 0) {
080                                            existsObj = Boolean.TRUE;
081                                    }
082                                    else {
083                                            existsObj = Boolean.FALSE;
084    
085                                            // JAMWiki turns images into links. The postProcess method
086                                            // turns them back to images, but the getOutgoingLinks does
087                                            // not call postProcess, so we must manual process this
088                                            // case.
089    
090                                            if (StringUtil.startsWith(title, "image:")) {
091                                                    continue;
092                                            }
093                                    }
094    
095                                    outgoingLinks.put(title, existsObj);
096                            }
097                    }
098    
099                    return outgoingLinks;
100            }
101    
102            public void setInterWikiConfiguration(String interWikiConfiguration) {
103            }
104    
105            public void setMainConfiguration(String mainConfiguration) {
106            }
107    
108            public boolean validate(long nodeId, String content) {
109                    return true;
110            }
111    
112            protected ParserInput getParserInput(long nodeId, String topicName) {
113                    ParserInput parserInput = new ParserInput(
114                            "Special:Node:" + nodeId, topicName);
115    
116                    // Dummy values
117    
118                    parserInput.setContext("/wiki");
119                    parserInput.setLocale(LocaleUtil.getDefault());
120                    parserInput.setUserDisplay("0.0.0.0");
121                    parserInput.setWikiUser(new WikiUser("DummyUser"));
122    
123                    // Useful values
124    
125                    parserInput.setAllowSectionEdit(false);
126    
127                    // Table of contents
128    
129                    TableOfContents tableOfContents = new TableOfContents();
130    
131                    tableOfContents.setForceTOC(true);
132    
133                    parserInput.setTableOfContents(tableOfContents);
134    
135                    return parserInput;
136            }
137    
138            protected ParserOutput getParserOutput(WikiPage page)
139                    throws PageContentException {
140    
141                    ParserInput parserInput = getParserInput(
142                            page.getNodeId(), page.getTitle());
143    
144                    ParserOutput parserOutput = null;
145    
146                    try {
147                            parserOutput = ParserUtil.parseMetadata(
148                                    parserInput, page.getContent());
149                    }
150                    catch (ParserException pe) {
151                            throw new PageContentException(pe);
152                    }
153    
154                    return parserOutput;
155            }
156    
157            protected String parsePage(
158                            WikiPage page, ParserOutput parserOutput, PortletURL viewPageURL,
159                            PortletURL editPageURL, String attachmentURLPrefix)
160                    throws PageContentException {
161    
162                    ParserInput parserInput = getParserInput(
163                            page.getNodeId(), page.getTitle());
164    
165                    String content = StringPool.BLANK;
166    
167                    try {
168                            content = page.getContent();
169    
170                            ImageTagMatcher imageTagMatcher = new ImageTagMatcher();
171    
172                            content = ParserUtil.parse(
173                                    parserInput, parserOutput,
174                                    imageTagMatcher.replaceMatches(content));
175                    }
176                    catch (ParserException pe) {
177                            throw new PageContentException(pe);
178                    }
179    
180                    // Post parse
181    
182                    if (attachmentURLPrefix != null) {
183                            DirectURLMatcher attachmentURLMatcher =
184                                    new DirectURLMatcher(page, attachmentURLPrefix);
185    
186                            String result = attachmentURLMatcher.replaceMatches(content);
187    
188                            if (result != null) {
189                                    content = result;
190                            }
191    
192                            ImageURLMatcher imageURLMatcher = new ImageURLMatcher(
193                                    attachmentURLPrefix);
194    
195                            content = imageURLMatcher.replaceMatches(content);
196                    }
197    
198                    if (editPageURL != null) {
199                            EditURLMatcher editURLMatcher = new EditURLMatcher(editPageURL);
200    
201                            content = editURLMatcher.replaceMatches(content);
202                    }
203    
204                    if (viewPageURL != null) {
205                            ViewURLMatcher viewURLMatcher = new ViewURLMatcher(viewPageURL);
206    
207                            content = viewURLMatcher.replaceMatches(content);
208                    }
209    
210                    return content;
211            }
212    
213    }