[Home]WikiPatches/TemplatePages

UseModWiki | WikiPatches | RecentChanges | Preferences

I wrote this patch for UseModWiki at work, as part of an informal bug tracking system (more info in the introduction to /ExtendedSearchSyntax). The patch adds a "use template" selection box to the top of the edit form. The templates themselves are all SubPages of a particular page that is specified in the code (WikiTemplates by default).

-- DavidMcNicol

See also
/ExtendedSearchSyntax

Syntax

There are a few standard substitutions that can be performed on the templates:

Code

The following sections of code should be grafted into your wiki script.

Configuration section

This should be added at the end of the configuration section, before the "you should not have to change anything below this line" comment.

# == WikiTemplates patch =================================================
use vars qw( $WTName $WTOverwriteExisting );
$WTName = "WikiTemplates";   # name of the page where templates are stored.
$WTOverwriteExisting = 1;    # 1 = templates can replace existing content.
# ========================================================================

The subroutines

These subroutines can be added anywhere. I put it after the DoEdit subroutine, to keep everything in the one place:

# == WikiTemplates patch =================================================
sub GetTemplateForm {

  my $id = shift;

  my @templates = &GetTemplateList();

  my $result = &GetFormStart() . "\n";

  $result .= $q->popup_menu(-name=>'template', -values=>\@templates);
  $result .= &GetHiddenValue("action", "edit") . "\n";
  $result .= &GetHiddenValue("id", $id) . "\n";

  $result .= $q->submit(-name=>'use', -value=>T('Use template')) . "\n";

  $result .= $q->endform();

  return $result;
}

sub GetTemplateList {

  my @list = T( "None" );

  my $dir = $PageDir . "/" . &GetPageDirectory($WTName) . "/" . $WTName;

  opendir(TEMPLATES, $dir);
  my @files = readdir(TEMPLATES);
  closedir(TEMPLATES);

  foreach my $file ( @files ) {
    next if $file =~ /^\./;
    push( @list, $file ) if $file =~ s/\.db$//;
  }

  return @list;
}

sub GetTemplateText {

  my $template = shift;
  my $id = shift;

  return unless $template;
  return if $template eq T("None");

  my $page = "$WTName/$template";

  &OpenPage( $page );
  &OpenDefaultText();

  return &PerformTemplateSubstitutions( $Text{'text'}, $id );
}

sub PerformTemplateSubstitutions {

  my $text = shift;
  my $id = shift;

  my $today = &CalcShortDayNow();
  my $username = &GetParam("username", "");

  $text =~ s/%%%\s*date\s*%%%/$today/gi;
  $text =~ s/%%%\s*this\s*%%%/$id/gi;
  $text =~ s/%%%\s*user\s*%%%/$username/gi;

  return $text;
}

sub CalcShortDay {
  my ($ts) = @_;

  $ts += $TimeZoneOffset;
  my ($sec, $min, $hour, $mday, $mon, $year) = localtime($ts);

  return sprintf( "%d-%0.2d-%0.2d", $year + 1900, $mon + 1, $mday );
}

sub CalcShortDayNow {
  return CalcShortDay($Now);
}
# ========================================================================

Modifying DoEdit

These sections of code should be added to the DoEdit subroutine. Their position is important.

This first section fetches a template if it has been requested. It should be added before the call to &OpenPage($id), which is around 17 lines below the start of the subroutine:

# == WikiTemplates patch =================================================
  my $templateText = "";
  if ( my $template = $q->param("template") ) {
    $templateText = &GetTemplateText( $template, $id );
  }
# ========================================================================

The second section replaces the text with the already fetched template text. It should be added after the $oldText = $Text{'text'} line, which is around 34 lines below the start of the subroutine:

# == WikiTemplates patch =================================================
  if ( $Page{'revision'} == 0 || $WTOverwriteExisting ) {
    $oldText = $templateText if $templateText;
  }
# ========================================================================

The final section displays the template form if required. It must appear before the print &GetFormStart() line, about 66 lines below the start of the subroutine:

# == WikiTemplates patch =================================================
  if ( $Page{'revision'} == 0 || $WTOverwriteExisting ) {
    print &GetTemplateForm($id) if $WTName;
  }
# ========================================================================

UseModWiki | WikiPatches | RecentChanges | Preferences
Edit text of this page | View other revisions | Search MetaWiki
Last edited May 22, 2009 3:23 pm by JuanmaMP (diff)
Search: