예제 #1
0
 private Vector validate(HttpServletRequest request, WikiUser user) throws Exception {
   Vector errors = new Vector();
   if (!StringUtils.hasText(user.getLogin())) {
     errors.add(new WikiMessage("error.loginempty"));
   }
   if (!Utilities.validateUserName(user.getLogin())) {
     errors.add(new WikiMessage("common.exception.name", user.getLogin()));
   }
   String oldPassword = request.getParameter("oldPassword");
   if (user.getUserId() > 0
       && WikiBase.getHandler().lookupWikiUser(user.getLogin(), oldPassword, false) == null) {
     errors.add(new WikiMessage("register.error.oldpasswordinvalid"));
   }
   String newPassword = request.getParameter("newPassword");
   String confirmPassword = request.getParameter("confirmPassword");
   if (user.getUserId() < 1 && !StringUtils.hasText(newPassword)) {
     errors.add(new WikiMessage("register.error.passwordempty"));
   }
   if (StringUtils.hasText(newPassword) || StringUtils.hasText(confirmPassword)) {
     if (!StringUtils.hasText(newPassword)) {
       errors.add(new WikiMessage("error.newpasswordempty"));
     } else if (!StringUtils.hasText(confirmPassword)) {
       errors.add(new WikiMessage("error.passwordconfirm"));
     } else if (!newPassword.equals(confirmPassword)) {
       errors.add(new WikiMessage("admin.message.passwordsnomatch"));
     }
   }
   return errors;
 }
예제 #2
0
 private static void setupAdminUser(WikiUser user, Connection conn) throws Exception {
   if (user == null) {
     throw new Exception("Admin user not specified");
   }
   if (WikiBase.getDataHandler().lookupWikiUser(user.getUserId(), conn) != null) {
     logger.warning("Admin user already exists");
   }
   WikiUserInfo userInfo = null;
   if (WikiBase.getUserHandler().isWriteable()) {
     userInfo = new WikiUserInfo();
     userInfo.setEncodedPassword(user.getPassword());
     userInfo.setUsername(user.getUsername());
     userInfo.setUserId(user.getUserId());
   }
   WikiBase.getDataHandler().writeWikiUser(user, userInfo, conn);
 }
예제 #3
0
 private void view(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   String virtualWiki = Utilities.getVirtualWikiFromURI(request);
   Pagination pagination = Utilities.buildPagination(request, next);
   WikiUser user = Utilities.currentUser();
   if (!user.hasRole(Role.ROLE_USER)) {
     throw new WikiException(new WikiMessage("watchlist.error.loginrequired"));
   }
   Collection changes =
       WikiBase.getDataHandler().getWatchlist(virtualWiki, user.getUserId(), pagination);
   next.addObject("numChanges", new Integer(changes.size()));
   next.addObject("changes", changes);
   pageInfo.setPageTitle(new WikiMessage("watchlist.title"));
   pageInfo.setContentJsp(JSP_WATCHLIST);
   pageInfo.setSpecial(true);
 }
예제 #4
0
 private void update(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   WikiUser user = Utilities.currentUser();
   if (!user.hasRole(Role.ROLE_USER)) {
     throw new WikiException(new WikiMessage("watchlist.error.loginrequired"));
   }
   String topicName = Utilities.getTopicFromRequest(request);
   String virtualWiki = Utilities.getVirtualWikiFromURI(request);
   Watchlist watchlist = Utilities.currentWatchlist(request, virtualWiki);
   WikiBase.getDataHandler()
       .writeWatchlistEntry(watchlist, virtualWiki, topicName, user.getUserId(), null);
   String article = Utilities.extractTopicLink(topicName);
   if (watchlist.containsTopic(topicName)) {
     // added to watchlist
     next.addObject("message", new WikiMessage("watchlist.caption.added", article));
   } else {
     // removed from watchlist
     next.addObject("message", new WikiMessage("watchlist.caption.removed", article));
   }
   this.view(request, next, pageInfo);
 }
예제 #5
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);
 }
예제 #6
0
 private void writePages(
     Writer writer, String virtualWiki, List<String> topicNames, boolean excludeHistory)
     throws DataAccessException, IOException, MigrationException {
   // note that effort is being made to re-use temporary objects as this
   // code can generate an OOM "GC overhead limit exceeded" with HUGE (500MB) topics
   // since the garbage collector ends up being invoked excessively.
   TopicVersion topicVersion;
   Topic topic;
   WikiUser user;
   // choose 100,000 as an arbitrary max
   Pagination pagination = new Pagination(100000, 0);
   List<Integer> topicVersionIds;
   Map<String, String> textAttributes = new HashMap<String, String>();
   textAttributes.put("xml:space", "preserve");
   for (String topicName : topicNames) {
     topicVersionIds = new ArrayList<Integer>();
     topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false);
     if (topic == null) {
       throw new MigrationException(
           "Failure while exporting: topic " + topicName + " does not exist");
     }
     writer.append("\n<page>");
     writer.append('\n');
     XMLUtil.buildTag(writer, "title", topic.getName(), true);
     writer.append('\n');
     XMLUtil.buildTag(writer, "id", topic.getTopicId());
     if (excludeHistory) {
       // only include the most recent version
       topicVersionIds.add(topic.getCurrentVersionId());
     } else {
       // FIXME - changes sorted newest-to-oldest, should be reverse
       List<RecentChange> changes =
           WikiBase.getDataHandler().getTopicHistory(topic, pagination, true);
       for (int i = (changes.size() - 1); i >= 0; i--) {
         topicVersionIds.add(changes.get(i).getTopicVersionId());
       }
     }
     for (int topicVersionId : topicVersionIds) {
       topicVersion = WikiBase.getDataHandler().lookupTopicVersion(topicVersionId);
       writer.append("\n<revision>");
       writer.append('\n');
       XMLUtil.buildTag(writer, "id", topicVersion.getTopicVersionId());
       writer.append('\n');
       XMLUtil.buildTag(
           writer, "timestamp", this.parseJAMWikiTimestamp(topicVersion.getEditDate()), true);
       writer.append("\n<contributor>");
       user =
           (topicVersion.getAuthorId() != null)
               ? WikiBase.getDataHandler().lookupWikiUser(topicVersion.getAuthorId())
               : null;
       if (user != null) {
         writer.append('\n');
         XMLUtil.buildTag(writer, "username", user.getUsername(), true);
         writer.append('\n');
         XMLUtil.buildTag(writer, "id", user.getUserId());
       } else if (Utilities.isIpAddress(topicVersion.getAuthorDisplay())) {
         writer.append('\n');
         XMLUtil.buildTag(writer, "ip", topicVersion.getAuthorDisplay(), true);
       } else {
         writer.append('\n');
         XMLUtil.buildTag(writer, "username", topicVersion.getAuthorDisplay(), true);
       }
       writer.append("\n</contributor>");
       writer.append('\n');
       XMLUtil.buildTag(writer, "comment", topicVersion.getEditComment(), true);
       writer.append('\n');
       XMLUtil.buildTag(writer, "text", topicVersion.getVersionContent(), textAttributes, true);
       writer.append("\n</revision>");
       // explicitly null out temp variables to improve garbage collection and
       // avoid OOM "GC overhead limit exceeded" errors on HUGE (500MB) topics
       topicVersion = null;
       user = null;
     }
     writer.append("\n</page>");
   }
 }