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