Exemplo n.º 1
0
 /**
  * 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);
 }
Exemplo n.º 2
0
 /**
  * Add a topic to the search index.
  *
  * @param writer The IndexWriter to use when updating the search index.
  * @param topic The Topic object that is to be added to the index.
  */
 private void addToIndex(IndexWriter writer, Topic topic) throws IOException {
   if (topic.getTopicType() == TopicType.REDIRECT) {
     // do not index redirects
     return;
   }
   Document standardDocument = createStandardDocument(topic);
   writer.addDocument(standardDocument);
   this.resetIndexSearcher(topic.getVirtualWiki());
 }
Exemplo n.º 3
0
 /**
  * Add a topic to the search index.
  *
  * @param topic The Topic object that is to be added to the index.
  */
 public void addToIndex(Topic topic) {
   try {
     long start = System.currentTimeMillis();
     IndexWriter writer = this.retrieveIndexWriter(topic.getVirtualWiki(), false);
     this.addToIndex(writer, topic);
     this.commit(writer, this.autoCommit);
     if (logger.isDebugEnabled()) {
       logger.debug(
           "Add to search index for topic "
               + topic.getVirtualWiki()
               + " / "
               + topic.getName()
               + " in "
               + ((System.currentTimeMillis() - start) / 1000.000)
               + " s.");
     }
   } catch (Exception e) {
     logger.error(
         "Exception while adding topic " + topic.getVirtualWiki() + " / " + topic.getName(), e);
   }
 }
Exemplo n.º 4
0
 /**
  * Action used when viewing a topic.
  *
  * @param request The servlet request object.
  * @param next The Spring ModelAndView object.
  * @param topicName The topic being viewed. This value must be a valid topic that can be loaded as
  *     a org.jamwiki.model.Topic object.
  */
 protected void viewTopic(
     HttpServletRequest request,
     ModelAndView next,
     WikiMessage pageTitle,
     Topic topic,
     boolean sectionEdit)
     throws Exception {
   // FIXME - what should the default be for topics that don't exist?
   String contents = "";
   if (topic == null) {
     throw new WikiException(new WikiMessage("common.exception.notopic"));
   }
   String virtualWiki = topic.getVirtualWiki();
   String topicName = topic.getName();
   String displayName = request.getRemoteAddr();
   WikiUser user = Utilities.currentUser(request);
   ParserInfo parserInfo = new ParserInfo(request.getContextPath(), request.getLocale());
   parserInfo.setWikiUser(user);
   parserInfo.setTopicName(topicName);
   parserInfo.setUserIpAddress(request.getRemoteAddr());
   parserInfo.setVirtualWiki(virtualWiki);
   parserInfo.setAllowSectionEdit(sectionEdit);
   contents = Utilities.parse(parserInfo, topic.getTopicContent(), topicName);
   if (StringUtils.hasText(request.getParameter("highlight"))) {
     // search servlet highlights search terms, so add that here
     contents = AbstractSearchEngine.highlightHTML(contents, request.getParameter("highlight"));
   }
   topic.setTopicContent(contents);
   if (topic.getTopicType() == Topic.TYPE_IMAGE) {
     List fileVersions =
         WikiBase.getHandler().getAllWikiFileVersions(virtualWiki, topicName, true);
     next.addObject("fileVersions", fileVersions);
   }
   this.pageInfo.setSpecial(false);
   this.pageInfo.setTopicName(topicName);
   next.addObject(JAMWikiServlet.PARAMETER_TOPIC_OBJECT, topic);
   this.pageInfo.setPageTitle(pageTitle);
 }
Exemplo n.º 5
0
 /**
  * Remove a topic from the search index.
  *
  * @param writer The IndexWriter to use when updating the search index.
  * @param topic The topic object that is to be removed from the index.
  */
 private void deleteFromIndex(IndexWriter writer, Topic topic) throws IOException {
   writer.deleteDocuments(new Term(FIELD_TOPIC_NAME, topic.getName()));
   this.resetIndexSearcher(topic.getVirtualWiki());
 }
Exemplo n.º 6
0
 /**
  * Utility method used when viewing a topic.
  *
  * @param request The current servlet request object.
  * @param next The current Spring ModelAndView object.
  * @param pageInfo The current WikiPageInfo object, which contains information needed for
  *     rendering the final JSP page.
  * @param pageTitle The title of the page being rendered.
  * @param topic The Topic object for the topic being displayed.
  * @param sectionEdit Set to <code>true</code> if edit links should be displayed for each section
  *     of the topic.
  * @throws Exception Thrown if any error occurs during processing.
  */
 protected static void viewTopic(
     HttpServletRequest request,
     ModelAndView next,
     WikiPageInfo pageInfo,
     WikiMessage pageTitle,
     Topic topic,
     boolean sectionEdit)
     throws Exception {
   // FIXME - what should the default be for topics that don't exist?
   if (topic == null) {
     throw new WikiException(new WikiMessage("common.exception.notopic"));
   }
   WikiUtil.validateTopicName(topic.getName());
   if (topic.getTopicType() == Topic.TYPE_REDIRECT
       && (request.getParameter("redirect") == null
           || !request.getParameter("redirect").equalsIgnoreCase("no"))) {
     Topic child = Utilities.findRedirectedTopic(topic, 0);
     if (!child.getName().equals(topic.getName())) {
       pageInfo.setRedirectName(topic.getName());
       pageTitle = new WikiMessage("topic.title", child.getName());
       topic = child;
     }
   }
   String virtualWiki = topic.getVirtualWiki();
   String topicName = topic.getName();
   WikiUser user = Utilities.currentUser();
   if (sectionEdit && !ServletUtil.isEditable(virtualWiki, topicName, user)) {
     sectionEdit = false;
   }
   ParserInput parserInput = new ParserInput();
   parserInput.setContext(request.getContextPath());
   parserInput.setLocale(request.getLocale());
   parserInput.setWikiUser(user);
   parserInput.setTopicName(topicName);
   parserInput.setUserIpAddress(request.getRemoteAddr());
   parserInput.setVirtualWiki(virtualWiki);
   parserInput.setAllowSectionEdit(sectionEdit);
   ParserDocument parserDocument = new ParserDocument();
   String content = Utilities.parse(parserInput, parserDocument, topic.getTopicContent());
   // FIXME - the null check should be unnecessary
   if (parserDocument != null && parserDocument.getCategories().size() > 0) {
     LinkedHashMap categories = new LinkedHashMap();
     for (Iterator iterator = parserDocument.getCategories().keySet().iterator();
         iterator.hasNext(); ) {
       String key = (String) iterator.next();
       String value =
           key.substring(
               NamespaceHandler.NAMESPACE_CATEGORY.length()
                   + NamespaceHandler.NAMESPACE_SEPARATOR.length());
       categories.put(key, value);
     }
     next.addObject("categories", categories);
   }
   topic.setTopicContent(content);
   if (topic.getTopicType() == Topic.TYPE_CATEGORY) {
     loadCategoryContent(next, virtualWiki, topic.getName());
   }
   if (topic.getTopicType() == Topic.TYPE_IMAGE || topic.getTopicType() == Topic.TYPE_FILE) {
     Collection fileVersions =
         WikiBase.getDataHandler().getAllWikiFileVersions(virtualWiki, topicName, true);
     for (Iterator iterator = fileVersions.iterator(); iterator.hasNext(); ) {
       // update version urls to include web root path
       WikiFileVersion fileVersion = (WikiFileVersion) iterator.next();
       String url =
           FilenameUtils.normalize(
               Environment.getValue(Environment.PROP_FILE_DIR_RELATIVE_PATH)
                   + "/"
                   + fileVersion.getUrl());
       url = FilenameUtils.separatorsToUnix(url);
       fileVersion.setUrl(url);
     }
     next.addObject("fileVersions", fileVersions);
     if (topic.getTopicType() == Topic.TYPE_IMAGE) {
       next.addObject("topicImage", new Boolean(true));
     } else {
       next.addObject("topicFile", new Boolean(true));
     }
   }
   pageInfo.setSpecial(false);
   pageInfo.setTopicName(topicName);
   next.addObject(ServletUtil.PARAMETER_TOPIC_OBJECT, topic);
   if (pageTitle != null) {
     pageInfo.setPageTitle(pageTitle);
   }
 }