Tags:
create new tag
, view all tags

Implemented: Formatted Search with Conditional Output

This is a small enhancement that allows conditional SEARCHes based on spreadsheet CALCulations.

Example

This is a silly example that shows the last few topics that have been edited just once, e.g. have version 1.1.

This SEARCH:
%SEARCH{ "." regex="on" order="modified" reverse="on" limit="100" nosearch="on" nototal="on" format="$percntCALC{$IF($NOT($EXACT($rev, 1.1)), <nop>, | [[$topic]] | $wikiusername | $date | $rev |)}$percnt" }%

Renders as:

Topic Changed by Changed on Rev

The $percntCALC{}$percnt is a deferred SpreadSheetPlugin %CALC{}% that executes for each search hit. In this example it returns a table row if the version of the topic is 1.1, else it returns a <nop>. TWiki removes <nop> tags at the end of the rendering process.

This hack can be use this for all kind of things, say, to show all topics that are up to exactly one week old. Or create a report that shows records with invalid fields, etc...

There is one catch however, which is best shown in steps. The SEARCH and conditional CALC generate a table like this:

| first valid row | |
| second valid row | next is suppressed |
<nop>
| 3rd valid row | next 2 are suppressed |
<nop>
<nop>
| etc | etc |

The TWiki table renderer (or TablePlugin if installed) turns this into:

first valid row  
second valid row next is suppressed
3rd valid row next 2 are suppressed
etc etc

That is, the table gets split apart because it is too late to remove the standalone <nop> lines at the end of the rendering process.

The patch below removes standalone <nop> lines at the end of the variable expansion, e.g. before the table rendering. Therefore you get consecutive rows as expected:

first valid row  
second valid row next is suppressed
3rd valid row next 2 are suppressed
etc etc

Patch to remove nop tags

Index: TWiki.pm
===================================================================
RCS file: /cvsroot/twiki/twiki/lib/TWiki.pm,v
retrieving revision 1.336
diff -c -r1.336 TWiki.pm
*** TWiki.pm    1 May 2004 06:54:06 -0000       1.336
--- TWiki.pm    1 May 2004 06:56:41 -0000
***************
*** 154,160 ****

  # ===========================
  # TWiki version:
! $wikiversion      = "29 Apr 2004";

  # ===========================
  # Key Global variables, required for writeDebug
--- 154,160 ----

  # ===========================
  # TWiki version:
! $wikiversion      = "30 Apr 2004";

  # ===========================
  # Key Global variables, required for writeDebug
***************
*** 3037,3042 ****
--- 3037,3046 ----
      $text =~ s/%TOC{([^}]*)}%/&handleToc($text,$theTopic,$theWeb,$1)/ge;
      $text =~ s/%TOC%/&handleToc($text,$theTopic,$theWeb,"")/ge;

+     # Codev.FormattedSearchWithConditionalOutput: remove <nop> lines, possibly introduced by
+     # SEARCHes with conditional CALC. This needs to be done after CALC and before table rendering
+     $text =~ s/^<nop>\r?\n//gm;
+
      # Ideally would put back in getRenderedVersion rather than here which would save removing
      # it again!  But this would mean altering many scripts to pass back verbatim
      $text = putBackVerbatim( $text, "verbatim", @verbatim );

This change is in TWikiAlphaRelease and at TWiki.org.

Contributors:
-- PeterThoeny - 30 Apr 2004

Thanks to RobHoffman for the idea. At work we use it for a TWikiApplication where we had to create two reports of records of some type, one showing "completed", one "incomplete" records. The SEARCH alone was not sufficient since the criteria for "completed" is based on a calculation.

-- PeterThoeny - 30 Apr 2004

Docs complete, added a Search with conditional output section to FormattedSearch

-- PeterThoeny - 02 Apr 2004

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2004-05-20 - PeterThoeny
 
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2015 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.