(no commit message)
[openafs-wiki.git] / TWiki / FormattedSearch.mdwn
1 # <a name="TWiki Formatted Search"></a> TWiki Formatted Search
2
3 _Inline search feature allows flexible formatting of search result_
4
5 The default output format of a <code>[[%SEARCH{...}%|Main/VarSEARCH]]</code> 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 `%SEARCH{ "food" format="| $topic | $summary |" }%`. See <code>[[%SEARCH{...}%|Main/VarSEARCH]]</code> for other search parameters, such as `separator=""`.
6
7 <div>
8   <ul>
9     <li><a href="#TWiki Formatted Search"> TWiki Formatted Search</a><ul>
10         <li><a href="#Syntax"> Syntax</a><ul>
11             <li><a href="#1. <code>header="..."</code> parameter"> 1. header="..." parameter</a></li>
12             <li><a href="#2. <code>footer="..."</code> parameter"> 2. footer="..." parameter</a></li>
13             <li><a href="#3. <code>format="..."</code> parameter"> 3. format="..." parameter</a></li>
14           </ul>
15         </li>
16         <li><a href="#Examples"> Examples</a><ul>
17             <li><a href="#Bullet list showing topic name a"> Bullet list showing topic name and summary</a></li>
18           </ul>
19         </li>
20       </ul>
21     </li>
22     <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
23         <li>
24           <ul>
25             <li><a href="#Table showing form field values"> Table showing form field values of topics with a form</a></li>
26             <li><a href="#Extract some text from a topic u"> Extract some text from a topic using regular expression</a></li>
27           </ul>
28         </li>
29       </ul>
30     </li>
31     <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
32         <li>
33           <ul>
34             <li><a href="#Nested Search"> Nested Search</a></li>
35           </ul>
36         </li>
37       </ul>
38     </li>
39     <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
40         <li>
41           <ul>
42             <li><a href="#Most recently changed pages"> Most recently changed pages</a></li>
43           </ul>
44         </li>
45       </ul>
46     </li>
47     <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
48         <li>
49           <ul>
50             <li><a href="#Search with conditional output"> Search with conditional output</a></li>
51           </ul>
52         </li>
53       </ul>
54     </li>
55     <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
56         <li>
57           <ul>
58             <li><a href="#Embedding search forms to return"> Embedding search forms to return a formatted result</a></li>
59           </ul>
60         </li>
61       </ul>
62     </li>
63     <li><a href="#TWiki Installation Error">TWiki Installation Error</a></li>
64   </ul>
65 </div>
66
67 ## <a name="Syntax"></a> Syntax
68
69 Two parameters can be used to specify a customized search result:
70
71 ### <a name="1. &lt;code&gt;header=&quot;...&quot;&lt;/code&gt; parameter"></a> 1. `header="..."` parameter
72
73 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="| *Topic:* | *Summary:* |"`
74
75 Variables that can be used in the header string:
76
77 <table border="1" cellpadding="0" cellspacing="0">
78   <tr>
79     <th bgcolor="#99CCCC"><strong> Name: </strong></th>
80     <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
81   </tr>
82   <tr>
83     <td><code>$web</code></td>
84     <td> Name of the web </td>
85   </tr>
86 </table>
87
88 ### <a name="2. &lt;code&gt;footer=&quot;...&quot;&lt;/code&gt; parameter"></a> 2. `footer="..."` parameter
89
90 Use the footer parameter to specify the footer of a search result. It should correspond to the format of the format parameter. This parameter is optional. <br /> Example: `footer="| *Topic* | *Summary* |"`
91
92 Variables that can be used in the footer string:
93
94 <table border="1" cellpadding="0" cellspacing="0">
95   <tr>
96     <th bgcolor="#99CCCC"><strong> Name: </strong></th>
97     <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
98   </tr>
99   <tr>
100     <td><code>$web</code></td>
101     <td> Name of the web </td>
102   </tr>
103   <tr>
104     <td><code>$ntopics</code></td>
105     <td> Number of topics found in current web </td>
106   </tr>
107   <tr>
108     <td><code>$nhits</code></td>
109     <td> Number of hits if <code>multiple="on"</code>. Cumulative across all topics in current web. Identical to <code>$ntopics</code> unless <code>multiple="on"</code></td>
110   </tr>
111 </table>
112
113 ### <a name="3. &lt;code&gt;format=&quot;...&quot;&lt;/code&gt; parameter"></a> 3. `format="..."` parameter
114
115 Use the format parameter to specify the format of one search hit. <br /> Example: `format="| $topic | $summary |"`
116
117 Variables that can be used in the format string:
118
119 <table border="1" cellpadding="0" cellspacing="0">
120   <tr>
121     <th bgcolor="#99CCCC"><strong> Name: </strong></th>
122     <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
123   </tr>
124   <tr>
125     <td><code>$web</code></td>
126     <td> Name of the web </td>
127   </tr>
128   <tr>
129     <td><code>$topic</code></td>
130     <td> Topic name </td>
131   </tr>
132   <tr>
133     <td><code>$topic(20)</code></td>
134     <td> Topic name, "<tt>- </tt>" hyphenated each 20 characters </td>
135   </tr>
136   <tr>
137     <td><code>$topic(30, -&lt;br /&gt;)</code></td>
138     <td> Topic name, hyphenated each 30 characters with separator "<tt>-&lt;br /&gt;</tt>" </td>
139   </tr>
140   <tr>
141     <td><code>$topic(40, ...)</code></td>
142     <td> Topic name, shortended to 40 characters with "<tt>...</tt>" indication </td>
143   </tr>
144   <tr>
145     <td><code>$parent</code></td>
146     <td> Name of parent topic; empty if not set </td>
147   </tr>
148   <tr>
149     <td><code>$parent(20)</code></td>
150     <td> Name of parent topic, same hyphenation/shortening like <code>$topic()</code></td>
151   </tr>
152   <tr>
153     <td><code>$text</code></td>
154     <td> Formatted topic text. In case of a <code>multiple="on"</code> search, it is the line found for each search hit. </td>
155   </tr>
156   <tr>
157     <td><code>$locked</code></td>
158     <td> LOCKED flag (if any) </td>
159   </tr>
160   <tr>
161     <td><code>$date</code></td>
162     <td> Time stamp of last topic update, e.g. <code>29 Jun 2010 - 16:14</code></td>
163   </tr>
164   <tr>
165     <td><code>$isodate</code></td>
166     <td> Time stamp of last topic update, e.g. <code>2010-06-29T16:14Z</code></td>
167   </tr>
168   <tr>
169     <td><code>$rev</code></td>
170     <td> Number of last topic revision, e.g. <code>4</code></td>
171   </tr>
172   <tr>
173     <td><code>$username</code></td>
174     <td> Login name of last topic update, e.g. <code>jsmith</code></td>
175   </tr>
176   <tr>
177     <td><code>$wikiname</code></td>
178     <td> Wiki user name of last topic update, e.g. <code>JohnSmith</code></td>
179   </tr>
180   <tr>
181     <td><code>$wikiusername</code></td>
182     <td> Wiki user name of last topic update, like <code>%USERSWEB%.JohnSmith</code></td>
183   </tr>
184   <tr>
185     <td><code>$createdate</code></td>
186     <td> Time stamp of topic revision 1 </td>
187   </tr>
188   <tr>
189     <td><code>$createusername</code></td>
190     <td> Login name of topic revision 1, e.g. <code>jsmith</code></td>
191   </tr>
192   <tr>
193     <td><code>$createwikiname</code></td>
194     <td> Wiki user name of topic revision 1, e.g. <code>JohnSmith</code></td>
195   </tr>
196   <tr>
197     <td><code>$createwikiusername</code></td>
198     <td> Wiki user name of topic revision 1, e.g. <code>%USERSWEB%.JohnSmith</code></td>
199   </tr>
200   <tr>
201     <td><code>$summary</code></td>
202     <td> Topic summary, just the plain text, all formatting and line breaks removed; up to 162 characters </td>
203   </tr>
204   <tr>
205     <td><code>$summary(50)</code></td>
206     <td> Topic summary, up to 50 characters shown </td>
207   </tr>
208   <tr>
209     <td><code>$summary(showvarnames)</code></td>
210     <td> Topic summary, with <code>%ALLTWIKI{...}%</code> variables shown as <code>ALLTWIKI{...}</code></td>
211   </tr>
212   <tr>
213     <td><code>$summary(noheader)</code></td>
214     <td> Topic summary, with leading <code>---+ headers</code> removed%BR% <strong><em>Note:</em></strong> The tokens can be combined, for example <code>$summary(100, showvarnames, noheader)</code></td>
215   </tr>
216   <tr>
217     <td><code>$changes</code></td>
218     <td> Summary of changes between latest rev and previous rev </td>
219   </tr>
220   <tr>
221     <td><code>$changes(n)</code></td>
222     <td> Summary of changes between latest rev and rev n </td>
223   </tr>
224   <tr>
225     <td><code>$formname</code></td>
226     <td> The name of the form attached to the topic; empty if none </td>
227   </tr>
228   <tr>
229     <td><code>$formfield(name)</code></td>
230     <td> The field value of a form field; for example, <code>$formfield(TopicClassification)</code> would get expanded to <code>PublicFAQ</code>. This applies only to topics that have a [[Main/TWikiForms]]</td>
231   </tr>
232   <tr>
233     <td><code>$formfield(name, 10)</code></td>
234     <td> Form field value, "<tt>- </tt>" hyphenated each 10 characters </td>
235   </tr>
236   <tr>
237     <td><code>$formfield(name, 20, -&lt;br /&gt;)</code></td>
238     <td> Form field value, hyphenated each 20 characters with separator "<tt>-&lt;br /&gt;</tt>" </td>
239   </tr>
240   <tr>
241     <td><code>$formfield(name, 30, ...)</code></td>
242     <td> Form field value, shortended to 30 characters with "<tt>...</tt>" indication </td>
243   </tr>
244   <tr>
245     <td><code>$pattern(reg-exp)</code></td>
246     <td> A regular expression pattern to extract some text from a topic (does not search meta data; use <code>$formfield</code> instead). In case of a <code>multiple="on"</code> search, the pattern is applied to the line found in each search hit.%BB% Specify a [[Main/RegularExpression]] that covers the whole text (topic or line), which typically starts with <code>.*</code>, and must end in <code>.*</code> %BB% Put text you want to keep in parenthesis, like <code>$pattern(.*?(from here.*?to here).*)</code> %BB% Example: <code>$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)</code> extracts the e-mail address from a bullet of format <code>* Email: ...</code> %BB% This example has non-greedy <code>.*?</code> patterns to scan for the first occurance of the Email bullet; use greedy <code>.*</code> patterns to scan for the last occurance %BB% Limitation: Do not use <code>.*)</code> inside the pattern, e.g. <code>$pattern(.*foo(.*)bar.*)</code> does not work, but <code>$pattern(.*foo(.*?)bar.*)</code> 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 </td>
247   </tr>
248   <tr>
249     <td><code>$count(reg-exp)</code></td>
250     <td> 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 <code>$pattern(reg-exp)</code>. Example: <code>$count(.*?(---[+][+][+][+]) .*)</code> counts the number of &lt;H4&gt; headers in a page. </td>
251   </tr>
252   <tr>
253     <td><code>$ntopics</code></td>
254     <td> Number of topics found in current web. This is the current topic count, not the total number of topics </td>
255   </tr>
256   <tr>
257     <td><code>$nhits</code></td>
258     <td> Number of hits if <code>multiple="on"</code>. Cumulative across all topics in current web. Identical to <code>$ntopics</code> unless <code>multiple="on"</code></td>
259   </tr>
260 </table>
261
262 ## <a name="Examples"></a> Examples
263
264 Here are some samples of formatted searches. The [[SearchPatternCookbook]] has other examples, such as [[creating a picklist of usernames|Main/SearchPatternCookbook#SearchUsernames]], [[searching for topic children|Main/SearchPatternCookbook#SearchTopicChildren]] and more.
265
266 <a name="SearchBulletList"></a>
267
268 ### <a name="Bullet list showing topic name a"></a> Bullet list showing topic name and summary
269
270 **Write this:**
271
272 `%SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * *Topic: Summary:*" format="   * [[$topic]]: $summary"  footer="   * *Topic: Summary*"  }%`
273
274 **To get this:**
275
276 # <a name="TWiki Installation Error"></a> TWiki Installation Error
277
278 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
279
280 ### <a name="Table showing form field values"></a><a name="Table showing form field values "></a> Table showing form field values of topics with a form
281
282 In a web where there is a form that contains a `TopicClassification` field, an `OperatingSystem` field and an `OsVersion` field we could write:
283
284 `| *Topic:* | *OperatingSystem:* | *OsVersion:* |`<br />`%SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" type="regex" nosearch="on" nototal="on" format="| [[$topic]] | $formfield(OperatingSystem) | $formfield(OsVersion) |" }%`
285
286 To get this:
287
288 <table border="1" cellpadding="1" cellspacing="0">
289   <tr>
290     <th bgcolor="#99CCCC"><strong>Topic:</strong></th>
291     <th bgcolor="#99CCCC"><strong>OperatingSystem:</strong></th>
292     <th bgcolor="#99CCCC"><strong>OsVersion:</strong></th>
293   </tr>
294   <tr>
295     <td>[[Sandbox/IncorrectDllVersionW32PTH10DLL]]</td>
296     <td>[[Sandbox/OsWin]]</td>
297     <td> 95/98 </td>
298   </tr>
299   <tr>
300     <td>[[Sandbox/WinDoze95Crash]]</td>
301     <td>[[Sandbox/OsWin]]</td>
302     <td> 95 </td>
303   </tr>
304 </table>
305
306 ### <a name="Extract some text from a topic u"></a> Extract some text from a topic using regular expression
307
308 **Write this:**
309
310 `%SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%`
311
312 **To get this:**
313
314 # <a name="TWiki Installation Error"></a> TWiki Installation Error
315
316 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
317
318 ### <a name="Nested Search"></a> Nested Search
319
320 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.
321
322 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).
323
324 - First search:
325   - `%SEARCH{ "culture" format="   * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%`
326 - Second search. For each hit we want this search:
327   - `%SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%`
328 - 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:
329   - Use `$percnt` to escape the leading percent of the second search
330   - Use `\"` to escape the double quotes
331   - Use `$dollar` to escape the `$` of `$topic`
332   - Use `$nop` to escape the `}%` sequence
333
334 **Write this:**
335
336 `%SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%`
337
338 **To get this:**
339
340 # <a name="TWiki Installation Error"></a> TWiki Installation Error
341
342 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
343
344 **_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.
345
346 ### <a name="Most recently changed pages"></a> Most recently changed pages
347
348 **Write this:**
349
350 `%SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [[$topic]] | $wikiusername  | $date |" limit="7" }%`
351
352 **To get this:**
353
354 # <a name="TWiki Installation Error"></a> TWiki Installation Error
355
356 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
357
358 ### <a name="Search with conditional output"></a> Search with conditional output
359
360 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:
361
362 1. Specify a search which returns more hits then you need
363 2. For each search hit apply a spreadsheet formula to determine if the hit is needed
364 3. If needed, format and output the result
365 4. Else supress the search hit
366
367 This requires the TWiki:Plugins.SpreadSheetPlugin. The following example shows all topics that are up to exactly one week old.
368
369 **Write this:**
370
371 `%CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%` %BR% `%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" }%`
372
373 - The first line sets the `weekold` variable to the serialized date of exactly one week ago
374 - The SEARCH has a deferred CALC. The `$percnt` makes sure that the CALC gets executed once for each search hit
375 - The CALC compares the date of the topic with the `weekold` date
376 - If topic is older, a `<nop>` is returned, which gets removed at the end of the TWiki rendering process
377 - Otherwise, the search hit is formatted and returned
378
379 **To get this:**
380
381 # <a name="TWiki Installation Error"></a> TWiki Installation Error
382
383 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
384
385 ### <a name="Embedding search forms to return"></a> Embedding search forms to return a formatted result
386
387 Use an HTML form and an embedded formatted search on the same topic. You can link them together with an `%URLPARAM{"..."}%` variable. Example:
388
389 **Write this:**
390
391     <form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
392     Find Topics:
393     <input type="text" name="q" size="32" value="%URLPARAM{"q" encode="entity"}%" />&nbsp;<input type="submit" class="twikiSubmit" value="Search" />
394     </form>
395     Result:
396     %SEARCH{ search="%URLPARAM{"q" encode="quote"}%" type="keyword" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
397
398 **To get this:**
399
400 <form action="http://www.dementia.org/twiki/view/%WEB%/%TOPIC%"> Find Topics: <input name="q" size="32" type="text" value="" /> <input type="submit" value="Search" /></form>
401
402 Result:
403
404 # <a name="TWiki Installation Error"></a> TWiki Installation Error
405
406 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
407
408 **_Related Topics:_** [[UserDocumentationCategory]], [[SearchHelp]], [[TWikiVariables#VarSEARCH]], [[SearchPatternCookbook]], [[RegularExpression]]
409
410 -- **_Contributors:_** TWiki:Main.PeterThoeny, TWiki:Main.CrawfordCurrie, TWiki:Main.SopanShewale