~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/data/System/SearchPatternCookbook.txt

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%META:TOPICINFO{author="ProjectContributor" date="1231502400" format="1.1" version="1"}%
 
2
%META:TOPICPARENT{name="FormattedSearch"}%
 
3
---+!! Search Pattern Cookbook
 
4
 
 
5
The Search function is very powerful. Searches using a RegularExpression play an important part of tapping Foswiki's full potential. Unfortunately RegularExpressions can be incredibly obscure to the uninitiated. 
 
6
 
 
7
Most people not familiar (enough) with Regular Expressions mostly cut and paste (and maybe tweak) from existing examples. This page intends to collect lots of examples together.
 
8
 
 
9
%TOC{ depth="2" }%
 
10
 
 
11
<!-- ============================== -->
 
12
#SearchTables
 
13
---++ Pattern 1: Extract values from a table
 
14
 
 
15
---+++ Problem definition
 
16
 
 
17
Suppose there is a topic with a table defining entries in a [[data form]]. I.e. they define select menu items in a form definition. They are then formatted like:
 
18
 
 
19
<verbatim>
 
20
| *Name* | *Type* | *Tooltip message* |
 
21
| option1 | option | |
 
22
| option2 | option | |
 
23
| option3 | option | |
 
24
</verbatim>
 
25
 
 
26
How to extract the 'name' values, i.e. 'option1', 'option2' and 'option3' and put them in a HTML form select input?
 
27
 
 
28
---+++ Solution
 
29
 
 
30
The following search pattern can be employed:
 
31
 
 
32
<verbatim>
 
33
<form>
 
34
<select>
 
35
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
 
36
</select>
 
37
</form>
 
38
</verbatim>
 
39
 
 
40
which is, in effect:
 
41
<form>
 
42
<select>
 
43
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
 
44
</select>
 
45
</form>
 
46
 
 
47
 
 
48
<!-- ============================== -->
 
49
#SearchFormClassification
 
50
---++ Pattern 2: List generated from form classification
 
51
 
 
52
---+++ Problem
 
53
 
 
54
Imagine a form-based topic classification, i.e. every page has a form with several fields. How to:
 
55
   1. create a search to display all topics where one form field is set to a certain value
 
56
   1. create a search to filter the list above based on the values of a second form field
 
57
 
 
58
---+++ Test case
 
59
 
 
60
In practice: %BR%
 
61
Image a form with two fields:
 
62
   * !TopicClassification = One, Two or Three
 
63
   * !TopicStatus = Test or Final
 
64
 
 
65
We will:
 
66
   1. List all topics where the !TopicClassification field is set to 'Two'
 
67
   2. Enable the user to filter this list based on the values of !TopicStatus
 
68
 
 
69
---+++ Solution
 
70
 
 
71
<verbatim>
 
72
%SEARCH{"TopicClassification='%URLPARAM{type}%'" type="query" nosearch="on" 
 
73
format="   * $topic - <font face=\"arial,helvetica\" size=\"1\"> 
 
74
_last modified by_ $wikiusername _on_ $date </font> %BR% &nbsp;&nbsp;&nbsp; 
 
75
<font face=\"arial,helvetica\" size=\"1\"> $formfield(TopicStatus) </font>" 
 
76
sort="topic"}%
 
77
</verbatim>
 
78
 
 
79
The filtering select dialogue is created as in Pattern 1:
 
80
 
 
81
<verbatim>
 
82
%STARTSIDEBAR%
 
83
*Filter:* %BR%
 
84
<form name="selectType" action="%SCRIPTURLPATH{"view"}%/%WEB%/" >
 
85
<select name="type" size="1" onchange="document.location=this.value;"> 
 
86
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="%WEB%" type="regex" 
 
87
multiple="on" nosearch="on" nototal="on" format="<option value=%BASETOPIC%?type=$pattern(^\| *(.*?) *\|.*)>$pattern(^\| *(.*?) *\|.*)</option>" }% 
 
88
<option value=%BASETOPIC%>All pages</option> </select>
 
89
</form>
 
90
%STOPSIDEBAR% 
 
91
</verbatim>
 
92
 
 
93
This will create similar functionality as Foswiki:Extensions.TopicClassificationAddOn
 
94
 
 
95
 
 
96
<!-- ============================== -->
 
97
#SearchTopicParent
 
98
---++ Pattern 3: Extract the parent of a given topic
 
99
 
 
100
---+++ Problem
 
101
 
 
102
How to get to the parent of the current topic to display on the page?
 
103
 
 
104
---+++ Solution 1: Using META
 
105
 
 
106
Use the META macro:
 
107
 
 
108
=%<nop>META{ "parent" dontrecurse="on" }%=
 
109
 
 
110
 
 
111
<!-- ============================== -->
 
112
#SearchTopicChildren
 
113
---++ Pattern 4: Show all Children of a given topic
 
114
 
 
115
---+++ Problem
 
116
 
 
117
How to get to the list of all children of the current topic to display on the page?
 
118
 
 
119
---+++ Solution
 
120
 
 
121
The parent information is stored in the topic meta data. Do a SEARCH to find all topic parent meta data pointing to the current topic:
 
122
 
 
123
<verbatim>
 
124
Children:
 
125
%SEARCH{ "parent.name='%TOPIC%'" type="query" nonoise="on" format="[[$topic]]" separator=", " }%
 
126
</verbatim>
 
127
 
 
128
*Note:* Replace =%<nop>TOPIC%= with =%<nop>BASETOPIC%= if you put this SEARCH into the skin or a sidebar.
 
129
 
 
130
 
 
131
<!-- ============================== -->
 
132
#SearchPublicWebsList
 
133
---++ Pattern 5: Search and display the home topics of public webs in a list
 
134
 
 
135
---+++ Problem
 
136
 
 
137
How to find and display public webs in a drop down list box.
 
138
 
 
139
---+++ Solution
 
140
 
 
141
<verbatim>
 
142
<form>
 
143
<select name="topic">
 
144
<option value="%TOPIC%">Select...</option>
 
145
%SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }%
 
146
</select>
 
147
<input type="submit"  value="Go" />
 
148
</form>
 
149
</verbatim>
 
150
 
 
151
---+++ Test case
 
152
 
 
153
Public webs can be found with the %<nop>WEBLIST% macro.
 
154
 
 
155
<form>
 
156
<select name="topic">
 
157
<option value="%TOPIC%">Select...</option>
 
158
%WEBLIST{ format="<option value=\"$name.%HOMETOPIC%\">$name</option>" webs="public" separator=" " }%
 
159
</select>
 
160
<input type="submit" value="Go" />
 
161
</form>
 
162
 
 
163
 
 
164
<!-- ============================== -->
 
165
#SearchBulletList
 
166
---++ Pattern 6: Create a select box with values from a bullet list
 
167
 
 
168
---+++ Problem
 
169
 
 
170
We have a topic with a bullet list with category names. In another topic we want to offer these values in a select box dropdown.
 
171
 
 
172
For example, !CategoryList has:
 
173
   * Clients
 
174
   * People
 
175
   * Rooms
 
176
   * Buildings
 
177
 
 
178
---+++ Solution
 
179
 
 
180
The following search pattern can be employed:
 
181
 
 
182
<verbatim>
 
183
<select name="type">
 
184
<option>Select category...</option>
 
185
%SEARCH{"   *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="<option>$pattern(.*   \*\s*([^\n]*).*)</option>"}%
 
186
</select>
 
187
</verbatim>
 
188
 
 
189
To render the bullet list as a comma-separated list, use the =separator= parameter:
 
190
<verbatim>
 
191
%SEARCH{"   *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" separator="," format="$pattern(.*   \*\s*([^\n]*).*)"}%
 
192
</verbatim>
 
193
 
 
194
 
 
195
<!-- ============================== -->
 
196
#SearchNamedBulletList
 
197
---++ Pattern 7: Extract a value from a named bullet list item
 
198
 
 
199
---+++ Problem
 
200
 
 
201
Display the user name in the user's topic title
 
202
 
 
203
---+++ Solution
 
204
 
 
205
Search for the =Name:= entry.
 
206
 
 
207
<verbatim>
 
208
%SEARCH{"   * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.*   \* Name: ([^\n]*).*)"}%
 
209
</verbatim>
 
210
 
 
211
---+++ Test case
 
212
 
 
213
To create a test case, we will put a name entry here:
 
214
 
 
215
   * Name: John Doe
 
216
 
 
217
Search result:
 
218
 
 
219
%SEARCH{"   * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.*   \* Name: ([^\n]*).*)"}%
 
220
 
 
221
<!-- ============================== -->
 
222
#MovedTopics
 
223
---++ Pattern 8: Search all topics that have been moved
 
224
 
 
225
---+++ Problem
 
226
 
 
227
How would I go about listing all moved topics ?
 
228
 
 
229
---+++ Solution
 
230
 
 
231
Search for the 'moved' meta data. Type this: 
 
232
 
 
233
=Moved topics: %<nop>SEARCH{ "moved.to!=''" type="query" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%=
 
234
 
 
235
---
 
236
*Related Topics:* UserDocumentationCategory, SearchHelp, [[Macros]], FormattedSearch, RegularExpression
 
237