| 1 | # Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2005-2006 Peter Thoeny, peter@thoeny.org |
|---|
| 4 | # |
|---|
| 5 | # For licensing info read LICENSE file in the TWiki root. |
|---|
| 6 | # This program is free software; you can redistribute it and/or |
|---|
| 7 | # modify it under the terms of the GNU General Public License |
|---|
| 8 | # as published by the Free Software Foundation; either version 2 |
|---|
| 9 | # of the License, or (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details, published at |
|---|
| 15 | # http://www.gnu.org/copyleft/gpl.html |
|---|
| 16 | # |
|---|
| 17 | # As per the GPL, removal of this notice is prohibited. |
|---|
| 18 | |
|---|
| 19 | package TWiki::Plugins::TopicCreatePlugin; |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | # ========================= |
|---|
| 23 | use vars qw( |
|---|
| 24 | $web $topic $user $installWeb $VERSION $debug $doInit |
|---|
| 25 | ); |
|---|
| 26 | |
|---|
| 27 | $VERSION = '1.000'; # 30 Apr 2005 |
|---|
| 28 | $doInit = 0; |
|---|
| 29 | |
|---|
| 30 | # ========================= |
|---|
| 31 | sub initPlugin |
|---|
| 32 | { |
|---|
| 33 | ( $topic, $web, $user, $installWeb ) = @_; |
|---|
| 34 | |
|---|
| 35 | # check for Plugins.pm versions |
|---|
| 36 | if( $TWiki::Plugins::VERSION < 1 ) { |
|---|
| 37 | TWiki::Func::writeWarning( "Version mismatch between TopicCreatePlugin and Plugins.pm" ); |
|---|
| 38 | return 0; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | # Get plugin debug flag |
|---|
| 42 | $debug = TWiki::Func::getPreferencesFlag( "TOPICCREATEPLUGIN_DEBUG" ); |
|---|
| 43 | #REMOVE THIS: |
|---|
| 44 | ##$debug=1; |
|---|
| 45 | |
|---|
| 46 | # Plugin correctly initialized |
|---|
| 47 | TWiki::Func::writeDebug( "- TWiki::Plugins::TopicCreatePlugin::initPlugin( $web.$topic ) is OK" ) if $debug; |
|---|
| 48 | $doInit = 1; |
|---|
| 49 | return 1; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # ========================= |
|---|
| 53 | sub beforeSaveHandler |
|---|
| 54 | { |
|---|
| 55 | ### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead |
|---|
| 56 | |
|---|
| 57 | TWiki::Func::writeDebug( "- TopicCreatePlugin::beforeSaveHandler( $_[2].$_[1] )" ) if $debug; |
|---|
| 58 | |
|---|
| 59 | unless( $_[0] =~ /%TOPIC(CREATE|ATTACH)\{.*?\}%/ ) { |
|---|
| 60 | # nothing to do |
|---|
| 61 | return; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | require TWiki::Plugins::TopicCreatePlugin::Func; |
|---|
| 65 | |
|---|
| 66 | if( $doInit ) { |
|---|
| 67 | $doInit = 0; |
|---|
| 68 | TWiki::Plugins::TopicCreatePlugin::Func::init( $web, $topic, $user, $debug ); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | $_[0] =~ s/%TOPICCREATE{(.*)}%[\n\r]*/TWiki::Plugins::TopicCreatePlugin::Func::handleTopicCreate($1, $_[2], $_[1], $_[0] )/geo; |
|---|
| 72 | |
|---|
| 73 | # To be tested and documented |
|---|
| 74 | # $_[0] =~ s/%TOPICPATCH{(.*)}%[\n\r]*/TWiki::Plugins::TopicCreatePlugin::Func::handleTopicPatch($1, $_[2], $_[1], $_[0] )/geo; |
|---|
| 75 | |
|---|
| 76 | if ($_[0] =~ /%TOPICATTACH/){ |
|---|
| 77 | my @attachMetaData = (); |
|---|
| 78 | $_[0] =~ s/%TOPICATTACH{(.*)}%[\n\r]*/TWiki::Plugins::TopicCreatePlugin::Func::handleTopicAttach($1, \@attachMetaData)/geo; |
|---|
| 79 | my $fileName = ""; |
|---|
| 80 | foreach my $fileMeta ( @attachMetaData ) { |
|---|
| 81 | $fileMeta =~ m/META:FILEATTACHMENT\{name\=\"(.*?)\"/; |
|---|
| 82 | $fileName = $1; |
|---|
| 83 | unless ($_[0] =~ m/META:FILEATTACHMENT\{name\=\"$fileName/ ) { |
|---|
| 84 | &TWiki::Func::writeDebug( "handleTopicAttach:: in unless $fileMeta") if $debug; |
|---|
| 85 | $_[0] .= "\n$fileMeta"; |
|---|
| 86 | } else { |
|---|
| 87 | &TWiki::Func::writeDebug( "handleTopicAttach:: in else $fileMeta") if $debug; |
|---|
| 88 | $_[0] =~ s/(%META:FILEATTACHMENT\{name=\"$fileName.*?\}%)/$fileMeta/; |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | 1; |
|---|
| 95 | |
|---|
| 96 | # EOF |
|---|