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