Search in sources :

Example 6 with ReferenceManager

use of org.apache.wiki.references.ReferenceManager in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs4.

/**
 *  Checks, if ReferenceManager is informed if a third page references an attachment.
 */
@Test
public void testAttachmentRefs4() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "[TestPage2]");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    m_engine.saveText("TestPage2", "[" + NAME1 + "/TestAtt.txt]");
    // and check post-conditions
    Collection<String> c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists");
    c = refMgr.findUnreferenced();
    Assertions.assertEquals(c.size(), 1, "unreferenced count");
    Assertions.assertEquals(NAME1, c.iterator().next(), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Example 7 with ReferenceManager

use of org.apache.wiki.references.ReferenceManager in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs2.

/**
 *  Is ReferenceManager updated properly if a page references
 *  its own attachments?
 */
/*
      FIXME: This is a deep problem.  The real problem is that the reference
      manager cannot know when it encounters a link like "testatt.txt" that it
      is really a link to an attachment IF the link is created before
      the attachment.  This means that when the attachment is created,
      the link will stay in the "uncreated" list.

      There are two issues here: first of all, TranslatorReader should
      able to return the proper attachment references (which I think
      it does), and second, the ReferenceManager should be able to
      remove any links that are not referred to, nor they are created.

      However, doing this in a relatively sane timeframe can be a problem.
    */
@Test
public void testAttachmentRefs2() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "[TestAtt.txt]");
    // check a few pre-conditions
    Collection<String> c = refMgr.findReferrers("TestAtt.txt");
    Assertions.assertTrue(c != null && c.iterator().next().equals(NAME1), "normal, unexisting page");
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assertions.assertTrue(c == null || c.size() == 0, "no attachment");
    c = refMgr.findUncreated();
    Assertions.assertTrue(c != null && c.size() == 1 && c.iterator().next().equals("TestAtt.txt"), "unknown attachment");
    // now we create the attachment
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    // and check post-conditions
    c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists: ");
    c = refMgr.findReferrers("TestAtt.txt");
    Assertions.assertTrue(c == null || c.size() == 0, "no normal page");
    c = refMgr.findReferrers(NAME1 + "/TestAtt.txt");
    Assertions.assertTrue(c != null && c.iterator().next().equals(NAME1), "attachment exists now");
    c = refMgr.findUnreferenced();
    Assertions.assertTrue(c.size() == 1 && c.iterator().next().equals(NAME1), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Example 8 with ReferenceManager

use of org.apache.wiki.references.ReferenceManager in project jspwiki by apache.

the class WikiEngineTest method testAttachmentRefs.

/**
 *  Checks, if ReferenceManager is informed of new attachments.
 */
@Test
public void testAttachmentRefs() throws Exception {
    final ReferenceManager refMgr = m_engine.getManager(ReferenceManager.class);
    final AttachmentManager attMgr = m_engine.getManager(AttachmentManager.class);
    m_engine.saveText(NAME1, "fooBar");
    final Attachment att = Wiki.contents().attachment(m_engine, NAME1, "TestAtt.txt");
    att.setAuthor("FirstPost");
    attMgr.storeAttachment(att, m_engine.makeAttachmentFile());
    // and check post-conditions
    Collection<String> c = refMgr.findUncreated();
    Assertions.assertTrue(c == null || c.size() == 0, "attachment exists: " + c);
    c = refMgr.findUnreferenced();
    Assertions.assertEquals(2, c.size(), "unreferenced count");
    final Iterator<String> i = c.iterator();
    final String first = i.next();
    final String second = i.next();
    Assertions.assertTrue((first.equals(NAME1) && second.equals(NAME1 + "/TestAtt.txt")) || (first.equals(NAME1 + "/TestAtt.txt") && second.equals(NAME1)), "unreferenced");
}
Also used : Attachment(org.apache.wiki.api.core.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) Test(org.junit.jupiter.api.Test)

Example 9 with ReferenceManager

use of org.apache.wiki.references.ReferenceManager in project jspwiki by apache.

the class ReferringPagesPlugin method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final ReferenceManager refmgr = context.getEngine().getManager(ReferenceManager.class);
    String pageName = params.get(PARAM_PAGE);
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    StringBuilder result = new StringBuilder(256);
    if (pageName == null) {
        pageName = context.getPage().getName();
    }
    final Page page = context.getEngine().getManager(PageManager.class).getPage(pageName);
    if (page != null) {
        Collection<String> links = refmgr.findReferrers(page.getName());
        String wikitext;
        super.initialize(context, params);
        final int items = TextUtil.parseIntParameter(params.get(PARAM_MAX), ALL_ITEMS);
        String extras = TextUtil.replaceEntities(params.get(PARAM_EXTRAS));
        if (extras == null) {
            extras = rb.getString("referringpagesplugin.more");
        }
        log.debug("Fetching referring pages for {} with a max of {}", page.getName(), items);
        if (links != null && links.size() > 0) {
            links = filterAndSortCollection(links);
            wikitext = wikitizeCollection(links, m_separator, items);
            result.append(applyColumnsStyle(makeHTML(context, wikitext)));
            if (items < links.size() && items > 0) {
                final Object[] args = { "" + (links.size() - items) };
                extras = MessageFormat.format(extras, args);
                result.append("<br />").append("<a class='morelink' href='").append(context.getURL(ContextEnum.PAGE_INFO.getRequestContext(), page.getName())).append("' ").append(">").append(extras).append("</a><br />");
            }
        }
        // If nothing was left after filtering or during search
        if (links == null || links.size() == 0) {
            wikitext = rb.getString("referringpagesplugin.nobody");
            result.append(makeHTML(context, wikitext));
        } else if (m_show.equals(PARAM_SHOW_VALUE_COUNT)) {
            result = new StringBuilder();
            result.append(links.size());
            if (m_lastModified) {
                result.append(" (").append(m_dateFormat.format(m_dateLastModified)).append(")");
            }
        }
        return result.toString();
    }
    return "";
}
Also used : PageManager(org.apache.wiki.pages.PageManager) ResourceBundle(java.util.ResourceBundle) Page(org.apache.wiki.api.core.Page) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 10 with ReferenceManager

use of org.apache.wiki.references.ReferenceManager in project jspwiki by apache.

the class UnusedPagesPlugin method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final ReferenceManager refmgr = context.getEngine().getManager(ReferenceManager.class);
    Collection<String> links = refmgr.findUnreferenced();
    // filter out attachments if "excludeattachments" was requested:
    final String prop = params.get(PARAM_EXCLUDEATTS);
    if (TextUtil.isPositive(prop)) {
        // remove links to attachments (recognizable by a slash in it)
        links.removeIf(link -> link.contains("/"));
    }
    super.initialize(context, params);
    links = filterAndSortCollection(links);
    String wikitext;
    if (m_show.equals(PARAM_SHOW_VALUE_COUNT)) {
        wikitext = "" + links.size();
        if (m_lastModified && links.size() != 0) {
            wikitext = links.size() + " (" + m_dateFormat.format(m_dateLastModified) + ")";
        }
        return makeHTML(context, wikitext);
    } else {
        wikitext = wikitizeCollection(links, m_separator, ALL_ITEMS);
        return applyColumnsStyle(makeHTML(context, wikitext));
    }
}
Also used : ReferenceManager(org.apache.wiki.references.ReferenceManager)

Aggregations

ReferenceManager (org.apache.wiki.references.ReferenceManager)11 Attachment (org.apache.wiki.api.core.Attachment)4 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)4 Test (org.junit.jupiter.api.Test)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 ResourceBundle (java.util.ResourceBundle)2 Page (org.apache.wiki.api.core.Page)2 ProviderException (org.apache.wiki.api.exceptions.ProviderException)2 WikiException (org.apache.wiki.api.exceptions.WikiException)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 TreeMap (java.util.TreeMap)1 PluginException (org.apache.wiki.api.exceptions.PluginException)1 AclManager (org.apache.wiki.auth.acl.AclManager)1 FilterManager (org.apache.wiki.filters.FilterManager)1 PageManager (org.apache.wiki.pages.PageManager)1 SearchManager (org.apache.wiki.search.SearchManager)1 URLConstructor (org.apache.wiki.url.URLConstructor)1