Changeset 5168


Ignore:
Timestamp:
09/29/09 09:30:14 (3 years ago)
Author:
AndrewJones
Message:

Item4757: fixed 'my variable masks earlier declaration...' warning; replaced tabs in doc with spaces; perl tidy'd

Location:
trunk/TopicCreatePlugin
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/TopicCreatePlugin/data/System/TopicCreatePlugin.txt

    r3353 r5168  
    88---++ <nop>TOPICCREATE - Automatically Create a Set of Foswiki Topics 
    99 
    10         * The =%<nop>TOPICCREATE{}%= Macro is useful for creating topics based on templates 
    11         * Example scenario: You have a notebook application where users can create new notebooks based on a notebook template topic. Each time a user creates a new notebook you would like to create a set of child topics, each based on a separate template. 
    12         * Syntax: =%<nop>TOPICCREATE{ &lt;attributes&gt; }%= 
    13         * Attributes recognized 
    14           | *Attribute* | *Meaning* | *Valid input* | *Default* | 
    15           | =template=""= | Name of Foswiki topic to be used as template | Any valid Foswiki topic | (required) | 
    16           | =topic=""= | Name of new Foswiki topic to create | Any valid <nop>WikiWord | (required) | 
    17           | =disable=""= | Name of the template topic containing this use of TOPICCREATE | Any valid topic name (<nop>WikiWord) | _recommended_ | 
    18           | =parameters=""= | List of parameters to pass to a child topic to initialize any urlparams.| Following syntax:%BR% =&lt;parameter-name&gt;=&lt;parameter-value&gt;= followed by any number of parameter name, value pairs separated by =&= | No Parameters | 
    19         * The action to create the Foswiki topic will not occur until one hits the =Save Changes= button in preview 
    20         * The =%<nop>TOPICCREATE{}%= variable will be removed upon saving of the topic 
    21         * Any attachments that are in the =template= will also be copied to the new topic 
    22         * =%<nop>TOPICCREATE%= is recursive 
    23         * Write =%<nop>TOPICCREATE{ template="<nop>MyTemplate" topic="<nop>%<nop>TOPIC%MyNewTopicName" parameters="MyParameterName=MyParameterValue&Hello=Goodbye" }%= to create a foswiki topic based on another topic =<nop>MyTemplate= with the name =<nop>%<nop>TOPIC%MyNewTopicName=. Any =%<nop>URLPARAM{}%=  with the name =MyParameterName= and =Hello= present in =MyTemplate= will be replaced with =MyParameterValue= and =Goodbye=. 
    24         * To prevent the action from happening in your template topic you should either include the =disable= argument, or escape it: =%<nop>TOPICCRE%<nop>NOP%ATE{...}%=. The =%<nop>NOP%= gets removed when a topic gets instantiated based on a template. 
     10   * The =%<nop>TOPICCREATE{}%= Macro is useful for creating topics based on templates 
     11   * Example scenario: You have a notebook application where users can create new notebooks based on a notebook template topic. Each time a user creates a new notebook you would like to create a set of child topics, each based on a separate template. 
     12   * Syntax: =%<nop>TOPICCREATE{ &lt;attributes&gt; }%= 
     13   * Attributes recognized 
     14     | *Attribute* | *Meaning* | *Valid input* | *Default* | 
     15     | =template=""= | Name of Foswiki topic to be used as template | Any valid Foswiki topic | (required) | 
     16     | =topic=""= | Name of new Foswiki topic to create | Any valid <nop>WikiWord | (required) | 
     17     | =disable=""= | Name of the template topic containing this use of TOPICCREATE | Any valid topic name (<nop>WikiWord) | _recommended_ | 
     18     | =parameters=""= | List of parameters to pass to a child topic to initialize any urlparams.| Following syntax:%BR% =&lt;parameter-name&gt;=&lt;parameter-value&gt;= followed by any number of parameter name, value pairs separated by =&= | No Parameters | 
     19   * The action to create the Foswiki topic will not occur until one hits the =Save Changes= button in preview 
     20   * The =%<nop>TOPICCREATE{}%= variable will be removed upon saving of the topic 
     21   * Any attachments that are in the =template= will also be copied to the new topic 
     22   * =%<nop>TOPICCREATE%= is recursive 
     23   * Write =%<nop>TOPICCREATE{ template="<nop>MyTemplate" topic="<nop>%<nop>TOPIC%MyNewTopicName" parameters="MyParameterName=MyParameterValue&Hello=Goodbye" }%= to create a foswiki topic based on another topic =<nop>MyTemplate= with the name =<nop>%<nop>TOPIC%MyNewTopicName=. Any =%<nop>URLPARAM{}%=  with the name =MyParameterName= and =Hello= present in =MyTemplate= will be replaced with =MyParameterValue= and =Goodbye=. 
     24   * To prevent the action from happening in your template topic you should either include the =disable= argument, or escape it: =%<nop>TOPICCRE%<nop>NOP%ATE{...}%=. The =%<nop>NOP%= gets removed when a topic gets instantiated based on a template. 
    2525 
    2626---++ <nop>TOPICATTACH - Automatically Copy Attachments to a Topic  
    2727 
    28         * Example scenario: You have a template topic and would like to attach a file whose content or name is determined by URL parameters at topic creation time.  This is usually done for a topic that was just created via TOPICCREATE as described above. 
    29         * Syntax: =%<nop>TOPICATTACH{ &lt;attributes&gt; }%= 
    30         * Attributes recognized 
    31           | *Attribute* | *Meaning* | *Valid input* | *Default* | 
    32           | =fromtopic=""= | Topic (or Web.Topic) that contains the attachment  | Any valid Foswiki topic | (required) | 
    33           | =fromfile=""= | The attachment to copy from | Any attached file from =fromtopic= | (required) | 
    34           | =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name (<nop>WikiWord) | (required) | 
    35           | =comment=""= | Comment to appear for the attachment within this topic, replacing the comment that was in the =fromtopic= | a one line string | {omit parameter if no change desired} | 
    36         * Write =%<nop>TOPICATTACH{ fromtopic="<nop>SourceTopic" fromfile="Sample.jpg" name="Snoopy.jpg" disable="..."}%= to attach =Sample.jpg= from topic =<nop>SourceTopic= as =Snoopy.jpg= 
    37         * The parameter value comment is used to change the comment (description) that shows for the topic in the table of attachments 
    38         * The "hidden" attribute of the topic is removed: the attachment can be hidden in the =fromtopic= if desired 
    39         * The =fromtopic= can specify a different web if desired.  For example =Foswiki<nop>.Some<nop>Template= would be valid as a =fromtopic= 
    40         * To prevent the action from happening in your template topic you *must* provide the =disable= parameter with the topic name containing the =%<nop>TOPICATTACH%=. 
     28   * Example scenario: You have a template topic and would like to attach a file whose content or name is determined by URL parameters at topic creation time.  This is usually done for a topic that was just created via TOPICCREATE as described above. 
     29   * Syntax: =%<nop>TOPICATTACH{ &lt;attributes&gt; }%= 
     30   * Attributes recognized 
     31     | *Attribute* | *Meaning* | *Valid input* | *Default* | 
     32     | =fromtopic=""= | Topic (or Web.Topic) that contains the attachment  | Any valid Foswiki topic | (required) | 
     33     | =fromfile=""= | The attachment to copy from | Any attached file from =fromtopic= | (required) | 
     34     | =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name (<nop>WikiWord) | (required) | 
     35     | =comment=""= | Comment to appear for the attachment within this topic, replacing the comment that was in the =fromtopic= | a one line string | {omit parameter if no change desired} | 
     36   * Write =%<nop>TOPICATTACH{ fromtopic="<nop>SourceTopic" fromfile="Sample.jpg" name="Snoopy.jpg" disable="..."}%= to attach =Sample.jpg= from topic =<nop>SourceTopic= as =Snoopy.jpg= 
     37   * The parameter value comment is used to change the comment (description) that shows for the topic in the table of attachments 
     38   * The "hidden" attribute of the topic is removed: the attachment can be hidden in the =fromtopic= if desired 
     39   * The =fromtopic= can specify a different web if desired.  For example =Foswiki<nop>.Some<nop>Template= would be valid as a =fromtopic= 
     40   * To prevent the action from happening in your template topic you *must* provide the =disable= parameter with the topic name containing the =%<nop>TOPICATTACH%=. 
    4141 
    4242---++ Limitations/TODO 
     
    4848Plugin settings are stored as preferences variables. To reference a plugin setting write ==%<nop>&lt;plugin&gt;_&lt;setting&gt;%==, i.e. ==%<nop>TOPICCREATEPLUGIN_SHORTDESCRIPTION%== 
    4949 
    50         * One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic: 
    51                 * Set SHORTDESCRIPTION = Automatically create a set of topics and attachments at topic save time 
     50   * One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic: 
     51      * Set SHORTDESCRIPTION = Automatically create a set of topics and attachments at topic save time 
    5252 
    53         * Debug plugin: (See output in =data/debug.txt=) 
    54                 * Set DEBUG = 0 
     53   * Debug plugin: (See output in =data/debug.txt=) 
     54      * Set DEBUG = 0 
    5555 
    5656---++ Installation Instructions 
    5757 
    5858%$INSTALL_INSTRUCTIONS% 
    59  
    60 ---++ Plugin Files 
    61  
    62    | *File:* | *Description:* | 
    63 %$MANIFEST% 
    6459 
    6560---++ Plugin Info 
     
    7267|  Plugin Version: | %$VERSION% | 
    7368|  Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; | 
     69|  29 Sep 2009: | Foswikitask:Item4757 - Fixed warning when using =%<nop>TOPICATTACH%= -- Foswiki:Main.AndrewJones | 
    7470|  31 Mar 2009: | Foswikitask:Item1262 - Ported to Foswiki and released as 1.0; Removed dependency on Unix utilities =cp= and =mkdir=, should now work on any OS that Foswiki does; Improved code to make it cleaner and use more of the official API; Removed depreciated API calls -- Foswiki:Main.AndrewJones | 
    7571|  11686 | TWikibug:Item2956 - Freeze !TWikiRelease04x00 and copy it to new branch MAIN | 
  • trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin.pm

    r3353 r5168  
    2020package Foswiki::Plugins::TopicCreatePlugin; 
    2121 
    22  
    2322# ========================= 
    2423use vars qw( 
    25         $web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC 
    26     ); 
     24  $web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC 
     25); 
    2726 
    28 our $VERSION = '$Rev$'; 
    29 our $RELEASE = '1.0'; 
    30 our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.'; 
     27our $VERSION           = '$Rev$'; 
     28our $RELEASE           = '1.1'; 
     29our $SHORTDESCRIPTION  = 'Displays the hostname of the server serving Foswiki.'; 
    3130our $NO_PREFS_IN_TOPIC = 0; 
    32 our $pluginName = 'TopicCreatePlugin'; 
     31our $pluginName        = 'TopicCreatePlugin'; 
    3332 
    3433our $doInit = 0; 
    3534 
    3635# ========================= 
    37 sub initPlugin 
    38 { 
     36sub initPlugin { 
    3937    ( $topic, $web, $user, $installWeb ) = @_; 
    4038 
    4139    # Get plugin debug flag 
    42     $debug = Foswiki::Func::getPluginPreferencesFlag( "DEBUG" ); 
     40    $debug = Foswiki::Func::getPluginPreferencesFlag("DEBUG"); 
    4341 
    4442    # Plugin correctly initialized 
    45     Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK" ) if $debug; 
     43    Foswiki::Func::writeDebug( 
     44        "- Foswiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK" 
     45    ) if $debug; 
    4646    $doInit = 1; 
    4747    return 1; 
     
    4949 
    5050# ========================= 
    51 sub beforeSaveHandler 
    52 { 
     51sub beforeSaveHandler { 
    5352### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead 
    5453 
    55     Foswiki::Func::writeDebug( "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )" ) if $debug; 
     54    Foswiki::Func::writeDebug( 
     55        "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )") 
     56      if $debug; 
    5657 
    57     unless( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) { 
     58    unless ( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) { 
     59 
    5860        # nothing to do 
    5961        return 1; 
     
    6264    require Foswiki::Plugins::TopicCreatePlugin::Func; 
    6365 
    64     if( $doInit ) { 
     66    if ($doInit) { 
    6567        $doInit = 0; 
    66         Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, $debug ); 
     68        Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, 
     69            $debug ); 
    6770    } 
    6871 
    69     $_[0] =~ s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo; 
     72    $_[0] =~ 
     73s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo; 
    7074 
    71     # To be completed, tested and documented 
    72     # $_[0] =~ s/%TOPICPATCH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicPatch($1, $_[2], $_[1], $_[0] )/geo; 
     75# To be completed, tested and documented 
     76# $_[0] =~ s/%TOPICPATCH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicPatch($1, $_[2], $_[1], $_[0] )/geo; 
    7377 
    74     if ($_[0] =~ /%TOPICATTACH/){ 
     78    if ( $_[0] =~ /%TOPICATTACH/ ) { 
    7579        my @attachMetaData = (); 
    76         $_[0] =~ s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo; 
     80        $_[0] =~ 
     81s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo; 
    7782        my $fileName = ""; 
    78         foreach my $fileMeta ( @attachMetaData ) { 
     83        foreach my $fileMeta (@attachMetaData) { 
    7984            $fileMeta =~ m/META:FILEATTACHMENT\{name\=\"(.*?)\"/; 
    8085            $fileName = $1; 
    81             unless ($_[0] =~ m/META:FILEATTACHMENT\{name\=\"$fileName/ ) { 
    82                 &Foswiki::Func::writeDebug( "handleTopicAttach:: in unless $fileMeta") if $debug; 
     86            unless ( $_[0] =~ m/META:FILEATTACHMENT\{name\=\"$fileName/ ) { 
     87                &Foswiki::Func::writeDebug( 
     88                    "handleTopicAttach:: in unless $fileMeta") 
     89                  if $debug; 
    8390                $_[0] .= "\n$fileMeta"; 
    84             } else { 
    85                 &Foswiki::Func::writeDebug( "handleTopicAttach:: in else $fileMeta") if $debug; 
    86                 $_[0] =~ s/(%META:FILEATTACHMENT\{name=\"$fileName.*?\}%)/$fileMeta/; 
     91            } 
     92            else { 
     93                &Foswiki::Func::writeDebug( 
     94                    "handleTopicAttach:: in else $fileMeta") 
     95                  if $debug; 
     96                $_[0] =~ 
     97                  s/(%META:FILEATTACHMENT\{name=\"$fileName.*?\}%)/$fileMeta/; 
    8798            } 
    8899        } 
  • trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm

    r3353 r5168  
    3030# ========================= 
    3131use vars qw( 
    32         $web $topic $user $debug 
    33     ); 
    34  
    35 # ========================= 
    36 sub init 
    37 { 
     32  $web $topic $user $debug 
     33); 
     34 
     35# ========================= 
     36sub init { 
    3837    ( $web, $topic, $user, $debug ) = @_; 
    3938 
     
    4140 
    4241    # Module initialized 
    43     Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::Func::init( $web.$topic )" ) if $debug; 
     42    Foswiki::Func::writeDebug( 
     43        "- Foswiki::Plugins::TopicCreatePlugin::Func::init( $web.$topic )") 
     44      if $debug; 
    4445    return 1; 
    4546} 
    4647 
    4748# ========================= 
    48 sub handleTopicCreate 
    49 { 
    50     my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 
    51  
    52     unless( defined( $theTopic ) ) { 
     49sub handleTopicCreate { 
     50    my ( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 
     51 
     52    unless ( defined($theTopic) ) { 
    5353        $theTopic = $topic; 
    5454    } 
    5555    my $errVar = "%<nop>TOPICCREATE{$theArgs}%"; 
    5656 
    57     my $template = Foswiki::Func::extractNameValuePair( $theArgs, "template" ) || 
    58         return _errorMsg( $errVar, "Parameter =templatete= is missing or empty." ); 
    59     my $parameters = Foswiki::Func::extractNameValuePair( $theArgs, "parameters" ) || ""; 
    60     my $topicName = Foswiki::Func::extractNameValuePair( $theArgs, "topic" ) || 
    61                     Foswiki::Func::extractNameValuePair( $theArgs, "name" ) || 
    62         return _errorMsg( $errVar, "Parameter =topic= is missing or empty." ); 
    63     my $disable = Foswiki::Func::extractNameValuePair( $theArgs, "disable" ) || ""; 
    64  
    65     if($disable eq $topic) { 
    66         #  saving the outer template itself should not invoke the create 
     57    my $template = Foswiki::Func::extractNameValuePair( $theArgs, "template" ) 
     58      || return _errorMsg( $errVar, 
     59        "Parameter =templatete= is missing or empty." ); 
     60    my $parameters = 
     61      Foswiki::Func::extractNameValuePair( $theArgs, "parameters" ) || ""; 
     62    my $topicName = 
     63         Foswiki::Func::extractNameValuePair( $theArgs, "topic" ) 
     64      || Foswiki::Func::extractNameValuePair( $theArgs, "name" ) 
     65      || return _errorMsg( $errVar, "Parameter =topic= is missing or empty." ); 
     66    my $disable = Foswiki::Func::extractNameValuePair( $theArgs, "disable" ) 
     67      || ""; 
     68 
     69    if ( $disable eq $topic ) { 
     70 
     71        #  saving the outer template itself should not invoke the create 
    6772        return "%TOPICCREATE{$theArgs}% "; 
    6873    } 
    69      
     74 
    7075    # expand relevant Foswiki Macros 
    71     $topicName = Foswiki::Func::expandCommonVariables( $topicName, $theTopic, $theWeb ); 
    72     $template = Foswiki::Func::expandCommonVariables( $template, $theTopic, $theWeb ); 
     76    $topicName = 
     77      Foswiki::Func::expandCommonVariables( $topicName, $theTopic, $theWeb ); 
     78    $template = 
     79      Foswiki::Func::expandCommonVariables( $template, $theTopic, $theWeb ); 
    7380 
    7481    my $topicWeb = $theWeb; 
    75     if( $topicName =~ /^([^\.]+)\.(.*)$/ ) { 
    76         $topicWeb = $1; 
     82    if ( $topicName =~ /^([^\.]+)\.(.*)$/ ) { 
     83        $topicWeb  = $1; 
    7784        $topicName = $2; 
    7885    } 
    7986 
    80     if( Foswiki::Func::topicExists( $topicWeb, $topicName ) ) { 
    81       #  Silently fail 
    82       return ""; 
     87    if ( Foswiki::Func::topicExists( $topicWeb, $topicName ) ) { 
     88 
     89        #  Silently fail 
     90        return ""; 
    8391    } 
    8492 
    8593    # check if template exists 
    8694    my $templateWeb = $theWeb; 
    87     if( $template =~ /^([^\.]+)\.(.*)$/ ) { 
     95    if ( $template =~ /^([^\.]+)\.(.*)$/ ) { 
    8896        $templateWeb = $1; 
    89         $template = $2; 
     97        $template    = $2; 
    9098    } 
    9199 
    92100    # Error, Warn user 
    93     unless( &Foswiki::Func::topicExists( $templateWeb, $template ) ) { 
    94         return _errorMsg( $errVar, "Template <nop>$templateWeb.$template does not exist."); 
    95     } 
    96      
    97     my ($meta, $text) = &Foswiki::Func::readTopic($templateWeb, $template); 
     101    unless ( &Foswiki::Func::topicExists( $templateWeb, $template ) ) { 
     102        return _errorMsg( $errVar, 
     103            "Template <nop>$templateWeb.$template does not exist." ); 
     104    } 
     105 
     106    my ( $meta, $text ) = &Foswiki::Func::readTopic( $templateWeb, $template ); 
    98107 
    99108    # Set topic parent 
     
    104113    #$text = Foswiki::Func::expandVariablesOnTopicCreation( $text ); 
    105114 
    106     my $localDate = &Foswiki::Time::formatTime( time(), $Foswiki::cfg{DefaultDateFormat} ); 
    107  
    108     my $wikiUserName = &Foswiki::Func::userToWikiName( $user ); 
    109     $text =~ s/%NOP{.*?}%//gos;  # Remove filler: Use it to remove access control at time of 
    110     $text =~ s/%NOP%//go;        # topic instantiation or to prevent search from hitting a template 
     115    my $localDate = 
     116      &Foswiki::Time::formatTime( time(), $Foswiki::cfg{DefaultDateFormat} ); 
     117 
     118    my $wikiUserName = &Foswiki::Func::userToWikiName($user); 
     119    $text =~ s/%NOP{.*?}%//gos 
     120      ;    # Remove filler: Use it to remove access control at time of 
     121    $text =~ s/%NOP%//go 
     122      ;    # topic instantiation or to prevent search from hitting a template 
    111123    $text =~ s/%DATE%/$localDate/go; 
    112124    $text =~ s/%WIKIUSERNAME%/$wikiUserName/go; 
    113125 
    114     # SMELL: see above - expandVariablesOnTopicCreation() also handles URLPARAM's 
     126   # SMELL: see above - expandVariablesOnTopicCreation() also handles URLPARAM's 
    115127    my @param = (); 
    116     my $temp = ""; 
     128    my $temp  = ""; 
    117129    while (1) { 
    118130        last unless ( $text =~ m/%URLPARAM\{(.*?)\}%/gs ); 
     
    122134    } 
    123135 
    124     my $ptemp = join  ", ", @param; 
    125     &Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::topicCreate " 
    126        . "$topicName $ptemp $parameters") if $debug; 
     136    my $ptemp = join ", ", @param; 
     137    &Foswiki::Func::writeDebug( 
     138            "- Foswiki::Plugins::TopicCreatePlugin::topicCreate " 
     139          . "$topicName $ptemp $parameters" ) 
     140      if $debug; 
    127141 
    128142    my $passedPar = ""; 
     
    132146        $text =~ s/%URLPARAM\{\"?$par\"?\}%/$passedPar/g; 
    133147    } 
     148 
    134149    # END SMELL 
    135150 
    136151    # Copy all Attachments over 
    137     my @attachments = $meta->find( 'FILEATTACHMENT' ); 
    138     foreach my $attach ( @attachments ){ 
    139         my $fileName = $attach->{ 'path' } || $attach->{ 'attachment' } || $attach->{ 'name' }; 
     152    my @attachments = $meta->find('FILEATTACHMENT'); 
     153    foreach my $attach (@attachments) { 
     154        my $fileName = 
     155             $attach->{'path'} 
     156          || $attach->{'attachment'} 
     157          || $attach->{'name'}; 
     158 
    140159        # TODO: We could keep the comment, date of upload, etc 
    141         _copyAttachment( $templateWeb, $template, $fileName, $topicWeb, $topicName, $fileName ); 
     160        _copyAttachment( 
     161            $templateWeb, $template,  $fileName, 
     162            $topicWeb,    $topicName, $fileName 
     163        ); 
    142164    } 
    143165 
    144166    # Recursively handle TOPICCREATE and TOPICATTCH 
    145     $text =~ s/%TOPICCREATE{(.*)}%[\n\r]*/handleTopicCreate( $1, $theWeb, $topicName )/geo; 
    146     $text =~ s/%TOPICATTCH{(.*)}%[\n\r]*/handleTopicAttach( $1, $theWeb, $topicName )/geo; 
    147  
    148     #my $error = &Foswiki::Func::saveTopicText( $topicWeb, $topicName, $text, 1, "dont notify" ); 
    149     &Foswiki::Func::saveTopic( $topicWeb, $topicName, $meta, $text, { minor => 1 } ); 
     167    $text =~ 
     168s/%TOPICCREATE{(.*)}%[\n\r]*/handleTopicCreate( $1, $theWeb, $topicName )/geo; 
     169    $text =~ 
     170s/%TOPICATTCH{(.*)}%[\n\r]*/handleTopicAttach( $1, $theWeb, $topicName )/geo; 
     171 
     172#my $error = &Foswiki::Func::saveTopicText( $topicWeb, $topicName, $text, 1, "dont notify" ); 
     173    &Foswiki::Func::saveTopic( $topicWeb, $topicName, $meta, $text, 
     174        { minor => 1 } ); 
    150175 
    151176    return ""; 
     
    155180# Untested and Undocumented, comes from this plugins TWiki days 
    156181# Feel free to complete and test this if you need it 
    157 sub handleTopicPatch 
    158 { 
    159     my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 
     182sub handleTopicPatch { 
     183    my ( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 
    160184 
    161185    my $errVar = "%<nop>TOPICPATCH{$theArgs}%"; 
    162     my $topicName = Foswiki::Func::extractNameValuePair( $theArgs, "topic" ) || 
    163         return "";   #  Silently fail if not specified 
    164     my $action = Foswiki::Func::extractNameValuePair( $theArgs, "action" ) || 
    165         return _errorMsg( $errVar, "Missing =action= parameter" ); 
    166     unless( $action =~ /^(append|replace)$/ ) { 
     186    my $topicName = Foswiki::Func::extractNameValuePair( $theArgs, "topic" ) 
     187      || return "";    #  Silently fail if not specified 
     188    my $action = Foswiki::Func::extractNameValuePair( $theArgs, "action" ) 
     189      || return _errorMsg( $errVar, "Missing =action= parameter" ); 
     190    unless ( $action =~ /^(append|replace)$/ ) { 
    167191        return _errorMsg( $errVar, "Unsupported =action= parameter" ); 
    168192    } 
    169     my $formfield = Foswiki::Func::extractNameValuePair( $theArgs, "formfield" ) || 
    170         return _errorMsg( $errVar, "Missing =formfield= parameter" ); 
     193    my $formfield = Foswiki::Func::extractNameValuePair( $theArgs, "formfield" ) 
     194      || return _errorMsg( $errVar, "Missing =formfield= parameter" ); 
    171195    my $value = Foswiki::Func::extractNameValuePair( $theArgs, "value" ) || ""; 
    172196 
     
    174198    $topicName =~ s/%TOPIC%/$theTopic/go; 
    175199    $topicName =~ s/%WEB%/$theWeb/go; 
    176     $topicName =~ s/.*\.//go;  # cut web for security (only current web) 
     200    $topicName =~ s/.*\.//go;    # cut web for security (only current web) 
    177201 
    178202    my $text = Foswiki::Func::readTopicText( $theWeb, $topicName ); 
    179203 
    180     if( $text =~ /^http/ ) { 
     204    if ( $text =~ /^http/ ) { 
    181205        return _errorMsg( $errVar, "No permission to update '$topicName'" ); 
    182     } elsif( $text eq "" ) { 
    183         return _errorMsg( $errVar, "Can't update '$topicName' because it does not exist" ); 
     206    } 
     207    elsif ( $text eq "" ) { 
     208        return _errorMsg( $errVar, 
     209            "Can't update '$topicName' because it does not exist" ); 
    184210    } 
    185211 
    186212    $text = _setMetaData( $text, "FIELD", $value, $formfield ); 
     213 
    187214    #$meta->putKeyed( 'FIELD', { 
    188215    #        name => $formfield, 
     
    190217    #}); 
    191218 
    192     my $error = Foswiki::Func::saveTopicText( $theWeb, $topicName, $text, "", "dont notify" ); 
    193  
    194     if( $error ) { 
    195         return _errorMsg( $errVar, "Can't update '$topicName' due to permissions" ); 
     219    my $error = Foswiki::Func::saveTopicText( $theWeb, $topicName, $text, "", 
     220        "dont notify" ); 
     221 
     222    if ($error) { 
     223        return _errorMsg( $errVar, 
     224            "Can't update '$topicName' due to permissions" ); 
    196225    } 
    197226 
     
    200229 
    201230# ========================= 
    202 sub handleTopicAttach 
    203 { 
    204     my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 
    205     my( $theArgs, $attachMetaDataRef ) = @_; 
     231sub handleTopicAttach { 
     232    my ( $theArgs, $attachMetaDataRef ) = @_; 
    206233 
    207234    my $errVar = "%<nop>TOPICATTACH{$theArgs}%"; 
    208     my $fromTopic = Foswiki::Func::extractNameValuePair( $theArgs, "fromtopic" ) || 
    209         return _errorMsg( $errVar, "Missing =fromtopic= parameter" ); 
    210     my $fromFile = Foswiki::Func::extractNameValuePair( $theArgs, "fromfile" ) || 
    211         return _errorMsg( $errVar, "Missing =fromfile= parameter" ); 
    212     my $attachComment = Foswiki::Func::extractNameValuePair( $theArgs, "comment" ); 
    213     my $disable = Foswiki::Func::extractNameValuePair( $theArgs, "disable" ) || ""; 
     235    my $fromTopic = Foswiki::Func::extractNameValuePair( $theArgs, "fromtopic" ) 
     236      || return _errorMsg( $errVar, "Missing =fromtopic= parameter" ); 
     237    my $fromFile = Foswiki::Func::extractNameValuePair( $theArgs, "fromfile" ) 
     238      || return _errorMsg( $errVar, "Missing =fromfile= parameter" ); 
     239    my $attachComment = 
     240      Foswiki::Func::extractNameValuePair( $theArgs, "comment" ); 
     241    my $disable = Foswiki::Func::extractNameValuePair( $theArgs, "disable" ) 
     242      || ""; 
    214243 
    215244    ## 11/18/05: override of attachment name not yet supported, requires messing with meta info 
     
    217246    my $name = $fromFile; 
    218247 
    219     if($disable eq $topic) { 
    220         #  saving the outer template itself should not invoke the create 
     248    if ( $disable eq $topic ) { 
     249 
     250        #  saving the outer template itself should not invoke the create 
    221251        return "%TOPICATTACH{$theArgs}% "; 
    222252    } 
     
    226256 
    227257    my $fromTopicWeb = $web; 
    228     if( $fromTopic =~ /^([^\.]+)\.(.*)$/ ) { 
     258    if ( $fromTopic =~ /^([^\.]+)\.(.*)$/ ) { 
    229259        $fromTopicWeb = $1; 
    230         $fromTopic = $2; 
    231     } 
    232  
    233     if( _existAttachment( $web, $topic, $name ) ) { 
    234         return _errorMsg( $errVar, "Attachment =$name= already exists in destination topic $web.$topic" ); 
     260        $fromTopic    = $2; 
     261    } 
     262 
     263    if ( _existAttachment( $web, $topic, $name ) ) { 
     264        return _errorMsg( $errVar, 
     265            "Attachment =$name= already exists in destination topic $web.$topic" 
     266        ); 
    235267    } 
    236268 
    237269    # Copy attachment over 
    238     if( _existAttachment( $fromTopicWeb, $fromTopic, $fromFile ) ) { 
    239         _copyAttachment( $fromTopicWeb, $fromTopic, $fromFile, $web, $topic, $name ); 
    240         # FIXME: use Foswiki::Func::readTopic( $web, $topic, $rev ) -> ( $meta, $text ); 
    241         # then use the Meta object 
    242         my $fromTopicText = &Foswiki::Func::readTopicText( $fromTopicWeb, $fromTopic, "", 1 ); 
     270    if ( _existAttachment( $fromTopicWeb, $fromTopic, $fromFile ) ) { 
     271        _copyAttachment( $fromTopicWeb, $fromTopic, $fromFile, $web, $topic, 
     272            $name ); 
     273 
     274# FIXME: use Foswiki::Func::readTopic( $web, $topic, $rev ) -> ( $meta, $text ); 
     275# then use the Meta object 
     276        my $fromTopicText = 
     277          &Foswiki::Func::readTopicText( $fromTopicWeb, $fromTopic, "", 1 ); 
    243278        $fromTopicText =~ m/(%META:FILEATTACHMENT\{name=\"$fromFile.*?\}%)/; 
    244279        my $attachInfo = $1; 
     
    249284        } 
    250285        push @$attachMetaDataRef, ($attachInfo); 
    251     } else { 
    252         &Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::handleTopicAttach:: $fromFile does not exist in $fromTopicWeb/$fromTopic" ) if $debug; 
    253         return _errorMsg( $errVar, "Attachment =$fromFile= does not exist in source topic $fromTopicWeb.$fromTopic" ); 
     286    } 
     287    else { 
     288        &Foswiki::Func::writeDebug( 
     289"- Foswiki::Plugins::TopicCreatePlugin::handleTopicAttach:: $fromFile does not exist in $fromTopicWeb/$fromTopic" 
     290        ) if $debug; 
     291        return _errorMsg( $errVar, 
     292"Attachment =$fromFile= does not exist in source topic $fromTopicWeb.$fromTopic" 
     293        ); 
    254294    } 
    255295    return ""; 
     
    257297 
    258298# ========================= 
    259 sub _errorMsg 
    260 { 
     299sub _errorMsg { 
    261300    my ( $theVar, $theText ) = @_; 
    262301    return "%RED% Error in $theVar: $theText %ENDCOLOR% "; 
     
    264303 
    265304# ========================= 
    266 sub _getAttachmentList 
    267 { 
     305sub _getAttachmentList { 
    268306    my ( $theWeb, $theTopic ) = @_; 
    269307} 
    270308 
    271309# ========================= 
    272 sub _existAttachment 
    273 { 
     310sub _existAttachment { 
    274311    my ( $theWeb, $theTopic, $theFile ) = @_; 
    275      
     312 
    276313    return Foswiki::Func::attachmentExists( $theWeb, $theTopic, $theFile ); 
    277314} 
    278315 
    279316# ========================= 
    280 sub _copyAttachment 
    281 { 
     317sub _copyAttachment { 
    282318    my ( $fromWeb, $fromTopic, $fromFile, $toWeb, $toTopic, $toFile ) = @_; 
    283      
     319 
    284320    my $pubDir = $Foswiki::cfg{PubDir}; 
    285      
     321 
    286322    my $filePath = "$pubDir/$fromWeb/$fromTopic/$fromFile"; 
    287     
    288     Foswiki::Func::saveAttachment( $toWeb, $toTopic, $toFile, { file => $filePath } ); 
    289      
    290     &Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::copyAttachment from $fromWeb/$fromTopic/$fromFile to $toWeb/$toTopic/$toFile") if $debug; 
     323 
     324    Foswiki::Func::saveAttachment( $toWeb, $toTopic, $toFile, 
     325        { file => $filePath } ); 
     326 
     327    &Foswiki::Func::writeDebug( 
     328"- Foswiki::Plugins::TopicCreatePlugin::copyAttachment from $fromWeb/$fromTopic/$fromFile to $toWeb/$toTopic/$toFile" 
     329    ) if $debug; 
    291330} 
    292331 
  • trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin/build.pl

    r3268 r5168  
    44# 
    55BEGIN { 
    6     unshift @INC, split(/:/, $ENV{FOSWIKI_LIBS}); 
     6    unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); 
    77} 
    88 
     
    1010 
    1111# Create the build object 
    12 $build = new Foswiki::Contrib::Build( 'TopicCreatePlugin' ); 
     12$build = new Foswiki::Contrib::Build('TopicCreatePlugin'); 
    1313 
    1414# Build the target on the command line, or the default target 
    15 $build->build($build->{target}); 
     15$build->build( $build->{target} ); 
    1616 
Note: See TracChangeset for help on using the changeset viewer.