@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 String buildSectionEditLink(ParserInput parserInput, int section) throws ParserException { if (!parserInput.getAllowSectionEdit()) { return ""; } if (parserInput.getLocale() == null) { logger.info( "Unable to build section edit links for " + parserInput.getTopicName() + " - locale is empty"); return ""; } // FIXME - template inclusion causes section edits to break, so disable for now Integer inclusion = (Integer) parserInput.getTempParam(TemplateTag.TEMPLATE_INCLUSION); boolean disallowInclusion = (inclusion != null && inclusion > 0); if (disallowInclusion) { return ""; } String url = ""; try { url = LinkUtil.buildEditLinkUrl( parserInput.getContext(), parserInput.getVirtualWiki(), parserInput.getTopicName(), null, section); } catch (DataAccessException e) { logger.error( "Failure while building link for topic " + parserInput.getVirtualWiki() + " / " + parserInput.getTopicName(), e); } // arguments are edit link URL and edit label text Object[] args = new Object[2]; args[0] = url; args[1] = Utilities.formatMessage("common.sectionedit", parserInput.getLocale()); try { return WikiUtil.formatFromTemplate(TEMPLATE_HEADER_EDIT_LINK, args); } catch (IOException e) { throw new ParserException(e); } }
/** * 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; }
@Override public void buildEditLinkUrl(int section) { if (fParserInput.getAllowSectionEdit()) { TagNode divTagNode = new TagNode("div"); divTagNode.addAttribute("style", "font-size:90%;float:right;margin-left:5px;", false); divTagNode.addChild(new ContentToken("[")); append(divTagNode); String url = ""; try { // Use correct section number. // Bliki starts with offset 0 so it must be "section+1" url = LinkUtil.buildEditLinkUrl( fParserInput.getContext(), fParserInput.getVirtualWiki(), fParserInput.getTopicName(), null, section + 1); } catch (Exception e) { logger.error( "Failure while building link for topic " + fParserInput.getVirtualWiki() + " / " + fParserInput.getTopicName(), e); } TagNode aTagNode = new TagNode("a"); aTagNode.addAttribute("href", url, false); aTagNode.addChild( new ContentToken( Utilities.formatMessage("common.sectionedit", fParserInput.getLocale()))); divTagNode.addChild(aTagNode); divTagNode.addChild(new ContentToken("]")); } }
@Override public void appendInternalLink( String topic, String hashSection, String topicDescription, String cssClass, boolean parseRecursive) { try { String virtualWiki = fParserInput.getVirtualWiki(); WikiLink wikiLink; if (hashSection != null) { wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic + "#" + hashSection); } else { wikiLink = LinkUtil.parseWikiLink(virtualWiki, topic); } String destination = wikiLink.getDestination(); String section = wikiLink.getSection(); String query = wikiLink.getQuery(); String href = buildTopicUrlNoEdit(fContextPath, virtualWiki, destination, section, query); String style = ""; if (StringUtils.isBlank(topic) && !StringUtils.isBlank(section)) { // do not check existence for section links } else { String articleName = topic.replace('_', ' '); if (LinkUtil.isExistingArticle(virtualWiki, articleName) == null) { style = "edit"; href = LinkUtil.buildEditLinkUrl(fContextPath, virtualWiki, topic, query, -1); } } WPATag aTagNode = new WPATag(); aTagNode.addAttribute("href", href, true); aTagNode.addAttribute("class", style, true); aTagNode.addObjectAttribute("wikilink", topic); pushNode(aTagNode); if (parseRecursive) { WikipediaParser.parseRecursive(topicDescription.trim(), this, false, true); } else { aTagNode.addChild(new ContentToken(topicDescription)); } popNode(); } catch (DataAccessException e1) { e1.printStackTrace(); append(new ContentToken(topicDescription)); } }
@Override public String getRawWikiContent( String namespace, String topicName, Map<String, String> templateParameters) { String result = super.getRawWikiContent(namespace, topicName, templateParameters); if (result != null) { return result; } try { topicName = topicName.replaceAll("_", " "); Topic topic = WikiBase.getDataHandler() .lookupTopic(fParserInput.getVirtualWiki(), namespace + ':' + topicName, false, null); if (topic == null) { return null; } return topic.getTopicContent(); } catch (Exception e) { e.printStackTrace(); } return result; }