Search in sources :

Example 66 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class DefaultRSSGenerator method generateWikiPageRSS.

/**
 * {@inheritDoc}
 */
@Override
public String generateWikiPageRSS(final Context wikiContext, final List<Page> changed, final Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName() + ": " + wikiContext.getPage().getName());
    feed.setFeedURL(wikiContext.getViewURL(wikiContext.getPage().getName()));
    final String language = m_engine.getManager(VariableManager.class).getVariable(wikiContext, PROP_CHANNEL_LANGUAGE);
    if (language != null) {
        feed.setChannelLanguage(language);
    } else {
        feed.setChannelLanguage(m_channelLanguage);
    }
    final String channelDescription = m_engine.getManager(VariableManager.class).getVariable(wikiContext, PROP_CHANNEL_DESCRIPTION);
    if (channelDescription != null) {
        feed.setChannelDescription(channelDescription);
    }
    changed.sort(new PageTimeComparator());
    int items = 0;
    for (final Iterator<Page> i = changed.iterator(); i.hasNext() && items < 15; items++) {
        final Page page = i.next();
        final Entry e = new Entry();
        e.setPage(page);
        String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), page.getName(), "version=" + page.getVersion());
        } else {
            url = m_engine.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), page.getName(), "version=" + page.getVersion());
        }
        // Unfortunately, this is needed because the code will again go through replacement conversion
        url = TextUtil.replaceString(url, "&amp;", "&");
        e.setURL(url);
        e.setTitle(getEntryTitle(page));
        e.setContent(getEntryDescription(page));
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : VariableManager(org.apache.wiki.variables.VariableManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) PageTimeComparator(org.apache.wiki.pages.PageTimeComparator)

Example 67 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class DefaultRSSGenerator method generateFullWikiRSS.

/**
 * {@inheritDoc}
 */
@Override
public String generateFullWikiRSS(final Context wikiContext, final Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName());
    feed.setFeedURL(m_engine.getBaseURL());
    feed.setChannelLanguage(m_channelLanguage);
    feed.setChannelDescription(m_channelDescription);
    final Set<Page> changed = m_engine.getManager(PageManager.class).getRecentChanges();
    final Session session = Wiki.session().guest(m_engine);
    int items = 0;
    for (final Iterator<Page> i = changed.iterator(); i.hasNext() && items < 15; items++) {
        final Page page = i.next();
        // Check if the anonymous user has view access to this page.
        if (!m_engine.getManager(AuthorizationManager.class).checkPermission(session, new PagePermission(page, PagePermission.VIEW_ACTION))) {
            // No permission, skip to the next one.
            continue;
        }
        final String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(ContextEnum.PAGE_ATTACH.getRequestContext(), page.getName(), null);
        } else {
            url = m_engine.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), page.getName(), null);
        }
        final Entry e = new Entry();
        e.setPage(page);
        e.setURL(url);
        e.setTitle(page.getName());
        e.setContent(getEntryDescription(page));
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) PagePermission(org.apache.wiki.auth.permissions.PagePermission) Session(org.apache.wiki.api.core.Session)

Example 68 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class DefaultReferenceManager 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 fails.
 */
@Override
public void initialize(final Collection<Page> pages) throws ProviderException {
    LOG.debug("Initializing new ReferenceManager with {} initial pages.", pages.size());
    final StopWatch sw = new StopWatch();
    sw.start();
    LOG.info("Starting cross reference scan of WikiPages");
    // First, try to serialize old data from disk.  If that fails, we'll go and update the entire reference lists (which'll take time)
    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
        // the pages to be read already...
        // 
        // Yes, this is a kludge.  We know.  Will be fixed.
        final long saved = unserializeFromDisk();
        for (final Page page : pages) {
            unserializeAttrsFromDisk(page);
        }
        // and update the references for them.
        for (final Page page : pages) {
            if (!(page instanceof Attachment)) {
                // Refresh with the latest copy
                final Page wp = m_engine.getManager(PageManager.class).getPage(page.getName());
                if (wp.getLastModified() == null) {
                    LOG.fatal("Provider returns null lastModified.  Please submit a bug report.");
                } else if (wp.getLastModified().getTime() > saved) {
                    updatePageReferences(wp);
                }
            }
        }
    } catch (final 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.
        for (final Page page : pages) {
            // We cannot build a reference list from the contents of attachments, so we skip them.
            if (!(page instanceof Attachment)) {
                updatePageReferences(page);
                serializeAttrsToDisk(page);
            }
        }
        serializeToDisk();
    }
    sw.stop();
    LOG.info("Cross reference scan done in {}", sw);
    WikiEventManager.addWikiEventListener(m_engine.getManager(PageManager.class), this);
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Page(org.apache.wiki.api.core.Page) Attachment(org.apache.wiki.api.core.Attachment) InternalWikiException(org.apache.wiki.InternalWikiException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 69 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class DefaultReferenceManager method updatePageReferences.

/**
 *  Does a full reference update.  Does not sync; assumes that you do it afterwards.
 */
private void updatePageReferences(final Page page) throws ProviderException {
    final String content = m_engine.getManager(PageManager.class).getPageText(page.getName(), PageProvider.LATEST_VERSION);
    final Collection<String> links = scanWikiLinks(page, content);
    final TreeSet<String> res = new TreeSet<>(links);
    final List<Attachment> attachments = m_engine.getManager(AttachmentManager.class).listAttachments(page);
    for (final Attachment att : attachments) {
        res.add(att.getName());
    }
    internalUpdateReferences(page.getName(), res);
}
Also used : PageManager(org.apache.wiki.pages.PageManager) Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager)

Example 70 with Attachment

use of org.apache.wiki.api.core.Attachment in project jspwiki by apache.

the class CachingAttachmentProvider method listAllChanged.

/**
 * {@inheritDoc}
 */
@Override
public List<Attachment> listAllChanged(final Date timestamp) throws ProviderException {
    final List<Attachment> all;
    if (!m_gotall) {
        all = m_provider.listAllChanged(timestamp);
        // Make sure that all attachments are in the cache.
        synchronized (this) {
            for (final Attachment att : all) {
                cachingManager.put(CachingManager.CACHE_ATTACHMENTS, att.getName(), att);
            }
            m_gotall = true;
        }
    } else {
        final List<String> keys = cachingManager.keys(CachingManager.CACHE_ATTACHMENTS);
        all = new ArrayList<>();
        for (final String key : keys) {
            final Attachment cachedAttachment = cachingManager.get(CachingManager.CACHE_ATTACHMENTS, key, () -> null);
            if (cachedAttachment != null) {
                all.add(cachedAttachment);
            }
        }
    }
    if (cachingManager.enabled(CachingManager.CACHE_ATTACHMENTS) && all.size() >= cachingManager.info(CachingManager.CACHE_ATTACHMENTS).getMaxElementsAllowed()) {
        LOG.warn("seems {} can't hold all attachments from your page repository, " + "so we're delegating on the underlying provider instead. Please consider increasing " + "your cache sizes on the ehcache configuration file to avoid this behaviour", CachingManager.CACHE_ATTACHMENTS);
        return m_provider.listAllChanged(timestamp);
    }
    return all;
}
Also used : Attachment(org.apache.wiki.api.core.Attachment)

Aggregations

Attachment (org.apache.wiki.api.core.Attachment)76 Test (org.junit.jupiter.api.Test)37 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)35 Page (org.apache.wiki.api.core.Page)27 ProviderException (org.apache.wiki.api.exceptions.ProviderException)20 PageManager (org.apache.wiki.pages.PageManager)19 File (java.io.File)15 Date (java.util.Date)10 Engine (org.apache.wiki.api.core.Engine)10 ReferenceManager (org.apache.wiki.references.ReferenceManager)9 InputStream (java.io.InputStream)8 FileInputStream (java.io.FileInputStream)6 IOException (java.io.IOException)6 InputStreamReader (java.io.InputStreamReader)6 StringWriter (java.io.StringWriter)6 Vector (java.util.Vector)6 WikiContext (org.apache.wiki.WikiContext)6 Permission (java.security.Permission)4 ArrayList (java.util.ArrayList)4 Hashtable (java.util.Hashtable)4