private ModelAndView loginRequired(HttpServletRequest request, WikiPageInfo pageInfo) throws Exception { String topicName = WikiUtil.getTopicFromRequest(request); String virtualWiki = pageInfo.getVirtualWikiName(); WikiUserDetailsImpl user = ServletUtil.currentUserDetails(); if (ServletUtil.isEditable(virtualWiki, topicName, user)) { return null; } if (!user.hasRole(Role.ROLE_EDIT_EXISTING)) { WikiMessage messageObject = new WikiMessage("login.message.edit"); return ServletUtil.viewLogin( request, pageInfo, WikiUtil.getTopicFromURI(request), messageObject); } if (!user.hasRole(Role.ROLE_EDIT_NEW) && WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null) == null) { WikiMessage messageObject = new WikiMessage("login.message.editnew"); return ServletUtil.viewLogin( request, pageInfo, WikiUtil.getTopicFromURI(request), messageObject); } Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); if (topic == null) { // this should never trigger, but better safe than sorry... return null; } if (topic.getAdminOnly()) { WikiMessage messageObject = new WikiMessage("login.message.editadmin", topicName); return ServletUtil.viewLogin( request, pageInfo, WikiUtil.getTopicFromURI(request), messageObject); } if (topic.getReadOnly()) { throw new WikiException(new WikiMessage("error.readonly")); } // it should be impossible to get here... throw new WikiException( new WikiMessage("error.unknown", "Unable to determine topic editing permissions")); }
/** 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); }