Search in sources :

Example 46 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class PageRenamer method renamePage.

/**
 *  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(final WikiContext context, final String renameFrom, final String renameTo, final boolean changeReferrers) throws WikiException {
    // 
    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
    // 
    String renameToClean = MarkupParser.cleanLink(renameTo.trim());
    if (renameToClean.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(renameToClean);
    if (toPage != null) {
        throw new WikiException("Page already exists " + renameToClean);
    }
    // 
    // 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
    // 
    // Remove references to attachments under old name
    @SuppressWarnings("unchecked") Collection<Attachment> attachmentsOldName = engine.getAttachmentManager().listAttachments(fromPage);
    for (Attachment att : attachmentsOldName) {
        WikiPage fromAttPage = engine.getPage(att.getName());
        engine.getReferenceManager().pageRemoved(fromAttPage);
    }
    engine.getPageManager().getProvider().movePage(renameFrom, renameToClean);
    if (engine.getAttachmentManager().attachmentsEnabled()) {
        engine.getAttachmentManager().getCurrentProvider().moveAttachmentsForPage(renameFrom, renameToClean);
    }
    // 
    // Add a comment to the page notifying what changed.  This adds a new revision
    // to the repo with no actual change.
    // 
    toPage = engine.getPage(renameToClean);
    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);
    // 
    if (changeReferrers) {
        updateReferrers(context, fromPage, toPage, referrers);
    }
    // 
    // re-index the page including its attachments
    // 
    engine.getSearchManager().reindexPage(toPage);
    @SuppressWarnings("unchecked") Collection<Attachment> attachmentsNewName = engine.getAttachmentManager().listAttachments(toPage);
    for (Attachment att : attachmentsNewName) {
        WikiPage toAttPage = engine.getPage(att.getName());
        // add reference to attachment under new page name
        engine.updateReferences(toAttPage);
        engine.getSearchManager().reindexPage(att);
    }
    // Currently not used internally by JSPWiki itself, but you can use it for something else.
    WikiEventManager.fireEvent(this, new WikiPageRenameEvent(this, renameFrom, renameToClean));
    // 
    return renameToClean;
}
Also used : InternalWikiException(org.apache.wiki.InternalWikiException) WikiException(org.apache.wiki.api.exceptions.WikiException) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine) InternalWikiException(org.apache.wiki.InternalWikiException) WikiPageRenameEvent(org.apache.wiki.event.WikiPageRenameEvent)

Example 47 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class ParentPageNameTag method doWikiStartTag.

/**
 * {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    if (page != null) {
        if (page instanceof Attachment) {
            pageContext.getOut().print(engine.beautifyTitle(((Attachment) page).getParentName()));
        } else {
            String name = page.getName();
            int entrystart = name.indexOf("_blogentry_");
            if (entrystart != -1) {
                name = name.substring(0, entrystart);
            }
            int commentstart = name.indexOf("_comments_");
            if (commentstart != -1) {
                name = name.substring(0, commentstart);
            }
            pageContext.getOut().print(engine.beautifyTitle(name));
        }
    }
    return SKIP_BODY;
}
Also used : WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine)

Example 48 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class BasicAttachmentProvider method getAttachmentInfo.

/**
 *  {@inheritDoc}
 */
public Attachment getAttachmentInfo(WikiPage page, String name, int version) throws ProviderException {
    Attachment att = new Attachment(m_engine, page.getName(), name);
    File dir = findAttachmentDir(att);
    if (!dir.exists()) {
        // log.debug("Attachment dir not found - thus no attachment can exist.");
        return null;
    }
    if (version == WikiProvider.LATEST_VERSION) {
        version = findLatestVersion(att);
    }
    att.setVersion(version);
    // Should attachment be cachable by the client (browser)?
    if (m_disableCache != null) {
        Matcher matcher = m_disableCache.matcher(name);
        if (matcher.matches()) {
            att.setCacheable(false);
        }
    }
    // System.out.println("Fetching info on version "+version);
    try {
        Properties props = getPageProperties(att);
        att.setAuthor(props.getProperty(version + ".author"));
        String changeNote = props.getProperty(version + ".changenote");
        if (changeNote != null) {
            att.setAttribute(WikiPage.CHANGENOTE, changeNote);
        }
        File f = findFile(dir, att);
        att.setSize(f.length());
        att.setLastModified(new Date(f.lastModified()));
    } catch (FileNotFoundException e) {
        log.error("Can't get attachment properties for " + att, e);
        return null;
    } catch (IOException e) {
        log.error("Can't read page properties", e);
        throw new ProviderException("Cannot read page properties: " + e.getMessage());
    }
    return att;
}
Also used : Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) Date(java.util.Date)

Example 49 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class BasicAttachmentProvider method listAttachments.

/**
 *  {@inheritDoc}
 */
public Collection listAttachments(WikiPage page) throws ProviderException {
    Collection<Attachment> result = new ArrayList<Attachment>();
    File dir = findPageDir(page.getName());
    if (dir != null) {
        String[] attachments = dir.list();
        if (attachments != null) {
            // 
            for (int i = 0; i < attachments.length; i++) {
                File f = new File(dir, attachments[i]);
                if (f.isDirectory()) {
                    String attachmentName = unmangleName(attachments[i]);
                    // 
                    if (attachmentName.endsWith(ATTDIR_EXTENSION)) {
                        attachmentName = attachmentName.substring(0, attachmentName.length() - ATTDIR_EXTENSION.length());
                    } else {
                        File propFile = new File(f, PROPERTY_FILE);
                        if (!propFile.exists()) {
                            // 
                            continue;
                        }
                    }
                    Attachment att = getAttachmentInfo(page, attachmentName, WikiProvider.LATEST_VERSION);
                    // 
                    if (att == null) {
                        throw new ProviderException("Attachment disappeared while reading information:" + " if you did not touch the repository, there is a serious bug somewhere. " + "Attachment = " + attachments[i] + ", decoded = " + attachmentName);
                    }
                    result.add(att);
                }
            }
        }
    }
    return result;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) ArrayList(java.util.ArrayList) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File)

Example 50 with Attachment

use of org.apache.wiki.attachment.Attachment in project jspwiki by apache.

the class CachingAttachmentProvider method listAllChanged.

/**
 * {@inheritDoc}
 */
public List listAllChanged(Date timestamp) throws ProviderException {
    List all = null;
    // we do a one-time build up of the cache, after this the cache is updated for every attachment add/delete
    if (m_gotall == false) {
        all = m_provider.listAllChanged(timestamp);
        synchronized (this) {
            for (Iterator i = all.iterator(); i.hasNext(); ) {
                Attachment att = (Attachment) i.next();
                m_attCache.put(new Element(att.getName(), att));
            }
            m_gotall = true;
        }
    } else {
        List<String> keys = m_attCache.getKeysWithExpiryCheck();
        all = new ArrayList();
        for (String key : keys) {
            Element element = m_attCache.get(key);
            Object cachedAttachment = element.getObjectValue();
            if (cachedAttachment != null) {
                all.add((Attachment) cachedAttachment);
            }
        }
    }
    return all;
}
Also used : Element(net.sf.ehcache.Element) Attachment(org.apache.wiki.attachment.Attachment)

Aggregations

Attachment (org.apache.wiki.attachment.Attachment)62 WikiPage (org.apache.wiki.WikiPage)26 Test (org.junit.Test)23 File (java.io.File)20 ProviderException (org.apache.wiki.api.exceptions.ProviderException)17 Collection (java.util.Collection)13 Date (java.util.Date)11 Iterator (java.util.Iterator)10 WikiEngine (org.apache.wiki.WikiEngine)10 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)7 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 List (java.util.List)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)5 Hashtable (java.util.Hashtable)4 Element (net.sf.ehcache.Element)4 PagePermission (org.apache.wiki.auth.permissions.PagePermission)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3