wiki-archive/twiki/data/TWiki/SearchPatternCookbook.txt,v

629 lines
20 KiB
Plaintext
Raw Normal View History

head 1.4;
access;
symbols;
locks; strict;
comment @# @;
1.4
date 2007.01.16.04.12.01; author TWikiContributor; state Exp;
branches;
next 1.3;
1.3
date 2006.06.25.16.26.26; author TWikiContributor; state Exp;
branches;
next 1.2;
1.2
date 2006.04.01.05.55.10; author TWikiContributor; state Exp;
branches;
next 1.1;
1.1
date 2006.02.01.12.01.18; author TWikiContributor; state Exp;
branches;
next ;
desc
@new-topic
@
1.4
log
@buildrelease
@
text
@%META:TOPICINFO{author="TWikiContributor" date="1168735119" format="1.1" version="4"}%
%META:TOPICPARENT{name="FormattedSearch"}%
---+!! Search Pattern Cookbook
The Search function in TWiki is very powerful. Especially searches using a RegularExpression play an important part of tapping TWiki's full potential. Unfortunately RegularExpressions can be incredibly obscure to the uninitiated.
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.
%TOC{ depth="2" }%
<!-- ============================== -->
#SearchTables
---++ Pattern 1: Extract values from a table
---+++ Problem definition
Suppose there is a topic with a table defining entries in a !TWikiForm. I.e. they define select menu items in a form template. They are then formatted like:
<verbatim>
| *Name* | *Type* | *Tooltip message* |
| option1 | option | |
| option2 | option | |
| option3 | option | |
</verbatim>
How to extract the 'name' values, i.e. 'option1', 'option2' and 'option3' and put them in a HTML form select input?
---+++ Solution
The following search pattern can be employed:
<verbatim>
<form>
<select>
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
</select>
</form>
</verbatim>
which is, in effect:
<form>
<select>
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
</select>
</form>
<!-- ============================== -->
#SearchFormClassification
---++ Pattern 2: List generated from form classification
---+++ Problem
Imagine a TWiki form-based topic classification, i.e. every page has a form with several fields. How to:
1. create a search to display all topics where one form field is set to a certain value
1. create a search to filter the list above based on the values of a second form field
---+++ Test case
In practice: %BR%
Image a TWiki form with two fields:
* !TopicClassification = One, Two or Three
* !TopicStatus = Test or Final
We will:
1. List all topics where the !TopicClassification field is set to 'Two'
2. Enable the user to filter this list based on the values of !TopicStatus
---+++ Solution
<verbatim>
%SEARCH{"[T]opicClassification.*value\=.*Two;[T]opicStatus.*value\=.*%URLPARAM{type}%"
type="regex" casesensitive="on" nosearch="on"
format=" * $topic - <font face=\"arial,helvetica\" size=\"1\">
_last modified by_ $wikiusername _on_ $date </font> %BR% &nbsp;&nbsp;&nbsp;
<font face=\"arial,helvetica\" size=\"1\"> $formfield(TopicStatus) </font>"
sort="topic"}%
</verbatim>
The filtering select dialogue is created as in Pattern 1:
<verbatim>
%STARTSIDEBAR%
*Filter:* %BR%
<form name="selectType" action="%SCRIPTURLPATH{"view"}%/%WEB%/" >
<select name="type" size="1" onchange="document.location=this.value;">
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="%WEB%" type="regex"
multiple="on" nosearch="on" nototal="on" format="<option value=%INCLUDINGTOPIC%?type=$pattern(^\| *(.*?) *\|.*)>$pattern(^\| *(.*?) *\|.*)</option>" }%
<option value=%INCLUDINGTOPIC%>All pages</option> </select>
</form>
%STOPSIDEBAR%
</verbatim>
This will create similar functionality as TWiki:Plugins.TopicClassificationAddOn
<!-- ============================== -->
#SearchUsernames
---++ Pattern 3: Creating lists of TWiki usernames
---+++ Problem
How to populate a list box with all usernames of registered TWiki users
---+++ Solution 1: Appropriate for Sep 2004 TWiki (Cairo)
<verbatim>
<form name="testing" action="%SCRIPTURLPATH{"view"}%/%MAINWEB%" method="get">
<select name="topic">
<option>Select user...</option>
%SEARCH{ "Name:;Email:;Country:" web="%MAINWEB%" type="regex" nosearch="on" nototal="on" format="<option>$topic</option>" }%
</select>
<input type="submit" value="Go" />
</form>
</verbatim>
Which expands to this: (here limited to all Z* users because TWiki.org has so many)
<form name="testing" action="%SCRIPTURLPATH{"view"}%/%MAINWEB%" method="get">
<select name="topic">
<option>Select user...</option>
%SEARCH{ "Name:;Email:;Country:" web="%MAINWEB%" type="regex" topic="Z*" nosearch="on" nototal="on" format="<option>$topic</option>" }%
</select>
<input type="submit" value="Go" />
</form>
This searches all topics in the Main web that contain "Name", "Email" and "Country" bullets. Alternatively, do a %TWIKIWEB%.FormattedSearch with =multiple="on"= on the [[%MAINWEB%.TWikiUsers]] topic.
---+++ Solution 2: As Solution 1, but with possibility for multi-selecting usernames
The example of Solution 1 produces the list box. Add a MULTIPLE to the _select_ statement, i.e.:
<verbatim>
<select name="topic" size="2" MULTIPLE>
</verbatim>
Please note that the Search pattern is unchanged compared to Solution 1. The change is in the HTML form element.
The abovementioned modification is, in effect:
<form name="testing" action="%SCRIPTURLPATH{"view"}%/%MAINWEB%" method="get">
<select name="topic" size="2" MULTIPLE>
<option>Select user...</option>
%SEARCH{ "Name:;Email:;Country:" web="%MAINWEB%" type="regex" topic="Z*" nosearch="on" nototal="on" format="<option>$topic</option>" }%
</select>
<input type="submit" value="Go" />
</form>
---+++ Solution 3: Appropriate for TWiki 4 (Dakar)
When the User information is stored in a UserForm (as is default in Dakar) then this list can be generated as follows:
<verbatim>
<form name="testing" action="%SCRIPTURLPATH{"view"}%/%MAINWEB%" method="get">
<select name="topic">
<option>Select user...</option>
%SEARCH{"%META:FORM.*[U]serForm" web="%MAINWEB%" type="regex" casesensitive="on" nosearch="on" format="<option>$topic</option>" sort="topic" excludetopic="Test*, TWiki*"}%
</select>
<input type="submit" value="Go" />
</form>
</verbatim>
In the above example:
* ==META:FORM.*[U]serForm== will search for all topics with a UserForm attached - change this if you have a different form where userdata is stored. Please note that this search does not actually extract anything from the form - it just uses it to identify the appropriate pages
* ==excludetopic="Test*, TWiki*"== allows to skip all topics starting with Test and TWiki, such as !TestUser or !TWikiAdmin. Use this if you have any special users who you do not want appearing in this list
<!-- ============================== -->
#SearchTopicParent
---++ Pattern 4: Extract the parent of a given topic
---+++ Problem
How to get to the parent of the current topic to display on the page?
---+++ Solution 1: Using META
Since TWiki 4.0 you can now use the META variable:
=%<nop>META{ "parent" dontrecurse="on" }%=
---+++ Solution 2: Using !SpreadSheetPlugin
You might think that the following Search would do the trick:
<verbatim>
%SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nonoise="on" format=" * $parent" }%
</verbatim>
However, the =$parent= link fails if the topic has no parent set (=$parent= will be empty). You can use some TWiki:Plugins/SpreadSheetPlugin magic to conditionally link to the parent or to =WebHome=:
<verbatim>
$percntCALC{$IF($EXACT($parent,),<nop>,$NOP( * $parent))}$percnt
</verbatim>
So the total Search query to find a topic's parent topic is:
<verbatim>
%SEARCH{ "^%BASETOPIC%$" scope="topic" type="regex" nonoise="on" format="$percntCALC{$IF($EXACT($parent,),<nop>,$NOP( * $parent))}$percnt" }%
</verbatim>
---++++ Test Case
The parent topic of this topic is:
%SEARCH{ "^%BASETOPIC%$" scope="topic" type="regex" nonoise="on" format="$percntCALC{$IF($EXACT($parent,),<nop>,$NOP( * $parent))}$percnt" }%
---+++ Solution 3: Using IF statement
This pattern can be rewritten using =%<nop>IF%=, removing the dependency on !SpreadSheetPlugin:
<verbatim>
%SEARCH{ "^%BASETOPIC%$" web="%BASEWEB%" scope="topic" type="regex" nonoise="on" format="$percntIF{$quot$parent$quot then=$quot * $parent$quot else=$quot<nop>$quot}$percnt" }%
</verbatim>
---++++ Test Case
The parent topic of this topic is: %BR%
%SEARCH{ "^%BASETOPIC%$" web="%BASEWEB%" scope="topic" type="regex" nonoise="on" format="$percntIF{$quot$parent$quot then=$quot * $parent$quot else=$quot<nop>$quot}$percnt" }%
<!-- ============================== -->
#SearchTopicChildren
---++ Pattern 5: Show all Children of a given topic
---+++ Problem
How to get to the list of all children of the current topic to display on the page?
---+++ Solution
The parent information is stored in the META:TOPICPARENT meta data. Do a SEARCH to find all topic parent meta data pointing to the current topic:
<verbatim>
Children:
%SEARCH{ "META\:TOPICPARENT.*\"%TOPIC%\"" type="regex" nonoise="on" format="[[$topic]]" separator=", " }%
</verbatim>
__Note:__ Replace =%<nop>TOPIC%= with =%<nop>BASETOPIC%= if you put this SEARCH into the skin or a sidebar.
<!-- ============================== -->
#SearchPublicWebsList
---++ Pattern 6: Search and display the home topics of public webs in a list
---+++ Problem
How to find and display public webs in a drop down list box.
---+++ Solution
_Thanks to TWiki:Main.PeterThoeny for these solutions._
<verbatim>
<form>
<select name="topic">
<option value="%TOPIC%">Select...</option>
%SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }%
</select>
<input type="submit" value="Go" />
</form>
</verbatim>
---+++ Test case
Public webs of TWiki.
<form>
<select name="topic">
<option value="%TOPIC%">Select...</option>
%SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }%
</select>
<input type="submit" value="Go" />
</form>
%T% For private webs, or any other webs you wish to exclude from the display, use "on" for the =Exclude web from a web="all" search= setting in the relevant web's !WebPreferences topic.
---+++ Alternative solution
This result can also be accomplished with the %<nop>WEBLIST% variable.
<form>
<select name="topic">
<option value="%TOPIC%">Select...</option>
%WEBLIST{ format="<option value=\"$name.%HOMETOPIC%\">$name</option>" webs="public" separator=" " }%
</select>
<input type="submit" value="Go" />
</form>
<!-- ============================== -->
#SearchBulletList
---++ Pattern 7: Create a select box with values from a bullet list
---+++ Problem
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.
For example, !CategoryList has:
* Clients
* People
* Rooms
* Buildings
---+++ Solution
The following search pattern can be employed:
<verbatim>
<select name="type">
<option>Select category...</option>
%SEARCH{" *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="<option>$pattern(.* \*\s*([^\n]*).*)</option>"}%
</select>
</verbatim>
To render the bullet list as a comma-separated list, use the =separator= parameter:
<verbatim>
%SEARCH{" *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" separator="," format="$pattern(.* \*\s*([^\n]*).*)"}%
</verbatim>
<!-- ============================== -->
#SearchNamedBulletList
---++ Pattern 8: Extract a value from a named bullet list item
---+++ Problem
Display the user name in the user's topic title
---+++ Solution
Search for the =Name:= entry.
<verbatim>
%SEARCH{" * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
</verbatim>
---+++ Test case
To create a test case, we will put a name entry here:
* Name: John Doe
Search result:
%SEARCH{" * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
<!-- ============================== -->
#SearchMetaData
---++ Pattern 9: Search for Form and Meta data: explained
---+++ Problem
Below is an example of a search that searches form data. The questions are:
* why is this searching the metadata, shouldn't it just search the text?
* what is the meaning of the =td..td= in the search expression?
<pre>
%<nop>SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" type="regex"
nosearch="on" nototal="on" format="| [[$topic]]&lt;br /> ($date - $rev -
[[%<nop>SCRIPTURLPATH{rdiff}%/$web/$topic][Diffs]]) |"}%
</pre>
---+++ Solution
%SEARCH depends on grep, and grep searches the whole file, including the meta data.
An example meta data form field is:
<pre>
%<nop>META:<nop>FIELD{name="OperatingSystem" title="OperatingSystem" value="Os<nop>Win"}%
</pre>
So a search for a form field could look like:
<pre>
%<nop>SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" type="regex" ... }%
</pre>
* Using square brackets is a trick to avoid a hit on the topic doing the search.
* The =.*= indicate that there can be any number of any character between =OperatingSystem= and =value= in the (whole) file
Now the original file format of the category table (the predecessor of the TWiki forms) looks like this:
<verbatim>
<td valign="top" align="right"> OperatingSystem: </td><td> OsWin </td>
</verbatim>
The following search finds topics in the old and new format:
<pre>
%<nop>SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" type="regex" ... }%
</pre>
The =td..td= matches =td&lt;&gt;td=; a simple search on ="[O]peratingSystem.*[O]sWin"= could find a hit in the topic text by coincidence.
A simple =%<nop>SEARCH{ "[O]peratingSystem.*value\=<nop>.*[O]sWin" ...}%= search is sufficient if you do not have topics in the old format.
<!-- ============================== -->
#MovedTopics
---++ Pattern 10: Search all topics that have been moved
---+++ Problem
How would I go about listing all moved topics ?
---+++ Solution
Search for the META:TOPICMOVED meta data. Type this:
=Moved topics: %<nop>SEARCH{ "%META\:TOPICMOVED" type="regex" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%=
to get this (limited to 10 results):
Moved topics: %SEARCH{ "%META\:TOPICMOVED" type="regex" format="$topic, " nosearch="on" noheader="on" nosummary="on" limit="10"}%
__Related Topics:__ UserDocumentationCategory, SearchHelp, TWikiVariables#VarSEARCH, FormattedSearch, RegularExpression
-- __Contributors:__ TWiki:Main.AntonAylward, TWiki:Main.ArthurClemens, TWiki:Main.JosMaccabiani, TWiki:Main.PeterThoeny, TWiki:Main.SueLocke
@
1.3
log
@buildrelease
@
text
@d1 2
a2 1
%META:TOPICINFO{author="TWikiContributor" date="1127406241" format="1.1" version="3"}%
d9 1
a9 1
%TOC%
d11 2
d16 1
d29 1
d35 1
a35 1
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" regex="on" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
d43 1
a43 1
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" regex="on" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
a45 1
%BR%
d47 3
d53 1
d59 1
d73 1
a73 1
regex="on" casesensitive="on" nosearch="on"
d87 1
a87 1
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="%WEB%" regex="on"
a94 1
%BR%
d96 3
d130 1
d148 1
d155 1
a155 1
%SEARCH{"%META:FORM.*[U]serForm" web="%MAINWEB%" regex="on" casesensitive="on" nosearch="on" format="<option>$topic</option>" sort="topic" excludetopic="Test*, TWiki*"}%
d165 3
d174 8
a181 1
---+++ Solution
d185 1
a185 1
%SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="[[$parent][parent_link]]" }%
d188 5
a192 1
However, the =[<nop>[$parent][parent_link]]= link fails if the topic has no parent set (=$parent= will be empty). You can use some Plugins.SpreadSheetPlugin magic to conditionally link to the parent or to =WebHome=: =[<nop>[$percntCALC{$IF($EXACT($parent,), %<nop>HOMETOPIC%, $parent)}$percnt][parent_link]]=
d196 12
a207 1
%SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="[<nop>[$percntCALC{$IF($EXACT($parent,), <nop>%HOMETOPIC%, $parent)}$percnt][parent_link]]" }%
d210 3
a212 2
---+++ Test Case
The parent topic of this topic is: %SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="$percntCALC{$IF($EXACT($parent,), %HOMETOPIC%, $parent)}$percnt" }%
d214 24
a237 1
---++ Pattern 5: Search and display the home topics of public webs in a list
d283 35
a317 1
---++ Pattern 6: Extract a value from a bullet list
d328 1
a328 1
%SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
d339 6
a344 1
%SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
a345 1
---++ Pattern 7: Search for Form and Meta data: explained
d347 1
d353 1
a353 1
%<nop>SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" regex="on"
d359 1
d368 1
a368 1
%<nop>SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" regex="on" ... }%
d379 1
a379 1
%<nop>SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" regex="on" ... }%
d386 5
a390 1
---++ Pattern 8: Search all topics that have been moved
d392 1
d396 1
d399 1
a399 1
=Moved topics: %<nop>SEARCH{ "%META\:TOPICMOVED" regex="on" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%=
d403 1
a403 4
Moved topics: %SEARCH{ "%META\:TOPICMOVED" regex="on" format="$topic, " nosearch="on" noheader="on" nosummary="on" limit="10"}%
---++ Contributors
d405 1
a405 1
TWiki:Main.AntonAylward, TWiki:Main.ArthurClemens, TWiki:Main.JosMaccabiani, TWiki:Main.PeterThoeny, TWiki:Main.SueLocke
d407 1
a407 1
__Related Topics:__ UserDocumentationCategory
@
1.2
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1127406241" format="1.1" version="2"}%
d88 1
a88 1
---++ Pattern 3a: listbox with all user names
d94 1
a94 1
---+++ Solution
d118 2
a119 8
---++ Pattern 3b: listbox with all user names - select multiple names
---+++ Problem
Suppose you want to send mail from a form on topic page to a selected list of __multiple__ %MAINWEB%.TWikiUsers
---+++ Solution
The example of Pattern 3a produces the list box. Add a MULTIPLE to the _select_ statement, i.e.:
d124 1
a124 1
Please note that the Search pattern is unchanged compared to Pattern 3a. The change is in the HTML form element.
d126 1
a126 2
---+++ Test case
The Search pattern 3a with the abovementioned modification is, in effect:
d135 16
@
1.1
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1127406241" format="1.1" version="1"}%
d47 2
a48 2
1. create a search to display all topics where one form field is set to a certain value
1. create a search to filter the list above based on the values of a second form field
d53 2
a54 2
* !TopicClassification = One, Two or Three
* !TopicStatus = Test or Final
d57 2
a58 2
1. List all topics where the !TopicClassification field is set to 'Two'
2. Enable the user to filter this list based on the values of !TopicStatus
d65 1
a65 1
format=" * $topic - <font face=\"arial,helvetica\" size=\"1\">
d223 1
a223 1
%SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
d230 1
a230 1
* Name: John Doe
d234 1
a234 1
%SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
d239 2
a240 2
* why is this searching the metadata, shouldn't it just search the text?
* what is the meaning of the =td..td= in the search expression?
d259 2
a260 2
* Using square brackets is a trick to avoid a hit on the topic doing the search.
* The =.*= indicate that there can be any number of any character between =OperatingSystem= and =value= in the (whole) file
@