Search in sources :

Example 1 with ReferenceManager

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

the class ReferringUndefinedPagesPlugin method execute.

@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    final ReferenceManager referenceManager = context.getEngine().getManager(ReferenceManager.class);
    final int items = TextUtil.parseIntParameter(params.get(PARAM_MAX), ALL_ITEMS);
    String extras = params.get(PARAM_EXTRAS);
    if (extras == null) {
        extras = rb.getString("referringundefinedpagesplugin.more");
    }
    final Collection<String> uncreatedPages = referenceManager.findUncreated();
    super.initialize(context, params);
    Collection<String> result = null;
    final TreeMap<String, String> sortedMap = new TreeMap<>();
    if (uncreatedPages != null) {
        for (final String uncreatedPageName : uncreatedPages) {
            final Collection<String> referrers = referenceManager.findReferrers(uncreatedPageName);
            if (referrers != null) {
                for (final String referringPage : referrers) {
                    sortedMap.put(referringPage, "");
                }
            }
        }
        result = sortedMap.keySet();
    }
    result = super.filterAndSortCollection(result);
    final String wikitext = wikitizeCollection(result, m_separator, items);
    final StringBuilder resultHTML = new StringBuilder();
    resultHTML.append(applyColumnsStyle(makeHTML(context, wikitext)));
    // add the more.... text
    if (items < result.size() && items > 0) {
        final Object[] args = { "" + (result.size() - items) };
        extras = MessageFormat.format(extras, args);
        resultHTML.append("<br/>").append(extras).append("<br/>");
    }
    return resultHTML.toString();
}
Also used : ResourceBundle(java.util.ResourceBundle) TreeMap(java.util.TreeMap) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 2 with ReferenceManager

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

the class UndefinedPagesPlugin 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);
    super.initialize(context, params);
    Collection<String> links = refmgr.findUncreated();
    links = filterAndSortCollection(links);
    if (m_lastModified) {
        throw new PluginException("parameter " + PARAM_LASTMODIFIED + " is not valid for the UndefinedPagesPlugin");
    }
    final String wikitext;
    if (m_show.equals(PARAM_SHOW_VALUE_COUNT)) {
        wikitext = "" + links.size();
        return makeHTML(context, wikitext);
    } else {
        wikitext = wikitizeCollection(links, m_separator, ALL_ITEMS);
        return applyColumnsStyle(makeHTML(context, wikitext));
    }
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 3 with ReferenceManager

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

the class WikiEngineTest method testAttachmentRefs3.

/**
 *  Checks, if ReferenceManager is informed if a link to an attachment is added.
 */
@Test
public void testAttachmentRefs3() 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());
    m_engine.saveText(NAME1, " [" + 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 4 with ReferenceManager

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

the class WikiEngine method initReferenceManager.

/**
 *  Initializes the reference manager. Scans all existing WikiPages for
 *  internal links and adds them to the ReferenceManager object.
 *
 *  @throws WikiException If the reference manager initialization fails.
 */
public void initReferenceManager() throws WikiException {
    try {
        // Build a new manager with default key lists.
        if (getManager(ReferenceManager.class) == null) {
            final ArrayList<Page> pages = new ArrayList<>();
            pages.addAll(getManager(PageManager.class).getAllPages());
            pages.addAll(getManager(AttachmentManager.class).getAllAttachments());
            initComponent(ReferenceManager.class, this);
            getManager(ReferenceManager.class).initialize(pages);
        }
    } catch (final ProviderException e) {
        LOG.fatal("PageProvider is unable to list pages: ", e);
    } catch (final Exception e) {
        throw new WikiException("Could not instantiate ReferenceManager: " + e.getMessage(), e);
    }
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) ArrayList(java.util.ArrayList) Page(org.apache.wiki.api.core.Page) WikiException(org.apache.wiki.api.exceptions.WikiException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 5 with ReferenceManager

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

the class WikiEngine method initialize.

/**
 *  Does all the real initialization.
 */
private void initialize(final Properties props) throws WikiException {
    m_startTime = new Date();
    m_properties = props;
    LOG.info("*******************************************");
    LOG.info("{} {} starting. Whee!", Release.APPNAME, Release.getVersionString());
    // begin initialization
    fireEvent(WikiEngineEvent.INITIALIZING);
    LOG.debug("Java version: {}", System.getProperty("java.runtime.version"));
    LOG.debug("Java vendor: {}", System.getProperty("java.vm.vendor"));
    LOG.debug("OS: {} {} {}", System.getProperty("os.name"), System.getProperty("os.version"), System.getProperty("os.arch"));
    LOG.debug("Default server locale: {}", Locale.getDefault());
    LOG.debug("Default server timezone: {}", TimeZone.getDefault().getDisplayName(true, TimeZone.LONG));
    if (m_servletContext != null) {
        LOG.info("Servlet container: {}", m_servletContext.getServerInfo());
        if (m_servletContext.getMajorVersion() < 3 || (m_servletContext.getMajorVersion() == 3 && m_servletContext.getMinorVersion() < 1)) {
            throw new InternalWikiException("JSPWiki requires a container which supports at least version 3.1 of Servlet specification");
        }
    }
    LOG.debug("Configuring WikiEngine...");
    // Create and find the default working directory.
    m_workDir = TextUtil.getStringProperty(props, PROP_WORKDIR, null);
    if (m_workDir == null) {
        m_workDir = System.getProperty("java.io.tmpdir", ".");
        m_workDir += File.separator + Release.APPNAME + "-" + m_appid;
    }
    try {
        final File f = new File(m_workDir);
        f.mkdirs();
        // 
        if (!f.exists()) {
            throw new WikiException("Work directory does not exist: " + m_workDir);
        }
        if (!f.canRead()) {
            throw new WikiException("No permission to read work directory: " + m_workDir);
        }
        if (!f.canWrite()) {
            throw new WikiException("No permission to write to work directory: " + m_workDir);
        }
        if (!f.isDirectory()) {
            throw new WikiException("jspwiki.workDir does not point to a directory: " + m_workDir);
        }
    } catch (final SecurityException e) {
        LOG.fatal("Unable to find or create the working directory: {}", m_workDir, e);
        throw new IllegalArgumentException("Unable to find or create the working dir: " + m_workDir, e);
    }
    LOG.info("JSPWiki working directory is '{}'", m_workDir);
    m_saveUserInfo = TextUtil.getBooleanProperty(props, PROP_STOREUSERNAME, m_saveUserInfo);
    m_useUTF8 = StandardCharsets.UTF_8.name().equals(TextUtil.getStringProperty(props, PROP_ENCODING, StandardCharsets.ISO_8859_1.name()));
    m_templateDir = TextUtil.getStringProperty(props, PROP_TEMPLATEDIR, "default");
    enforceValidTemplateDirectory();
    m_frontPage = TextUtil.getStringProperty(props, PROP_FRONTPAGE, "Main");
    // 
    try {
        final String aclClassName = m_properties.getProperty(PROP_ACL_MANAGER_IMPL, ClassUtil.getMappedClass(AclManager.class.getName()).getName());
        final String urlConstructorClassName = TextUtil.getStringProperty(props, PROP_URLCONSTRUCTOR, "DefaultURLConstructor");
        final Class<URLConstructor> urlclass = ClassUtil.findClass("org.apache.wiki.url", urlConstructorClassName);
        initComponent(CommandResolver.class, this, props);
        initComponent(urlclass.getName(), URLConstructor.class);
        initComponent(CachingManager.class, this, props);
        initComponent(PageManager.class, this, props);
        initComponent(PluginManager.class, this, props);
        initComponent(DifferenceManager.class, this, props);
        initComponent(AttachmentManager.class, this, props);
        initComponent(VariableManager.class, props);
        initComponent(SearchManager.class, this, props);
        initComponent(AuthenticationManager.class);
        initComponent(AuthorizationManager.class);
        initComponent(UserManager.class);
        initComponent(GroupManager.class);
        initComponent(EditorManager.class, this);
        initComponent(ProgressManager.class, this);
        initComponent(aclClassName, AclManager.class);
        initComponent(WorkflowManager.class);
        initComponent(TasksManager.class);
        initComponent(InternationalizationManager.class, this);
        initComponent(TemplateManager.class, this, props);
        initComponent(FilterManager.class, this, props);
        initComponent(AdminBeanManager.class, this);
        initComponent(PageRenamer.class, this, props);
        // RenderingManager depends on FilterManager events.
        initComponent(RenderingManager.class);
        // ReferenceManager has the side effect of loading all pages. Therefore, after this point, all page attributes are available.
        // initReferenceManager is indirectly using m_filterManager, so it has to be called after it was initialized.
        initReferenceManager();
        // Hook the different manager routines into the system.
        getManager(FilterManager.class).addPageFilter(getManager(ReferenceManager.class), -1001);
        getManager(FilterManager.class).addPageFilter(getManager(SearchManager.class), -1002);
    } catch (final RuntimeException e) {
        // RuntimeExceptions may occur here, even if they shouldn't.
        LOG.fatal("Failed to start managers.", e);
        throw new WikiException("Failed to start managers: " + e.getMessage(), e);
    } catch (final ClassNotFoundException e) {
        LOG.fatal("JSPWiki could not start, URLConstructor was not found: {}", e.getMessage(), e);
        throw new WikiException(e.getMessage(), e);
    } catch (final InstantiationException e) {
        LOG.fatal("JSPWiki could not start, URLConstructor could not be instantiated: {}", e.getMessage(), e);
        throw new WikiException(e.getMessage(), e);
    } catch (final IllegalAccessException e) {
        LOG.fatal("JSPWiki could not start, URLConstructor cannot be accessed: {}", e.getMessage(), e);
        throw new WikiException(e.getMessage(), e);
    } catch (final Exception e) {
        // Final catch-all for everything
        LOG.fatal("JSPWiki could not start, due to an unknown exception when starting.", e);
        throw new WikiException("Failed to start. Caused by: " + e.getMessage() + "; please check log files for better information.", e);
    }
    // Initialize the good-to-have-but-not-fatal modules.
    try {
        if (TextUtil.getBooleanProperty(props, RSSGenerator.PROP_GENERATE_RSS, false)) {
            initComponent(RSSGenerator.class, this, props);
        }
    } catch (final Exception e) {
        LOG.error("Unable to start RSS generator - JSPWiki will still work, but there will be no RSS feed.", e);
    }
    final Map<String, String> extraComponents = ClassUtil.getExtraClassMappings();
    initExtraComponents(extraComponents);
    // initialization complete
    fireEvent(WikiEngineEvent.INITIALIZED);
    LOG.info("WikiEngine configured.");
    m_isConfigured = true;
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) SearchManager(org.apache.wiki.search.SearchManager) Date(java.util.Date) WikiException(org.apache.wiki.api.exceptions.WikiException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) FilterManager(org.apache.wiki.filters.FilterManager) ReferenceManager(org.apache.wiki.references.ReferenceManager) File(java.io.File) AclManager(org.apache.wiki.auth.acl.AclManager) URLConstructor(org.apache.wiki.url.URLConstructor)

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