data/TWiki/FormattedSearch.txt
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
     1 %META:TOPICINFO{author="TWikiContributor" date="1168735492" format="1.1" version="19"}%
     2 %META:TOPICPARENT{name="TWikiVariables"}%
     3 %STARTINCLUDE%
     4 ---+ TWiki Formatted Search
     5 
     6 _Inline search feature allows flexible formatting of search result_
     7 
     8 The default output format of a =[[VarSEARCH][%<nop>SEARCH{...}%]]= is a table consisting of topic names and topic summaries. Use the =format="..."= parameter to customize the search result. The format parameter typically defines a bullet or a table row containing variables, such as =%<nop>SEARCH{ "food" format="| $topic | $summary |" }%=. See =[[VarSEARCH][%<nop>SEARCH{...}%]]= for other search parameters, such as =separator=""=.
     9 
    10 %TOC%
    11 
    12 ---++ Syntax
    13 
    14 Two parameters can be used to specify a customized search result:
    15 
    16 ---+++ 1. =header="..."= parameter
    17 
    18 Use the header parameter to specify the header of a search result. It should correspond to the format of the format parameter. This parameter is optional. <br /> Example: =header="| <nop>*Topic:*<nop> | <nop>*Summary:*<nop> |"=
    19 
    20 Variables that can be used in the header string:
    21 
    22 | *Name:* | *Expands To:* |
    23 | =$web= | Name of the web |
    24 %INCLUDE{FormatTokens}%
    25 
    26 ---+++ 2. =format="..."= parameter
    27 
    28 Use the format parameter to specify the format of one search hit.
    29 <br /> Example: =format="| $topic | $summary |"=
    30 
    31 Variables that can be used in the format string:
    32 
    33 | *Name:* | *Expands To:* |
    34 | =$web= | Name of the web |
    35 | =$topic= | Topic name |
    36 | =$topic(20)= | Topic name, "<tt>- </tt>" hyphenated each 20 characters |
    37 | =$topic(30, -&lt;br /&gt;)= | Topic name, hyphenated each 30 characters with separator "<tt>-&lt;br /&gt;</tt>" |
    38 | =$topic(40, ...)= | Topic name, shortended to 40 characters with "<tt>...</tt>" indication |
    39 | =$parent= | Name of parent topic; empty if not set |
    40 | =$parent(20)= | Name of parent topic, same hyphenation/shortening like =$topic()= |
    41 | =$text= | Formatted topic text. In case of a =multiple="on"= search, it is the line found for each search hit. |
    42 | =$locked= | LOCKED flag (if any) |
    43 | =$date= | Time stamp of last topic update, e.g. =%GMTIME{"$day $mon $year - $hour:$min"}%= |
    44 | =$isodate= | Time stamp of last topic update, e.g. =%GMTIME{"$year-$mo-$dayT$hour:$minZ"}%= |
    45 | =$rev= | Number of last topic revision, e.g. =4= |
    46 | =$username= | Login name of last topic update, e.g. =jsmith= |
    47 | =$wikiname= | Wiki user name of last topic update, e.g. =<nop>JohnSmith= |
    48 | =$wikiusername= | Wiki user name of last topic update, like =%USERSWEB%.<nop>JohnSmith= |
    49 | =$createdate= | Time stamp of topic revision 1 |
    50 | =$createusername= | Login name of topic revision 1, e.g. =jsmith= |
    51 | =$createwikiname= | Wiki user name of topic revision 1, e.g. =<nop>JohnSmith= |
    52 | =$createwikiusername= | Wiki user name of topic revision 1, e.g. =%USERSWEB%.<nop>JohnSmith= |
    53 | =$summary= | Topic summary, just the plain text, all formatting and line breaks removed; up to 162 characters |
    54 | =$summary(50)= | Topic summary, up to 50 characters shown |
    55 | =$summary(showvarnames)= | Topic summary, with =%<nop>ALLTWIKI{...}%= variables shown as =ALLTWIKI{...}= |
    56 | =$summary(noheader)= | Topic summary, with leading =---+ headers= removed%BR% __Note:__ The tokens can be combined, for example =$summary(100, showvarnames, noheader)= |
    57 | =$changes= | Summary of changes between latest rev and previous rev |
    58 | =$changes(n)= | Summary of changes between latest rev and rev n |
    59 | =$formname= | The name of the form attached to the topic; empty if none |
    60 | =$formfield(name)= | The field value of a form field; for example, =$formfield(<nop>TopicClassification)= would get expanded to =PublicFAQ=. This applies only to topics that have a [[TWikiForms][TWikiForm]] |
    61 | =$formfield(name, 10)= | Form field value, "<tt>- </tt>" hyphenated each 10 characters |
    62 | =$formfield(name, 20, -&lt;br /&gt;)= | Form field value, hyphenated each 20 characters with separator "<tt>-&lt;br /&gt;</tt>" |
    63 | =$formfield(name, 30, ...)= | Form field value, shortended to 30 characters with "<tt>...</tt>" indication |
    64 | =$pattern(reg-exp)= | A regular expression pattern to extract some text from a topic (does not search meta data; use =$formfield= instead). In case of a =multiple="on"= search, the pattern is applied to the line found in each search hit.%BB% Specify a RegularExpression that covers the whole text (topic or line), which typically starts with =.*=, and must end in =.*= %BB% Put text you want to keep in parenthesis, like =$pattern(.*?(from here.*?to here).*)= %BB% Example: =$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)= extracts the e-mail address from a bullet of format =* Email: ...= %BB% This example has non-greedy =.*?= patterns to scan for the first occurance of the Email bullet; use greedy =.*= patterns to scan for the last occurance %BB% Limitation: Do not use =.*)= inside the pattern, e.g. =$pattern(.*foo(.*)bar.*)= does not work, but =$pattern(.*foo(.*?)bar.*)= does %BB% Note: Make sure that the integrity of a web page is not compromised; for example, if you include an HTML table make sure to include everything including the table end tag |
    65 | =$count(reg-exp)= | Count of number of times a regular expression pattern appears in the text of a topic (does not search meta data). Follows guidelines for use and limitations outlined above under =$pattern(reg-exp)=. Example: =$count(.*?(---[+][+][+][+]) .*)= counts the number of &lt;H4&gt; headers in a page. |
    66 %INCLUDE{FormatTokens}%
    67 
    68 ---++ Examples
    69 
    70 Here are some samples of formatted searches. The SearchPatternCookbook has other examples, such as [[SearchPatternCookbook#SearchUsernames][creating a picklist of usernames]], [[SearchPatternCookbook#SearchTopicChildren][searching for topic children]] and more.
    71 
    72 #SearchBulletList
    73 ---+++ Bullet list showing topic name and summary
    74 
    75 *Write this:*
    76 
    77 =%<nop>SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * <nop>*Topic: Summary:*" format="   * [<nop>[$topic]]: $summary" }%=
    78 
    79 *To get this:*
    80 
    81 %SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * *Topic: Summary:*" format="   * [[$topic]]: $summary" }%
    82 
    83 
    84 ---+++ Table showing form field values of topics with a form
    85 
    86 In a web where there is a form that contains a =Topic<nop>Classification= field, an =Operating<nop>System= field and an =Os<nop>Version= field we could write:
    87 
    88 =| <nop>*Topic:*<nop> | <nop>*<nop>OperatingSystem:*<nop> | <nop>*<nop>OsVersion:*<nop> |= <br />
    89 =%<nop>SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" type="regex" nosearch="on" nototal="on" format="| [<nop>[$topic]] | $formfield(<nop>OperatingSystem) | $formfield(<nop>OsVersion) |" }%=
    90 
    91 To get this:
    92 
    93 <table border="1" cellspacing="0" cellpadding="1">
    94 <tr>
    95  <th bgcolor="#99CCCC"> <strong>Topic:</strong> </th>
    96  <th bgcolor="#99CCCC"> <strong>OperatingSystem:</strong> </th>
    97  <th bgcolor="#99CCCC"> <strong>OsVersion:</strong> </th></tr>
    98 <tr>
    99  <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/IncorrectDllVersionW32PTH10DLL">IncorrectDllVersionW32PTH10DLL</a>  </td><td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsWin">OsWin</a>  </td><td>  95/98  </td></tr>
   100 <tr>
   101  <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/WinDoze95Crash">WinDoze95Crash</a>  </td>
   102  <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsWin">OsWin</a>  </td>
   103  <td>  95  </td></tr>
   104 </table>
   105 
   106 
   107 ---+++ Extract some text from a topic using regular expression
   108 
   109 *Write this:*
   110 
   111 =%<nop>SEARCH{ "__Back to\:__ <nop>TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [<nop>[$topic][Answer...]]" }%=
   112 
   113 *To get this:*
   114 
   115 %SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%
   116 
   117 
   118 ---+++ Nested Search
   119 
   120 Search can be nested. For example, search for some topics, then form a new search for each topic found in the first search. The idea is to build the nested search string using a formatted search in the first search.
   121 
   122 Here is an example. Let's search for all topics that contain the word "culture" (first search), and let's find out where each topic found is linked from (second search).
   123 
   124    * First search:
   125       * =%<nop>SEARCH{ "culture" format="   * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%=
   126    * Second search. For each hit we want this search:
   127       * =%<nop>SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%=
   128    * Now let's nest the two. We need to escape the second search, e.g. the first search will build a valid second search string. Note that we escape the second search so that it does not get evaluated prematurely by the first search:
   129       * Use =$percnt= to escape the leading percent of the second search
   130       * Use =\"= to escape the double quotes
   131       * Use =$dollar= to escape the =$= of =$topic=
   132       * Use =$nop= to escape the =}%= sequence
   133 
   134 *Write this:*
   135 
   136 =%<nop>SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%=
   137 
   138 *To get this:*
   139 
   140 %SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%
   141 
   142 __Note:__ Nested search can be slow, especially if you nest more then 3 times. Nesting is limited to 16 levels. For each new nesting level you need to "escape the escapes", e.g. write =$dollarpercntSEARCH{= for level three, =$dollardollarpercntSEARCH{= for level four, etc.
   143 
   144 ---+++ Most recently changed pages
   145 
   146 *Write this:*
   147 
   148 =%<nop>SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [<nop>[$topic]] | $wikiusername  | $date |" limit="7" }%=
   149 
   150 *To get this:*
   151 
   152 %SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [[$topic]] | $wikiusername  | $date |" limit="7" }%
   153 
   154 ---+++ Search with conditional output
   155 
   156 A regular expression search is flexible, but there are limitations. For example, you cannot show all topics that are up to exactly one week old, or create a report that shows all records with invalid form fields or fields within a certain range, etc. You need some additional logic to format output based on a condition:
   157    1. Specify a search which returns more hits then you need
   158    1. For each search hit apply a spreadsheet formula to determine if the hit is needed
   159    1. If needed, format and output the result
   160    1. Else supress the search hit
   161 
   162 This requires the TWiki:Plugins.SpreadSheetPlugin. The following example shows all topics that are up to exactly one week old.
   163 
   164 *Write this:*
   165 
   166 =%<nop>CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%= %BR%
   167 =%<nop>SEARCH{ "." scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on" format="$percntCALC{$IF($TIME($date) &lt; $GET(weekold), &lt;nop&gt;, | [<nop>[$topic]] | $wikiusername | $date | $rev |)}$percnt" limit="100" }%=
   168 
   169    * The first line sets the =weekold= variable to the serialized date of exactly one week ago
   170    * The SEARCH has a deferred CALC. The =$percnt= makes sure that the CALC gets executed once for each search hit
   171    * The CALC compares the date of the topic with the =weekold= date
   172    * If topic is older, a =&lt;nop&gt;= is returned, which gets removed at the end of the TWiki rendering process
   173    * Otherwise, the search hit is formatted and returned
   174 
   175 *To get this:*
   176 
   177 %CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%
   178 %SEARCH{ "." scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on" format="$percntCALC{$IF($TIME($date) < $GET(weekold), <nop>, | [[$topic]] | $wikiusername | $date | $rev |)}$percnt" limit="100" }%
   179 
   180 ---+++ Embedding search forms to return a formatted result
   181 
   182 Use an HTML form and an embedded formatted search on the same topic. You can link them together with an =%<nop>URLPARAM{"..."}%= variable. Example:
   183 
   184 *Write this:*
   185 
   186 <verbatim>
   187 <form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
   188 Find Topics: 
   189 <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />&nbsp;<input type="submit" class="twikiSubmit" value="Search" />
   190 </form>
   191 Result:
   192 %SEARCH{ search="%URLPARAM{"q"}%" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
   193 </verbatim>
   194 
   195 *To get this:*
   196 
   197 <form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
   198 Find Topics: 
   199 <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />&nbsp;<input type="submit" class="twikiSubmit" value="Search" />
   200 </form>
   201 Result:
   202 %SEARCH{ search="%URLPARAM{"q"}%" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
   203 
   204 __Related Topics:__ UserDocumentationCategory, SearchHelp, TWikiVariables#VarSEARCH, SearchPatternCookbook, RegularExpression
   205 
   206 -- __Contributors:__ TWiki:Main.PeterThoeny, TWiki:Main.CrawfordCurrie