Skip to content

Commit

Permalink
Item4757: fixed 'my variable masks earlier declaration...' warning; r…
Browse files Browse the repository at this point in the history
…eplaced tabs in doc with spaces; perl tidy'd

git-svn-id: http://svn.foswiki.org/trunk/TopicCreatePlugin@5168 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
AndrewJones authored and AndrewJones committed Sep 29, 2009
1 parent e9aafea commit 1069d09
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 174 deletions.
70 changes: 33 additions & 37 deletions data/System/TopicCreatePlugin.txt
Expand Up @@ -7,37 +7,37 @@ This Plugin allows you to automatically create topics or attach files at topic s

---++ <nop>TOPICCREATE - Automatically Create a Set of Foswiki Topics

* The =%<nop>TOPICCREATE{}%= Macro is useful for creating topics based on templates
* 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.
* Syntax: =%<nop>TOPICCREATE{ &lt;attributes&gt; }%=
* Attributes recognized
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =template=""= | Name of Foswiki topic to be used as template | Any valid Foswiki topic | (required) |
| =topic=""= | Name of new Foswiki topic to create | Any valid <nop>WikiWord | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICCREATE | Any valid topic name (<nop>WikiWord) | _recommended_ |
| =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 |
* The action to create the Foswiki topic will not occur until one hits the =Save Changes= button in preview
* The =%<nop>TOPICCREATE{}%= variable will be removed upon saving of the topic
* Any attachments that are in the =template= will also be copied to the new topic
* =%<nop>TOPICCREATE%= is recursive
* 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=.
* 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.
* The =%<nop>TOPICCREATE{}%= Macro is useful for creating topics based on templates
* 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.
* Syntax: =%<nop>TOPICCREATE{ &lt;attributes&gt; }%=
* Attributes recognized
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =template=""= | Name of Foswiki topic to be used as template | Any valid Foswiki topic | (required) |
| =topic=""= | Name of new Foswiki topic to create | Any valid <nop>WikiWord | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICCREATE | Any valid topic name (<nop>WikiWord) | _recommended_ |
| =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 |
* The action to create the Foswiki topic will not occur until one hits the =Save Changes= button in preview
* The =%<nop>TOPICCREATE{}%= variable will be removed upon saving of the topic
* Any attachments that are in the =template= will also be copied to the new topic
* =%<nop>TOPICCREATE%= is recursive
* 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=.
* 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.

---++ <nop>TOPICATTACH - Automatically Copy Attachments to a Topic

* 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.
* Syntax: =%<nop>TOPICATTACH{ &lt;attributes&gt; }%=
* Attributes recognized
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =fromtopic=""= | Topic (or Web.Topic) that contains the attachment | Any valid Foswiki topic | (required) |
| =fromfile=""= | The attachment to copy from | Any attached file from =fromtopic= | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name (<nop>WikiWord) | (required) |
| =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} |
* Write =%<nop>TOPICATTACH{ fromtopic="<nop>SourceTopic" fromfile="Sample.jpg" name="Snoopy.jpg" disable="..."}%= to attach =Sample.jpg= from topic =<nop>SourceTopic= as =Snoopy.jpg=
* The parameter value comment is used to change the comment (description) that shows for the topic in the table of attachments
* The "hidden" attribute of the topic is removed: the attachment can be hidden in the =fromtopic= if desired
* The =fromtopic= can specify a different web if desired. For example =Foswiki<nop>.Some<nop>Template= would be valid as a =fromtopic=
* To prevent the action from happening in your template topic you *must* provide the =disable= parameter with the topic name containing the =%<nop>TOPICATTACH%=.
* 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.
* Syntax: =%<nop>TOPICATTACH{ &lt;attributes&gt; }%=
* Attributes recognized
| *Attribute* | *Meaning* | *Valid input* | *Default* |
| =fromtopic=""= | Topic (or Web.Topic) that contains the attachment | Any valid Foswiki topic | (required) |
| =fromfile=""= | The attachment to copy from | Any attached file from =fromtopic= | (required) |
| =disable=""= | Name of the template topic containing this use of TOPICATTACH | Any valid topic name (<nop>WikiWord) | (required) |
| =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} |
* Write =%<nop>TOPICATTACH{ fromtopic="<nop>SourceTopic" fromfile="Sample.jpg" name="Snoopy.jpg" disable="..."}%= to attach =Sample.jpg= from topic =<nop>SourceTopic= as =Snoopy.jpg=
* The parameter value comment is used to change the comment (description) that shows for the topic in the table of attachments
* The "hidden" attribute of the topic is removed: the attachment can be hidden in the =fromtopic= if desired
* The =fromtopic= can specify a different web if desired. For example =Foswiki<nop>.Some<nop>Template= would be valid as a =fromtopic=
* To prevent the action from happening in your template topic you *must* provide the =disable= parameter with the topic name containing the =%<nop>TOPICATTACH%=.

---++ Limitations/TODO
* The =TOPICATTACH= must be in the 'top level' topic being saved by a =TOPICCREATE=. It can not be in a recursive/subordinate topic invoked by the TOPICCREATE
Expand All @@ -47,21 +47,16 @@ This Plugin allows you to automatically create topics or attach files at topic s

Plugin settings are stored as preferences variables. To reference a plugin setting write ==%<nop>&lt;plugin&gt;_&lt;setting&gt;%==, i.e. ==%<nop>TOPICCREATEPLUGIN_SHORTDESCRIPTION%==

* One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
* Set SHORTDESCRIPTION = Automatically create a set of topics and attachments at topic save time
* One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
* Set SHORTDESCRIPTION = Automatically create a set of topics and attachments at topic save time

* Debug plugin: (See output in =data/debug.txt=)
* Set DEBUG = 0
* Debug plugin: (See output in =data/debug.txt=)
* Set DEBUG = 0

---++ Installation Instructions

%$INSTALL_INSTRUCTIONS%

---++ Plugin Files

| *File:* | *Description:* |
%$MANIFEST%

---++ Plugin Info

| Plugin Author(s): | Foswiki:Main.AndrewJones |
Expand All @@ -71,6 +66,7 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
| Plugin Release: | %$RELEASE% |
| Plugin Version: | %$VERSION% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 29 Sep 2009: | Foswikitask:Item4757 - Fixed warning when using =%<nop>TOPICATTACH%= -- Foswiki:Main.AndrewJones |
| 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 |
| 11686 | TWikibug:Item2956 - Freeze !TWikiRelease04x00 and copy it to new branch MAIN |
| 10613 | TWikibug:Item2473 - files in pub and data directories shouldn't be marked executable in svn (Ordnung muss sein) |
Expand Down
67 changes: 39 additions & 28 deletions lib/Foswiki/Plugins/TopicCreatePlugin.pm
Expand Up @@ -19,71 +19,82 @@

package Foswiki::Plugins::TopicCreatePlugin;


# =========================
use vars qw(
$web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC
);
$web $topic $user $installWeb $debug $doInit $VERSION $RELEASE $SHORTDESCRIPTION $pluginName $NO_PREFS_IN_TOPIC
);

our $VERSION = '$Rev$';
our $RELEASE = '1.0';
our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.';
our $VERSION = '$Rev$';
our $RELEASE = '1.1';
our $SHORTDESCRIPTION = 'Displays the hostname of the server serving Foswiki.';
our $NO_PREFS_IN_TOPIC = 0;
our $pluginName = 'TopicCreatePlugin';
our $pluginName = 'TopicCreatePlugin';

our $doInit = 0;

# =========================
sub initPlugin
{
sub initPlugin {
( $topic, $web, $user, $installWeb ) = @_;

# Get plugin debug flag
$debug = Foswiki::Func::getPluginPreferencesFlag( "DEBUG" );
$debug = Foswiki::Func::getPluginPreferencesFlag("DEBUG");

# Plugin correctly initialized
Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK" ) if $debug;
Foswiki::Func::writeDebug(
"- Foswiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK"
) if $debug;
$doInit = 1;
return 1;
}

# =========================
sub beforeSaveHandler
{
sub beforeSaveHandler {
### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead

Foswiki::Func::writeDebug( "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )" ) if $debug;
Foswiki::Func::writeDebug(
"- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )")
if $debug;

unless ( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) {

unless( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) {
# nothing to do
return 1;
}

require Foswiki::Plugins::TopicCreatePlugin::Func;

if( $doInit ) {
if ($doInit) {
$doInit = 0;
Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, $debug );
Foswiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user,
$debug );
}

$_[0] =~ s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo;
$_[0] =~
s/%TOPICCREATE{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo;

# To be completed, tested and documented
# $_[0] =~ s/%TOPICPATCH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicPatch($1, $_[2], $_[1], $_[0] )/geo;
# To be completed, tested and documented
# $_[0] =~ s/%TOPICPATCH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicPatch($1, $_[2], $_[1], $_[0] )/geo;

if ($_[0] =~ /%TOPICATTACH/){
if ( $_[0] =~ /%TOPICATTACH/ ) {
my @attachMetaData = ();
$_[0] =~ s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo;
$_[0] =~
s/%TOPICATTACH{(.*)}%[\n\r]*/Foswiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo;
my $fileName = "";
foreach my $fileMeta ( @attachMetaData ) {
foreach my $fileMeta (@attachMetaData) {
$fileMeta =~ m/META:FILEATTACHMENT\{name\=\"(.*?)\"/;
$fileName = $1;
unless ($_[0] =~ m/META:FILEATTACHMENT\{name\=\"$fileName/ ) {
&Foswiki::Func::writeDebug( "handleTopicAttach:: in unless $fileMeta") if $debug;
unless ( $_[0] =~ m/META:FILEATTACHMENT\{name\=\"$fileName/ ) {
&Foswiki::Func::writeDebug(
"handleTopicAttach:: in unless $fileMeta")
if $debug;
$_[0] .= "\n$fileMeta";
} else {
&Foswiki::Func::writeDebug( "handleTopicAttach:: in else $fileMeta") if $debug;
$_[0] =~ s/(%META:FILEATTACHMENT\{name=\"$fileName.*?\}%)/$fileMeta/;
}
else {
&Foswiki::Func::writeDebug(
"handleTopicAttach:: in else $fileMeta")
if $debug;
$_[0] =~
s/(%META:FILEATTACHMENT\{name=\"$fileName.*?\}%)/$fileMeta/;
}
}
}
Expand Down

0 comments on commit 1069d09

Please sign in to comment.