Search in sources :

Example 1 with PageTimeComparator

use of org.apache.wiki.util.comparators.PageTimeComparator in project jspwiki by apache.

the class MetaWeblogHandler method getRecentPosts.

/**
 *  Returns a list of the recent posts to this weblog.
 *
 *  @param blogid The id of the blog.
 *  @param username The username to use
 *  @param password The password
 *  @param numberOfPosts How many posts to find
 *  @throws XmlRpcException If something goes wrong
 *  @return As per MetaweblogAPI specification
 */
// FIXME: The implementation is suboptimal, as it
// goes through all of the blog entries.
@SuppressWarnings("unchecked")
public Hashtable getRecentPosts(String blogid, String username, String password, int numberOfPosts) throws XmlRpcException {
    Hashtable<String, Hashtable<String, Object>> result = new Hashtable<String, Hashtable<String, Object>>();
    log.info("metaWeblog.getRecentPosts() called");
    WikiPage page = m_context.getEngine().getPage(blogid);
    checkPermissions(page, username, password, "view");
    try {
        WeblogPlugin plugin = new WeblogPlugin();
        List<WikiPage> changed = plugin.findBlogEntries(m_context.getEngine(), blogid, new Date(0L), new Date());
        Collections.sort(changed, new PageTimeComparator());
        int items = 0;
        for (Iterator<WikiPage> i = changed.iterator(); i.hasNext() && items < numberOfPosts; items++) {
            WikiPage p = i.next();
            result.put("entry", makeEntry(p));
        }
    } catch (ProviderException e) {
        log.error("Failed to list recent posts", e);
        throw new XmlRpcException(0, e.getMessage());
    }
    return result;
}
Also used : WeblogPlugin(org.apache.wiki.plugin.WeblogPlugin) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Hashtable(java.util.Hashtable) WikiPage(org.apache.wiki.WikiPage) Date(java.util.Date) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 2 with PageTimeComparator

use of org.apache.wiki.util.comparators.PageTimeComparator in project jspwiki by apache.

the class BasicAttachmentProvider method listAllChanged.

/**
 *  {@inheritDoc}
 */
// FIXME: Very unoptimized.
public List listAllChanged(Date timestamp) throws ProviderException {
    File attDir = new File(m_storageDir);
    if (!attDir.exists()) {
        throw new ProviderException("Specified attachment directory " + m_storageDir + " does not exist!");
    }
    ArrayList<Attachment> list = new ArrayList<Attachment>();
    String[] pagesWithAttachments = attDir.list(new AttachmentFilter());
    for (int i = 0; i < pagesWithAttachments.length; i++) {
        String pageId = unmangleName(pagesWithAttachments[i]);
        pageId = pageId.substring(0, pageId.length() - DIR_EXTENSION.length());
        Collection c = listAttachments(new WikiPage(m_engine, pageId));
        for (Iterator it = c.iterator(); it.hasNext(); ) {
            Attachment att = (Attachment) it.next();
            if (att.getLastModified().after(timestamp)) {
                list.add(att);
            }
        }
    }
    Collections.sort(list, new PageTimeComparator());
    return list;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Attachment(org.apache.wiki.attachment.Attachment) Iterator(java.util.Iterator) Collection(java.util.Collection) File(java.io.File) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 3 with PageTimeComparator

use of org.apache.wiki.util.comparators.PageTimeComparator in project jspwiki by apache.

the class RSSGenerator method generateBlogRSS.

/**
 *  Creates RSS from modifications as if this page was a blog (using the WeblogPlugin).
 *
 *  @param wikiContext The WikiContext, as usual.
 *  @param changed A list of the changed pages.
 *  @param feed A valid Feed object.  The feed will be used to create the RSS/Atom, depending
 *              on which kind of an object you want to put in it.
 *  @return A String of valid RSS or Atom.
 *  @throws ProviderException If reading of pages was not possible.
 */
@SuppressWarnings("unchecked")
protected String generateBlogRSS(WikiContext wikiContext, List changed, Feed feed) throws ProviderException {
    if (log.isDebugEnabled())
        log.debug("Generating RSS for blog, size=" + changed.size());
    String ctitle = m_engine.getVariable(wikiContext, PROP_CHANNEL_TITLE);
    if (ctitle != null)
        feed.setChannelTitle(ctitle);
    else
        feed.setChannelTitle(m_engine.getApplicationName() + ":" + wikiContext.getPage().getName());
    feed.setFeedURL(wikiContext.getViewURL(wikiContext.getPage().getName()));
    String language = m_engine.getVariable(wikiContext, PROP_CHANNEL_LANGUAGE);
    if (language != null)
        feed.setChannelLanguage(language);
    else
        feed.setChannelLanguage(m_channelLanguage);
    String channelDescription = m_engine.getVariable(wikiContext, PROP_CHANNEL_DESCRIPTION);
    if (channelDescription != null) {
        feed.setChannelDescription(channelDescription);
    }
    Collections.sort(changed, new PageTimeComparator());
    int items = 0;
    for (Iterator i = changed.iterator(); i.hasNext() && items < 15; items++) {
        WikiPage page = (WikiPage) i.next();
        Entry e = new Entry();
        e.setPage(page);
        String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(WikiContext.ATTACH, page.getName(), null, true);
        } else {
            url = m_engine.getURL(WikiContext.VIEW, page.getName(), null, true);
        }
        e.setURL(url);
        // 
        // Title
        // 
        String pageText = m_engine.getPureText(page.getName(), WikiProvider.LATEST_VERSION);
        String title = "";
        int firstLine = pageText.indexOf('\n');
        if (firstLine > 0) {
            title = pageText.substring(0, firstLine).trim();
        }
        if (title.length() == 0)
            title = page.getName();
        // Remove wiki formatting
        while (title.startsWith("!")) title = title.substring(1);
        e.setTitle(title);
        if (firstLine > 0) {
            int maxlen = pageText.length();
            if (maxlen > MAX_CHARACTERS)
                maxlen = MAX_CHARACTERS;
            if (maxlen > 0) {
                pageText = m_engine.textToHTML(wikiContext, pageText.substring(firstLine + 1, maxlen).trim());
                if (maxlen == MAX_CHARACTERS)
                    pageText += "...";
                e.setContent(pageText);
            } else {
                e.setContent(title);
            }
        } else {
            e.setContent(title);
        }
        e.setAuthor(getAuthor(page));
        feed.addEntry(e);
    }
    return feed.getString();
}
Also used : WikiPage(org.apache.wiki.WikiPage) Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Example 4 with PageTimeComparator

use of org.apache.wiki.util.comparators.PageTimeComparator 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 5 with PageTimeComparator

use of org.apache.wiki.util.comparators.PageTimeComparator in project jspwiki by apache.

the class RSSGenerator method generateWikiPageRSS.

/**
 *  Create RSS/Atom as if this page was a wikipage (in contrast to Blog mode).
 *
 * @param wikiContext The WikiContext
 * @param changed A List of changed WikiPages.
 * @param feed A Feed object to fill.
 * @return the RSS representation of the wiki context
 */
@SuppressWarnings("unchecked")
protected String generateWikiPageRSS(WikiContext wikiContext, List changed, Feed feed) {
    feed.setChannelTitle(m_engine.getApplicationName() + ": " + wikiContext.getPage().getName());
    feed.setFeedURL(wikiContext.getViewURL(wikiContext.getPage().getName()));
    String language = m_engine.getVariable(wikiContext, PROP_CHANNEL_LANGUAGE);
    if (language != null)
        feed.setChannelLanguage(language);
    else
        feed.setChannelLanguage(m_channelLanguage);
    String channelDescription = m_engine.getVariable(wikiContext, PROP_CHANNEL_DESCRIPTION);
    if (channelDescription != null) {
        feed.setChannelDescription(channelDescription);
    }
    Collections.sort(changed, new PageTimeComparator());
    int items = 0;
    for (Iterator i = changed.iterator(); i.hasNext() && items < 15; items++) {
        WikiPage page = (WikiPage) i.next();
        Entry e = new Entry();
        e.setPage(page);
        String url;
        if (page instanceof Attachment) {
            url = m_engine.getURL(WikiContext.ATTACH, page.getName(), "version=" + page.getVersion(), true);
        } else {
            url = m_engine.getURL(WikiContext.VIEW, page.getName(), "version=" + page.getVersion(), true);
        }
        // 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 : WikiPage(org.apache.wiki.WikiPage) Iterator(java.util.Iterator) Attachment(org.apache.wiki.attachment.Attachment) PageTimeComparator(org.apache.wiki.util.comparators.PageTimeComparator)

Aggregations

PageTimeComparator (org.apache.wiki.util.comparators.PageTimeComparator)5 WikiPage (org.apache.wiki.WikiPage)4 Attachment (org.apache.wiki.attachment.Attachment)4 Iterator (java.util.Iterator)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)3 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 TreeSet (java.util.TreeSet)1 WeblogPlugin (org.apache.wiki.plugin.WeblogPlugin)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1