/** * 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(); }
/** * Utility method for determining if an article name corresponds to a valid wiki link. In this * case an "article name" could be an existing topic, a "Special:" page, a user page, an interwiki * link, etc. This method will return true if the given name corresponds to a valid special page, * user page, topic, or other existing article. * * @param virtualWiki The virtual wiki for the topic being checked. * @param articleName The name of the article that is being checked. * @return <code>true</code> if there is an article that exists for the given name and virtual * wiki. * @throws DataAccessException Thrown if an error occurs during lookup. */ public static boolean isExistingArticle(String virtualWiki, String articleName) throws DataAccessException { // if (StringUtils.isBlank(virtualWiki) || StringUtils.isBlank(articleName)) // { if (StringUtils.isBlank(articleName)) { return false; } if (PseudoTopicHandler.isPseudoTopic(articleName)) { return true; } if (InterWikiHandler.isInterWiki(articleName)) { return true; } // if // (StringUtils.isBlank(Environment.getValue(Environment.PROP_BASE_FILE_DIR // )) || !Environment.getBooleanValue(Environment.PROP_BASE_INITIALIZED)) { // // not initialized yet // return false; // } return (WikiBase.getDataHandler().lookupTopic(virtualWiki, articleName, false, null) != null); }