Exemplo n.º 1
0
 /**
  * Utility method for determining if a namespace name is valid for use on the Wiki, meaning that
  * it is not empty and does not contain any invalid characters.
  *
  * @param name The namespace name to validate.
  * @throws WikiException Thrown if the user name is invalid.
  */
 public static void validateNamespaceName(String name) throws WikiException {
   if (name == null
       || (name.length() != 0 && StringUtils.isBlank(name))
       || name.length() != name.trim().length()) {
     // name cannot be null, contain only whitespace, or have trailing whitespace
     throw new WikiException(new WikiMessage("admin.vwiki.error.namespace.whitespace", name));
   }
   Matcher m = WikiUtil.INVALID_NAMESPACE_NAME_PATTERN.matcher(name);
   if (m.find()) {
     throw new WikiException(new WikiMessage("admin.vwiki.error.namespace.characters", name));
   }
   List<Namespace> namespaces = null;
   try {
     namespaces = WikiBase.getDataHandler().lookupNamespaces();
   } catch (DataAccessException e) {
     throw new WikiException(new WikiMessage("error.unknown", e.getMessage()));
   }
   for (Namespace namespace : namespaces) {
     // verify that the namespace name is unique
     if (name.equals(namespace.getDefaultLabel())) {
       throw new WikiException(new WikiMessage("admin.vwiki.error.namespace.unique", name));
     }
     // verify that there are no translated namespaces with the same name
     for (String namespaceTranslation : namespace.getNamespaceTranslations().values()) {
       if (name.equals(namespaceTranslation)) {
         throw new WikiException(new WikiMessage("admin.vwiki.error.namespace.unique", name));
       }
     }
   }
 }
Exemplo n.º 2
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());
 }
Exemplo n.º 3
0
 private void writeSiteInfo(Writer writer, String virtualWikiName)
     throws DataAccessException, IOException {
   VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName);
   writer.append("\n<siteinfo>");
   String sitename = virtualWiki.getSiteName();
   writer.append('\n');
   XMLUtil.buildTag(writer, "sitename", sitename, true);
   String base = this.retrieveBaseUrl();
   writer.append('\n');
   XMLUtil.buildTag(writer, "base", base, true);
   String generator = "JAMWiki " + WikiVersion.CURRENT_WIKI_VERSION;
   writer.append('\n');
   XMLUtil.buildTag(writer, "generator", generator, true);
   /*
   Cannot have two titles differing only by case of first letter.  Default behavior through 1.5, $wgCapitalLinks = true
   	<enumeration value="first-letter" />
   Complete title is case-sensitive. Behavior when $wgCapitalLinks = false
   	<enumeration value="case-sensitive" />
   Cannot have two titles differing only by case. Not yet implemented as of MediaWiki 1.5
   	<enumeration value="case-insensitive" />
   */
   writer.append('\n');
   XMLUtil.buildTag(writer, "case", "case-sensitive", true);
   writer.append("\n<namespaces>");
   Map<String, String> attributes = new HashMap<String, String>();
   List<Namespace> namespaces = WikiBase.getDataHandler().lookupNamespaces();
   for (Namespace namespace : namespaces) {
     attributes.put("key", Integer.toString(namespace.getId()));
     writer.append('\n');
     XMLUtil.buildTag(writer, "namespace", namespace.getLabel(virtualWikiName), attributes, true);
   }
   writer.append("\n</namespaces>");
   writer.append("\n</siteinfo>");
 }
Exemplo n.º 4
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();
 }
Exemplo n.º 5
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());
 }
Exemplo n.º 6
0
 /**
  * Given a namespace name, determine the topic type.
  *
  * @param namespace The namespace.
  * @return The topic type that matches the namespace.
  */
 public static TopicType findTopicTypeForNamespace(Namespace namespace) {
   if (namespace != null) {
     if (namespace.getId().equals(Namespace.CATEGORY_ID)) {
       return TopicType.CATEGORY;
     }
     if (namespace.getId().equals(Namespace.TEMPLATE_ID)) {
       return TopicType.TEMPLATE;
     }
     if (namespace.getId().equals(Namespace.JAMWIKI_ID)) {
       return TopicType.SYSTEM_FILE;
     }
     if (namespace.getId().equals(Namespace.FILE_ID)) {
       // FIXME - handle TYPE_FILE
       return TopicType.IMAGE;
     }
   }
   return TopicType.ARTICLE;
 }
Exemplo n.º 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();
 }
Exemplo n.º 8
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);
   }
 }
Exemplo n.º 9
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());
 }
Exemplo n.º 10
0
 @Override
 public String getTemplateNamespace() {
   // FIXME - this does not return the virtual wiki specific namespace
   return Namespace.namespace(Namespace.TEMPLATE_ID).getDefaultLabel();
 }
Exemplo n.º 11
0
 @Override
 public String getImageNamespace() {
   // FIXME - this does not return the virtual wiki specific namespace
   return Namespace.namespace(Namespace.FILE_ID).getDefaultLabel();
 }
Exemplo n.º 12
0
 @Override
 public String getCategoryNamespace() {
   // FIXME - this does not return the virtual wiki specific namespace
   return Namespace.namespace(Namespace.CATEGORY_ID).getDefaultLabel();
 }