Example #1
0
 protected static void setupSpecialPage(
     Locale locale,
     String virtualWiki,
     String topicName,
     WikiUser user,
     boolean adminOnly,
     Connection conn)
     throws Exception {
   logger.info("Setting up special page " + virtualWiki + " / " + topicName);
   String contents = Utilities.readSpecialPage(locale, topicName);
   Topic topic = new Topic();
   topic.setName(topicName);
   topic.setVirtualWiki(virtualWiki);
   topic.setTopicContent(contents);
   topic.setAdminOnly(adminOnly);
   // FIXME - hard coding
   TopicVersion topicVersion =
       new TopicVersion(
           user, user.getLastLoginIpAddress(), "Automatically created by system setup", contents);
   WikiBase.getDataHandler()
       .writeTopic(
           topic,
           topicVersion,
           Utilities.parserDocument(topic.getTopicContent(), virtualWiki, topicName),
           true,
           conn);
 }
Example #2
0
 /** Functionality to handle the "Preview" button being clicked. */
 private void preview(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = WikiUtil.getTopicFromRequest(request);
   String virtualWiki = pageInfo.getVirtualWikiName();
   String contents = (String) request.getParameter("contents");
   Topic previewTopic = new Topic(virtualWiki, topicName);
   previewTopic.setTopicContent(contents);
   next.addObject("editPreview", "true");
   ServletUtil.viewTopic(request, next, pageInfo, null, previewTopic, false, false);
 }
Example #3
0
 private void preview(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String contents = (String) request.getParameter("contents");
   Topic previewTopic = new Topic();
   previewTopic.setName(topicName);
   previewTopic.setTopicContent(contents);
   previewTopic.setVirtualWiki(virtualWiki);
   pageInfo.setAction(WikiPageInfo.ACTION_EDIT_PREVIEW);
   next.addObject("contents", contents);
   viewTopic(request, next, pageInfo, null, previewTopic, false);
 }
Example #4
0
 /** Crate a test topic. */
 protected Topic setupTopic(VirtualWiki virtualWiki, String topicName, String contents)
     throws DataAccessException, IOException, WikiException {
   if (virtualWiki == null) {
     virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki("en");
   }
   Topic topic = new Topic(virtualWiki.getName(), topicName);
   topic.setTopicContent(contents);
   if (topicName.toLowerCase().startsWith("image:")) {
     this.setupImage(virtualWiki, topic);
     return topic;
   }
   this.setupTopic(topic);
   return topic;
 }
Example #5
0
 private void viewVersion(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   // display an older version
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   int topicVersionId = Integer.parseInt(request.getParameter("topicVersionId"));
   TopicVersion topicVersion =
       WikiBase.getHandler().lookupTopicVersion(virtualWiki, topicName, topicVersionId);
   if (topicVersion == null) {
     throw new WikiException(new WikiMessage("common.exception.notopic"));
   }
   Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName);
   topic.setTopicContent(topicVersion.getVersionContent());
   String versionDate = DateFormat.getDateTimeInstance().format(topicVersion.getEditDate());
   WikiMessage pageTitle = new WikiMessage("topic.title", topicName + " @" + versionDate);
   viewTopic(request, next, pageInfo, pageTitle, topic, false, false);
 }
Example #6
0
 protected void writeTopic(HttpServletRequest request, String editComment) throws Exception {
   String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
   String topicName =
       NamespaceHandler.NAMESPACE_JAMWIKI
           + NamespaceHandler.NAMESPACE_SEPARATOR
           + Utilities.decodeFromRequest(filename(request));
   String contents =
       "<pre><nowiki>\n" + Utilities.readFile(filename(request)) + "\n</nowiki></pre>";
   Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null);
   if (topic == null) {
     topic = new Topic();
     topic.setVirtualWiki(virtualWiki);
     topic.setName(topicName);
   }
   topic.setTopicContent(contents);
   topic.setReadOnly(true);
   topic.setTopicType(Topic.TYPE_SYSTEM_FILE);
   WikiUser user = Utilities.currentUser();
   TopicVersion topicVersion =
       new TopicVersion(user, request.getRemoteAddr(), editComment, contents);
   WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, true, null);
 }
Example #7
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);
 }
Example #8
0
 /** Functionality to handle the "Save" button being clicked. */
 private void save(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = WikiUtil.getTopicFromRequest(request);
   String virtualWiki = pageInfo.getVirtualWikiName();
   Topic topic = loadTopic(virtualWiki, topicName);
   Topic lastTopic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null);
   if (lastTopic != null
       && !lastTopic.getCurrentVersionId().equals(retrieveLastTopicVersionId(request, topic))) {
     // someone else has edited the topic more recently
     resolve(request, next, pageInfo);
     return;
   }
   String contents = request.getParameter("contents");
   String sectionName = "";
   if (!StringUtils.isBlank(request.getParameter("section"))) {
     // load section of topic
     int section = Integer.valueOf(request.getParameter("section"));
     ParserOutput parserOutput = new ParserOutput();
     String[] spliceResult =
         ParserUtil.parseSplice(
             parserOutput,
             request.getContextPath(),
             request.getLocale(),
             virtualWiki,
             topicName,
             section,
             contents);
     contents = spliceResult[1];
     sectionName = parserOutput.getSectionName();
   }
   if (contents == null) {
     logger.warning("The topic " + topicName + " has no content");
     throw new WikiException(new WikiMessage("edit.exception.nocontent", topicName));
   }
   // strip line feeds
   contents = StringUtils.remove(contents, '\r');
   String lastTopicContent =
       (lastTopic != null) ? StringUtils.remove(lastTopic.getTopicContent(), '\r') : "";
   if (lastTopic != null && StringUtils.equals(lastTopicContent, contents)) {
     // topic hasn't changed. redirect to prevent user from refreshing and re-submitting
     ServletUtil.redirect(next, virtualWiki, topic.getName());
     return;
   }
   String editComment = request.getParameter("editComment");
   if (handleSpam(request, next, topicName, contents, editComment)) {
     this.loadEdit(request, next, pageInfo, contents, virtualWiki, topicName, false);
     return;
   }
   // parse for signatures and other syntax that should not be saved in raw form
   WikiUser user = ServletUtil.currentWikiUser();
   ParserInput parserInput = new ParserInput();
   parserInput.setContext(request.getContextPath());
   parserInput.setLocale(request.getLocale());
   parserInput.setWikiUser(user);
   parserInput.setTopicName(topicName);
   parserInput.setUserDisplay(ServletUtil.getIpAddress(request));
   parserInput.setVirtualWiki(virtualWiki);
   ParserOutput parserOutput = ParserUtil.parseMetadata(parserInput, contents);
   // parse signatures and other values that need to be updated prior to saving
   contents = ParserUtil.parseMinimal(parserInput, contents);
   topic.setTopicContent(contents);
   if (!StringUtils.isBlank(parserOutput.getRedirect())) {
     // set up a redirect
     topic.setRedirectTo(parserOutput.getRedirect());
     topic.setTopicType(TopicType.REDIRECT);
   } else if (topic.getTopicType() == TopicType.REDIRECT) {
     // no longer a redirect
     topic.setRedirectTo(null);
     topic.setTopicType(TopicType.ARTICLE);
   }
   int charactersChanged = StringUtils.length(contents) - StringUtils.length(lastTopicContent);
   TopicVersion topicVersion =
       new TopicVersion(
           user, ServletUtil.getIpAddress(request), editComment, contents, charactersChanged);
   if (request.getParameter("minorEdit") != null) {
     topicVersion.setEditType(TopicVersion.EDIT_MINOR);
   }
   WikiBase.getDataHandler()
       .writeTopic(topic, topicVersion, parserOutput.getCategories(), parserOutput.getLinks());
   // update watchlist
   WikiUserDetailsImpl userDetails = ServletUtil.currentUserDetails();
   if (!userDetails.hasRole(Role.ROLE_ANONYMOUS)) {
     Watchlist watchlist = ServletUtil.currentWatchlist(request, virtualWiki);
     boolean watchTopic = (request.getParameter("watchTopic") != null);
     if (watchlist.containsTopic(topicName) != watchTopic) {
       WikiBase.getDataHandler()
           .writeWatchlistEntry(watchlist, virtualWiki, topicName, user.getUserId());
     }
   }
   // redirect to prevent user from refreshing and re-submitting
   String target = topic.getName();
   if (!StringUtils.isBlank(sectionName)) {
     target += "#" + sectionName;
   }
   ServletUtil.redirect(next, virtualWiki, target);
 }
Example #9
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);
   }
 }
Example #10
0
 private void save(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String topicName = JAMWikiServlet.getTopicFromRequest(request);
   String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request);
   Topic topic = loadTopic(virtualWiki, topicName);
   TopicVersion lastTopicVersion =
       WikiBase.getHandler().lookupLastTopicVersion(virtualWiki, topicName);
   if (lastTopicVersion != null
       && lastTopicVersion.getTopicVersionId()
           != retrieveLastTopicVersionId(request, virtualWiki, topicName)) {
     // someone else has edited the topic more recently
     resolve(request, next, pageInfo);
     return;
   }
   String contents = request.getParameter("contents");
   if (StringUtils.hasText(request.getParameter("section"))) {
     // load section of topic
     int section = (new Integer(request.getParameter("section"))).intValue();
     ParserDocument parserDocument =
         Utilities.parseSplice(request, virtualWiki, topicName, section, contents);
     contents = parserDocument.getContent();
   }
   if (contents == null) {
     logger.warning("The topic " + topicName + " has no content");
     throw new WikiException(new WikiMessage("edit.exception.nocontent", topicName));
   }
   if (lastTopicVersion != null && lastTopicVersion.getVersionContent().equals(contents)) {
     viewTopic(request, next, pageInfo, topicName);
     return;
   }
   // parse for signatures and other syntax that should not be saved in raw form
   WikiUser user = Utilities.currentUser(request);
   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);
   ParserDocument parserDocument = Utilities.parseSave(parserInput, contents);
   contents = parserDocument.getContent();
   topic.setTopicContent(contents);
   if (StringUtils.hasText(parserDocument.getRedirect())) {
     // set up a redirect
     topic.setRedirectTo(parserDocument.getRedirect());
     topic.setTopicType(Topic.TYPE_REDIRECT);
   } else if (topic.getTopicType() == Topic.TYPE_REDIRECT) {
     // no longer a redirect
     topic.setRedirectTo(null);
     topic.setTopicType(Topic.TYPE_ARTICLE);
   }
   TopicVersion topicVersion =
       new TopicVersion(
           user, request.getRemoteAddr(), request.getParameter("editComment"), contents);
   if (request.getParameter("minorEdit") != null) {
     topicVersion.setEditType(TopicVersion.EDIT_MINOR);
   }
   WikiBase.getHandler().writeTopic(topic, topicVersion, parserDocument);
   // a save request has been made
   JAMWikiServlet.removeCachedContents();
   viewTopic(request, next, pageInfo, topicName);
 }