1 rizwank 1.1 %META:TOPICINFO{author="PeterThoeny" date="1097558262" format="1.0" version="1.17"}%
2 %META:TOPICPARENT{name="TWikiVariables"}%
3 %TOC%
4 %STARTINCLUDE%
5 ---# TWiki Formatted Search Results
6
7 _Inline search feature allows flexible formatting of search result_
8
9 The default format of a =[[TWikiSearch][%<nop>SEARCH{...}%]]= as documented in [[TWikiVariables#VarSEARCH][TWikiVariables]] 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 =%<nop>SEARCH{ "food" format="| $topic | $summary |" }%=.
10
11 ---++ Syntax
12
13 Two parameters can be used to specify a customized search result:
14
15 1. =header="..."= parameter
16
17 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="| <nop>*Topic:*<nop> | <nop>*Summary:*<nop> |"=
18
19 2. =format="..."= parameter
20
21 Use the format parameter to specify the format of one search hit.
22 rizwank 1.1 <br /> Example: =format="| $topic | $summary |"=
23
24 Variables that can be used in the format string:
25
26 | *Name:* | *Expands To:* |
27 | =$web= | Name of the web |
28 | =$topic= | Topic name |
29 | =$topic(20)= | Topic name, "<tt>- </tt>" hyphenated each 20 characters |
30 | =$topic(30, -<br />)= | Topic name, hyphenated each 30 characters with separator "<tt>-<br /></tt>" |
31 | =$topic(40, ...)= | Topic name, shortended to 40 characters with "<tt>...</tt>" indication |
32 | =$parent= | Name of parent topic; empty if not set |
33 | =$parent(20)= | Name of parent topic, same hyphenation/shortening like =$topic()= |
34 | =$text= | Formatted topic text. In case of a =multiple="on"= search, it is the line found for each search hit. |
35 | =$locked= | LOCKED flag (if any) |
36 | =$date= | Time stamp of last topic update, e.g. =%GMTIME{"$day $mon $year - $hour:$min"}%= |
37 | =$isodate= | Time stamp of last topic update, e.g. =%GMTIME{"$year-$mo-$dayT$hour:$minZ"}%= |
38 | =$rev= | Number of last topic revision, e.g. =1.4= |
39 | =$username= | Login name of last topic update, e.g. =jsmith= |
40 | =$wikiname= | Wiki user name of last topic update, e.g. =<nop>JohnSmith= |
41 | =$wikiusername= | Wiki user name of last topic update, like =%MAINWEB%.<nop>JohnSmith= |
42 | =$createdate= | Time stamp of topic revision 1.1 |
43 rizwank 1.1 | =$createusername= | Login name of topic revision 1.1, e.g. =jsmith= |
44 | =$createwikiname= | Wiki user name of topic revision 1.1, e.g. =<nop>JohnSmith= |
45 | =$createwikiusername= | Wiki user name of topic revision 1.1, e.g. =%MAINWEB%.<nop>JohnSmith= |
46 | =$summary= | Topic summary, with 162 characters |
47 | =$summary(50)= | Topic summary, with 50 characters |
48 | =$summary(showvarnames)= | Topic summary, with =%<nop>ALLTWIKI{...}%= variables shown as =ALLTWIKI{...}= |
49 | =$summary(noheader)= | Topic summary, with leading =---+ headers= removed%BR% __Note:__ The tokens can be combined into a comma separated list like =$summary(100, showvarnames, noheader)= |
50 | =$formname= | The name of the form attached to the topic; empty if none |
51 | =$formfield(name)= | The field value of a form field; for example, =$formfield(<nop>TopicClassification)= would get expanded to =PublicFAQ=. This applies only to topics that have a [[TWikiForms][TWikiForm]] |
52 | =$formfield(name, 10)= | Form field value, "<tt>- </tt>" hyphenated each 10 characters |
53 | =$formfield(name, 20, -<br />)= | Form field value, hyphenated each 20 characters with separator "<tt>-<br /></tt>" |
54 | =$formfield(name, 30, ...)= | Form field value, shortended to 30 characters with "<tt>...</tt>" indication |
55 | =$pattern(reg-exp)= | A regular expression pattern to extract some text from a topic (does not search meta data; use =$formfield= instead). In case of a =multiple="on"= search, the pattern is applied to the line found in each search hit.%BB% Specify a RegularExpression that covers the whole text (topic or line), which typically starts with =.*=, and must end in =.*= %BB% Put text you want to keep in parenthesis, like =$pattern(.*?(from here.*?to here).*)= %BB% Example: =$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)= extracts the email address from a bullet of format =* Email: ...= %BB% This example has non-greedy =.*?= patterns to scan for the first occurance of the Email bullet; use greedy =.*= patterns to scan for the last occurance %BB% Limitation: Do not use =.*)= inside the pattern, e.g. =$pattern(.*foo(.*)bar.*)= does not work, but =$pattern(.*foo(.*?)bar.*)= 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 |
56 | =$n= or =$n()= | New line |
57 | =$nop= or =$nop()= | Is a "no operation". This variable gets removed; useful for nested search |
58 | =$quot= | Double quote (="=). Alternatively write =\"= to escape it |
59 | =$percnt= | Percent sign (=%=) |
60 | =$dollar= | Dollar sign (=$=) |
61
62
63 ---++ Examples
64 rizwank 1.1
65
66 #SearchBulletList
67 ---+++ Bullet list showing topic name and summary
68
69 *Write this:*
70
71 =%<nop>SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header=" * <nop>*Topic: Summary:*" format=" * [<nop>[$topic]]: $summary" }%=
72
73 *To get this:*
74
75 %SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header=" * *Topic: Summary:*" format=" * [[$topic]]: $summary" }%
76
77
78 ---+++ Table showing form field values of topics with a form
79
80 In a web where there is a form that contains a =Topic<nop>Classification= field, an =Operating<nop>System= field and an =Os<nop>Version= field we could write:
81
82 =| <nop>*Topic:*<nop> | <nop>*<nop>OperatingSystem:*<nop> | <nop>*<nop>OsVersion:*<nop> |= <br />
83 =%<nop>SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" regex="on" nosearch="on" nototal="on" format="| [<nop>[$topic]] | $formfield(<nop>OperatingSystem) | $formfield(<nop>OsVersion) |" }%=
84
85 rizwank 1.1 To get this:
86
87 <table border="1" cellspacing="0" cellpadding="1">
88 <tr>
89 <th bgcolor="#99CCCC"> <strong>Topic:</strong> </th>
90 <th bgcolor="#99CCCC"> <strong>OperatingSystem:</strong> </th>
91 <th bgcolor="#99CCCC"> <strong>OsVersion:</strong> </th></tr>
92 <tr>
93 <td> <a href="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/Sandbox/IncorrectDllVersionW32PTH10DLL">IncorrectDllVersionW32PTH10DLL</a> </td><td> <a href="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/Sandbox/OsWin">OsWin</a> </td><td> 95/98 </td></tr>
94 <tr>
95 <td> <a href="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/Sandbox/WinDoze95Crash">WinDoze95Crash</a> </td>
96 <td> <a href="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/Sandbox/OsWin">OsWin</a> </td>
97 <td> 95 </td></tr>
98 </table>
99
100
101 ---+++ Extract some text from a topic using regular expression
102
103 *Write this:*
104
105 =%<nop>SEARCH{ "__Back to\:__ <nop>TWikiFAQ" scope="text" regex="on" nosearch="on" nototal="on" header="TWiki FAQs:" format=" * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [<nop>[$topic][Answer...]]" }%=
106 rizwank 1.1
107 *To get this:*
108
109 %SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" regex="on" nosearch="on" nototal="on" header="TWiki FAQs:" format=" * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%
110
111
112 ---+++ Nested Search
113
114 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.
115
116 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).
117
118 * First search:
119 * =%<nop>SEARCH{ "culture" format=" * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%=
120 * Second search. For each hit we want this search:
121 * =%<nop>SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%=
122 * 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:
123 * Use =$percnt= to escape the leading percent of the second search
124 * Use =\"= to escape the double quotes
125 * Use =$dollar= to escape the =$= of =$topic=
126 * Use =$nop= to escape the =}%= sequence
127 rizwank 1.1
128 *Write this:*
129
130 =%<nop>SEARCH{ "culture" format=" * $topic is referenced by:$n * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%=
131
132 *To get this:*
133
134 %SEARCH{ "culture" format=" * $topic is referenced by:$n * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%
135
136 __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.
137
138 ---+++ Most recently changed pages
139
140 *Write this:*
141
142 =%<nop>SEARCH{ "\.*" scope="topic" regex="on" nosearch="on" nototal="on" order="modified" reverse="on" format="| [<nop>[$topic]] | $wikiusername | $date |" limit="7" }%=
143
144 *To get this:*
145
146 %SEARCH{ "\.*" scope="topic" regex="on" nosearch="on" nototal="on" order="modified" reverse="on" format="| [[$topic]] | $wikiusername | $date |" limit="7" }%
147
148 rizwank 1.1 ---+++ Search with conditional output
149
150 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:
151 1. Specify a search which returns more hits then you need
152 1. For each search hit apply a spreadsheet formula to determine if the hit is needed
153 1. If needed, format and output the result
154 1. Else supress the search hit
155
156 This requires the TWiki:Plugins.SpreadSheetPlugin. The following example shows all topics that are up to exactly one week old.
157
158 *Write this:*
159
160 =%<nop>CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%= %BR%
161 =%<nop>SEARCH{ "." scope="topic" regex="on" nosearch="on" nototal="on" order="modified" reverse="on" format="$percntCALC{$IF($TIME($date) < $GET(weekold), <nop>, | [<nop>[$topic]] | $wikiusername | $date | $rev |)}$percnt" limit="100" }%=
162
163 * The first line sets the =weekold= variable to the serialized date of exactly one week ago
164 * The SEARCH has a deferred CALC. The =$percnt= makes sure that the CALC gets executed once for each search hit
165 * The CALC compares the date of the topic with the =weekold= date
166 * If topic is older, a =<nop>= is returned, which gets removed at the end of the TWiki rendering process
167 * Otherwise, the search hit is formatted and returned
168
169 rizwank 1.1 *To get this:*
170
171 %CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%
172 %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" }%
173
174 ---+++ Embedding search forms to return a formatted result
175
176 Use an HTML form and an embedded formatted search on the same topic. You can link them together with an =%<nop>URLPARAM{"..."}%= variable. Example:
177
178 *Write this:*
179
180 <verbatim>
181 <form action="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%">
182 Find Topics:
183 <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />
184 <input type="submit" value="Search" />
185 </form>
186 Result:
187 %SEARCH{ search="%URLPARAM{"q"}%" format=" * $web.$topic: %BR% $summary" nosearch="on" }%
188 </verbatim>
189
190 rizwank 1.1 *To get this:*
191
192 <form action="%SCRIPTURLPATH%/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%">
193 Find Topics:
194 <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />
195 <input type="submit" value="Search" />
196 </form>
197 Result:
198 %SEARCH{ search="%URLPARAM{"q"}%" format=" * $web.$topic: %BR% $summary" nosearch="on" }%
199
200 -- TWiki:Main.PeterThoeny - 26 Jul 2004
201
|