/** * 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 wikiLink The WikiLink object containing all relevant information about the link being * generated. * @throws DataAccessException Thrown if any error occurs while retrieving topic information. */ public static String buildTopicUrl(String context, String virtualWiki, WikiLink wikiLink) throws DataAccessException { String topic = wikiLink.getDestination(); String section = wikiLink.getSection(); String query = wikiLink.getQuery(); String url = LinkUtil.buildTopicUrlNoEdit(context, virtualWiki, topic, section, query); if (StringUtils.isBlank(topic) && !StringUtils.isBlank(section)) { // do not check existence for section links return url; } if (!LinkUtil.isExistingArticle(virtualWiki, topic)) { url = LinkUtil.buildEditLinkUrl(context, virtualWiki, topic, query, -1); } return url; }
@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); }
/** * Given a topic, if that topic is a redirect find the target topic of the redirection. * * @param parent The topic being queried. If this topic is a redirect then the redirect target * will be returned, otherwise the topic itself is returned. * @param attempts The maximum number of child topics to follow. This parameter prevents infinite * loops if topics redirect back to one another. * @return If the parent topic is a redirect then this method returns the target topic that is * being redirected to, otherwise the parent topic is returned. * @throws DataAccessException Thrown if any error occurs while retrieving data. */ public static Topic findRedirectedTopic(Topic parent, int attempts) throws DataAccessException { int count = attempts; String target = parent.getRedirectTo(); if (parent.getTopicType() != TopicType.REDIRECT || StringUtils.isBlank(target)) { logger.error("getRedirectTarget() called for non-redirect topic " + parent.getName()); return parent; } // avoid infinite redirection count++; if (count > 10) { // TODO throw new WikiException(new WikiMessage("topic.redirect.infinite")); return parent; } String virtualWiki = parent.getVirtualWiki(); WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, target); if (wikiLink.getVirtualWiki() != null) { virtualWiki = wikiLink.getVirtualWiki().getName(); } // get the topic that is being redirected to Topic child = WikiBase.getDataHandler().lookupTopic(virtualWiki, wikiLink.getDestination(), false, null); if (child == null) { // child being redirected to doesn't exist, return parent return parent; } if (StringUtils.isBlank(child.getRedirectTo())) { // found a topic that is not a redirect, return return child; } // child is a redirect, keep looking return findRedirectedTopic(child, count); }
/** * Modify the current ModelAndView object to create a Spring redirect response, meaning that the * view name becomes "redirect:" followed by the redirection target. * * @param next The current ModelAndView object, which will be reset by this method. * @param virtualWiki The virtual wiki name for the page being redirected to. * @param destination The topic or page name that is the redirection target. An example might be * "Special:Login". */ protected static void redirect(ModelAndView next, String virtualWiki, String destination) throws Exception { String target = LinkUtil.buildInternalLinkUrl(null, virtualWiki, destination); String view = ServletUtil.SPRING_REDIRECT_PREFIX + target; next.clear(); next.setViewName(view); }
@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)); } }
/** * 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; }
private StringBuffer nextPage( Pagination pagination, String baseUrl, int count, boolean previous) { HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest(); StringBuffer output = new StringBuffer(); try { Object[] objects = new Object[1]; objects[0] = new Integer(pagination.getNumResults()); if (pagination.getOffset() == 0 && previous) { output.append( Utilities.formatMessage("common.pagination.previous", request.getLocale(), objects)); return output; } if (pagination.getNumResults() != count && !previous) { output.append( Utilities.formatMessage("common.pagination.next", request.getLocale(), objects)); return output; } output.append("<a href=\""); String virtualWiki = Utilities.getVirtualWikiFromRequest(request); WikiLink wikiLink = LinkUtil.parseWikiLink(baseUrl); int offset = pagination.getOffset() + pagination.getNumResults(); if (previous) { offset = pagination.getOffset() - pagination.getNumResults(); if (offset < 0) offset = 0; } String query = LinkUtil.appendQueryParam( wikiLink.getQuery(), "num", new Integer(pagination.getNumResults()).toString()); query += "&offset=" + offset; wikiLink.setQuery(query); output.append(LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWiki, wikiLink)); output.append("\">"); if (previous) { output.append( Utilities.formatMessage("common.pagination.previous", request.getLocale(), objects)); } else { output.append( Utilities.formatMessage("common.pagination.next", request.getLocale(), objects)); } output.append("</a>"); } catch (Exception e) { logger.warning("Failure while building pagination element", e); } return output; }
/** * 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()); } }
private void jumpTo(HttpServletRequest request, HttpServletResponse response, ModelAndView next) throws Exception { String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request); String text = request.getParameter("text"); // FIXME - if topic doesn't exist, should probably go to an edit page // or else give an error message // FIXME - need a better way to do redirects String redirectURL = LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWiki, text); redirect(redirectURL, response); }
/** * Utility method for building an anchor tag that links to an image page and includes the HTML * image tag to display the image. * * @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 topicName The name of the image for which a link is being created. * @param frame Set to <code>true</code> if the image should display with a frame border. * @param thumb Set to <code>true</code> if the image should display as a thumbnail. * @param align Indicates how the image should horizontally align on the page. Valid values are * "left", "right" and "center". * @param caption An optional text caption to display for the image. If no caption is used then * this value should be either empty or <code>null</code>. * @param maxDimension A value in pixels indicating the maximum width or height value allowed for * the image. Images will be resized so that neither the width or height exceeds this value. * @param suppressLink If this value is <code>true</code> then the generated HTML will include the * image tag without a link to the image topic page. * @param style The CSS class to use with the img HTML tag. This value can be <code>null</code> or * empty if no custom style is used. * @param escapeHtml Set to <code>true</code> if the 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 The full HTML required to display an image enclosed within an HTML anchor tag that * links to the image topic page. * @throws DataAccessException Thrown if any error occurs while retrieving image information. * @throws IOException Thrown if any error occurs while reading image information. */ public static String buildImageLinkHtml( String context, String virtualWiki, String topicName, boolean frame, boolean thumb, String align, String caption, int maxDimension, boolean suppressLink, String style, boolean escapeHtml) throws DataAccessException, IOException { // TODO check this for GAE/J String url = LinkUtil.buildImageFileUrl(context, virtualWiki, topicName); if (url == null) { WikiLink uploadLink = LinkUtil.parseWikiLink("Special:Upload"); return LinkUtil.buildInternalLinkHtml( context, virtualWiki, uploadLink, topicName, "edit", null, true); } // WikiFile wikiFile = WikiBase.getDataHandler().lookupWikiFile(virtualWiki, // topicName); Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); StringBuffer html = new StringBuffer(); // if (topic.getTopicType() == Topic.TYPE_FILE) { // // file, not an image // if (StringUtils.isBlank(caption)) { // caption = topicName.substring(NamespaceHandler.NAMESPACE_IMAGE.length() + // 1); // } // html.append("<a href=\"").append(url).append("\">"); // if (escapeHtml) { // html.append(StringEscapeUtils.escapeHtml(caption)); // } else { // html.append(caption); // } // html.append("</a>"); // return html.toString(); // } return "<span class=\"error\">LinkUtil.buildImageLinkHtml(>) not supported</span>"; }
/** * Given a topic name, determine if that name corresponds to a comments page. * * @param virtualWiki The current virtual wiki. * @param topicName The topic name (non-null) to examine to determine if it is a comments page or * not. * @return <code>true</code> if the page is a comments page, <code>false</code> otherwise. */ public static boolean isCommentsPage(String virtualWiki, String topicName) { WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, topicName); if (wikiLink.getNamespace().getId().equals(Namespace.SPECIAL_ID)) { return false; } try { return (Namespace.findCommentsNamespace(wikiLink.getNamespace()) != null); } catch (DataAccessException e) { throw new IllegalStateException("Database error while retrieving comments namespace", 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; }
/** * Given an article name, extract an appropriate topic article name. For example, if the article * name is "Comments:Topic" then the return value is "Topic". * * @param virtualWiki The current virtual wiki. * @param name The article name from which a topic article name is to be constructed. * @return The topic article name for the article name. */ public static String extractTopicLink(String virtualWiki, String name) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("Topic name must not be empty in extractTopicLink"); } WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, name); Namespace mainNamespace = Namespace.findMainNamespace(wikiLink.getNamespace()); if (mainNamespace == null) { throw new IllegalArgumentException("Topic " + name + " does not have a main namespace"); } return (!StringUtils.isBlank(mainNamespace.getLabel(virtualWiki))) ? mainNamespace.getLabel(virtualWiki) + Namespace.SEPARATOR + wikiLink.getArticle() : wikiLink.getArticle(); }
private StringBuffer buildOption(int num, Pagination pagination, String baseUrl) { HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest(); StringBuffer output = new StringBuffer(); try { if (num == pagination.getNumResults()) { output.append(num); return output; } output.append("<a href=\""); String virtualWiki = Utilities.getVirtualWikiFromRequest(request); WikiLink wikiLink = LinkUtil.parseWikiLink(baseUrl); String query = LinkUtil.appendQueryParam(wikiLink.getQuery(), "num", new Integer(num).toString()); query += "&offset=0"; wikiLink.setQuery(query); output.append(LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWiki, wikiLink)); output.append("\">"); output.append(num); output.append("</a>"); } catch (Exception e) { logger.warning("Failure while building pagination element", e); } return output; }
// FIXME - shouldn't need to pass in response private boolean register( HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo) throws Exception { pageInfo.setSpecial(true); pageInfo.setAction(WikiPageInfo.ACTION_REGISTER); pageInfo.setPageTitle(new WikiMessage("register.title")); String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request); WikiUser user = new WikiUser(); String userIdString = request.getParameter("userId"); if (StringUtils.hasText(userIdString)) { int userId = new Integer(userIdString).intValue(); if (userId > 0) user = WikiBase.getHandler().lookupWikiUser(userId); } user.setLogin(request.getParameter("login")); user.setDisplayName(request.getParameter("displayName")); user.setEmail(request.getParameter("email")); String newPassword = request.getParameter("newPassword"); if (StringUtils.hasText(newPassword)) { user.setEncodedPassword(Encryption.encrypt(newPassword)); } // FIXME - need to distinguish between add & update user.setCreateIpAddress(request.getRemoteAddr()); user.setLastLoginIpAddress(request.getRemoteAddr()); next.addObject("newuser", user); Vector errors = validate(request, user); if (errors.size() > 0) { next.addObject("errors", errors); String oldPassword = request.getParameter("oldPassword"); String confirmPassword = request.getParameter("confirmPassword"); if (oldPassword != null) next.addObject("oldPassword", oldPassword); if (newPassword != null) next.addObject("newPassword", newPassword); if (confirmPassword != null) next.addObject("confirmPassword", confirmPassword); return false; } else { WikiBase.getHandler().writeWikiUser(user); request.getSession().setAttribute(JAMWikiServlet.PARAMETER_USER, user); VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName); String topic = virtualWiki.getDefaultTopicName(); String redirect = LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWikiName, topic); // FIXME - can a redirect be done with Spring? redirect(redirect, response); return true; } }
/** * Given an article name, return the appropriate comments topic article name. For example, if the * article name is "Topic" then the return value is "Comments:Topic". * * @param virtualWiki The current virtual wiki. * @param name The article name from which a comments article name is to be constructed. * @return The comments article name for the article name. */ public static String extractCommentsLink(String virtualWiki, String name) { if (StringUtils.isBlank(name)) { throw new IllegalArgumentException("Topic name must not be empty in extractCommentsLink"); } WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, name); Namespace commentsNamespace = null; try { commentsNamespace = Namespace.findCommentsNamespace(wikiLink.getNamespace()); } catch (DataAccessException e) { throw new IllegalStateException("Database error while retrieving comments namespace", e); } if (commentsNamespace == null) { throw new IllegalArgumentException("Topic " + name + " does not have a comments namespace"); } return (!StringUtils.isBlank(commentsNamespace.getLabel(virtualWiki))) ? commentsNamespace.getLabel(virtualWiki) + Namespace.SEPARATOR + wikiLink.getArticle() : wikiLink.getArticle(); }
/** * Action used when redirecting to a login page. * * @param request The servlet request object. * @param next The Spring ModelAndView object. * @param topic The topic to be redirected to. Valid examples are "Special:Admin", * "StartingPoints", etc. */ protected void viewLogin(HttpServletRequest request, ModelAndView next, String topic) throws Exception { this.pageInfo = new WikiPageInfo(); String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request); String redirect = request.getParameter("redirect"); if (!StringUtils.hasText(redirect)) { if (!StringUtils.hasText(topic)) { VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName); topic = virtualWiki.getDefaultTopicName(); } redirect = LinkUtil.buildInternalLinkUrl( request.getContextPath(), virtualWikiName, topic, null, request.getQueryString()); } next.addObject("redirect", redirect); this.pageInfo.setPageTitle(new WikiMessage("login.title")); this.pageInfo.setPageAction(JAMWikiServlet.ACTION_LOGIN); this.pageInfo.setSpecial(true); }
/** * 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; }
/** * Parse a raw Wiki link of the form "[[link|text]]", and return a WikiLink object representing * the link. * * @param raw The raw Wiki link text. * @return A WikiLink object that represents the link. */ protected static WikiLink parseWikiLink(String raw) { if (StringUtils.isBlank(raw)) { return new WikiLink(); } Matcher m = WIKI_LINK_PATTERN.matcher(raw.trim()); if (!m.matches()) { return new WikiLink(); } String url = m.group(2); WikiLink wikiLink = LinkUtil.parseWikiLink(url); wikiLink.setColon((m.group(1) != null)); wikiLink.setText(m.group(4)); String suffix = m.group(5); if (!StringUtils.isBlank(suffix)) { if (StringUtils.isBlank(wikiLink.getText())) { wikiLink.setText(wikiLink.getDestination() + suffix); } else { wikiLink.setText(wikiLink.getText() + suffix); } } return wikiLink; }
/** * Utility method for determining if a topic name is valid for use on the Wiki, meaning that it is * not empty and does not contain any invalid characters. * * @param virtualWiki The current virtual wiki. * @param name The topic name to validate. * @throws WikiException Thrown if the user name is invalid. */ public static void validateTopicName(String virtualWiki, String name) throws WikiException { if (StringUtils.isBlank(virtualWiki)) { throw new WikiException(new WikiMessage("common.exception.novirtualwiki")); } if (StringUtils.isBlank(name)) { throw new WikiException(new WikiMessage("common.exception.notopic")); } if (PseudoTopicHandler.isPseudoTopic(name)) { throw new WikiException(new WikiMessage("common.exception.pseudotopic", name)); } WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, name); String article = StringUtils.trimToNull(wikiLink.getArticle()); if (StringUtils.startsWith(article, "/")) { throw new WikiException(new WikiMessage("common.exception.name", name)); } if (wikiLink.getNamespace().getId().equals(Namespace.SPECIAL_ID)) { throw new WikiException(new WikiMessage("common.exception.name", name)); } Matcher m = WikiUtil.INVALID_TOPIC_NAME_PATTERN.matcher(name); if (m.find()) { throw new WikiException(new WikiMessage("common.exception.name", name)); } }
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)); }
@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("]")); } }
@Test public void testAppendQueryParam5() throws Throwable { String result = LinkUtil.appendQueryParam("testLinkUtilQuery", "testLinkUtilParam", "testLinkUtilValue"); assertEquals("result", "?testLinkUtilQuery&testLinkUtilParam=testLinkUtilValue", result); }
@Test public void testAppendQueryParam6() throws Throwable { String result = LinkUtil.appendQueryParam("", "testLinkUtilParam", " "); assertEquals("result", "?testLinkUtilParam=", result); }
@Test public void testAppendQueryParam7() throws Throwable { String result = LinkUtil.appendQueryParam(null, "", "testLinkUtilValue"); assertNull("result", result); }
@Test public void testbuildTopicUrl() throws Throwable { String result = LinkUtil.buildTopicUrl("testLinkUtilContext", "testLinkUtilVirtualWiki", "", true); assertNull("result", result); }
@Test public void testParseWikiLink() throws Throwable { WikiLink result = LinkUtil.parseWikiLink("en", "testLinkUtilRaw"); assertEquals("result.getArticle()", "testLinkUtilRaw", result.getArticle()); }