Search in sources :

Example 21 with Attachment

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

the class MarkdownRendererTest method testAttachmentLink.

@Test
public void testAttachmentLink() throws Exception {
    String src = "This should be an [attachment link](Test/TestAtt.txt)";
    newPage("Test");
    Attachment att = new Attachment(testEngine, "Test", "TestAtt.txt");
    att.setAuthor("FirstPost");
    testEngine.getAttachmentManager().storeAttachment(att, testEngine.makeAttachmentFile());
    Assert.assertEquals("<p>This should be an <a href=\"/test/attach/Test/TestAtt.txt\" class=\"attachment\">attachment link</a>" + "<a href=\"/test/PageInfo.jsp?page=Test/TestAtt.txt\" class=\"infolink\">" + "<img src=\"/test/images/attachment_small.png\" border=\"0\" alt=\"(info)\" />" + "</a></p>\n", translate(src));
}
Also used : Attachment(org.apache.wiki.attachment.Attachment) Test(org.junit.Test)

Example 22 with Attachment

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

the class ReferenceManager method initialize.

/**
 *  Initializes the entire reference manager with the initial set of pages
 *  from the collection.
 *
 *  @param pages A collection of all pages you want to be included in the reference
 *               count.
 *  @since 2.2
 *  @throws ProviderException If reading of pages fail.
 */
public void initialize(Collection pages) throws ProviderException {
    log.debug("Initializing new ReferenceManager with " + pages.size() + " initial pages.");
    StopWatch sw = new StopWatch();
    sw.start();
    log.info("Starting cross reference scan of WikiPages");
    // 
    try {
        // 
        // Unserialize things.  The loop below cannot be combined with
        // the other loop below, simply because engine.getPage() has
        // side effects such as loading initializing the user databases,
        // which in turn want all of the pages to be read already...
        // 
        // Yes, this is a kludge.  We know.  Will be fixed.
        // 
        long saved = unserializeFromDisk();
        for (Iterator it = pages.iterator(); it.hasNext(); ) {
            WikiPage page = (WikiPage) it.next();
            unserializeAttrsFromDisk(page);
        }
        // 
        // Now we must check if any of the pages have been changed
        // while we were in the electronic la-la-land, and update
        // the references for them.
        // 
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // Skip attachments
            } else {
                // Refresh with the latest copy
                page = m_engine.getPage(page.getName());
                if (page.getLastModified() == null) {
                    log.fatal("Provider returns null lastModified.  Please submit a bug report.");
                } else if (page.getLastModified().getTime() > saved) {
                    updatePageReferences(page);
                }
            }
        }
    } catch (Exception e) {
        log.info("Unable to unserialize old refmgr information, rebuilding database: " + e.getMessage());
        buildKeyLists(pages);
        // Scan the existing pages from disk and update references in the manager.
        Iterator it = pages.iterator();
        while (it.hasNext()) {
            WikiPage page = (WikiPage) it.next();
            if (page instanceof Attachment) {
            // We cannot build a reference list from the contents
            // of attachments, so we skip them.
            } else {
                updatePageReferences(page);
                serializeAttrsToDisk(page);
            }
        }
        serializeToDisk();
    }
    sw.stop();
    log.info("Cross reference scan done in " + sw);
    WikiEventUtils.addWikiEventListener(m_engine.getPageManager(), WikiPageEvent.PAGE_DELETED, this);
}
Also used : Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConcurrentModificationException(java.util.ConcurrentModificationException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 23 with Attachment

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

the class WikiEngine method beautifyTitle.

/**
 *  Beautifies the title of the page by appending spaces in suitable
 *  places, if the user has so decreed in the properties when constructing
 *  this WikiEngine.  However, attachment names are only beautified by
 *  the name.
 *
 *  @param title The title to beautify
 *  @return A beautified title (or, if beautification is off,
 *          returns the title without modification)
 *  @since 1.7.11
 */
public String beautifyTitle(String title) {
    if (m_beautifyTitle) {
        try {
            Attachment att = m_attachmentManager.getAttachmentInfo(title);
            if (att == null) {
                return TextUtil.beautifyString(title);
            }
            String parent = TextUtil.beautifyString(att.getParentName());
            return parent + "/" + att.getFileName();
        } catch (ProviderException e) {
            return title;
        }
    }
    return title;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Attachment(org.apache.wiki.attachment.Attachment)

Example 24 with Attachment

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

the class WikiEngine method getRecentChanges.

/**
 *  Returns a Collection of WikiPages, sorted in time
 *  order of last change (i.e. first object is the most
 *  recently changed).  This method also includes attachments.
 *
 *  @return Collection of WikiPage objects.  In reality, the returned
 *          collection is a Set, but due to API compatibility reasons,
 *          we're not changing the signature soon...
 */
// FIXME: Should really get a Date object and do proper comparisons.
// This is terribly wasteful.
@SuppressWarnings("unchecked")
public Collection getRecentChanges() {
    try {
        Collection<WikiPage> pages = m_pageManager.getAllPages();
        Collection<Attachment> atts = m_attachmentManager.getAllAttachments();
        TreeSet<WikiPage> sortedPages = new TreeSet<WikiPage>(new PageTimeComparator());
        sortedPages.addAll(pages);
        sortedPages.addAll(atts);
        return sortedPages;
    } catch (ProviderException e) {
        log.error("Unable to fetch all pages: ", e);
        return null;
    }
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) Attachment(org.apache.wiki.attachment.Attachment) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 25 with Attachment

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

the class PageRenamer method getReferencesToChange.

private Set<String> getReferencesToChange(WikiPage fromPage, WikiEngine engine) {
    Set<String> referrers = new TreeSet<String>();
    @SuppressWarnings("unchecked") Collection<String> r = engine.getReferenceManager().findReferrers(fromPage.getName());
    if (r != null)
        referrers.addAll(r);
    try {
        @SuppressWarnings("unchecked") Collection<Attachment> attachments = engine.getAttachmentManager().listAttachments(fromPage);
        for (Attachment att : attachments) {
            @SuppressWarnings("unchecked") Collection<String> c = engine.getReferenceManager().findReferrers(att.getName());
            if (c != null)
                referrers.addAll(c);
        }
    } catch (ProviderException e) {
        // We will continue despite this error
        log.error("Provider error while fetching attachments for rename", e);
    }
    return referrers;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) 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