@Override public void appendInterWikiLink(String namespace, String title, String topicDescription) { String hrefLink = getInterwikiMap().get(namespace.toLowerCase()); if (hrefLink != null) { String virtualWiki = fParserInput.getVirtualWiki(); WikiLink wikiLink = LinkUtil.parseWikiLink( virtualWiki, namespace + Namespace.SEPARATOR + title + "|" + topicDescription); String destination = wikiLink.getDestination(); destination = destination.substring( wikiLink.getNamespace().getLabel(virtualWiki).length() + Namespace.SEPARATOR.length()); hrefLink = hrefLink.replace("${title}", Utilities.encodeAndEscapeTopicName(title)); TagNode aTagNode = new TagNode("a"); aTagNode.addAttribute("href", hrefLink, true); aTagNode.addAttribute("class", "interwiki", false); pushNode(aTagNode); WikipediaParser.parseRecursive(topicDescription.trim(), this, false, true); popNode(); } else { append(new ContentToken(topicDescription)); } }
private void loadEdit( HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo, String contents, String virtualWiki, String topicName, boolean useSection) throws Exception { pageInfo.setPageTitle(new WikiMessage("edit.title", topicName)); pageInfo.setTopicName(topicName); WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, topicName); if (wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) { ServletUtil.loadCategoryContent(next, virtualWiki, topicName); } if (request.getParameter("editComment") != null) { next.addObject("editComment", request.getParameter("editComment")); } if (useSection && request.getParameter("section") != null) { next.addObject("section", request.getParameter("section")); } next.addObject("minorEdit", (request.getParameter("minorEdit") != null)); Watchlist watchlist = ServletUtil.currentWatchlist(request, virtualWiki); if (request.getParameter("watchTopic") != null || (watchlist.containsTopic(topicName) && !isPreview(request))) { next.addObject("watchTopic", true); } pageInfo.setContentJsp(JSP_EDIT); WikiUser user = ServletUtil.currentWikiUser(); String editor = user.getEditor(); next.addObject("editor", editor); next.addObject("contents", contents); }
/** * Parse a raw Wiki link of the form "[[link|text]]", and return a WikiLink object representing * the link. * * @param parserInput Input configuration settings for this parser instance. * @param raw The raw Wiki link text. * @return A WikiLink object that represents the link. */ protected static WikiLink parseWikiLink( ParserInput parserInput, ParserOutput parserOutput, String raw) throws ParserException { if (StringUtils.isBlank(raw)) { return new WikiLink(); } raw = raw.trim(); String suffix = ((!raw.endsWith("]]")) ? raw.substring(raw.lastIndexOf("]]") + 2) : null); // for performance reasons use String methods rather than regex // private static final Pattern WIKI_LINK_PATTERN = // Pattern.compile("\\[\\[\\s*(\\:\\s*)?\\s*(.+?)(\\s*\\|\\s*(.+))?\\s*\\]\\]([a-z]*)"); raw = raw.substring(raw.indexOf("[[") + 2, raw.lastIndexOf("]]")).trim(); // parse in case there is a template or magic word - [[{{PAGENAME}}]] raw = JFlexParserUtil.parseFragment(parserInput, parserOutput, raw, JFlexParser.MODE_TEMPLATE); boolean colon = false; if (raw.startsWith(":")) { colon = true; raw = raw.substring(1).trim(); } String text = null; int pos = raw.indexOf('|'); if (pos != -1 && pos != (raw.length() - 1)) { text = raw.substring(pos + 1).trim(); raw = raw.substring(0, pos).trim(); } String virtualWiki = parserInput.getVirtualWiki(); WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, raw); if (!colon && wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) { // do not set default text for categories wikiLink.setText(null); } if (wikiLink.getVirtualWiki() != null && !StringUtils.equals(wikiLink.getVirtualWiki().getName(), virtualWiki) && StringUtils.isBlank(wikiLink.getDestination())) { // use the root topic name as the destination wikiLink.setDestination(wikiLink.getVirtualWiki().getRootTopicName()); if (StringUtils.isBlank(wikiLink.getText())) { wikiLink.setText(wikiLink.getVirtualWiki().getName() + Namespace.SEPARATOR); } } if (wikiLink.getInterwiki() != null && StringUtils.isBlank(wikiLink.getDestination()) && StringUtils.isBlank(wikiLink.getText())) { wikiLink.setText(wikiLink.getInterwiki().getInterwikiPrefix() + Namespace.SEPARATOR); } wikiLink.setColon(colon); if (text != null) { wikiLink.setText(text); } if (!StringUtils.isBlank(suffix)) { wikiLink.setText(wikiLink.getText() + suffix); } return wikiLink; }
private String processLinkMetadata( ParserInput parserInput, ParserOutput parserOutput, int mode, String raw, WikiLink wikiLink) throws ParserException { String result = raw; if (!wikiLink.getColon() && wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) { String sortKey = wikiLink.getText(); if (!StringUtils.isBlank(sortKey)) { sortKey = JFlexParserUtil.parseFragment(parserInput, sortKey, JFlexParser.MODE_PREPROCESS); } parserOutput.addCategory(wikiLink.getDestination(), sortKey); if (mode > JFlexParser.MODE_MINIMAL) { // keep the category around in minimal parsing mode, otherwise suppress it from the output result = ""; } } if (!StringUtils.isBlank(wikiLink.getDestination())) { parserOutput.addLink(wikiLink.getDestination()); } return result; }
/** * Initialize topic values for a Topic object. This method will check to see if a topic with the * specified name exists, and if it does exist then that topic will be returned. Otherwise a new * topic will be initialized, setting initial parameters such as topic name, virtual wiki, and * topic type. * * @param virtualWiki The virtual wiki name for the topic being initialized. * @param topicName The name of the topic being initialized. * @return A new topic object with basic fields initialized, or if a topic with the given name * already exists then the pre-existing topic is returned. * @throws Exception Thrown if any error occurs while retrieving or initializing the topic object. */ protected static Topic initializeTopic(String virtualWiki, String topicName) throws Exception { WikiUtil.validateTopicName(topicName); Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); if (topic != null) { return topic; } topic = new Topic(); topic.setName(topicName); topic.setVirtualWiki(virtualWiki); WikiLink wikiLink = LinkUtil.parseWikiLink(topicName); String namespace = wikiLink.getNamespace(); if (namespace != null) { if (namespace.equals(NamespaceHandler.NAMESPACE_CATEGORY)) { topic.setTopicType(Topic.TYPE_CATEGORY); } else if (namespace.equals(NamespaceHandler.NAMESPACE_TEMPLATE)) { topic.setTopicType(Topic.TYPE_TEMPLATE); } } return topic; }
private void loadEdit( HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo, String virtualWiki, String topicName, boolean useSection) throws Exception { pageInfo.setPageTitle(new WikiMessage("edit.title", topicName)); pageInfo.setTopicName(topicName); WikiLink wikiLink = LinkUtil.parseWikiLink(topicName); String namespace = wikiLink.getNamespace(); if (namespace != null && namespace.equals(NamespaceHandler.NAMESPACE_CATEGORY)) { loadCategoryContent(next, virtualWiki, topicName); } if (request.getParameter("editComment") != null) { next.addObject("editComment", request.getParameter("editComment")); } if (useSection && request.getParameter("section") != null) { next.addObject("section", request.getParameter("section")); } next.addObject("minorEdit", new Boolean(request.getParameter("minorEdit") != null)); }
/** Parse a Mediawiki link of the form "[[topic|text]]" and return the resulting HTML output. */ public String parse(JFlexLexer lexer, String raw, Object... args) throws ParserException { boolean containsNestedLinks = (args.length > 0 && StringUtils.equals(args[0].toString(), "nested")); WikiLink wikiLink = JFlexParserUtil.parseWikiLink(lexer.getParserInput(), raw); if (StringUtils.isBlank(wikiLink.getDestination()) && StringUtils.isBlank(wikiLink.getSection())) { // no destination or section return raw; } if (containsNestedLinks) { // if there is a nested link it must be an image, otherwise the syntax is invalid. if (wikiLink.getColon() || !wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) { int start = raw.indexOf("[["); int end = raw.lastIndexOf("]]"); String content = raw.substring(start + "[[".length(), end); return "[[" + JFlexParserUtil.parseFragment(lexer.getParserInput(), content, lexer.getMode()) + "]]"; } } raw = this.processLinkMetadata( lexer.getParserInput(), lexer.getParserOutput(), lexer.getMode(), raw, wikiLink); if (lexer.getMode() <= JFlexParser.MODE_PREPROCESS) { // do not parse to HTML when in preprocess mode return raw; } if (!wikiLink.getColon() && wikiLink.getNamespace().getId().equals(Namespace.FILE_ID)) { // parse as an image return lexer.parse(JFlexLexer.TAG_TYPE_IMAGE_LINK, raw); } try { if (!StringUtils.isBlank(wikiLink.getInterWiki())) { // inter-wiki link return LinkUtil.interWiki(wikiLink); } String virtualWiki = lexer.getParserInput().getVirtualWiki(); if (wikiLink.getVirtualWiki() != null) { // link to another virtual wiki virtualWiki = wikiLink.getVirtualWiki().getName(); } if (StringUtils.isBlank(wikiLink.getText()) && !StringUtils.isBlank(wikiLink.getDestination())) { wikiLink.setText(wikiLink.getDestination()); if (!StringUtils.isBlank(wikiLink.getSection())) { wikiLink.setText( wikiLink.getText() + "#" + Utilities.decodeAndEscapeTopicName(wikiLink.getSection(), true)); } } else if (StringUtils.isBlank(wikiLink.getText()) && !StringUtils.isBlank(wikiLink.getSection())) { wikiLink.setText(Utilities.decodeAndEscapeTopicName("#" + wikiLink.getSection(), true)); } else { // pass a parameter via the parserInput to prevent nested links from being generated lexer.getParserInput().getTempParams().put(LINK_CAPTION, true); wikiLink.setText( JFlexParserUtil.parseFragment( lexer.getParserInput(), wikiLink.getText(), lexer.getMode())); lexer.getParserInput().getTempParams().remove(LINK_CAPTION); } if (StringUtils.equals(wikiLink.getDestination(), lexer.getParserInput().getTopicName()) && StringUtils.equals(virtualWiki, lexer.getParserInput().getVirtualWiki()) && StringUtils.isBlank(wikiLink.getSection())) { // same page, bold the text and return return "<b>" + (StringUtils.isBlank(wikiLink.getText()) ? wikiLink.getDestination() : wikiLink.getText()) + "</b>"; } // do not escape text html - already done by parser return LinkUtil.buildInternalLinkHtml( lexer.getParserInput().getContext(), virtualWiki, wikiLink, wikiLink.getText(), null, null, false); } catch (DataAccessException e) { logger.severe("Failure while parsing link " + raw, e); return ""; } catch (ParserException e) { logger.severe("Failure while parsing link " + raw, e); return ""; } }