#! perl -w # Plugin for TWiki Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2003 Martin@Cleaver.org # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it # Copyright (C) 2001-2003 Peter Thoeny, peter@thoeny.com # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # # Updated: 6/2007 by Ross Kramer # -- Now more Dakar compliant # ========================= use strict; use TWiki; use TWiki::Func; use TWiki::Render; use TWiki::Meta; # ========================= package TWiki::Plugins::MsOfficeAttachmentsAsHTMLPlugin; # ========================= use vars qw( $web $topic $user $installWeb $VERSION $RELEASE $pluginName $debug $xslDoc $replacementNote ); $VERSION = '1.010'; $RELEASE = 'Dakar'; $pluginName = 'MsOfficeAttachmentsAsHTMLPlugin'; # Name of this Plugin # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if( $TWiki::Plugins::VERSION < 1 ) { TWiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" ); return 0; } # Get plugin debug flag $replacementNote = 'This text was automatically generated from the attachment $attachment' ."\n".'%INCLUDE{%ATTACHURL%/$convertedAttachmentPath}%'."\n"; # Plugin correctly initialized TWiki::Func::writeDebug( "- TWiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ); return 1; } # ========================= sub beforeAttachmentSaveHandler { ### my ( $attachmentAttr, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead my $attachmentAttr = $_[0]; my $topic = $_[1]; my $web = $_[2]; writeDebug( "- ${pluginName}::beforeAttachmentSaveHandler( $_[2].$_[1])"); writeDebug("attributes: $attachmentAttr"); foreach my $attribute (keys %$attachmentAttr) { writeDebug("$attribute = ". $attachmentAttr->{$attribute}); } my $attachmentName = $attachmentAttr->{"attachment"}; writeDebug("Hmm. Got a $attachmentName"); unless ($attachmentName =~ m/.doc$/) { writeDebug("Not converting - this is not a .doc\n"); return; } writeDebug("Converting $attachmentName"); eval { my $errors = convert($attachmentAttr->{"tmpFilename"}, $attachmentName); TWiki::Func::writeWarning("$web.$topic ".$attachmentAttr->{"user"}." ".$errors); }; TWiki::Func::writeWarning("$! $@") if ($@); # Now, how do I give the user an error? # This handler is called by TWiki::Store::saveAttachment just before the save action. # New hook in TWiki::Plugins $VERSION = '1.010?' } sub writeDebug { my ($s) = @_; TWiki::Func::writeDebug($s); # if $debug; } sub convert { # to HTML my ($filename, $attachmentName) = @_; my $errors = ""; # Even though getPubDir is deprecated, it still works and I didn't have the patience to use it's replacments: # readAttachment or saveAttachment. Anyone else want to change it over, feel free :-) # I also eliminated the extra subdir of /$web/$topic/_MsOfficeAttachmentsAsHTMLPlugin. It seemed to make TWiki happier. my $attachdir = TWiki::Func::getPubDir()."/$web/$topic"; writeDebug("\$attachdir = $attachdir"); unless (-d $attachdir) { system("mkdir -p $attachdir") || writeDebug("Couldn't make $attachdir"); } # This also seemed to make setting up Include in the Topic happier by changing the extension to .txt instead of .html # my $convertedAttachmentFile = $attachmentName.".html"; my $convertedAttachmentFile = $attachmentName.".txt"; my $convertedAttachmentPath = $convertedAttachmentFile; my $cmd = "/usr/bin/wvHtml --targetdir=$attachdir $filename $convertedAttachmentFile"; writeDebug($cmd); # Here's were I cheated. I create a temp file called $web.$topic.txt in /tmp and put "$attachmentName:$convertedAttachmentPath" into it. # This way I was able to bring over those two variables to the afterSaveHandler which calls replaceTextWithIncludeFromAttachment # without jumping through too many hoops. open(JUNK, ">/tmp/$web.$topic.txt"); print JUNK "$attachmentName:$convertedAttachmentPath"; close(JUNK); my $x = `$cmd 2>&1`; writeDebug($x); writeDebug("after"); } sub replaceTextWithIncludeFromAttachment { my ($attachmentName, $convertedAttachmentPath) = @_; my $oopsUrl = TWiki::Func::setTopicEditLock( $web, $topic, 1 ); my $text = TWiki::Func::readTopicText( $web, $topic ); writeDebug("Web = $web Topic = $topic"); writeDebug("Before: \$text = $text"); $replacementNote =~ s/\$attachment/$attachmentName/; $replacementNote =~ s/\$convertedAttachmentPath/$convertedAttachmentPath/; $text .= $replacementNote; $oopsUrl = TWiki::Func::saveTopicText( $web, $topic, $text, 1, 0 ); # save topic text TWiki::Func::setTopicEditLock( $web, $topic, 0 ); # unlock topic my $newtext = TWiki::Func::readTopicText( $web, $topic ); writeDebug("Current Topic Page: $newtext"); if( $oopsUrl ) { my $err = "can't get lock on $web.$topic text\n"; writeDebug("$err - $oopsUrl"); return; } writeDebug("Ok. writing $web.$topic\n"); return; } sub afterSaveHandler { # do not uncomment, use $_[0], $_[1]... instead my $text = $_[0]; my $topic = $_[1]; my $web = $_[2]; my @chimp; # We can use $web and $topic to find the associated cheat file in /tmp that we wrote in convert my $cheatfile = "/tmp/$web.$topic.txt"; if (-e $cheatfile) { open(ZOO, "/tmp/$web.$topic.txt"); while () { my($chimpname, $chimppath) = split(":", $_); push (@chimp, $chimpname); push (@chimp, $chimppath); } close(ZOO); # Clean up the uneeded file unlink("/tmp/$web.$topic.txt"); # And now we got our variables to feed to replaceTextWithIncludeFromAttachment replaceTextWithIncludeFromAttachment($chimp[0], $chimp[1]); } writeDebug("afterSaveHandler called: \$text = $text\n\n\$topic = $topic, \$web = $web"); TWiki::Func::writeDebug( "- ${pluginName}::afterSaveHandler( $_[2].$_[1] )" ) if $debug; } 1;