/******************************************************************************
 *
 * Copyright (C) 1997-2015 by Dimitri van Heesch.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation under the terms of the GNU General Public License is hereby 
 * granted. No representations are made about the suitability of this software 
 * for any purpose. It is provided "as is" without express or implied warranty.
 * See the GNU General Public License for more details.
 *
 * Documents produced by analyze are derivative works derived from the
 * input used in their production; they are not affected by this license.
 *
 */
/*! \page extsearch External Indexing and Searching

[TOC]

\section extsearch_intro Introduction

With release 1.8.3, analyze provides the ability to search through HTML using
an external indexing tool and search engine.
This has several advantages:

- For large projects it can have significant performance advantages over
  analyze's built-in search engine, as analyze uses a rather simple indexing
  algorithm.
- It allows combining the search data of multiple projects into one index,
  allowing a global search across multiple analyze projects.
- It allows adding additional data to the search index, i.e. other web pages
  not produced by analyze.
- The search engine needs to run on a web server, but clients can still browse
  the web pages locally.

To avoid that everyone has to start writing their own indexer and search 
engine, analyze provides an example tool for each action: `analzindexer` 
for indexing the data and `analzsearch.cgi` for searching through the index.

The data flow is shown in the following diagram:

\image html extsearch_flow.png "External Search Data Flow"
\image latex extsearch_flow.eps "External Search Data Flow" height=10cm

- `analyze` produces the raw search data
- `analzindexer` indexes the data into a search database `analzsearch.db`
- when a user performs a search from a analyze generated HTML page, 
  the CGI binary `analzsearch.cgi` will be invoked.
- the `analzsearch.cgi` tool will perform a query on the database and return
  the results. 
- The browser will show the search results.

\section extsearch_config Configuring

The first step is to make the search engine available via a web server.
If you use `analzsearch.cgi` this means making the
<a href="https://en.wikipedia.org/wiki/Common_Gateway_Interface">CGI</a> binary
available from the web server (i.e. be able to run it from a 
browser via an URL starting with http:)

How to setup a web server is outside the scope of this document,
but if you for instance have Apache installed, you could simply copy the 
`analzsearch.cgi` file from analyze's `bin` directory to the `cgi-bin` directory of the
Apache web server. Read the <a href="https://httpd.apache.org/docs/2.2/howto/cgi.html">apache documentation</a> for details.

To test if `analzsearch.cgi` is accessible start your web browser and
point to URL to the binary and add `?test` at the end

    http://yoursite.com/path/to/cgi/analzsearch.cgi?test

You should get the following message:

    Test failed: cannot find search index analzsearch.db

If you use Internet Explorer you may be prompted to download a file,
which will then contain this message. 

Since we didn't create or install a `analzsearch.db` it is OK for the test to
fail for this reason. How to correct this is discussed in the next section.

Before continuing with the next section add the above 
URL (without the `?test` part) to the \ref cfg_searchengine_url "SEARCHENGINE_URL" tag in
analyze's configuration file:

    SEARCHENGINE_URL = http://yoursite.com/path/to/cgi/analzsearch.cgi

\subsection extsearch_single Single project index

To use the external search option, make sure the following options are enabled
in analyze's configuration file:

    SEARCHENGINE           = YES
    SERVER_BASED_SEARCH    = YES
    EXTERNAL_SEARCH        = YES

This will make analyze generate a file called `searchdata.xml` in the output 
directory (configured with \ref cfg_output_directory "OUTPUT_DIRECTORY").
You can change the file name (and location) with the 
\ref cfg_searchdata_file "SEARCHDATA_FILE" option.

The next step is to put the raw search data into an index for efficient 
searching. You can use `analzindexer` for this. Simply run it from the command 
line:

    analzindexer searchdata.xml

This will create a directory called `analzsearch.db` with some files in it.
By default the directory will be created at the location from which `analzindexer`
was started, but you can change the directory using the `-o` option.

Copy the `analzsearch.db` directory to the same directory as where 
the `analzsearch.cgi` is located and rerun the browser test by pointing 
the browser to

    http://yoursite.com/path/to/cgi/analzsearch.cgi?test

You should now get the following message:

    Test successful.

Now you should be able to search for words and symbols from the HTML output.

\subsection extsearch_multi Multi project index

In case you have more than one analyze project and these projects are related, 
it may be desirable to allow searching for words in all projects from within 
the documentation of any of the projects.

To make this possible all that is needed is to combine the search data
for all projects into a single index, e.g. for two projects A and B for which the
searchdata.xml is generated in directories project_A and project_B run:

    analzindexer project_A/searchdata.xml project_B/searchdata.xml

and then copy the resulting `analzsearch.db` to the directory where also
`analzsearch.cgi` is located.

The `searchdata.xml` file doesn't contain any absolute paths or links, 
so how can the search results from multiple projects be linked back to the right documentation set?
This is where the \ref cfg_external_search_id "EXTERNAL_SEARCH_ID" and
\ref cfg_extra_search_mappings "EXTRA_SEARCH_MAPPINGS" options come into play.

To be able to identify the different projects, one needs to
set a unique ID using \ref cfg_external_search_id "EXTERNAL_SEARCH_ID"
for each project.

To link the search results to the right project, you need to define a
mapping per project using the \ref cfg_extra_search_mappings "EXTRA_SEARCH_MAPPINGS" tag.
With this option to can define the mapping from IDs of other projects to the
(relative) location of documentation of those projects.

So for projects A and B the relevant part of the configuration file 
could look as follows:

    project_A/analzfile
    ------------------
    EXTERNAL_SEARCH_ID    = A
    EXTRA_SEARCH_MAPPINGS = B=../../project_B/html

for project A and for project B

    project_B/analzfile
    ------------------
    EXTERNAL_SEARCH_ID    = B
    EXTRA_SEARCH_MAPPINGS = A=../../project_A/html
    
with these settings, projects A and B can share the same search database,
and the search results will link to the right documentation set.

\section extsearch_update Updating the index

When you modify the source code, you should re-run `analyze` to get up to date
documentation again. When using external searching you also need to update the
search index by re-running `analzindexer`. You could wrap the call to `analyze`
and `analzindexer` together in a script to make this process easier.

\section extsearch_api Programming interface

Previous sections have assumed you use the tools `analzindexer` 
and `analzsearch.cgi` to do the indexing and searching, but you could also 
write your own index and search tools if you like.

For this 3 interfaces are important
- The format of the input for the index tool.
- The format of the input for the search engine.
- The format of the output of search engine.

The next subsections describe these interfaces in more detail.

\subsection extsearch_api_index Indexer input format

The search data produced by analyze follows the 
<a href="https://cwiki.apache.org/confluence/display/solr/UpdateXmlMessages">Solr XML index message</a>
format.

The input for the indexer is an XML file, which consists of one `<add>` tag containing 
multiple `<doc>` tags, which in turn contain multiple `<field>` tags. 

Here is an example of one doc node, which contains the search data and meta data for 
one method:

    <add>
      ...
      <doc>
        <field name="type">function</field>
        <field name="name">QXmlReader::setDTDHandler</field>
        <field name="args">(QXmlDTDHandler *handler)=0</field>
        <field name="tag">QGGools.tag</field>
        <field name="url">de/df6/class_q_xml_reader.html#a0b24b1fe26a4c32a8032d68ee14d5dba</field>
        <field name="keywords">setDTDHandler QXmlReader::setDTDHandler QXmlReader</field>
        <field name="text">Sets the DTD handler to handler DTDHandler()</field>
      </doc>
      ...
    </add>

Each field has a name. The following field names are supported:
- *type*: the type of the search entry; can be one of: source, function, slot, 
          signal, variable, typedef, enum, enumvalue, property, event, related, 
          friend, define, file, namespace, concept, group, package, page, dir, module,
          constants, library, type, union, interface, protocol category, exception, class,
          struct, service, singleton
- *name*: the name of the search entry; for a method this is the qualified name of the method,
          for a class it is the name of the class, etc.
- *args*: the parameter list (in case of functions or methods)
- *tag*:  the name of the tag file used for this project.
- *url*:  the (relative) URL to the HTML documentation for this entry.
- *keywords*: important words that are representative for the entry. When searching for such
          keyword, this entry should get a higher rank in the search results.
- *text*: the documentation associated with the item. Note that only words are present, no markup.

@note Due to the potentially large size of the XML file, it is recommended to use a 
<a href="https://en.wikipedia.org/wiki/Simple_API_for_XML">SAX based parser</a> to process it.

\subsection extsearch_api_search_in Search URL format

When the search engine is invoked from a analyze generated HTML page, a number of parameters are
passed to via the <a href="https://en.wikipedia.org/wiki/Query_string">query string</a>.

The following fields are passed:
- *q*:  the query text as entered by the user
- *n*:  the number of search results requested.
- *p*:  the number of search page for which to return the results. Each page has *n* values.
- *cb*: the name of the callback function, used for JSON with padding, see the next section.
       
From the complete list of search results, the range `[n*p - n*(p+1)-1]` should be returned.

Here is an example of how a query looks like.

    http://yoursite.com/path/to/cgi/analzsearch.cgi?q=list&n=20&p=1&cb=dummy

It represents a query for the word 'list' (`q=list`) requesting 20 search results (`n=20`), 
starting with the result number 20 (`p=1`) and using callback 'dummy' (`cb=dummy`):


@note The values are <a href="https://en.wikipedia.org/wiki/Percent-encoding">URL encoded</a> so they
have to be decoded before they can be used.

\subsection extsearch_api_search_out Search results format

When invoking the search engine as shown in the previous subsection, it should reply with
the results. The format of the reply is
<a href="https://en.wikipedia.org/wiki/JSONP">JSON with padding</a>, which is basically
a JavaScript struct wrapped in a function call. The name of function should be the name of
the callback (as passed with the *cb* field in the query).

With the example query as shown the previous subsection the main structure of the reply should
look as follows:

    dummy({
      "hits":179,
      "first":20,
      "count":20,
      "page":1,
      "pages":9,
      "query": "list",
      "items":[
      ...
     ]})

The fields have the following meaning:
- *hits*:  the total number of search results (could be more than was requested).
- *first*: the index of first result returned: \f$\min(n*p,\mbox{\em hits})\f$.
- *count*: the actual number of results returned: \f$\min(n,\mbox{\em hits}-\mbox{\em first})\f$
- *page*:  the page number of the result: \f$p\f$
- *pages*: the total number of pages: \f$\left\lceil\frac{\mbox{\em hits}}{n}\right\rceil\f$.
- *items*: an array containing the search data per result.

Here is an example of how the element of the *items* array should look like:

    {"type": "function",
     "name": "QDir::entryInfoList(const OString &nameFilter, int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const",
     "tag": "QGGools.tag",
     "url": "d5/d8d/class_q_dir.html#a9439ea6b331957f38dbad981c4d050ef",
     "fragments":[
       "Returns a <span class=\"hl\">list</span> of QFileInfo objects for all files and directories...",
       "... pointer to a QFileInfoList The <span class=\"hl\">list</span> is owned by the QDir object...",
       "... to keep the entries of the <span class=\"hl\">list</span> after a subsequent call to this..."
     ]
    },

The fields for such an item have the following meaning:
- *type*: the type of the item, as found in the field with name "type" in the raw search data.
- *name*: the name of the item, including the parameter list, as found in the fields with
          name "name" and "args" in the raw search data.
- *tag*:  the name of the tag file, as found in the field with name "tag" in the raw search data.
- *url*:  the name of the (relative) URL to the documentation, as found in the field with name "url"
          in the raw search data.
- "fragments": an array with 0 or more fragments of text containing words that have been search for.
          These words should be wrapped in `<span class="hl">` and `</span>` tags to highlight them
          in the output.
*/
