Tags:
create new tag
, view all tags
Hello Everyone!

I just wrote a little patch to extend the functionality of the %INCLUDE variable. My problem was that I wanted to use a topic for the warning instead of a given static text. Using the $topic was to limiting.

Example to display a generic side-bar if the user has not created one:

<div class="twikiLeftBarPersonal">
%INCLUDE{"%MAINWEB%.%WIKINAME%LeftBar" warn="topic:%MAINWEB%.GenericLeftBar"}%
</div>

The patch is a very simple one inside TWiki.pm and adjusts handleIncludeFile. Feel free to send me comments if this is a feasible approach. The patch was done against the stable version from last Thursday (say 15.09.2004).

 
--- ../../cgi-bin/twiki2/lib/TWiki.pm   Mon Sep 20 16:53:42 2004
+++ TWiki.pm    Mon Sep 20 19:47:15 2004
@@ -2050,12 +2050,52 @@
 
         # give up, file not found
         $warn = TWiki::Prefs::getPreferencesValue( "INCLUDEWARNING" ) unless( $warn );
-        if( $warn =~ /^on$/i ) {
+

+       # the warning is refering to another topic
+       if ($warn =~ /^topic:(.*)$/i ) {
+           $incfile = $1;
+
+           if( $incfile =~ /^http\:/ ) {
+               # include web page
+               return handleIncludeUrl( $incfile, $pattern, $theWeb, $theTopic );
+           }
+           
+           # CrisBailiff, PeterThoeny 12 Jun 2000: Add security
+           $incfile =~ s/$securityFilter//go;    # zap anything suspicious
+           if( $doSecureInclude ) {
+               # Filter out ".." from filename, this is to
+               # prevent includes of "../../file"
+               $incfile =~ s/\.+/\./g;
+           } else {
+               # danger, could include .htpasswd with relative path
+               $incfile =~ s/passwd//gi;    # filter out passwd filename
+           }
+
+           # check for topic
+           $fileName = "$dataDir/$theWeb/$incfile.txt";      # TopicName
+           last TRY if( -e $fileName );
+           my $incwebfile = $incfile;
+           $incwebfile =~ s/\.([^\.]*)$/\/$1/;
+           $fileName = "$dataDir/$incwebfile.txt";           # Web.TopicName
+           last TRY if( -e $fileName );
+           $fileName = "$dataDir/$theWeb/$incfile";          # TopicName.txt
+           last TRY if( -e $fileName );
+           $fileName = "$dataDir/$incfile";                  # Web/TopicName.txt
+           last TRY if( -e $fileName );
+
+           # The recursive topic include always gives a warning
+            return showError( "Warning: Can't recusively INCLUDE <nop>$incfile, topic not found" );
+
+       } elsif( $warn =~ /^on$/i ) {
+
             return showError( "Warning: Can't INCLUDE <nop>$incfile, topic not found" );
+
         } elsif( $warn && $warn !~ /^(off|no)$/i ) {
+
             $incfile =~ s/\//\./go;
             $warn =~ s/\$topic/$incfile/go;
             return $warn;
+
         } # else fail silently
         return "";
     }

-- ChristopherOezbek - 20 Sep 2004

Hey! I was thinking of exactly this earlier today. This way the personal left bar can decide to include the standard left bar or not, and place it above or below.

The problem I see with the above implementation is what if the second topic doesn't exist?

A more flexable solution would be to handle alttopic and altpattern parameters, leaving the warn parameter as a fallback. You could even make it handle multiple sets of numbered parameters: alttopic1, altpattern1, alttopic2, altpattern2 etc.

-- SamHasler - 20 Sep 2004

Christopher's proposal is easy to understand.

May I suggest a small syntax change to make it consistent with other TWiki markup: Change "topic:Web.TopicName" syntax to "$topic(Web.TopicName)". Sam, the issue you mentioned is covered by the error reporting when the warn topic does not exist.

The code could be made cleaner by not duplicating too much code, either by a recursive call (with a "final include" flag in parameter), or with an extra function call.

-- PeterThoeny - 23 Sep 2004

Main.GenericLeftBar is hardly a warning topic. I'd like to see an implementation that reflects the way the functionality is going to be used, which allows for more flexability than the example usage above requires yet still allows a per include configurable warning should no topic exist. I'm sure someone has a use for multiple alternative includes.

Am I misunderstanding the code or is TRY out of place in the above patch?

-- SamHasler - 23 Sep 2004

Peter: I was afraid of doing a recursive call, because I understand the system too little. But I agree with Sam that alttopic1,2,... might be a good idea.

I can change the regular expression to reflect the syntax $topic(TopicName).

Sam: The label TRY is indeed outside the patch-information (a little bit above).

-- ChristopherOezbek - 29 Sep 2004

i need to revisit this patch because i wanted to have a custom WebTopBar per web. at first, i tried changing %TMPL:DEF{"topbar"}% so that instead of reading %INCLUDE{"%TWIKIWEB%.WebTopBar"}% , i tried changing it to %INCLUDE{"WebTopBar" warn="%INCLUDE{%TWIKIWEB%.WebTopBar}%"}% , but that didn't work. would a better patch be to evaluate the warn parameter or should this patch be updated to the style that PeterThoeny requested?

-- WillNorris - 18 Oct 2004

is this solved by StrategyToConditionallyIncludeTopic?

-- WillNorris - 19 Oct 2004

I don't know if there is a way to query using CALC whether a topic exists (probably). If this is possible, then this patch becomes obsolete as the conditional is a much cleaner solution.

-- ChristopherOezbek - 21 Oct 2004

Edit | Attach | Watch | Print version | History: r12 < r11 < r10 < r9 < r8 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r12 - 2006-01-03 - 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-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.