Changeset 5168
- Timestamp:
- 09/29/09 09:30:14 (3 years ago)
- Location:
- trunk/TopicCreatePlugin
- Files:
-
- 4 edited
-
data/System/TopicCreatePlugin.txt (modified) (3 diffs)
-
lib/Foswiki/Plugins/TopicCreatePlugin.pm (modified) (3 diffs)
-
lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm (modified) (14 diffs)
-
lib/Foswiki/Plugins/TopicCreatePlugin/build.pl (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/TopicCreatePlugin/data/System/TopicCreatePlugin.txt
r3353 r5168 8 8 ---++ <nop>TOPICCREATE - Automatically Create a Set of Foswiki Topics 9 9 10 * The =%<nop>TOPICCREATE{}%= Macro is useful for creating topics based on templates11 * 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{ <attributes> }%=13 * Attributes recognized14 | *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% =<parameter-name>=<parameter-value>= 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 preview20 * The =%<nop>TOPICCREATE{}%= variable will be removed upon saving of the topic21 * Any attachments that are in the =template= will also be copied to the new topic22 * =%<nop>TOPICCREATE%= is recursive23 * 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{ <attributes> }%= 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% =<parameter-name>=<parameter-value>= 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. 25 25 26 26 ---++ <nop>TOPICATTACH - Automatically Copy Attachments to a Topic 27 27 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{ <attributes> }%=30 * Attributes recognized31 | *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 attachments38 * The "hidden" attribute of the topic is removed: the attachment can be hidden in the =fromtopic= if desired39 * 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{ <attributes> }%= 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%=. 41 41 42 42 ---++ Limitations/TODO … … 48 48 Plugin settings are stored as preferences variables. To reference a plugin setting write ==%<nop><plugin>_<setting>%==, i.e. ==%<nop>TOPICCREATEPLUGIN_SHORTDESCRIPTION%== 49 49 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 time50 * 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 52 52 53 * Debug plugin: (See output in =data/debug.txt=)54 * Set DEBUG = 053 * Debug plugin: (See output in =data/debug.txt=) 54 * Set DEBUG = 0 55 55 56 56 ---++ Installation Instructions 57 57 58 58 %$INSTALL_INSTRUCTIONS% 59 60 ---++ Plugin Files61 62 | *File:* | *Description:* |63 %$MANIFEST%64 59 65 60 ---++ Plugin Info … … 72 67 | Plugin Version: | %$VERSION% | 73 68 | Change History: | <!-- versions below in reverse order --> | 69 | 29 Sep 2009: | Foswikitask:Item4757 - Fixed warning when using =%<nop>TOPICATTACH%= -- Foswiki:Main.AndrewJones | 74 70 | 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 | 75 71 | 11686 | TWikibug:Item2956 - Freeze !TWikiRelease04x00 and copy it to new branch MAIN | -
trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin.pm
r3353 r5168 20 20 package Foswiki::Plugins::TopicCreatePlugin; 21 21 22 23 22 # ========================= 24 23 use vars qw( 25 $web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC26 );24 $web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC 25 ); 27 26 28 our $VERSION = '$Rev$';29 our $RELEASE = '1.0';30 our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.';27 our $VERSION = '$Rev$'; 28 our $RELEASE = '1.1'; 29 our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.'; 31 30 our $NO_PREFS_IN_TOPIC = 0; 32 our $pluginName = 'TopicCreatePlugin';31 our $pluginName = 'TopicCreatePlugin'; 33 32 34 33 our $doInit = 0; 35 34 36 35 # ========================= 37 sub initPlugin 38 { 36 sub initPlugin { 39 37 ( $topic, $web, $user, $installWeb ) = @_; 40 38 41 39 # Get plugin debug flag 42 $debug = Foswiki::Func::getPluginPreferencesFlag( "DEBUG");40 $debug = Foswiki::Func::getPluginPreferencesFlag("DEBUG"); 43 41 44 42 # 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; 46 46 $doInit = 1; 47 47 return 1; … … 49 49 50 50 # ========================= 51 sub beforeSaveHandler 52 { 51 sub beforeSaveHandler { 53 52 ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead 54 53 55 Foswiki::Func::writeDebug( "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )" ) if $debug; 54 Foswiki::Func::writeDebug( 55 "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )") 56 if $debug; 56 57 57 unless( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) { 58 unless ( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) { 59 58 60 # nothing to do 59 61 return 1; … … 62 64 require Foswiki::Plugins::TopicCreatePlugin::Func; 63 65 64 if ( $doInit) {66 if ($doInit) { 65 67 $doInit = 0; 66 Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, $debug ); 68 Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, 69 $debug ); 67 70 } 68 71 69 $_[0] =~ s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo; 72 $_[0] =~ 73 s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo; 70 74 71 # To be completed, tested and documented72 # $_[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; 73 77 74 if ( $_[0] =~ /%TOPICATTACH/){78 if ( $_[0] =~ /%TOPICATTACH/ ) { 75 79 my @attachMetaData = (); 76 $_[0] =~ s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo; 80 $_[0] =~ 81 s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo; 77 82 my $fileName = ""; 78 foreach my $fileMeta ( @attachMetaData) {83 foreach my $fileMeta (@attachMetaData) { 79 84 $fileMeta =~ m/META:FILEATTACHMENT\{name\=\"(.*?)\"/; 80 85 $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; 83 90 $_[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/; 87 98 } 88 99 } -
trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm
r3353 r5168 30 30 # ========================= 31 31 use vars qw( 32 $web $topic $user $debug 33 ); 34 35 # ========================= 36 sub init 37 { 32 $web $topic $user $debug 33 ); 34 35 # ========================= 36 sub init { 38 37 ( $web, $topic, $user, $debug ) = @_; 39 38 … … 41 40 42 41 # 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; 44 45 return 1; 45 46 } 46 47 47 48 # ========================= 48 sub handleTopicCreate 49 { 50 my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 51 52 unless( defined( $theTopic ) ) { 49 sub handleTopicCreate { 50 my ( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 51 52 unless ( defined($theTopic) ) { 53 53 $theTopic = $topic; 54 54 } 55 55 my $errVar = "%<nop>TOPICCREATE{$theArgs}%"; 56 56 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 67 72 return "%TOPICCREATE{$theArgs}% "; 68 73 } 69 74 70 75 # 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 ); 73 80 74 81 my $topicWeb = $theWeb; 75 if ( $topicName =~ /^([^\.]+)\.(.*)$/ ) {76 $topicWeb = $1;82 if ( $topicName =~ /^([^\.]+)\.(.*)$/ ) { 83 $topicWeb = $1; 77 84 $topicName = $2; 78 85 } 79 86 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 ""; 83 91 } 84 92 85 93 # check if template exists 86 94 my $templateWeb = $theWeb; 87 if ( $template =~ /^([^\.]+)\.(.*)$/ ) {95 if ( $template =~ /^([^\.]+)\.(.*)$/ ) { 88 96 $templateWeb = $1; 89 $template = $2;97 $template = $2; 90 98 } 91 99 92 100 # 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 ); 98 107 99 108 # Set topic parent … … 104 113 #$text = Foswiki::Func::expandVariablesOnTopicCreation( $text ); 105 114 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 111 123 $text =~ s/%DATE%/$localDate/go; 112 124 $text =~ s/%WIKIUSERNAME%/$wikiUserName/go; 113 125 114 # SMELL: see above - expandVariablesOnTopicCreation() also handles URLPARAM's126 # SMELL: see above - expandVariablesOnTopicCreation() also handles URLPARAM's 115 127 my @param = (); 116 my $temp = "";128 my $temp = ""; 117 129 while (1) { 118 130 last unless ( $text =~ m/%URLPARAM\{(.*?)\}%/gs ); … … 122 134 } 123 135 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; 127 141 128 142 my $passedPar = ""; … … 132 146 $text =~ s/%URLPARAM\{\"?$par\"?\}%/$passedPar/g; 133 147 } 148 134 149 # END SMELL 135 150 136 151 # 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 140 159 # 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 ); 142 164 } 143 165 144 166 # 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 =~ 168 s/%TOPICCREATE{(.*)}%[\n\r]*/handleTopicCreate( $1, $theWeb, $topicName )/geo; 169 $text =~ 170 s/%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 } ); 150 175 151 176 return ""; … … 155 180 # Untested and Undocumented, comes from this plugins TWiki days 156 181 # Feel free to complete and test this if you need it 157 sub handleTopicPatch 158 { 159 my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 182 sub handleTopicPatch { 183 my ( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 160 184 161 185 my $errVar = "%<nop>TOPICPATCH{$theArgs}%"; 162 my $topicName = Foswiki::Func::extractNameValuePair( $theArgs, "topic" ) ||163 return "";# Silently fail if not specified164 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)$/ ) { 167 191 return _errorMsg( $errVar, "Unsupported =action= parameter" ); 168 192 } 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" ); 171 195 my $value = Foswiki::Func::extractNameValuePair( $theArgs, "value" ) || ""; 172 196 … … 174 198 $topicName =~ s/%TOPIC%/$theTopic/go; 175 199 $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) 177 201 178 202 my $text = Foswiki::Func::readTopicText( $theWeb, $topicName ); 179 203 180 if ( $text =~ /^http/ ) {204 if ( $text =~ /^http/ ) { 181 205 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" ); 184 210 } 185 211 186 212 $text = _setMetaData( $text, "FIELD", $value, $formfield ); 213 187 214 #$meta->putKeyed( 'FIELD', { 188 215 # name => $formfield, … … 190 217 #}); 191 218 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" ); 196 225 } 197 226 … … 200 229 201 230 # ========================= 202 sub handleTopicAttach 203 { 204 my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_; 205 my( $theArgs, $attachMetaDataRef ) = @_; 231 sub handleTopicAttach { 232 my ( $theArgs, $attachMetaDataRef ) = @_; 206 233 207 234 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 || ""; 214 243 215 244 ## 11/18/05: override of attachment name not yet supported, requires messing with meta info … … 217 246 my $name = $fromFile; 218 247 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 221 251 return "%TOPICATTACH{$theArgs}% "; 222 252 } … … 226 256 227 257 my $fromTopicWeb = $web; 228 if ( $fromTopic =~ /^([^\.]+)\.(.*)$/ ) {258 if ( $fromTopic =~ /^([^\.]+)\.(.*)$/ ) { 229 259 $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 ); 235 267 } 236 268 237 269 # 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 ); 243 278 $fromTopicText =~ m/(%META:FILEATTACHMENT\{name=\"$fromFile.*?\}%)/; 244 279 my $attachInfo = $1; … … 249 284 } 250 285 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 ); 254 294 } 255 295 return ""; … … 257 297 258 298 # ========================= 259 sub _errorMsg 260 { 299 sub _errorMsg { 261 300 my ( $theVar, $theText ) = @_; 262 301 return "%RED% Error in $theVar: $theText %ENDCOLOR% "; … … 264 303 265 304 # ========================= 266 sub _getAttachmentList 267 { 305 sub _getAttachmentList { 268 306 my ( $theWeb, $theTopic ) = @_; 269 307 } 270 308 271 309 # ========================= 272 sub _existAttachment 273 { 310 sub _existAttachment { 274 311 my ( $theWeb, $theTopic, $theFile ) = @_; 275 312 276 313 return Foswiki::Func::attachmentExists( $theWeb, $theTopic, $theFile ); 277 314 } 278 315 279 316 # ========================= 280 sub _copyAttachment 281 { 317 sub _copyAttachment { 282 318 my ( $fromWeb, $fromTopic, $fromFile, $toWeb, $toTopic, $toFile ) = @_; 283 319 284 320 my $pubDir = $Foswiki::cfg{PubDir}; 285 321 286 322 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; 291 330 } 292 331 -
trunk/TopicCreatePlugin/lib/Foswiki/Plugins/TopicCreatePlugin/build.pl
r3268 r5168 4 4 # 5 5 BEGIN { 6 unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS});6 unshift @INC, split( /:/, $ENV{FOSWIKI_LIBS} ); 7 7 } 8 8 … … 10 10 11 11 # Create the build object 12 $build = new Foswiki::Contrib::Build( 'TopicCreatePlugin');12 $build = new Foswiki::Contrib::Build('TopicCreatePlugin'); 13 13 14 14 # Build the target on the command line, or the default target 15 $build->build( $build->{target});15 $build->build( $build->{target} ); 16 16
Note: See TracChangeset
for help on using the changeset viewer.
