/** * This method finds all the pages which have anything to do with the fromPage and change any * referrers it can figure out in that page. * * @param context WikiContext in which we operate * @param fromPage The old page * @param toPage The new page */ @SuppressWarnings("unchecked") private void updateReferrers( WikiContext context, WikiPage fromPage, WikiPage toPage, Set<String> referrers) { WikiEngine engine = context.getEngine(); if (referrers.isEmpty()) return; // No referrers for (String pageName : referrers) { // In case the page was just changed from under us, let's do this // small kludge. if (pageName.equals(fromPage.getName())) { pageName = toPage.getName(); } WikiPage p = engine.getPage(pageName); String sourceText = engine.getPureText(p); String newText = replaceReferrerString(context, sourceText, fromPage.getName(), toPage.getName()); if (m_camelCase) newText = replaceCCReferrerString(context, newText, fromPage.getName(), toPage.getName()); if (!sourceText.equals(newText)) { p.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName()); p.setAuthor(context.getCurrentUser().getName()); try { engine.getPageManager().putPageText(p, newText); engine.updateReferences(p); } catch (ProviderException e) { // // We fail with an error, but we will try to continue to rename // other referrers as well. // log.error("Unable to perform rename.", e); } } } }
/** * Stores an attachment directly from a stream. If the attachment did not exist previously, this * method will create it. If it did exist, it stores a new version. * * @param att Attachment to store this under. * @param in InputStream from which the attachment contents will be read. * @throws IOException If writing the attachment failed. * @throws ProviderException If something else went wrong. */ public void storeAttachment(Attachment att, InputStream in) throws IOException, ProviderException { if (m_provider == null) { return; } // // Checks if the actual, real page exists without any modifications // or aliases. We cannot store an attachment to a non-existant page. // if (!m_engine.getPageManager().pageExists(att.getParentName())) { // the caller should catch the exception and use the exception text as an i18n key throw new ProviderException("attach.parent.not.exist"); } m_provider.putAttachmentData(att, in); m_engine.getReferenceManager().updateReferences(att.getName(), new java.util.Vector()); WikiPage parent = new WikiPage(m_engine, att.getParentName()); m_engine.updateReferences(parent); m_engine.getSearchManager().reindexPage(att); }
/** * Renames a page. * * @param context The current context. * @param renameFrom The name from which to rename. * @param renameTo The new name. * @param changeReferrers If true, also changes all the referrers. * @return The final new name (in case it had to be modified) * @throws WikiException If the page cannot be renamed. */ public String renamePage( WikiContext context, String renameFrom, String renameTo, boolean changeReferrers) throws WikiException { // // Sanity checks first // if (renameFrom == null || renameFrom.length() == 0) { throw new WikiException("From name may not be null or empty"); } if (renameTo == null || renameTo.length() == 0) { throw new WikiException("To name may not be null or empty"); } // // Clean up the "to" -name so that it does not contain anything illegal // renameTo = MarkupParser.cleanLink(renameTo.trim()); if (renameTo.equals(renameFrom)) { throw new WikiException("You cannot rename the page to itself"); } // // Preconditions: "from" page must exist, and "to" page must not yet exist. // WikiEngine engine = context.getEngine(); WikiPage fromPage = engine.getPage(renameFrom); if (fromPage == null) { throw new WikiException("No such page " + renameFrom); } WikiPage toPage = engine.getPage(renameTo); if (toPage != null) { throw new WikiException("Page already exists " + renameTo); } // // Options // m_camelCase = TextUtil.getBooleanProperty( engine.getWikiProperties(), JSPWikiMarkupParser.PROP_CAMELCASELINKS, m_camelCase); Set<String> referrers = getReferencesToChange(fromPage, engine); // // Do the actual rename by changing from the frompage to the topage, including // all of the attachments // engine.getPageManager().getProvider().movePage(renameFrom, renameTo); if (engine.getAttachmentManager().attachmentsEnabled()) { engine .getAttachmentManager() .getCurrentProvider() .moveAttachmentsForPage(renameFrom, renameTo); } // // Add a comment to the page notifying what changed. This adds a new revision // to the repo with no actual change. // toPage = engine.getPage(renameTo); if (toPage == null) throw new InternalWikiException( "Rename seems to have failed for some strange reason - please check logs!"); toPage.setAttribute(WikiPage.CHANGENOTE, fromPage.getName() + " ==> " + toPage.getName()); toPage.setAuthor(context.getCurrentUser().getName()); engine.getPageManager().putPageText(toPage, engine.getPureText(toPage)); // // Update the references // engine.getReferenceManager().pageRemoved(fromPage); engine.updateReferences(toPage); // // Update referrers // if (changeReferrers) { updateReferrers(context, fromPage, toPage, referrers); } // // re-index the page // engine.getSearchManager().reindexPage(toPage); Collection<Attachment> attachments = engine.getAttachmentManager().listAttachments(toPage); for (Attachment att : attachments) { engine.getSearchManager().reindexPage(att); } // // Done, return the new name. // return renameTo; }