/** * Return the URL of the index page for the wiki. * * @throws DataAccessException Thrown if any error occurs while retrieving data. */ public static String getBaseUrl() throws DataAccessException { VirtualWiki virtualWiki = VirtualWiki.defaultVirtualWiki(); String url = Environment.getValue(Environment.PROP_SERVER_URL); url += LinkUtil.buildTopicUrl( WEBAPP_CONTEXT_PATH, virtualWiki.getName(), virtualWiki.getRootTopicName(), true); return url; }
/** * Utility method for building a URL link to a wiki edit page for a specified topic. * * @param context The servlet context for the link that is being created. * @param virtualWiki The virtual wiki for the link that is being created. * @param topic The name of the topic for which an edit link is being created. * @param query Any existing query parameters to append to the edit link. This value may be either * <code>null</code> or empty. * @param section The section defined by the name parameter within the HTML page for the topic * being edited. If provided then the edit link will allow editing of only the specified * section. * @return A url that links to the edit page for the specified topic. Note that this method * returns only the URL, not a fully-formed HTML anchor tag. * @throws DataAccessException Thrown if any error occurs while builing the link URL. */ public static String buildEditLinkUrl( String context, String virtualWiki, String topic, String query, int section) throws DataAccessException { query = LinkUtil.appendQueryParam(query, "topic", topic); if (section > 0) { query += "&section=" + section; } WikiLink wikiLink = new WikiLink(); // FIXME - hard coding wikiLink.setDestination("Special:Edit"); wikiLink.setQuery(query); return LinkUtil.buildTopicUrl(context, virtualWiki, wikiLink); }
/** * Build the HTML anchor link to a topic page for a given WikLink object. * * @param context The servlet context for the link that is being created. * @param virtualWiki The virtual wiki for the link that is being created. * @param wikiLink The WikiLink object containing all relevant information about the link being * generated. * @param text The text to display as the link content. * @param style The CSS class to use with the anchor HTML tag. This value can be <code>null</code> * or empty if no custom style is used. * @param target The anchor link target, or <code>null</code> or empty if no target is needed. * @param escapeHtml Set to <code>true</code> if the link caption should be HTML escaped. This * value should be <code>true</code> in any case where the caption is not guaranteed to be * free from potentially malicious HTML code. * @return An HTML anchor link that matches the given input parameters. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildInternalLinkHtml( String context, String virtualWiki, WikiLink wikiLink, String text, String style, String target, boolean escapeHtml) throws DataAccessException { String url = LinkUtil.buildTopicUrl(context, virtualWiki, wikiLink); String topic = wikiLink.getDestination(); if (StringUtils.isBlank(text)) { text = topic; } if (!StringUtils.isBlank(topic) && StringUtils.isBlank(style)) { if (!StringUtils.isEmpty(virtualWiki) && InterWikiHandler.isInterWiki(virtualWiki)) { style = "interwiki"; } else if (!LinkUtil.isExistingArticle(virtualWiki, topic)) { style = "edit"; } } if (!StringUtils.isBlank(style)) { style = " class=\"" + style + "\""; } else { style = ""; } if (!StringUtils.isBlank(target)) { target = " target=\"" + target + "\""; } else { target = ""; } if (StringUtils.isBlank(topic) && !StringUtils.isBlank(wikiLink.getSection())) { topic = wikiLink.getSection(); } StringBuffer html = new StringBuffer(); html.append("<a href=\"").append(url).append('\"').append(style); html.append(" title=\"") .append(StringEscapeUtils.escapeHtml(topic)) .append('\"') .append(target) .append('>'); if (escapeHtml) { html.append(StringEscapeUtils.escapeHtml(text)); } else { html.append(text); } html.append("</a>"); return html.toString(); }
/** * Build a URL to the topic page for a given topic. * * @param context The servlet context path. If this value is <code>null</code> then the resulting * URL will NOT include context path, which breaks HTML links but is useful for servlet * redirection URLs. * @param virtualWiki The virtual wiki for the link that is being created. * @param topic The topic name for the URL that is being generated. * @param validateTopic Set to <code>true</code> if the topic must exist and must not be a * "Special:" page. If the topic does not exist then a link to an edit page will be returned. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildTopicUrl( String context, String virtualWiki, String topic, boolean validateTopic) throws DataAccessException { if (StringUtils.isBlank(topic)) { return null; } WikiLink wikiLink = LinkUtil.parseWikiLink(topic); if (validateTopic) { return LinkUtil.buildTopicUrl(context, virtualWiki, wikiLink); } else { return LinkUtil.buildTopicUrlNoEdit( context, virtualWiki, wikiLink.getDestination(), wikiLink.getSection(), wikiLink.getQuery()); } }
@Test public void testbuildTopicUrl() throws Throwable { String result = LinkUtil.buildTopicUrl("testLinkUtilContext", "testLinkUtilVirtualWiki", "", true); assertNull("result", result); }