none
[openafs-wiki.git] / TWiki / FormattedSearch.mdwn
1 <div>
2   <ul>
3     <li><a href="#TWiki Formatted Search Results"> TWiki Formatted Search Results</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="#Embedding search forms to return"> Embedding search forms to return a formatted result</a></li>
40           </ul>
41         </li>
42       </ul>
43     </li>
44     <li><a href="#TWiki Installation Error">TWiki Installation Error</a></li>
45   </ul>
46 </div>
47
48 # <a name="TWiki Formatted Search Results"></a> TWiki Formatted Search Results
49
50 _Inline search feature allows flexible formatting of search result_
51
52 The `%SEARCH{...}%` variable documented in [[TWikiVariables]] has a fixed format for the search result, that is, a table consisting of topic names and topic summaries. Use the `format="..."` parameter to specify a customized format of the search result. The string of the format parameter is typically a bullet list or table row containing variables (such as `%SEARCH{ "food" format="| $topic | $summary |" }%`).
53
54 ## <a name="Syntax"></a> Syntax
55
56 Two parameters can be used to specify a customized search result:
57
58 1. `header="..."` parameter
59
60 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:* |"`
61
62 2. `format="..."` parameter
63
64 Use the format parameter to specify the format of one search hit. <br /> Example: `format="| $topic | $summary |"`
65
66 Variables that can be used in the format string:
67
68 <table border="1" cellpadding="0" cellspacing="0">
69   <tr>
70     <th bgcolor="#99CCCC"><strong> Name: </strong></th>
71     <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
72   </tr>
73   <tr>
74     <td><code>$web</code></td>
75     <td> Name of the web </td>
76   </tr>
77   <tr>
78     <td><code>$topic</code></td>
79     <td> Topic name </td>
80   </tr>
81   <tr>
82     <td><code>$topic(20)</code></td>
83     <td> Topic name, "<tt>- </tt>" hyphenated each 20 characters </td>
84   </tr>
85   <tr>
86     <td><code>$topic(30, -&lt;br /&gt;)</code></td>
87     <td> Topic name, hyphenated each 30 characters with separator "<tt>-&lt;br /&gt;</tt>" </td>
88   </tr>
89   <tr>
90     <td><code>$topic(40, ...)</code></td>
91     <td> Topic name, shortended to 40 characters with "<tt>...</tt>" indication </td>
92   </tr>
93   <tr>
94     <td><code>$text</code></td>
95     <td> Formatted topic text. In case of a <code>multiple="on"</code> search, it is the line found for each search hit. </td>
96   </tr>
97   <tr>
98     <td><code>$locked</code></td>
99     <td> LOCKED flag (if any) </td>
100   </tr>
101   <tr>
102     <td><code>$date</code></td>
103     <td> Time stamp of last topic update, e.g. <code>29 Jun 2010 - 15:44</code></td>
104   </tr>
105   <tr>
106     <td><code>$isodate</code></td>
107     <td> Time stamp of last topic update, e.g. <code>2010-06-29T15:44Z</code></td>
108   </tr>
109   <tr>
110     <td><code>$rev</code></td>
111     <td> Number of last topic revision, e.g. <code>1.4</code></td>
112   </tr>
113   <tr>
114     <td><code>$username</code></td>
115     <td> Login name of last topic update, e.g. <code>jsmith</code></td>
116   </tr>
117   <tr>
118     <td><code>$wikiname</code></td>
119     <td> Wiki user name of last topic update, e.g. <code>JohnSmith</code></td>
120   </tr>
121   <tr>
122     <td><code>$wikiusername</code></td>
123     <td> Wiki user name of last topic update, like <code>Main.JohnSmith</code></td>
124   </tr>
125   <tr>
126     <td><code>$createdate</code></td>
127     <td> Time stamp of topic revision 1.1 </td>
128   </tr>
129   <tr>
130     <td><code>$createusername</code></td>
131     <td> Login name of topic revision 1.1, e.g. <code>jsmith</code></td>
132   </tr>
133   <tr>
134     <td><code>$createwikiname</code></td>
135     <td> Wiki user name of topic revision 1.1, e.g. <code>JohnSmith</code></td>
136   </tr>
137   <tr>
138     <td><code>$createwikiusername</code></td>
139     <td> Wiki user name of topic revision 1.1, e.g. <code>Main.JohnSmith</code></td>
140   </tr>
141   <tr>
142     <td><code>$summary</code></td>
143     <td> Topic summary </td>
144   </tr>
145   <tr>
146     <td><code>$formname</code></td>
147     <td> The name of the form attached to the topic; empty if none </td>
148   </tr>
149   <tr>
150     <td><code>$formfield(name)</code></td>
151     <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>
152   </tr>
153   <tr>
154     <td><code>$formfield(name, 10)</code></td>
155     <td> Form field value, "<tt>- </tt>" hyphenated each 10 characters </td>
156   </tr>
157   <tr>
158     <td><code>$formfield(name, 20, -&lt;br /&gt;)</code></td>
159     <td> Form field value, hyphenated each 20 characters with separator "<tt>-&lt;br /&gt;</tt>" </td>
160   </tr>
161   <tr>
162     <td><code>$formfield(name, 30, ...)</code></td>
163     <td> Form field value, shortended to 30 characters with "<tt>...</tt>" indication </td>
164   </tr>
165   <tr>
166     <td><code>$pattern(reg-exp)</code></td>
167     <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. The pattern must cover the whole text (topic or line). For example, <code>$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)</code> extracts the email address from a bullet of format <code>* Email: ...</code>. 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. </td>
168   </tr>
169   <tr>
170     <td><code>$n</code> or <code>$n()</code></td>
171     <td> New line </td>
172   </tr>
173   <tr>
174     <td><code>$nop</code> or <code>$nop()</code></td>
175     <td> Is a "no operation". This variable gets removed; useful for nested search </td>
176   </tr>
177   <tr>
178     <td><code>$quot</code></td>
179     <td> Double quote (<code>"</code>). Alternatively write <code>\"</code> to escape it </td>
180   </tr>
181   <tr>
182     <td><code>$percnt</code></td>
183     <td> Percent sign (<code>%</code>) </td>
184   </tr>
185   <tr>
186     <td><code>$dollar</code></td>
187     <td> Dollar sign (<code>$</code>) </td>
188   </tr>
189 </table>
190
191 **_Note:_** For `$pattern(reg-exp)`, specify a [[RegularExpression]] that scans from start to end and contains the text you want to keep in parenthesis, like `$pattern(.*?(from here.*?to here).*)`. You need to make sure that the integrity of a web page is not compromised; for example, if you include a table make sure to include everything including the table end tag.
192
193 ## <a name="Examples"></a> Examples
194
195 <a name="SearchBulletList"></a>
196
197 ### <a name="Bullet list showing topic name a"></a> Bullet list showing topic name and summary
198
199 **Write this:**
200
201 `%SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * *Topic: Summary:*" format="   * [[$topic]]: $summary" }%`
202
203 **To get this:**
204
205 # <a name="TWiki Installation Error"></a> TWiki Installation Error
206
207 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
208
209 ### <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
210
211 **Write this in the Know web:**
212
213 `| *Topic:* | *OperatingSystem:* | *OsVersion:* |`<br />`%SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" regex="on" nosearch="on" nototal="on" format="| [[$topic]] | $formfield(OperatingSystem) | $formfield(OsVersion) |" }%`
214
215 **To get this:**
216
217 <table border="1" cellpadding="1" cellspacing="0">
218   <tr>
219     <th bgcolor="#99CCCC"><strong>Topic:</strong></th>
220     <th bgcolor="#99CCCC"><strong>OperatingSystem:</strong></th>
221     <th bgcolor="#99CCCC"><strong>OsVersion:</strong></th>
222   </tr>
223   <tr>
224     <td><a href="http://www.dementia.org/twiki//view/Know/IncorrectDllVersionW32PTH10DLL">IncorrectDllVersionW32PTH10DLL</a></td>
225     <td><a href="http://www.dementia.org/twiki//view/Know/OsWin">OsWin</a></td>
226     <td> 95/98 </td>
227   </tr>
228   <tr>
229     <td><a href="http://www.dementia.org/twiki//view/Know/WinDoze95Crash">WinDoze95Crash</a></td>
230     <td><a href="http://www.dementia.org/twiki//view/Know/OsWin">OsWin</a></td>
231     <td> 95 </td>
232   </tr>
233 </table>
234
235 ### <a name="Extract some text from a topic u"></a> Extract some text from a topic using regular expression
236
237 **Write this:**
238
239 `%SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" regex="on" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%`
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="Nested Search"></a> Nested Search
248
249 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.
250
251 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).
252
253 - First search:
254   - `%SEARCH{ "culture" format="   * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%`
255 - Second search. For each hit we want this search:
256   - `%SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%`
257 - 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:
258   - Use `$percnt` to escape the leading percent of the second search
259   - Use `\"` to escape the double quotes
260   - Use `$dollar` to escape the `$` of `$topic`
261   - Use `$nop` to escape the `}%` sequence
262
263 **Write this:**
264
265 `%SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%`
266
267 **To get this:**
268
269 # <a name="TWiki Installation Error"></a> TWiki Installation Error
270
271 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
272
273 ### <a name="Most recently changed pages"></a> Most recently changed pages
274
275 **Write this:**
276
277 `%SEARCH{ "\.*" scope="topic" regex="on" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [[$topic]] | $wikiusername  | $date |" limit="7" }%`
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="Embedding search forms to return"></a> Embedding search forms to return a formatted result
286
287 Use an HTML form and an embedded formatted search on the same topic. You can link them together with an `%URLPARAM{"..."}%` variable. Example:
288
289 **Write this:**
290
291     <form action="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%">
292     Find Topics:
293     <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />
294     <input type="submit" value="Search" />
295     </form>
296     Result:
297     %SEARCH{ search="%URLPARAM{"q"}%" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
298
299 **To get this:**
300
301 <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>
302
303 Result:
304
305 # <a name="TWiki Installation Error"></a> TWiki Installation Error
306
307 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
308
309 -- TWiki:Main.PeterThoeny - 16 Mar 2004