<%
/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
%>

<%
String formattedContent = null;

try {
	double version = ParamUtil.getDouble(request, "version");

	PortletURL curViewPageURL = PortletURLUtil.clone(viewPageURL, renderResponse);
	PortletURL curEditPageURL = PortletURLUtil.clone(editPageURL, renderResponse);
	String attachmentURLPrefix = themeDisplay.getPathMain() + "/wiki/get_page_attachment?p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + wikiPage.getNodeId() + "&title=" + HttpUtil.encodeURL(wikiPage.getTitle()) + "&fileName=";

	WikiPageDisplay pageDisplay = null;

	if (!preview && (version == 0)) {
		pageDisplay = WikiCacheUtil.getDisplay(wikiPage.getNodeId(), title, curViewPageURL, curEditPageURL, attachmentURLPrefix);
	}

	if (pageDisplay != null) {
		formattedContent = pageDisplay.getFormattedContent();
	}
	else {
		formattedContent = WikiUtil.convert(wikiPage, curViewPageURL, curEditPageURL, attachmentURLPrefix);
	}
%>

	<div class="wiki-body">
		<c:choose>
			<c:when test="<%= !followRedirect && (redirectPage != null) %>">
				<div class="portlet-msg-info">
					<%= LanguageUtil.format(pageContext, "this-page-is-currently-redirected-to-x", redirectPage.getTitle()) %>
				</div>

				<c:if test="<%= WikiPagePermission.contains(permissionChecker, wikiPage, ActionKeys.UPDATE) %>">
					<div>
						<input type="button" value="<liferay-ui:message key="remove-redirect" />" onClick="location.href = '<portlet:renderURL><portlet:param name="struts_action" value="/wiki/edit_page" /><portlet:param name="nodeId" value="<%= String.valueOf(node.getNodeId()) %>" /><portlet:param name="title" value="<%= title %>" /><portlet:param name="removeRedirect" value="true" /></portlet:renderURL>'" />
					</div>
				</c:if>
			</c:when>
			<c:when test="<%= Validator.isNull(formattedContent) && WikiPagePermission.contains(permissionChecker, wikiPage, ActionKeys.UPDATE) %>">
				<div class="portlet-msg-info">
					<aui:a href="<%= editPageURL.toString() %>" label="this-page-is-empty-edit-it-to-add-some-text" />
				</div>
			</c:when>
			<c:otherwise>
				<%= formattedContent %>
			</c:otherwise>
		</c:choose>
	</div>

<%
}
catch (WikiFormatException wfe) {
%>

	<div class="portlet-msg-error">
		<liferay-ui:message key="the-format-of-this-page-is-not-supported-the-page-content-will-be-shown-unformatted" />
	</div>

	<div class="wiki-body">
		<pre>
<%= wikiPage.getContent() %>
		</pre>
	</div>

<%
}
catch (Exception e) {
	_log.error("Error formatting the wiki page " + wikiPage.getPageId() + " with the format " + wikiPage.getFormat(), e);
%>

	<div class="portlet-msg-error">
		<liferay-ui:message key="an-error-occurred-while-formatting-the-wiki-page" />
	</div>

<%
}
%>

<%!
private static Log _log = LogFactoryUtil.getLog("portal-web.docroot.html.wiki.view_page_content.jsp");
%>