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