Example #1
0
 /**
  * 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);
   }
 }
Example #2
0
 @Test(expected = IllegalArgumentException.class)
 public void testSetNamespace2() throws Throwable {
   WikiLink wikiLink = new WikiLink("/wiki", "en", null);
   wikiLink.setNamespace(null);
   assertEquals(
       "wikiLink.getNamespace()", Namespace.namespace(Namespace.FILE_ID), wikiLink.getNamespace());
 }
Example #3
0
 @Test
 public void testSetNamespace() throws Throwable {
   WikiLink wikiLink = new WikiLink("/wiki", "en", null);
   wikiLink.setNamespace(Namespace.namespace(Namespace.FILE_ID));
   assertEquals(
       "wikiLink.getNamespace()", Namespace.namespace(Namespace.FILE_ID), wikiLink.getNamespace());
 }
Example #4
0
 /**
  * Generate the HTML for an interwiki anchor link.
  *
  * @param wikiLink The WikiLink object containing all relevant information about the link being
  *     generated.
  * @return The HTML anchor tag for the interwiki link.
  */
 public static String interWiki(WikiLink wikiLink) {
   // remove namespace from link destination
   String destination = wikiLink.getDestination();
   String namespace = wikiLink.getNamespace();
   destination =
       destination.substring(
           wikiLink.getNamespace().length() + NamespaceHandler.NAMESPACE_SEPARATOR.length());
   String url = InterWikiHandler.formatInterWiki(namespace, destination);
   String text =
       (!StringUtils.isBlank(wikiLink.getText())) ? wikiLink.getText() : wikiLink.getDestination();
   return "<a class=\"interwiki\" rel=\"nofollow\" title=\""
       + text
       + "\" href=\""
       + url
       + "\">"
       + text
       + "</a>";
 }
Example #5
0
 @Test
 public void testConstructor() throws Throwable {
   WikiLink wikiLink = new WikiLink("/wiki", "en", null);
   assertNull("wikiLink.getQuery()", wikiLink.getQuery());
   assertNull("wikiLink.getSection()", wikiLink.getSection());
   assertNull("wikiLink.getText()", wikiLink.getText());
   assertNull("wikiLink.getArticle()", wikiLink.getArticle());
   assertEquals(
       "wikiLink.getNamespace()", Namespace.namespace(Namespace.MAIN_ID), wikiLink.getNamespace());
   assertNull("wikiLink.getDestination()", wikiLink.getDestination());
   assertFalse("wikiLink.getColon()", wikiLink.getColon());
 }
Example #6
0
 /**
  * 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();
 }
Example #7
0
 /**
  * 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();
 }
Example #8
0
 /**
  * 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));
   }
 }