001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.search;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.commons.lang3.time.StopWatch;
023import org.apache.logging.log4j.LogManager;
024import org.apache.logging.log4j.Logger;
025import org.apache.wiki.ajax.AjaxUtil;
026import org.apache.wiki.ajax.WikiAjaxDispatcherServlet;
027import org.apache.wiki.ajax.WikiAjaxServlet;
028import org.apache.wiki.api.core.Context;
029import org.apache.wiki.api.core.ContextEnum;
030import org.apache.wiki.api.core.Engine;
031import org.apache.wiki.api.core.Page;
032import org.apache.wiki.api.exceptions.FilterException;
033import org.apache.wiki.api.exceptions.NoRequiredPropertyException;
034import org.apache.wiki.api.filters.BasePageFilter;
035import org.apache.wiki.api.search.SearchResult;
036import org.apache.wiki.api.spi.Wiki;
037import org.apache.wiki.auth.AuthorizationManager;
038import org.apache.wiki.auth.permissions.PermissionFactory;
039import org.apache.wiki.event.WikiEvent;
040import org.apache.wiki.event.WikiEventManager;
041import org.apache.wiki.event.WikiPageEvent;
042import org.apache.wiki.pages.PageManager;
043import org.apache.wiki.parser.MarkupParser;
044import org.apache.wiki.references.ReferenceManager;
045import org.apache.wiki.util.ClassUtil;
046import org.apache.wiki.util.TextUtil;
047
048import jakarta.servlet.http.HttpServletRequest;
049import jakarta.servlet.http.HttpServletResponse;
050import java.io.IOException;
051import java.util.ArrayList;
052import java.util.Collection;
053import java.util.Collections;
054import java.util.Comparator;
055import java.util.HashMap;
056import java.util.Iterator;
057import java.util.List;
058import java.util.Locale;
059import java.util.Map;
060import java.util.Properties;
061import java.util.Set;
062import org.apache.wiki.api.plugin.Plugin;
063import org.apache.wiki.plugin.PluginManager;
064import static org.apache.wiki.search.SearchManager.PLUGIN_SEARCH;
065
066
067/**
068 *  Manages searching the Wiki.
069 *
070 *  @since 2.2.21.
071 */
072public class DefaultSearchManager extends BasePageFilter implements SearchManager {
073
074    private static final Logger LOG = LogManager.getLogger( DefaultSearchManager.class );
075
076    private SearchProvider m_searchProvider;
077
078    /**
079     *  Creates a new SearchManager.
080     *
081     *  @param engine The Engine that owns this SearchManager.
082     *  @param properties The list of Properties.
083     *  @throws FilterException If it cannot be instantiated.
084     */
085    public DefaultSearchManager( final Engine engine, final Properties properties ) throws FilterException {
086        initialize( engine, properties );
087        WikiEventManager.addWikiEventListener( m_engine.getManager( PageManager.class ), this );
088
089        // TODO: Replace with custom annotations. See JSPWIKI-566
090        WikiAjaxDispatcherServlet.registerServlet( JSON_SEARCH, new JSONSearch() );
091        WikiAjaxDispatcherServlet.registerServlet( PLUGIN_SEARCH, new PluginSearch() );
092    }
093
094    /**
095     *  Provides a JSON AJAX API to the JSPWiki Plugin discovery mechanism,
096     *  primarily used for [{}] based auto complete
097     */
098    public class PluginSearch implements WikiAjaxServlet {
099
100        public static final String AJAX_ACTION_PLUGINS = "plugins";
101        public static final int DEFAULT_MAX_RESULTS = 20;
102        public int maxResults = DEFAULT_MAX_RESULTS;
103
104        /** {@inheritDoc} */
105        @Override
106        public String getServletMapping() {
107            return PLUGIN_SEARCH;
108        }
109        public static class SimpleSnipData {
110            public String displayName;
111            public String snip;
112        }
113
114        /** {@inheritDoc} */
115        @Override
116        public void service( final HttpServletRequest req,
117                             final HttpServletResponse resp,
118                             final String actionName,
119                             final List< String > params ) throws IOException {
120            String result = "[]";
121            resp.setContentType("application/json");
122            if( actionName != null && StringUtils.isNotBlank( actionName ) ) {
123                if( actionName.equals( AJAX_ACTION_PLUGINS ) ) {
124                    LOG.debug( "Calling getPlugins() START" );
125                    PluginManager mgr = m_engine.getManager(PluginManager.class);
126                    final List< Plugin > plugins = mgr.getDiscoveredPlugins();
127                    List< SimpleSnipData > callResults = new ArrayList<>();
128                   
129                    Locale locale = req.getLocale();
130                    if (locale == null) {
131                        locale = Locale.getDefault();
132                    }
133                    final Locale sorter = locale;
134                    Collections.sort(plugins, new Comparator<Plugin>() {
135                        @Override
136                        public int compare(Plugin o1, Plugin o2) {
137                            return o1.getDisplayName(sorter).compareTo(o2.getDisplayName(sorter));
138                        }
139                    });
140                    for (Plugin p : plugins) {
141                        try {
142                            SimpleSnipData data = new SimpleSnipData();
143                            data.snip = p.getSnipExample();
144                            data.displayName = p.getDisplayName(locale);
145                            callResults.add(data);
146                        } catch (Throwable t) {
147                            LOG.warn("failed to get plugin informatiom from " + p.getClass().getCanonicalName(), t);
148                        }
149                    }
150                    LOG.debug("Calling getSuggestions() DONE. " + callResults.size() );
151                    result = AjaxUtil.toJson( callResults );
152                } 
153            }
154            LOG.debug( "result=" + result );
155            resp.getWriter().write( result );
156        }
157    }
158    
159    /**
160     *  Provides a JSON AJAX API to the JSPWiki Search Engine.
161     */
162    public class JSONSearch implements WikiAjaxServlet {
163
164        public static final String AJAX_ACTION_SUGGESTIONS = "suggestions";
165        public static final String AJAX_ACTION_PAGES = "pages";
166        public static final int DEFAULT_MAX_RESULTS = 20;
167        public int maxResults = DEFAULT_MAX_RESULTS;
168
169        /** {@inheritDoc} */
170        @Override
171        public String getServletMapping() {
172            return JSON_SEARCH;
173        }
174
175        /** {@inheritDoc} */
176        @Override
177        public void service( final HttpServletRequest req,
178                             final HttpServletResponse resp,
179                             final String actionName,
180                             final List< String > params ) throws IOException {
181            String result = "[]";
182            resp.setContentType("application/json");
183            if( StringUtils.isNotBlank( actionName ) ) {
184                if( params.isEmpty() ) {
185                    return;
186                }
187                final String itemId = params.get( 0 );
188                LOG.debug( "itemId=" + itemId );
189                if( params.size() > 1 ) {
190                    final String maxResultsParam = params.get( 1 );
191                    LOG.debug( "maxResultsParam=" + maxResultsParam );
192                    if( StringUtils.isNotBlank( maxResultsParam ) && StringUtils.isNumeric( maxResultsParam ) ) {
193                        maxResults = Integer.parseInt( maxResultsParam );
194                    }
195                }
196
197                if( actionName.equals( AJAX_ACTION_SUGGESTIONS ) ) {
198                    LOG.debug( "Calling getSuggestions() START" );
199                    final Context wikiContext = Wiki.context().create( m_engine, req, ContextEnum.PAGE_VIEW.getRequestContext() );
200                    final List< String > callResults = getSuggestions( itemId, maxResults, wikiContext );
201                    LOG.debug( "Calling getSuggestions() DONE. " + callResults.size() );
202                    result = AjaxUtil.toJson( callResults );
203                } else if( actionName.equals( AJAX_ACTION_PAGES ) ) {
204                    LOG.debug("Calling findPages() START");
205                    final Context wikiContext = Wiki.context().create( m_engine, req, ContextEnum.PAGE_VIEW.getRequestContext() );
206                    final List< Map< String, Object > > callResults = findPages( itemId, maxResults, wikiContext );
207                    LOG.debug( "Calling findPages() DONE. " + callResults.size() );
208                    result = AjaxUtil.toJson( callResults );
209                }
210            }
211            LOG.debug( "result=" + result );
212            resp.getWriter().write( result );
213        }
214
215        /** The suggestions are shown to everybody, so they hold only the names of the pages that the viewer may view. */
216        private boolean mayView( final Context wikiContext, final String pageName ) {
217            final Page page = m_engine.getManager( PageManager.class ).getPage( pageName );
218            return page == null || m_engine.getManager( AuthorizationManager.class )
219                                           .checkPermission( wikiContext.getWikiSession(), PermissionFactory.getPagePermission( page, "view" ) );
220        }
221
222        /**
223         *  Provides a list of suggestions to use for a page name. Currently, the algorithm just looks into the value parameter,
224         *  and returns all page names from that.
225         *
226         *  @param wikiName the page name
227         *  @param maxLength maximum number of suggestions
228         *  @param wikiContext the context of the viewer, whose view permission decides which page names are suggested
229         *  @return the suggestions
230         */
231        public List< String > getSuggestions( String wikiName, final int maxLength, final Context wikiContext ) {
232            final StopWatch sw = new StopWatch();
233            sw.start();
234            final List< String > list = new ArrayList<>( maxLength );
235            if( !wikiName.isEmpty() ) {
236                // split pagename and attachment filename
237                String filename = "";
238                final int pos = wikiName.indexOf("/");
239                if( pos >= 0 ) {
240                    filename = wikiName.substring( pos ).toLowerCase();
241                    wikiName = wikiName.substring( 0, pos );
242                }
243
244                final String cleanWikiName = MarkupParser.cleanLink(wikiName).toLowerCase() + filename;
245                final String oldStyleName = MarkupParser.wikifyLink(wikiName).toLowerCase() + filename;
246                final Set< String > allPages = m_engine.getManager( ReferenceManager.class ).findCreated();
247               
248                int counter = 0;
249                for( final Iterator< String > i = allPages.iterator(); i.hasNext() && counter < maxLength; ) {
250                    final String p = i.next();
251                    final String pp = p.toLowerCase();
252                    if( ( pp.startsWith( cleanWikiName) || pp.startsWith( oldStyleName ) ) && mayView( wikiContext, p ) ) {
253                        list.add( p );
254                        counter++;
255                    }
256                }
257            }
258
259            sw.stop();
260            LOG.debug( "Suggestion request for {} done in {}", wikiName, sw );
261            return list;
262        }
263
264        /**
265         *  Performs a full search of pages.
266         *
267         *  @param searchString The query string
268         *  @param maxLength How many hits to return
269         *  @return the pages found
270         */
271        public List< Map< String, Object > > findPages( final String searchString, final int maxLength, final Context wikiContext ) {
272            final StopWatch sw = new StopWatch();
273            sw.start();
274
275            final List< Map< String, Object > > list = new ArrayList<>( maxLength );
276            if( !searchString.isEmpty() ) {
277                try {
278                    final Collection< SearchResult > c;
279                    if( m_searchProvider instanceof LuceneSearchProvider ) {
280                        c = ( ( LuceneSearchProvider )m_searchProvider ).findPages( searchString, 0, wikiContext );
281                    } else {
282                        c = m_searchProvider.findPages( searchString, wikiContext );
283                    }
284
285                    int count = 0;
286                    for( final Iterator< SearchResult > i = c.iterator(); i.hasNext() && count < maxLength; count++ ) {
287                        final SearchResult sr = i.next();
288                        final HashMap< String, Object > hm = new HashMap<>();
289                        hm.put( "page", sr.getPage().getName() );
290                        hm.put( "score", sr.getScore() );
291                        list.add( hm );
292                    }
293                } catch( final Exception e ) {
294                    LOG.info( "AJAX search failed; ", e );
295                }
296            }
297
298            sw.stop();
299            LOG.debug( "AJAX search complete in {}", sw );
300            return list;
301        }
302    }
303
304
305    /** {@inheritDoc} */
306    @Override
307    public void initialize( final Engine engine, final Properties properties ) throws FilterException {
308        m_engine = engine;
309        loadSearchProvider(properties);
310
311        try {
312            m_searchProvider.initialize( engine, properties );
313        } catch( final NoRequiredPropertyException | IOException e ) {
314            LOG.error( e.getMessage(), e );
315        }
316    }
317
318    private void loadSearchProvider( final Properties properties ) {
319        // See if we're using Lucene, and if so, ensure that its index directory is up-to-date.
320        final String providerClassName = TextUtil.getStringProperty( properties, PROP_SEARCHPROVIDER, DEFAULT_SEARCHPROVIDER );
321
322        try {
323            m_searchProvider = ClassUtil.buildInstance( "org.apache.wiki.search", providerClassName );
324        } catch( final ReflectiveOperationException e ) {
325            LOG.warn( "Failed loading SearchProvider, will use BasicSearchProvider.", e );
326        }
327
328        if( null == m_searchProvider ) {
329            m_searchProvider = new BasicSearchProvider();
330        }
331        LOG.debug( "Loaded search provider {}", m_searchProvider );
332    }
333
334    /** {@inheritDoc} */
335    @Override
336    public SearchProvider getSearchEngine()
337    {
338        return m_searchProvider;
339    }
340
341    /** {@inheritDoc} */
342    @Override
343    public void actionPerformed( final WikiEvent event ) {
344        if( event instanceof WikiPageEvent ) {
345            final String pageName = ( ( WikiPageEvent ) event ).getPageName();
346            if( event.getType() == WikiPageEvent.PAGE_DELETE_REQUEST ) {
347                final Page p = m_engine.getManager( PageManager.class ).getPage( pageName );
348                if( p != null ) {
349                    pageRemoved( p );
350                }
351            }
352            if( event.getType() == WikiPageEvent.PAGE_REINDEX ) {
353                final Page p = m_engine.getManager( PageManager.class ).getPage( pageName );
354                if( p != null ) {
355                    reindexPage( p );
356                }
357            }
358        }
359    }
360
361}