Search in sources :

Example 1 with SearchResult

use of org.apache.wiki.api.search.SearchResult in project jspwiki by apache.

the class SearchAdapterTest method testNewSearchResultFrom.

@Test
public void testNewSearchResultFrom() {
    final org.apache.wiki.search.SearchResult old = new org.apache.wiki.search.SearchResult() {

        @Override
        public WikiPage getPage() {
            return null;
        }

        @Override
        public int getScore() {
            return 0;
        }

        @Override
        public String[] getContexts() {
            return new String[0];
        }
    };
    final SearchResult sr = SearchAdapter.newSearchResultFrom(old);
    Assertions.assertEquals(old.getPage(), sr.getPage());
    Assertions.assertEquals(old.getScore(), sr.getScore());
    Assertions.assertArrayEquals(old.getContexts(), sr.getContexts());
}
Also used : SearchResult(org.apache.wiki.api.search.SearchResult) Test(org.junit.jupiter.api.Test)

Example 2 with SearchResult

use of org.apache.wiki.api.search.SearchResult in project jspwiki by apache.

the class BasicSearchProvider method findPages.

private Collection<SearchResult> findPages(final QueryItem[] query, final Context wikiContext) {
    final TreeSet<SearchResult> res = new TreeSet<>(new SearchResultComparator());
    final SearchMatcher matcher = new SearchMatcher(m_engine, query);
    final Collection<Page> allPages;
    try {
        allPages = m_engine.getManager(PageManager.class).getAllPages();
    } catch (final ProviderException pe) {
        log.error("Unable to retrieve page list", pe);
        return null;
    }
    final AuthorizationManager mgr = m_engine.getManager(AuthorizationManager.class);
    for (final Page page : allPages) {
        try {
            if (page != null) {
                final PagePermission pp = new PagePermission(page, PagePermission.VIEW_ACTION);
                if (wikiContext == null || mgr.checkPermission(wikiContext.getWikiSession(), pp)) {
                    final String pageName = page.getName();
                    final String pageContent = m_engine.getManager(PageManager.class).getPageText(pageName, PageProvider.LATEST_VERSION) + attachmentNames(page);
                    final SearchResult comparison = matcher.matchPageContent(pageName, pageContent);
                    if (comparison != null) {
                        res.add(comparison);
                    }
                }
            }
        } catch (final ProviderException pe) {
            log.error("Unable to retrieve page from cache", pe);
        } catch (final IOException ioe) {
            log.error("Failed to search page", ioe);
        }
    }
    return res;
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) TreeSet(java.util.TreeSet) SearchResult(org.apache.wiki.api.search.SearchResult) Page(org.apache.wiki.api.core.Page) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) IOException(java.io.IOException) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 3 with SearchResult

use of org.apache.wiki.api.search.SearchResult in project jspwiki by apache.

the class SearchResultIteratorTag method nextResult.

private int nextResult() {
    if (m_iterator != null && m_iterator.hasNext() && m_count++ < m_maxItems) {
        final SearchResult r = (SearchResult) m_iterator.next();
        // Create a wiki context for the result
        final Engine engine = m_wikiContext.getEngine();
        final HttpServletRequest request = m_wikiContext.getHttpRequest();
        final Command command = PageCommand.VIEW.targetedCommand(r.getPage());
        final Context context = Wiki.context().create(engine, request, command);
        // Stash it in the page context
        pageContext.setAttribute(Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute(getId(), r);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) PageCommand(org.apache.wiki.ui.PageCommand) Command(org.apache.wiki.api.core.Command) SearchResult(org.apache.wiki.api.search.SearchResult) Engine(org.apache.wiki.api.core.Engine)

Example 4 with SearchResult

use of org.apache.wiki.api.search.SearchResult in project jspwiki by apache.

the class SearchManagerTest method testSimpleSearch4.

@Test
public void testSimpleSearch4() throws Exception {
    final String txt = "It was the dawn of the third age of mankind, ten years after the Earth-Minbari War.";
    final MockHttpServletRequest request = m_engine.newHttpRequest();
    request.getParameterMap().put("page", new String[] { "TestPage" });
    final Context ctx = Wiki.context().create(m_engine, request, ContextEnum.PAGE_EDIT.getRequestContext());
    m_engine.getManager(PageManager.class).saveText(ctx, txt);
    Collection<SearchResult> res = new ArrayList<>();
    Awaitility.await("testSimpleSearch4").until(findsResultsFor(res, "mankind"));
    Assertions.assertEquals(1, res.size(), "result not found");
    m_engine.getManager(PageManager.class).saveText(ctx, "[{ALLOW view Authenticated}] It was the dawn of the third age of mankind... page is blocked");
    res = m_mgr.findPages("mankind", ctx);
    Assertions.assertNotNull(res, "null result");
    Assertions.assertEquals(0, res.size(), "result found, should be blocked");
    m_engine.deleteTestPage("TestPage");
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) MockHttpServletRequest(net.sourceforge.stripes.mock.MockHttpServletRequest) ArrayList(java.util.ArrayList) SearchResult(org.apache.wiki.api.search.SearchResult) Test(org.junit.jupiter.api.Test)

Example 5 with SearchResult

use of org.apache.wiki.api.search.SearchResult in project jspwiki by apache.

the class SaveWikiPageTaskTest method testSaveWikiPageTask.

@Test
public void testSaveWikiPageTask() throws Exception {
    final TestEngine engine = TestEngine.build(with("jspwiki.lucene.initialdelay", "0"), with("jspwiki.lucene.indexdelay", "1"));
    final String pageName = "TestSaveWikiPageTestPage";
    final Page page = Wiki.contents().page(engine, pageName);
    engine.saveText(pageName, "initial text on first revision");
    final Context context = Wiki.context().create(engine, engine.newHttpRequest(), page);
    final SaveWikiPageTask task = new SaveWikiPageTask();
    task.setWorkflow(1, new HashMap<>());
    task.getWorkflowContext().put(WorkflowManager.WF_WP_SAVE_FACT_PROPOSED_TEXT, "correct horse battery staple");
    final Collection<SearchResult> res = new ArrayList<>();
    Assertions.assertEquals(Outcome.STEP_COMPLETE, task.execute(context));
    Awaitility.await("ensure page gets indexed").until(findsResultsFor(context, res, "horse"));
    Assertions.assertEquals(1, res.size(), "no pages found");
    Assertions.assertEquals(pageName, res.iterator().next().getPage().getName(), "page");
}
Also used : Context(org.apache.wiki.api.core.Context) ArrayList(java.util.ArrayList) Page(org.apache.wiki.api.core.Page) SearchResult(org.apache.wiki.api.search.SearchResult) TestEngine(org.apache.wiki.TestEngine) Test(org.junit.jupiter.api.Test)

Aggregations

SearchResult (org.apache.wiki.api.search.SearchResult)12 Context (org.apache.wiki.api.core.Context)5 Page (org.apache.wiki.api.core.Page)5 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 PageManager (org.apache.wiki.pages.PageManager)4 IOException (java.io.IOException)3 TreeSet (java.util.TreeSet)3 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)3 PagePermission (org.apache.wiki.auth.permissions.PagePermission)3 File (java.io.File)2 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)2 TestEngine (org.apache.wiki.TestEngine)2 Engine (org.apache.wiki.api.core.Engine)2 ProviderException (org.apache.wiki.api.exceptions.ProviderException)2 SearchResultComparator (org.apache.wiki.search.SearchResultComparator)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PageContext (javax.servlet.jsp.PageContext)1