/** * Retrieve the content of a topic from the cache, or if it is not yet in the cache then add it to * the cache. * * @param context The servlet context for the topic being retrieved. May be <code>null</code> if * the <code>cook</code> parameter is set to <code>false</code>. * @param locale The locale for the topic being retrieved. May be <code>null</code> if the <code> * cook</code> parameter is set to <code>false</code>. * @param virtualWiki The virtual wiki for the topic being retrieved. * @param topicName The name of the topic being retrieved. * @param cook A parameter indicating whether or not the content should be parsed before it is * added to the cache. Stylesheet content (CSS) is not parsed, but most other content is * parsed. * @return The parsed or unparsed (depending on the <code>cook</code> parameter) topic content. */ protected static String cachedContent( String context, Locale locale, String virtualWiki, String topicName, boolean cook) { String content = null; String key = WikiCache.key(virtualWiki, topicName); Element cacheElement = WikiCache.retrieveFromCache(WikiBase.CACHE_PARSED_TOPIC_CONTENT, key); if (cacheElement != null) { content = (String) cacheElement.getObjectValue(); return (content == null) ? null : new String(content); } try { Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); content = topic.getTopicContent(); if (cook) { ParserInput parserInput = new ParserInput(); parserInput.setContext(context); parserInput.setLocale(locale); parserInput.setVirtualWiki(virtualWiki); parserInput.setTopicName(topicName); content = Utilities.parse(parserInput, null, content); } WikiCache.addToCache(WikiBase.CACHE_PARSED_TOPIC_CONTENT, key, content); } catch (Exception e) { logger.warning("error getting cached page " + virtualWiki + " / " + topicName, e); return null; } return content; }
private void view( HttpServletRequest request, HttpServletResponse response, ModelAndView next, WikiPageInfo pageInfo) throws Exception { String topicName = WikiUtil.getTopicFromURI(request); if (StringUtils.isBlank(topicName)) { String virtualWikiName = pageInfo.getVirtualWikiName(); VirtualWiki virtualWiki = WikiBase.getDataHandler().lookupVirtualWiki(virtualWikiName); topicName = virtualWiki.getRootTopicName(); } String virtualWiki = pageInfo.getVirtualWikiName(); if (StringUtils.isBlank(virtualWiki)) { virtualWiki = VirtualWiki.defaultVirtualWiki().getName(); } Topic topic = ServletUtil.initializeTopic(virtualWiki, topicName); if (topic.getTopicId() <= 0) { // topic does not exist, return 404 and display empty page response.setStatus(HttpServletResponse.SC_NOT_FOUND); WikiMessage wikiMessage = new WikiMessage("topic.notcreated"); // topic name is escaped from WikiUtil.getTopicFromURI, so do not double-escape wikiMessage.setParamsWithoutEscaping(new String[] {topicName}); next.addObject("notopic", wikiMessage); } WikiMessage pageTitle = new WikiMessage("topic.title", topicName); ServletUtil.viewTopic(request, next, pageInfo, pageTitle, topic, true, true); }
/** * Create a basic Lucene document to add to the index. This document is suitable to be parsed with * the StandardAnalyzer. */ private Document createStandardDocument(Topic topic) { String topicContent = topic.getTopicContent(); if (topicContent == null) { topicContent = ""; } Document doc = new Document(); // store the (not analyzed) topic name to use when deleting records from the index. doc.add( new Field( FIELD_TOPIC_NAME, topic.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); // add the topic namespace (not analyzed) topic namespace to allow retrieval by namespace. // this field is used internally in searches. doc.add( new Field( FIELD_TOPIC_NAMESPACE, topic.getNamespace().getId().toString(), Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); // analyze the topic name so that (for example) a search for "New York" will match "New York // City" Field nameField = new Field(FIELD_TOPIC_NAME_ANALYZED, topic.getName(), Field.Store.NO, Field.Index.ANALYZED); // make the topic name worth 3x as much as topic content in searches nameField.setBoost(3.0f); doc.add(nameField); // analyze & store the topic content so that it is searchable and also usable for display in // search result summaries doc.add(new Field(FIELD_TOPIC_CONTENT, topicContent, Field.Store.YES, Field.Index.ANALYZED)); return doc; }
/** * Initialize topic values for the topic being edited. If a topic with the specified name already * exists then it will be initialized, otherwise a new topic is created. */ private Topic loadTopic(String virtualWiki, String topicName) throws Exception { Topic topic = ServletUtil.initializeTopic(virtualWiki, topicName); if (topic.getReadOnly()) { throw new WikiException(new WikiMessage("error.readonly")); } return topic; }
/** * 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()); }
/** 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); }
@Test public void testImportFromFileWithUnsortedHistory() throws Throwable { String virtualWiki = VIRTUAL_WIKI_EN; List<String> results = this.importTestFile(FILE_ONE_TOPIC_WITH_UNSORTED_HISTORY); Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, TOPIC_NAME3, false, null); // validate that the current topic content is correct assertEquals( "Incorrect topic ordering: " + topic.getTopicId() + " / " + topic.getCurrentVersionId(), "Newest Revision", topic.getTopicContent()); }
private void resolve(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception { String topicName = WikiUtil.getTopicFromRequest(request); String virtualWiki = pageInfo.getVirtualWikiName(); Topic lastTopic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); String contents1 = lastTopic.getTopicContent(); String contents2 = request.getParameter("contents"); next.addObject("lastTopicVersionId", lastTopic.getCurrentVersionId()); next.addObject("contentsResolve", contents2); this.loadDiff(request, next, pageInfo, contents1, contents2); this.loadEdit(request, next, pageInfo, contents1, virtualWiki, topicName, false); next.addObject("editResolve", "true"); }
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); }
/** 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; }
/** * 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 topicName The topic being viewed. This value must be a valid topic that can be loaded as * a org.jamwiki.model.Topic object. * @throws Exception Thrown if any error occurs during processing. */ protected static void viewTopic( HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo, String topicName) throws Exception { String virtualWiki = WikiUtil.getVirtualWikiFromURI(request); if (!StringUtils.hasText(virtualWiki)) { virtualWiki = WikiBase.DEFAULT_VWIKI; } Topic topic = ServletUtil.initializeTopic(virtualWiki, topicName); if (topic.getTopicId() <= 0) { // topic does not exist, display empty page next.addObject("notopic", new WikiMessage("topic.notcreated", topicName)); } WikiMessage pageTitle = new WikiMessage("topic.title", topicName); viewTopic(request, next, pageInfo, pageTitle, topic, true); }
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); }
private ModelAndView loginRequired(HttpServletRequest request) throws Exception { String topicName = JAMWikiServlet.getTopicFromRequest(request); String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request); if (!StringUtils.hasText(topicName) || !StringUtils.hasText(virtualWiki)) { return null; } if (Environment.getBooleanValue(Environment.PROP_TOPIC_FORCE_USERNAME) && Utilities.currentUser(request) == null) { WikiMessage errorMessage = new WikiMessage("edit.exception.login"); return viewLogin(request, JAMWikiServlet.getTopicFromURI(request), errorMessage); } Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName); if (topic != null && topic.getAdminOnly() && !Utilities.isAdmin(request)) { WikiMessage errorMessage = new WikiMessage("edit.exception.loginadmin", topicName); return viewLogin(request, JAMWikiServlet.getTopicFromURI(request), errorMessage); } return null; }
/** * 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; }
public static String getCachedContent( HttpServletRequest request, String virtualWiki, String topicName, boolean cook) { String content = (String) cachedContents.get(virtualWiki + "-" + topicName); if (content == null) { try { Topic topic = WikiBase.getHandler().lookupTopic(virtualWiki, topicName); content = topic.getTopicContent(); if (cook) { ParserInfo parserInfo = new ParserInfo(request.getContextPath(), request.getLocale()); parserInfo.setVirtualWiki(virtualWiki); content = Utilities.parse(parserInfo, content, topicName); } cachedContents.put(virtualWiki + "-" + topicName, content); } catch (Exception e) { logger.warn("error getting cached page " + virtualWiki + " / " + topicName, e); return null; } } return content; }
/** * Initialize topic values for a Topic object. This method will check to see if a topic with the * specified name exists, and if it does exist then that topic will be returned. Otherwise a new * topic will be initialized, setting initial parameters such as topic name, virtual wiki, and * topic type. * * @param virtualWiki The virtual wiki name for the topic being initialized. * @param topicName The name of the topic being initialized. * @return A new topic object with basic fields initialized, or if a topic with the given name * already exists then the pre-existing topic is returned. * @throws Exception Thrown if any error occurs while retrieving or initializing the topic object. */ protected static Topic initializeTopic(String virtualWiki, String topicName) throws Exception { WikiUtil.validateTopicName(topicName); Topic topic = WikiBase.getDataHandler().lookupTopic(virtualWiki, topicName, false, null); if (topic != null) { return topic; } topic = new Topic(); topic.setName(topicName); topic.setVirtualWiki(virtualWiki); WikiLink wikiLink = LinkUtil.parseWikiLink(topicName); String namespace = wikiLink.getNamespace(); if (namespace != null) { if (namespace.equals(NamespaceHandler.NAMESPACE_CATEGORY)) { topic.setTopicType(Topic.TYPE_CATEGORY); } else if (namespace.equals(NamespaceHandler.NAMESPACE_TEMPLATE)) { topic.setTopicType(Topic.TYPE_TEMPLATE); } } return topic; }
private void edit(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception { String topicName = WikiUtil.getTopicFromRequest(request); String virtualWiki = pageInfo.getVirtualWikiName(); Topic topic = loadTopic(virtualWiki, topicName); // topic name might be updated by loadTopic topicName = topic.getName(); Integer lastTopicVersionId = retrieveLastTopicVersionId(request, topic); next.addObject("lastTopicVersionId", lastTopicVersionId); String contents = (String) request.getParameter("contents"); if (isPreview(request)) { preview(request, next, pageInfo); } else if (isShowChanges(request)) { showChanges(request, next, pageInfo, virtualWiki, topicName, lastTopicVersionId); } else if (!StringUtils.isBlank(request.getParameter("topicVersionId"))) { // editing an older version Integer topicVersionId = Integer.valueOf(request.getParameter("topicVersionId")); TopicVersion topicVersion = WikiBase.getDataHandler().lookupTopicVersion(topicVersionId); if (topicVersion == null) { throw new WikiException(new WikiMessage("common.exception.notopic")); } contents = topicVersion.getVersionContent(); if (!lastTopicVersionId.equals(topicVersionId)) { next.addObject("topicVersionId", topicVersionId); } } else if (!StringUtils.isBlank(request.getParameter("section"))) { // editing a section of a topic int section = Integer.valueOf(request.getParameter("section")); String[] sliceResults = ParserUtil.parseSlice( request.getContextPath(), request.getLocale(), virtualWiki, topicName, section); contents = sliceResults[1]; String sectionName = sliceResults[0]; String editComment = "/* " + sectionName + " */ "; next.addObject("editComment", editComment); } else { // editing a full new or existing topic contents = (topic == null) ? "" : topic.getTopicContent(); } this.loadEdit(request, next, pageInfo, contents, virtualWiki, topicName, true); }
@Override public String getRawWikiContent( String namespace, String topicName, Map<String, String> templateParameters) { String result = super.getRawWikiContent(namespace, topicName, templateParameters); if (result != null) { return result; } try { topicName = topicName.replaceAll("_", " "); Topic topic = WikiBase.getDataHandler() .lookupTopic(fParserInput.getVirtualWiki(), namespace + ':' + topicName, false, null); if (topic == null) { return null; } return topic.getTopicContent(); } catch (Exception e) { e.printStackTrace(); } return result; }
/** * 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); } }
private void edit(HttpServletRequest request, ModelAndView next, WikiPageInfo pageInfo) throws Exception { String topicName = JAMWikiServlet.getTopicFromRequest(request); String virtualWiki = JAMWikiServlet.getVirtualWikiFromURI(request); Topic topic = loadTopic(virtualWiki, topicName); // topic name might be updated by loadTopic topicName = topic.getName(); int lastTopicVersionId = retrieveLastTopicVersionId(request, virtualWiki, topicName); next.addObject("lastTopicVersionId", new Integer(lastTopicVersionId)); loadEdit(request, next, pageInfo, virtualWiki, topicName, true); String contents = null; if (isPreview(request)) { preview(request, next, pageInfo); return; } pageInfo.setAction(WikiPageInfo.ACTION_EDIT); if (StringUtils.hasText(request.getParameter("topicVersionId"))) { // editing an older version int topicVersionId = Integer.parseInt(request.getParameter("topicVersionId")); TopicVersion topicVersion = WikiBase.getHandler().lookupTopicVersion(topicName, topicVersionId); if (topicVersion == null) { throw new WikiException(new WikiMessage("common.exception.notopic")); } contents = topicVersion.getVersionContent(); if (lastTopicVersionId != topicVersionId) { next.addObject("topicVersionId", new Integer(topicVersionId)); } } else if (StringUtils.hasText(request.getParameter("section"))) { // editing a section of a topic int section = (new Integer(request.getParameter("section"))).intValue(); ParserDocument parserDocument = Utilities.parseSlice(request, virtualWiki, topicName, section); contents = parserDocument.getContent(); } else { // editing a full new or existing topic contents = (topic == null) ? "" : topic.getTopicContent(); } next.addObject("contents", contents); }
/** * 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); }
/** * Set up images separately - one image is created in both virtual wikis, the second image is set * up in only the shared virtual wiki. */ private void setupImage(VirtualWiki virtualWiki, Topic topic) throws DataAccessException, IOException, WikiException { if (!topic.getName().toLowerCase().startsWith("image:")) { throw new IllegalArgumentException( "Cannot call JAMWikiUtilTest.setupImage for non-image topics"); } TopicVersion topicVersion = new TopicVersion( null, "127.0.0.1", null, topic.getTopicContent(), topic.getTopicContent().length()); topic.setTopicType(TopicType.IMAGE); topicVersion.setEditType(TopicVersion.EDIT_UPLOAD); // hard code image details - Image:Test Image.jpg will be created for both the "en" // and "test" virtual wikis, while Image:Test Image2.jpg will be created only for // the "test" virtual wiki. WikiFileVersion wikiFileVersion = new WikiFileVersion(); if (topic.getName().equals("Image:Test Image.jpg") && virtualWiki.getName().equals("en")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image.jpg", "/test_image.jpg", "image/jpeg", 61136, null); } else if (topic.getName().equals("Image:Test Image.jpg") && virtualWiki.getName().equals("test")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image_shared.jpg", "/test_image_shared.jpg", "image/jpeg", 61136, null); } else if (topic.getName().equals("Image:Test Image2.jpg") && virtualWiki.getName().equals("test")) { WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); ImageUtil.writeWikiFile( topic, wikiFileVersion, null, "127.0.0.1", "test_image2_shared.jpg", "/test_image2_shared.jpg", "image/jpeg", 61136, null); } }
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); }
/** * 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); }
/** * 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; }
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")); }
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); }
@Test public void testImportFromFileWithTwoTopics() throws Throwable { String virtualWiki = VIRTUAL_WIKI_EN; List<String> results = this.importTestFile(FILE_TEST_TWO_TOPICS_WITH_HISTORY); // validate that the first topic parsed assertTrue("Parsed topic '" + TOPIC_NAME1 + "'", results.contains(TOPIC_NAME1)); Topic topic1 = WikiBase.getDataHandler().lookupTopic(virtualWiki, TOPIC_NAME1, false, null); // validate that the parsed topic correctly set topic values assertEquals("Topic name '" + TOPIC_NAME1 + "' set correctly", TOPIC_NAME1, topic1.getName()); assertTrue( "Topic content set correctly", topic1.getTopicContent().indexOf("Link to user page: [[User:Test User]]") != -1); // validate that namespaces were converted from Mediawiki to JAMWiki correctly assertTrue( "Topic content namespaces updated correctly", topic1.getTopicContent().indexOf("Link to user talk page: [[User comments: Test User]]") != -1); // validate that the second topic parsed assertTrue("Parsed topic '" + TOPIC_NAME2 + "'", results.contains(TOPIC_NAME2)); Topic topic2 = WikiBase.getDataHandler().lookupTopic(virtualWiki, TOPIC_NAME2, false, null); // validate that the parsed topic correctly set topic values assertEquals("Topic name '" + TOPIC_NAME2 + "' set correctly", TOPIC_NAME2, topic2.getName()); }
/** Crate a test topic. Cannot be used for images. */ protected void setupTopic(Topic topic) throws DataAccessException, WikiException { TopicVersion topicVersion = new TopicVersion( null, "127.0.0.1", null, topic.getTopicContent(), topic.getTopicContent().length()); WikiBase.getDataHandler().writeTopic(topic, topicVersion, null, null); }
/** * 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()); }