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