Skip to content

Commit

Permalink
Item9925: replace smelly direct file writes with a Foswiki::Func call
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/ChartPlugin@11481 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
CrawfordCurrie authored and CrawfordCurrie committed Apr 17, 2011
1 parent e37b1e1 commit 01aeda9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
27 changes: 14 additions & 13 deletions data/System/ChartPlugin.txt
Expand Up @@ -186,7 +186,7 @@ If the required parameters are not defined, then an error message is returned or
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- specify latest version first -->&nbsp; |
| 17 Apr 2011: | v1.6.0. Foswiki:Main.CrawfordCurrie supported range inversion, added unit tests |
| 17 Apr 2011: | v1.6.0. Foswiki:Main.CrawfordCurrie supported range inversion, added unit tests, fixed file permissions |
| 13 Dec 2010: | v1.5.2. Foswiki:Main.OliverKrueger fixed a problem with subwebs. |
| 03 Nov 2010: | v1.5.1. Clarified the meaning of the data parameter for scatter diagrams |
| 07 Apr 2010: | Foswiki:Main.WillNorris: better parameter validation, minor doc updates |
Expand All @@ -212,15 +212,16 @@ If the required parameters are not defined, then an error message is returned or
__Related Topics:__ %SYSTEMWEB%.DefaultPreferences, %USERSWEB%.SitePreferences, [[%SYSTEMWEB%.Plugins][Plugins]]


%META:FILEATTACHMENT{name="line1.png" attr="h" comment="" date="1093917753" path="line1.png" size="1220" user="PeterThoeny" version="1.4"}%
%META:FILEATTACHMENT{name="line2.png" attr="h" comment="" date="1093917765" path="line2.png" size="1993" user="PeterThoeny" version="1.4"}%
%META:FILEATTACHMENT{name="lineexample.png" attr="h" comment="" date="1084659332" path="C:\DATA\MyTWiki\Plugins\ChartPlugin\pub\TWiki\ChartPlugin\lineexample.png" size="2462" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="areaexample.png" attr="h" comment="" date="1024533210" path="C:\Data\MyTWiki\Plugins\ChartPlugin\pub\TWiki\ChartPlugin\areaexample.png" size="3370" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="combo1.png" attr="h" comment="" date="1093917959" path="combo1.png" size="1696" user="PeterThoeny" version="1.2"}%
%META:FILEATTACHMENT{name="comboexample.png" attr="h" comment="" date="1084659314" path="C:\DATA\MyTWiki\Plugins\ChartPlugin\pub\TWiki\ChartPlugin\comboexample.png" size="3071" user="PeterThoeny" version="1.2"}%
%META:FILEATTACHMENT{name="scatter1.png" attr="h" comment="" date="1093917951" path="scatter1.png" size="2071" user="PeterThoeny" version="1.2"}%
%META:FILEATTACHMENT{name="scatterexample.png" attr="h" comment="" date="1093918171" path="scatterexample.png" size="1858" user="PeterThoeny" version="1.2"}%
%META:FILEATTACHMENT{name="area1.png" attr="h" comment="" date="1093917730" path="area1.png" size="1774" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="bar1.png" attr="h" comment="" date="1093917742" path="bar1.png" size="1041" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="combo2.png" attr="h" comment="" date="1093917971" path="combo2.png" size="1583" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="barexample.png" attr="h" comment="" date="1093918158" path="barexample.png" size="1886" user="PeterThoeny" version="1.1"}%
%META:FILEATTACHMENT{name="line1.png" attr="h" date="1093917753"}%
%META:FILEATTACHMENT{name="line2.png" attr="h" date="1093917765"}%
%META:FILEATTACHMENT{name="lineexample.png" attr="h" date="1084659332"}%
%META:FILEATTACHMENT{name="areaexample.png" attr="h" date="1024533210"}%
%META:FILEATTACHMENT{name="combo1.png" attr="h" date="1093917959"}%
%META:FILEATTACHMENT{name="comboexample.png" attr="h" date="1084659314"}%
%META:FILEATTACHMENT{name="scatter1.png" attr="h" date="1093917951"}%
%META:FILEATTACHMENT{name="scatterexample.png" attr="h" date="1093918171"}%
%META:FILEATTACHMENT{name="area1.png" attr="h" date="1093917730"}%
%META:FILEATTACHMENT{name="bar1.png" attr="h" date="1093917742"}%
%META:FILEATTACHMENT{name="combo2.png" attr="h" date="1093917971"}%
%META:FILEATTACHMENT{name="barexample.png" attr="h" date="1093918158"}%
%META:FILEATTACHMENT{name="logo.png" attr="h" date="1093918158"}%
22 changes: 6 additions & 16 deletions lib/Foswiki/Plugins/ChartPlugin.pm
Expand Up @@ -198,11 +198,9 @@ sub _min {
return $v2;
}

# Generate the file name in which the graphic file will be placed. Also
# make sure that the directory in which the graphic file will be placed
# exists. If not, create it.
# Generate the file name in which the graphic file will be placed.
sub _make_filename {
my ( $type, $name, $topic, $web ) = @_;
my ( $type, $name ) = @_;
# Generate the file name to be created
my $fullname;
# If GD version 1.19 or earlier, then create gif files else png files.
Expand All @@ -212,13 +210,7 @@ sub _make_filename {
$fullname = "_ChartPlugin_${type}_${name}.gif";
}

# before save, create directories if they don't exist.
my $tempPath = Foswiki::Func::getPubDir() . "/$web/$topic";
use File::Path;
eval { File::Path::mkpath( $tempPath, 0, $Foswiki::cfg{RCS}{dirPermission} ); };

# Return both the directory and the filename
return ($tempPath, $fullname);
return $fullname;
}

# This routine returns an red colored error message.
Expand Down Expand Up @@ -428,11 +420,9 @@ sub _makeChart {
$DataValueDefault = '' if ($DataValueDefault eq "none");
$chart->setDefaultDataValue($DataValueDefault);

# Get the name of the directory and filename in which to create the
# graphics file.
my ($dir, $filename) = _make_filename($type, $name, $topic, $web);
$chart->setFileDir($dir);
$chart->setFileName($filename);
# Get the filename in which to create the graphics file.
my $filename = _make_filename($type, $name);
$chart->setAttachmentName($web, $topic, $filename);

# Validate the legend data making sure it only specifies a single row
# or a single column.
Expand Down
39 changes: 21 additions & 18 deletions lib/Foswiki/Plugins/ChartPlugin/Chart.pm
Expand Up @@ -112,11 +112,7 @@
# setColors(@c) - Set array of colors to be used by each data set.
# getColors - Get array of colors
#
# setFileDir($dir) - Set the directory in which the created chart will be placed.
# getFileDir - Get directory in which the chart is to be placed.
#
# setFileName($name) - Set the name of the chart file
# getFileName - Get the name oft he chart file
# setAttachmentName($web, $topic, $name) - Set the name of the chart file
#
# setMargin($margin) - Set the margin (in pixels) allocated around the
# entire chart.
Expand Down Expand Up @@ -201,8 +197,7 @@ use POSIX;
setLineColors getLineColors
setColors getColors
setGridColor getGridColor
setFileDir getFileDir
setFileName getFileName
setAttachmentName
setMargin getMargin
setPointSize getPointSize
setLineWidth getLineWidth
Expand Down Expand Up @@ -415,11 +410,12 @@ sub getColors { my ($this) = @_; return @{$$this{COLORS}}; }
sub setGridColor { my ($this, @gridColor) = @_; $$this{GRID_COLOR} = \@gridColor; }
sub getGridColor { my ($this) = @_; return @{$$this{GRID_COLOR}}; }

sub setFileDir { my ($this, $dir) = @_; $$this{FILE_DIR} = $dir; }
sub getFileDir { my ($this) = @_; return $$this{FILE_DIR}; }

sub setFileName { my ($this, $name) = @_; $$this{FILE_NAME} = $name; }
sub getFileName { my ($this) = @_; return $$this{FILE_NAME}; }
sub setAttachmentName {
my ($this, $web, $topic, $name) = @_;
$this->{WEB} = $web;
$this->{TOPIC} = $topic;
$this->{FILE_NAME} = $name;
}

sub setMargin { my ($this, $margin) = @_; $$this{MARGIN} = $margin; }
sub getMargin { my ($this) = @_; return $$this{MARGIN}; }
Expand Down Expand Up @@ -1469,18 +1465,25 @@ sub makeChart {

########################################################################
# OK, the chart is all drawn so all we need to do is write it out to
# the specified file.
my $dir = $this->getFileDir();
my $filename = $this->getFileName();
umask( 002 );
open(IMAGE, ">$dir/$filename") or return "Can't create file '$dir/$filename: $!";
binmode IMAGE;
# a file.
my $workfile = Foswiki::Func::getWorkArea('ChartPlugin');
my $tmpfile = "$workfile/$$".time();
open(IMAGE, ">$tmpfile");
binmode(IMAGE);
if( $GD::VERSION > 1.19 ) {
print IMAGE $im->png;
} else {
print IMAGE $im->gif;
}
close IMAGE;
# And save it as an attachment. Note that there are no access control checks!
my $topicObject = new Foswiki::Meta($Foswiki::Plugins::SESSION, $this->{WEB}, $this->{TOPIC});
$topicObject->attach( name => $this->{FILE_NAME},
dontlog => 1,
hide => 1,
file => $tmpfile,
notopicchange => 1);
unlink($tmpfile);
return undef;
}

Expand Down
Binary file added pub/System/ChartPlugin/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01aeda9

Please sign in to comment.