Skip to content

Commit

Permalink
Item1262: removed dependencies on Unix cp and mkdir; cleaned up most …
Browse files Browse the repository at this point in the history
…of the code to use official APIs and Foswiki::Meta; Removed depreciated API calls

git-svn-id: http://svn.foswiki.org/trunk/TopicCreatePlugin@3353 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
AndrewJones authored and AndrewJones committed Mar 31, 2009
1 parent 8dd278d commit 3e6e70c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 121 deletions.
16 changes: 5 additions & 11 deletions data/System/TopicCreatePlugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This Plugin allows you to automatically create topics or attach files at topic s

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

* Example scenario: You have a template topic and would like to attach a file whose's 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.
* 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* |
Expand All @@ -39,9 +39,9 @@ This Plugin allows you to automatically create topics or attach files at topic s
* 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:
* There should be a =name= argument to rename the topic as it is copied
* 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.
---++ 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
* There is a half-baked =%<nop>TOPICPATCH%= implementation from this plugins TWiki days which requires completing, testing and documenting. You are more than welcome to implement this if you would find it useful.

---++ Plugin Settings

Expand All @@ -53,12 +53,6 @@ Plugin settings are stored as preferences variables. To reference a plugin setti
* Debug plugin: (See output in =data/debug.txt=)
* Set DEBUG = 0

---++ Known issues

* This Plugin is Unix specific and depends on the Unix utilities =cp= and =mkdir=.
* For safety reasons, the path to the utilities is hardcoded in the Plugin module. The path might need to be changed depending on your environment.
* Precautions have been taken to make this Plugin safe. It does not have ANY WARRANTY, does not even have the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. It is up to the administrator to decide if this Plugin is safe for a public Foswiki site.

---++ Installation Instructions

%$INSTALL_INSTRUCTIONS%
Expand All @@ -77,14 +71,14 @@ 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; |
| 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) |
| 7960 | TWikibug:Item1238 - plugins cleanup: added BuildContrib support/infrastructure |
| 7585 | TWikibug:Item1016 - initial import of TopicCreatePlugin (thanks TWiki:Main.StanleyKnutson and others) |
| 21 Nov 2005: | Initial TOPICCATTACH |
| 30 Apr 2005: | Initial version |
| Dependencies: | %$DEPENDENCIES% |
| Other Dependencies: | Unix utilities =cp= and =mkdir= |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Plugin Home: | http://foswiki.org/Extensions/%TOPIC% |
| Support: | http://foswiki.org/Support/%TOPIC% |
Expand Down
8 changes: 3 additions & 5 deletions lib/Foswiki/Plugins/TopicCreatePlugin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ sub initPlugin
( $topic, $web, $user, $installWeb ) = @_;

# Get plugin debug flag
$debug = Foswiki::Func::getPreferencesFlag( "TOPICCREATEPLUGIN_DEBUG" );
#REMOVE THIS:
##$debug=1;
$debug = Foswiki::Func::getPluginPreferencesFlag( "DEBUG" );

# Plugin correctly initialized
Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK" ) if $debug;
Expand All @@ -58,7 +56,7 @@ sub beforeSaveHandler

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

require Foswiki::Plugins::TopicCreatePlugin::Func;
Expand All @@ -70,7 +68,7 @@ sub beforeSaveHandler

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

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

if ($_[0] =~ /%TOPICATTACH/){
Expand Down
144 changes: 42 additions & 102 deletions lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@ use strict;
# =========================
use vars qw(
$web $topic $user $debug
$renderingWeb
$cpCmd $mkdirCmd
);


# =========================
# Change these platform dependent settings if needed:
$cpCmd = "/bin/cp -p";
$mkdirCmd = "/bin/mkdir";


# =========================
sub init
{
Expand Down Expand Up @@ -75,17 +66,10 @@ sub handleTopicCreate
# saving the outer template itself should not invoke the create
return "%TOPICCREATE{$theArgs}% ";
}

# SMELL: shouldn't this expand all variables? (eg, if you using something like Web.%NEWTOPIC%?)
# should i just expand the loaded topic or continue expanded the variables in these variables
# (i'm concerned about the implications of expanding the topic because this can be called recursively)

# expand relevant Foswiki Macros
$topicName = Foswiki::Func::expandCommonVariables( $topicName, $theTopic, $theWeb );
$template = Foswiki::Func::expandCommonVariables( $template, $theTopic, $theWeb );
# expand relevant Foswiki Macros
# $topicName =~ s/%TOPIC%/$theTopic/go;
# $topicName =~ s/%WEB%/$theWeb/go;
# $template =~ s/%TOPIC%/$theTopic/go;
# $template =~ s/%WEB%/$theWeb/go;

my $topicWeb = $theWeb;
if( $topicName =~ /^([^\.]+)\.(.*)$/ ) {
Expand All @@ -109,17 +93,18 @@ sub handleTopicCreate
unless( &Foswiki::Func::topicExists( $templateWeb, $template ) ) {
return _errorMsg( $errVar, "Template <nop>$templateWeb.$template does not exist.");
}

my $text = &Foswiki::Func::readTopicText( $templateWeb, $template, "", 1 );
my ($meta, $text) = &Foswiki::Func::readTopic($templateWeb, $template);

# Set topic parent
# SMELL: should use $meta object
$text = _setMetaData( $text, "TOPICPARENT", $theTopic );

# SMELL: replace 'gmtime' with foswiki preferences variable (i think there's one defined for this...)
my $localDate = &Foswiki::Func::formatTime( time(), "\$day \$month \$year", "gmtime" );
$meta->putKeyed( 'TOPICPARENT', { name => $theTopic } );

# SMELL: replace with expandVariablesOnTopicCreation( $text );
# but then we seem to loose our parameters... Leaving it as it is for now
#$text = Foswiki::Func::expandVariablesOnTopicCreation( $text );

my $localDate = &Foswiki::Time::formatTime( time(), $Foswiki::cfg{DefaultDateFormat} );

my $wikiUserName = &Foswiki::Func::userToWikiName( $user );
$text =~ s/%NOP{.*?}%//gos; # Remove filler: Use it to remove access control at time of
$text =~ s/%NOP%//go; # topic instantiation or to prevent search from hitting a template
Expand Down Expand Up @@ -148,29 +133,27 @@ sub handleTopicCreate
}
# END SMELL

# Copy Attachments over
my $pubDir = &Foswiki::Func::getPubDir();
if( -e "$pubDir/$templateWeb/$template" ) {
# Right now if topic already exists, it silently fails above,
# need to fix this if something else happens
`$mkdirCmd $pubDir/$topicWeb/$topicName`;
`$cpCmd $pubDir/$templateWeb/$template/* $pubDir/$topicWeb/$topicName/`;
# Copy all Attachments over
my @attachments = $meta->find( 'FILEATTACHMENT' );
foreach my $attach ( @attachments ){
my $fileName = $attach->{ 'path' } || $attach->{ 'attachment' } || $attach->{ 'name' };
# TODO: We could keep the comment, date of upload, etc
_copyAttachment( $templateWeb, $template, $fileName, $topicWeb, $topicName, $fileName );
}

# Recursively handle TOPICCREATE and TOPICATTCH
$text =~ s/%TOPICCREATE{(.*)}%[\n\r]*/handleTopicCreate( $1, $theWeb, $topicName )/geo;
$text =~ s/%TOPICATTCH{(.*)}%[\n\r]*/handleTopicAttach( $1, $theWeb, $topicName )/geo;

my $error = &Foswiki::Func::saveTopicText( $topicWeb, $topicName, $text, 1, "dont notify" );

if( $error ) {
return "%RED%Error saving $topicName%ENDCOLOR%$error";
}
#my $error = &Foswiki::Func::saveTopicText( $topicWeb, $topicName, $text, 1, "dont notify" );
&Foswiki::Func::saveTopic( $topicWeb, $topicName, $meta, $text, { minor => 1 } );

return "";
}

# =========================
# Untested and Undocumented, comes from this plugins TWiki days
# Feel free to complete and test this if you need it
sub handleTopicPatch
{
my( $theArgs, $theWeb, $theTopic, $theTopicText ) = @_;
Expand Down Expand Up @@ -201,6 +184,10 @@ sub handleTopicPatch
}

$text = _setMetaData( $text, "FIELD", $value, $formfield );
#$meta->putKeyed( 'FIELD', {
# name => $formfield,
# value => $value
#});

my $error = Foswiki::Func::saveTopicText( $theWeb, $topicName, $text, "", "dont notify" );

Expand Down Expand Up @@ -250,14 +237,16 @@ sub handleTopicAttach
# Copy attachment over
if( _existAttachment( $fromTopicWeb, $fromTopic, $fromFile ) ) {
_copyAttachment( $fromTopicWeb, $fromTopic, $fromFile, $web, $topic, $name );
# FIXME: use Foswiki::Func::readTopic( $web, $topic, $rev ) -> ( $meta, $text );
# then use the Meta object
my $fromTopicText = &Foswiki::Func::readTopicText( $fromTopicWeb, $fromTopic, "", 1 );
$fromTopicText =~ m/(%META:FILEATTACHMENT\{name=\"$fromFile.*?\}%)/;
my $attachInfo = $1;
$attachInfo =~ s/attr="h"/attr=""/;
$attachInfo =~ s/name=".*" /name="$name" /;
if ($attachComment) {
$attachInfo =~ s/comment=".*" /comment="$attachComment" /;
}
my $attachInfo = $1;
$attachInfo =~ s/attr="h"/attr=""/;
$attachInfo =~ s/name=".*" /name="$name" /;
if ($attachComment) {
$attachInfo =~ s/comment=".*" /comment="$attachComment" /;
}
push @$attachMetaDataRef, ($attachInfo);
} else {
&Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::handleTopicAttach:: $fromFile does not exist in $fromTopicWeb/$fromTopic" ) if $debug;
Expand All @@ -266,49 +255,6 @@ sub handleTopicAttach
return "";
}

# =========================
sub _setMetaData
{
my( $theText, $theMeta, $theValue, $theName) = @_;

if( $theMeta =~ /^(FILEATTACHMENT|FIELD)$/ ) {
$theText =~ s/(%META:FIELD{name\=\"$theName\".*value=\")[^\"]*/$1$theValue/;
return $theText;
}

if( $theText =~ s/(\%META:$theMeta.*?name=\")[^\"]*/$1$theValue/o ) {
# replaced existing meta data
return $theText;
}
if( $theMeta eq "TOPICPARENT" ) {
$theText = "\%META:TOPICPARENT\{name=\"$theValue\"\}\%\n" . $theText;

} else {
$theText =~ s/\n?\r?$/\n\%META:$theMeta\{name=\"$theValue\"\}\%\n/o;
}

return $theText;
}

# =========================
sub _getMetaData
{
my ( $theText, $theMeta, $theName ) = @_;
my $value = "";

if ( $theMeta =~ m/^(FILEATTACHMENT|FIELD)$/ ) {
return "" unless ( $theText =~ m/%META:$theMeta\{name\=\"$theName\".*value=\"([^\"]*)/ );
$value = $1 || "";
return $value;
} elsif ( $theMeta ) {
return "" unless ( $theText =~ m/%META:$theMeta\{name\=\"([^\"]*)/ );
$value = $1 || "";
return $value;
}

return $value;
}

# =========================
sub _errorMsg
{
Expand All @@ -326,28 +272,22 @@ sub _getAttachmentList
sub _existAttachment
{
my ( $theWeb, $theTopic, $theFile ) = @_;

my $pubDir = &Foswiki::Func::getPubDir();

&Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::checking $pubDir/$theWeb/$theTopic/$theFile");

return( -e "$pubDir/$theWeb/$theTopic/$theFile" );

return Foswiki::Func::attachmentExists( $theWeb, $theTopic, $theFile );
}

# =========================
sub _copyAttachment
{
my ( $fromWeb, $fromTopic, $fromFile, $toWeb, $toTopic, $toFile ) = @_;

my $pubDir = &Foswiki::Func::getPubDir();
unless( -e "$pubDir/$toWeb/$toTopic") {
`$mkdirCmd $pubDir/$toWeb/$toTopic`;
}
# IMPLICIT ASSUMPTION of RCS backend storage, should really use storage api
`$cpCmd $pubDir/$fromWeb/$fromTopic/$fromFile $pubDir/$toWeb/$toTopic/$toFile`;
`$cpCmd $pubDir/$fromWeb/$fromTopic/$fromFile,v $pubDir/$toWeb/$toTopic/$toFile,v`;
&Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::copyAttachment from $fromWeb/$fromTopic/$fromFile to $toWeb/$toTopic/$toFile -- $cpCmd $pubDir/$fromWeb/$fromTopic/$fromFile,v $pubDir/$toWeb/$toTopic/$toFile,v") if $debug;


my $pubDir = $Foswiki::cfg{PubDir};

my $filePath = "$pubDir/$fromWeb/$fromTopic/$fromFile";

Foswiki::Func::saveAttachment( $toWeb, $toTopic, $toFile, { file => $filePath } );

&Foswiki::Func::writeDebug( "- Foswiki::Plugins::TopicCreatePlugin::copyAttachment from $fromWeb/$fromTopic/$fromFile to $toWeb/$toTopic/$toFile") if $debug;
}

1;
Expand Down
6 changes: 3 additions & 3 deletions lib/Foswiki/Plugins/TopicCreatePlugin/MANIFEST
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
lib/Foswiki/Plugins/TopicCreatePlugin.pm
lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm
data/System/TopicCreatePlugin.txt
lib/Foswiki/Plugins/TopicCreatePlugin.pm 0644 Perl module
lib/Foswiki/Plugins/TopicCreatePlugin/Func.pm 0644 Perl module
data/System/TopicCreatePlugin.txt 0644 Documentation

0 comments on commit 3e6e70c

Please sign in to comment.