Ejemplo n.º 1
0
 private WikiUser setAdminUser(HttpServletRequest request) throws Exception {
   String username = request.getParameter("username");
   WikiUser user = new WikiUser(username);
   user.setCreateIpAddress(ServletUtil.getIpAddress(request));
   user.setLastLoginIpAddress(ServletUtil.getIpAddress(request));
   return user;
 }
Ejemplo n.º 2
0
 private void loadEdit(
     HttpServletRequest request,
     ModelAndView next,
     WikiPageInfo pageInfo,
     String contents,
     String virtualWiki,
     String topicName,
     boolean useSection)
     throws Exception {
   pageInfo.setPageTitle(new WikiMessage("edit.title", topicName));
   pageInfo.setTopicName(topicName);
   WikiLink wikiLink = LinkUtil.parseWikiLink(virtualWiki, topicName);
   if (wikiLink.getNamespace().getId().equals(Namespace.CATEGORY_ID)) {
     ServletUtil.loadCategoryContent(next, virtualWiki, topicName);
   }
   if (request.getParameter("editComment") != null) {
     next.addObject("editComment", request.getParameter("editComment"));
   }
   if (useSection && request.getParameter("section") != null) {
     next.addObject("section", request.getParameter("section"));
   }
   next.addObject("minorEdit", (request.getParameter("minorEdit") != null));
   Watchlist watchlist = ServletUtil.currentWatchlist(request, virtualWiki);
   if (request.getParameter("watchTopic") != null
       || (watchlist.containsTopic(topicName) && !isPreview(request))) {
     next.addObject("watchTopic", true);
   }
   pageInfo.setContentJsp(JSP_EDIT);
   WikiUser user = ServletUtil.currentWikiUser();
   String editor = user.getEditor();
   next.addObject("editor", editor);
   next.addObject("contents", contents);
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
0
 /**
  * Determine if a user has permission to move a topic.
  *
  * @param virtualWiki The virtual wiki name for the topic in question.
  * @param topicName The name of the topic in question.
  * @param user The current Wiki user, or <code>null</code> if there is no current user.
  * @return <code>true</code> if the user is allowed to move the topic, <code>false</code>
  *     otherwise.
  */
 protected static boolean isMoveable(String virtualWiki, String topicName, WikiUser user)
     throws Exception {
   if (user == null || !user.hasRole(Role.ROLE_MOVE)) {
     // no permission granted to move pages
     return false;
   }
   Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null);
   if (topic == null) {
     // cannot move a topic that doesn't exist
     return false;
   }
   if (topic.getReadOnly()) {
     return false;
   }
   if (topic.getAdminOnly() && (user == null || !user.hasRole(Role.ROLE_ADMIN))) {
     return false;
   }
   return true;
 }
Ejemplo n.º 7
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);
 }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
0
 private boolean initialize(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo)
     throws Exception {
   setProperties(request, next);
   WikiUser user = setAdminUser(request);
   List<WikiMessage> errors = validate(request, user);
   if (!errors.isEmpty()) {
     this.view(request, next, pageInfo);
     next.addObject("errors", errors);
     next.addObject("username", user.getUsername());
     next.addObject("newPassword", request.getParameter("newPassword"));
     next.addObject("confirmPassword", request.getParameter("confirmPassword"));
     return false;
   }
   if (previousInstall() && request.getParameter("override") == null) {
     // user is trying to do a new install when a previous installation exists
     next.addObject("upgrade", "true");
     next.addObject("username", user.getUsername());
     next.addObject("newPassword", request.getParameter("newPassword"));
     next.addObject("confirmPassword", request.getParameter("confirmPassword"));
     return false;
   }
   Environment.setBooleanValue(Environment.PROP_BASE_INITIALIZED, true);
   Environment.setValue(Environment.PROP_BASE_WIKI_VERSION, WikiVersion.CURRENT_WIKI_VERSION);
   String username = request.getParameter("username");
   String newPassword = request.getParameter("newPassword");
   String encryptedPassword = Encryption.encrypt(newPassword);
   WikiBase.reset(request.getLocale(), user, username, encryptedPassword);
   JAMWikiAuthenticationConfiguration.resetJamwikiAnonymousAuthorities();
   JAMWikiAuthenticationConfiguration.resetDefaultGroupRoles();
   Environment.saveProperties();
   // the setup process does not add new topics to the index (currently)
   // TODO - remove this once setup uses safe connection handling
   WikiBase.getSearchEngine().refreshIndex();
   // force current user credentials to be removed and re-validated.
   SecurityContextHolder.clearContext();
   return true;
 }
Ejemplo n.º 10
0
 /**
  * Determine if a user has permission to edit a topic.
  *
  * @param virtualWiki The virtual wiki name for the topic in question.
  * @param topicName The name of the topic in question.
  * @param user The current Wiki user, or <code>null</code> if there is no current user.
  * @return <code>true</code> if the user is allowed to edit the topic, <code>false</code>
  *     otherwise.
  */
 protected static boolean isEditable(String virtualWiki, String topicName, WikiUser user)
     throws Exception {
   if (user == null || !user.hasRole(Role.ROLE_EDIT_EXISTING)) {
     // user does not have appropriate permissions
     return false;
   }
   if (!user.hasRole(Role.ROLE_EDIT_NEW)
       && WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null) == null) {
     // user does not have appropriate permissions
     return false;
   }
   Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null);
   if (topic == null) {
     // new topic, edit away...
     return true;
   }
   if (topic.getAdminOnly() && (user == null || !user.hasRole(Role.ROLE_ADMIN))) {
     return false;
   }
   if (topic.getReadOnly()) {
     return false;
   }
   return true;
 }
Ejemplo n.º 11
0
 // FIXME - shouldn't need to pass in response
 private boolean register(
     HttpServletRequest request,
     HttpServletResponse response,
     ModelAndView next,
     WikiPageInfo pageInfo)
     throws Exception {
   pageInfo.setSpecial(true);
   pageInfo.setAction(WikiPageInfo.ACTION_REGISTER);
   pageInfo.setPageTitle(new WikiMessage("register.title"));
   String virtualWikiName = JAMWikiServlet.getVirtualWikiFromURI(request);
   WikiUser user = new WikiUser();
   String userIdString = request.getParameter("userId");
   if (StringUtils.hasText(userIdString)) {
     int userId = new Integer(userIdString).intValue();
     if (userId > 0) user = WikiBase.getHandler().lookupWikiUser(userId);
   }
   user.setLogin(request.getParameter("login"));
   user.setDisplayName(request.getParameter("displayName"));
   user.setEmail(request.getParameter("email"));
   String newPassword = request.getParameter("newPassword");
   if (StringUtils.hasText(newPassword)) {
     user.setEncodedPassword(Encryption.encrypt(newPassword));
   }
   // FIXME - need to distinguish between add & update
   user.setCreateIpAddress(request.getRemoteAddr());
   user.setLastLoginIpAddress(request.getRemoteAddr());
   next.addObject("newuser", user);
   Vector errors = validate(request, user);
   if (errors.size() > 0) {
     next.addObject("errors", errors);
     String oldPassword = request.getParameter("oldPassword");
     String confirmPassword = request.getParameter("confirmPassword");
     if (oldPassword != null) next.addObject("oldPassword", oldPassword);
     if (newPassword != null) next.addObject("newPassword", newPassword);
     if (confirmPassword != null) next.addObject("confirmPassword", confirmPassword);
     return false;
   } else {
     WikiBase.getHandler().writeWikiUser(user);
     request.getSession().setAttribute(JAMWikiServlet.PARAMETER_USER, user);
     VirtualWiki virtualWiki = WikiBase.getHandler().lookupVirtualWiki(virtualWikiName);
     String topic = virtualWiki.getDefaultTopicName();
     String redirect =
         LinkUtil.buildInternalLinkUrl(request.getContextPath(), virtualWikiName, topic);
     // FIXME - can a redirect be done with Spring?
     redirect(redirect, response);
     return true;
   }
 }
Ejemplo n.º 12
0
 private List<WikiMessage> validate(HttpServletRequest request, WikiUser user) throws Exception {
   List<WikiMessage> errors = ServletUtil.validateSystemSettings(Environment.getInstance());
   if (StringUtils.isBlank(user.getUsername())) {
     errors.add(new WikiMessage("error.loginempty"));
   }
   String newPassword = request.getParameter("newPassword");
   String confirmPassword = request.getParameter("confirmPassword");
   if (newPassword != null || confirmPassword != null) {
     if (newPassword == null) {
       errors.add(new WikiMessage("error.newpasswordempty"));
     } else if (confirmPassword == null) {
       errors.add(new WikiMessage("error.passwordconfirm"));
     } else if (!newPassword.equals(confirmPassword)) {
       errors.add(new WikiMessage("admin.message.passwordsnomatch"));
     }
   }
   return errors;
 }
 /** Override the parent method to update the last login date on successful authentication. */
 protected void successfulAuthentication(
     HttpServletRequest request,
     HttpServletResponse response,
     FilterChain chain,
     Authentication auth)
     throws IOException, ServletException {
   super.successfulAuthentication(request, response, chain, auth);
   Object principal = auth.getPrincipal();
   // find authenticated username
   String username = null;
   if (principal instanceof UserDetails) {
     // using custom authentication with Spring Security UserDetail service
     username = ((UserDetails) principal).getUsername();
   } else if (principal instanceof String) {
     // external authentication returns only username
     username = String.valueOf(principal);
   }
   if (username != null) {
     try {
       WikiUser wikiUser = WikiBase.getDataHandler().lookupWikiUser(username);
       if (wikiUser != null) {
         wikiUser.setLastLoginDate(new Timestamp(System.currentTimeMillis()));
         WikiBase.getDataHandler().writeWikiUser(wikiUser, wikiUser.getUsername(), "");
         // update password reset challenge fields, just in case
         wikiUser.setChallengeValue(null);
         wikiUser.setChallengeDate(null);
         wikiUser.setChallengeIp(null);
         wikiUser.setChallengeTries(0);
         WikiBase.getDataHandler().updatePwResetChallengeData(wikiUser);
       }
     } catch (WikiException e) {
       // log but do not throw - failure to update last login date is non-fatal
       logger.error("Failure while updating last login date for " + username, e);
     }
   }
 }
Ejemplo n.º 14
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>");
   }
 }
Ejemplo n.º 15
0
 protected void loadDefaults(
     HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) {
   // load cached top area, nav bar, etc.
   try {
     this.buildLayout(request, next);
   } catch (Exception e) {
     logger.error("Unable to build default page layout", e);
   }
   // add link to user page and comments page
   WikiUser user = Utilities.currentUser(request);
   if (user != null) {
     next.addObject("userpage", WikiBase.NAMESPACE_USER + user.getLogin());
     next.addObject("usercomments", WikiBase.NAMESPACE_USER_COMMENTS + user.getLogin());
     next.addObject("adminUser", new Boolean(user.getAdmin()));
   }
   if (!this.pageInfo.getSpecial()) {
     // FIXME - this is really ugly
     String article = this.pageInfo.getTopicName();
     String comments = WikiBase.NAMESPACE_COMMENTS + article;
     if (article != null && article.startsWith(WikiBase.NAMESPACE_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_COMMENTS.length();
       article = article.substring(pos);
       comments = WikiBase.NAMESPACE_COMMENTS + article;
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_SPECIAL)) {
       int pos = WikiBase.NAMESPACE_SPECIAL.length();
       article = article.substring(pos);
       comments = WikiBase.NAMESPACE_COMMENTS + article;
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_USER_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_USER_COMMENTS.length();
       comments = article;
       article = WikiBase.NAMESPACE_USER + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_USER)) {
       int pos = WikiBase.NAMESPACE_USER.length();
       comments = WikiBase.NAMESPACE_USER_COMMENTS + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_IMAGE_COMMENTS)) {
       int pos = WikiBase.NAMESPACE_IMAGE_COMMENTS.length();
       comments = article;
       article = WikiBase.NAMESPACE_IMAGE + article.substring(pos);
     } else if (article != null && article.startsWith(WikiBase.NAMESPACE_IMAGE)) {
       int pos = WikiBase.NAMESPACE_IMAGE.length();
       comments = WikiBase.NAMESPACE_IMAGE_COMMENTS + article.substring(pos);
     }
     next.addObject("article", article);
     next.addObject("comments", comments);
     String editLink = "Special:Edit?topic=" + Utilities.encodeURL(this.pageInfo.getTopicName());
     if (StringUtils.hasText(request.getParameter("topicVersionId"))) {
       editLink += "&topicVersionId=" + request.getParameter("topicVersionId");
     }
     next.addObject("edit", editLink);
   }
   next.addObject(JAMWikiServlet.PARAMETER_TOPIC, this.pageInfo.getTopicName());
   if (!StringUtils.hasText(this.pageInfo.getTopicName())) {
     try {
       next.addObject(JAMWikiServlet.PARAMETER_TOPIC, JAMWikiServlet.getTopicFromURI(request));
     } catch (Exception e) {
       logger.error("Unable to load topic value in JAMWikiServlet", e);
     }
   }
   next.addObject(JAMWikiServlet.PARAMETER_ADMIN, new Boolean(this.pageInfo.getAdmin()));
   next.addObject(JAMWikiServlet.PARAMETER_SPECIAL, new Boolean(this.pageInfo.getSpecial()));
   next.addObject(JAMWikiServlet.PARAMETER_TITLE, this.pageInfo.getPageTitle());
   next.addObject(JAMWikiServlet.PARAMETER_ACTION, this.pageInfo.getPageAction());
   // reset pageInfo object - seems not to reset with each servlet call
   this.pageInfo = new WikiPageInfo();
 }
Ejemplo n.º 16
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);
 }
Ejemplo n.º 17
0
 /**
  * Build a map of links and the corresponding link text to be used as the user menu links for the
  * WikiPageInfo object.
  */
 private static LinkedHashMap buildUserMenu() {
   LinkedHashMap links = new LinkedHashMap();
   WikiUser user = Utilities.currentUser();
   if (user.hasRole(Role.ROLE_ANONYMOUS) && !user.hasRole(Role.ROLE_EMBEDDED)) {
     links.put("Special:Login", new WikiMessage("common.login"));
     links.put("Special:Account", new WikiMessage("usermenu.register"));
   }
   if (user.hasRole(Role.ROLE_USER)) {
     String userPage =
         NamespaceHandler.NAMESPACE_USER
             + NamespaceHandler.NAMESPACE_SEPARATOR
             + user.getUsername();
     String userCommentsPage =
         NamespaceHandler.NAMESPACE_USER_COMMENTS
             + NamespaceHandler.NAMESPACE_SEPARATOR
             + user.getUsername();
     String username = user.getUsername();
     if (StringUtils.hasText(user.getDisplayName())) {
       username = user.getDisplayName();
     }
     links.put(userPage, new WikiMessage("usermenu.user", username));
     links.put(userCommentsPage, new WikiMessage("usermenu.usercomments"));
     links.put("Special:Watchlist", new WikiMessage("usermenu.watchlist"));
   }
   if (user.hasRole(Role.ROLE_USER) && !user.hasRole(Role.ROLE_NO_ACCOUNT)) {
     links.put("Special:Account", new WikiMessage("usermenu.account"));
   }
   if (user.hasRole(Role.ROLE_USER) && !user.hasRole(Role.ROLE_EMBEDDED)) {
     links.put("Special:Logout", new WikiMessage("common.logout"));
   }
   if (user.hasRole(Role.ROLE_SYSADMIN)) {
     links.put("Special:Admin", new WikiMessage("usermenu.admin"));
   } else if (user.hasRole(Role.ROLE_TRANSLATE)) {
     links.put("Special:Translation", new WikiMessage("tab.admin.translations"));
   }
   return links;
 }
Ejemplo n.º 18
0
 /**
  * Build a map of links and the corresponding link text to be used as the tab menu links for the
  * WikiPageInfo object.
  */
 private static LinkedHashMap buildTabMenu(HttpServletRequest request, WikiPageInfo pageInfo) {
   LinkedHashMap links = new LinkedHashMap();
   WikiUser user = Utilities.currentUser();
   String pageName = pageInfo.getTopicName();
   String virtualWiki = WikiUtil.getVirtualWikiFromURI(request);
   try {
     if (pageInfo.getAdmin()) {
       if (user.hasRole(Role.ROLE_SYSADMIN)) {
         links.put("Special:Admin", new WikiMessage("tab.admin.configuration"));
         links.put("Special:Maintenance", new WikiMessage("tab.admin.maintenance"));
         links.put("Special:Roles", new WikiMessage("tab.admin.roles"));
       }
       if (user.hasRole(Role.ROLE_TRANSLATE)) {
         links.put("Special:Translation", new WikiMessage("tab.admin.translations"));
       }
     } else if (pageInfo.getSpecial()) {
       links.put(pageName, new WikiMessage("tab.common.special"));
     } else {
       String article = Utilities.extractTopicLink(pageName);
       String comments = Utilities.extractCommentsLink(pageName);
       links.put(article, new WikiMessage("tab.common.article"));
       links.put(comments, new WikiMessage("tab.common.comments"));
       if (ServletUtil.isEditable(virtualWiki, pageName, user)) {
         String editLink = "Special:Edit?topic=" + Utilities.encodeForURL(pageName);
         if (StringUtils.hasText(request.getParameter("topicVersionId"))) {
           editLink += "&topicVersionId=" + request.getParameter("topicVersionId");
         }
         links.put(editLink, new WikiMessage("tab.common.edit"));
       }
       String historyLink = "Special:History?topic=" + Utilities.encodeForURL(pageName);
       links.put(historyLink, new WikiMessage("tab.common.history"));
       if (ServletUtil.isMoveable(virtualWiki, pageName, user)) {
         String moveLink = "Special:Move?topic=" + Utilities.encodeForURL(pageName);
         links.put(moveLink, new WikiMessage("tab.common.move"));
       }
       if (user.hasRole(Role.ROLE_USER)) {
         Watchlist watchlist = WikiUtil.currentWatchlist(request, virtualWiki);
         boolean watched = (watchlist.containsTopic(pageName));
         String watchlistLabel = (watched) ? "tab.common.unwatch" : "tab.common.watch";
         String watchlistLink = "Special:Watchlist?topic=" + Utilities.encodeForURL(pageName);
         links.put(watchlistLink, new WikiMessage(watchlistLabel));
       }
       if (pageInfo.isUserPage()) {
         WikiLink wikiLink = LinkUtil.parseWikiLink(pageName);
         String contributionsLink =
             "Special:Contributions?contributor=" + Utilities.encodeForURL(wikiLink.getArticle());
         links.put(contributionsLink, new WikiMessage("tab.common.contributions"));
       }
       String linkToLink = "Special:LinkTo?topic=" + Utilities.encodeForURL(pageName);
       links.put(linkToLink, new WikiMessage("tab.common.links"));
       if (user.hasRole(Role.ROLE_ADMIN)) {
         String manageLink = "Special:Manage?topic=" + Utilities.encodeForURL(pageName);
         links.put(manageLink, new WikiMessage("tab.common.manage"));
       }
       String printLink = "Special:Print?topic=" + Utilities.encodeForURL(pageName);
       links.put(printLink, new WikiMessage("tab.common.print"));
     }
   } catch (Exception e) {
     logger.severe("Unable to build tabbed menu links", e);
   }
   return links;
 }