Skip to content

Commit

Permalink
Item706: New FortunePlugin - further updates
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/FortunePlugin@2140 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Jan 24, 2009
1 parent 0699a0b commit e8c7874
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 85 deletions.
19 changes: 10 additions & 9 deletions data/System/FortunePlugin.txt
@@ -1,26 +1,27 @@
%META:TOPICINFO{author="BaseUserMapping_333" date="1231212263" format="1.1" reprev="1.4" version="1.4"}%
%META:TOPICINFO{author="ProjectContributor" date="1232167030" format="1.1" reprev="1.6" version="1.6"}%
---+ Fortune Plugin
This plugin uses the Unix/Linux =fortune= command to insert a random fortune into a topic. On windows environments, or if =fortune= is not installed, it uses the CPAN =Fortune= implementation.
---++ Usage
The following macros are implemented:
* =%%NOP%FORTUNE{"database"}%= Insert a random fortune into the topic from the named "database".
* =%%NOP%FORTUNE{"database" LENGTH="S"}%= Insert a random fortune into the topic from the named "database".
* LENGTH parameter (optional) can be set to S or SHORT to select short fortunes, and L or LONG for long fortunes. This option only works if the unix =fortune-mod= program is used.
* =%%NOP%FORTUNE_LIST{"database"}%= List the contents of the named database
* =%%NOP%FORTUNE_DB_LIST{}%= List the available database files in the fortune directory.
* Set TINYMCEPLUGIN_DISABLE = 1

---++ Examples
---+++ Example: =%%NOP%FORTUNE{"foswiki"}%=
---++ Examples
---+++ Example: =%%NOP%FORTUNE{"foswiki" LENGTH="S"}%=
If installed, a random fortune from the foswiki database will appear here: <blockquote>
%FORTUNE{"foswiki"}% </blockquote>
%FORTUNE{"foswiki" LENGTH="S"}% </blockquote>

---+++
---+++ Example: =%%NOP%FORTUNE_LIST{"foswiki"}%=
If installed, the list of fortunes in the foswiki database
---+++
<blockquote>
%FORTUNE_LIST{"foswiki"}% </blockquote>

---+++ Example: =%%NOP%FORTUNE_DB_LIST{}%=
If installed, the list of fortune databases is shown.
If installed, the list of fortune databases is shown. Note that two different format results are possible. The Unix =fortune-mod= program reports the percentage of fortunes supplied by each file. The Perl =Fortune= module shows a simple fortune count.
---+++
<blockquote> %FORTUNE_DB_LIST{}% </blockquote>

Expand Down Expand Up @@ -78,5 +79,5 @@ The original =foswiki= file and the related =foswiki.dat= file must be placed in
| Home: | http://foswiki.org/Extensions/%TOPIC%Dev |
| Feedback: | http://foswiki.org/Extensions/%TOPIC%Dev |

%META:FILEATTACHMENT{name="foswiki" attachment="foswiki" attr="" comment="" date="1231128584" path="foswiki" size="1323" stream="IO::File=GLOB(0x9d599a8)" tmpFilename="/usr/tmp/CGItemp11681" user="BaseUserMapping_333" version="1"}%
%META:FILEATTACHMENT{name="foswiki.dat" attachment="foswiki.dat" attr="" comment="" date="1231128606" path="foswiki.dat" size="72" stream="IO::File=GLOB(0x97fe450)" tmpFilename="/usr/tmp/CGItemp11713" user="BaseUserMapping_333" version="1"}%
%META:FILEATTACHMENT{name="foswiki" attachment="foswiki" attr="" comment="" date="1231128584" path="foswiki" size="1323" stream="IO::File=GLOB(0x9d599a8)" tmpFilename="/usr/tmp/CGItemp11681" user="ProjectContributor" version="1"}%
%META:FILEATTACHMENT{name="foswiki.dat" attachment="foswiki.dat" attr="" comment="" date="1231128606" path="foswiki.dat" size="72" stream="IO::File=GLOB(0x97fe450)" tmpFilename="/usr/tmp/CGItemp11713" user="ProjectContributor" version="1"}%
96 changes: 66 additions & 30 deletions lib/Foswiki/Plugins/FortunePlugin.pm
Expand Up @@ -120,15 +120,6 @@ sub initPlugin {
return 0;
}

# Example code of how to get a preference value, register a macro
# handler and register a RESTHandler (remove code you do not need)

# Set your per-installation plugin configuration in LocalSite.cfg,
# like this:
# $Foswiki::cfg{Plugins}{EmptyPlugin}{ExampleSetting} = 1;
# Optional: See %SYSTEMWEB%.DevelopingPlugins#ConfigSpec for information
# on integrating your plugin configuration with =configure=.

$fortune_bin = $Foswiki::cfg{Plugins}{FortunePlugin}{FortuneProgram}
|| ""; # If not set, use Perl Fortune from CPAN

Expand All @@ -145,7 +136,7 @@ sub initPlugin {
$fortune_db = $Foswiki::cfg{Plugins}{FortunePlugin}{FortuneDBPath}
|| Foswiki::Func::getPubDir()
. "/$Foswiki::cfg{SystemWebName}"
. "/FortunePlugin";
. "/FortunePlugin/";

unless ( $fortune_db && ( -d $fortune_db ) ) {
Foswiki::Func::writeWarning('FortuneDBPath not provided or not found ');
Expand Down Expand Up @@ -177,16 +168,36 @@ for the fortune.
sub _FORTUNE {
my ( $session, $params, $theTopic, $theWeb ) = @_;

my $db = $params->{_DEFAULT} || "foswiki";
my $db = $params->{_DEFAULT} || "foswiki";
my $len = $params->{LENGTH} || "";

if ($len) {
if ( $len =~ m/^(SHORT|S)$/o ) {
$len = "-s";
}
else {
if ( $len =~ m/^(LONG|L)$/o ) {
$len = "-l";
}
else {
return
"<font color=\"red\"><nop>Fortune Error: Length paremater must be SHORT, LONG, S or L. (was: $len)</font>";
}
}
}

if ($fortune_bin) {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$fortune_bin %DATABASE|F% ",
Foswiki::Sandbox->sysCommand( "$fortune_bin %DATABASE|U% $len ",
DATABASE => "$db" );
return $output;
}
else {
my $ffile = new Fortune( "$fortune_db" . "$db" );
my $ffile = undef;
eval { $ffile = new Fortune( "$fortune_db" . "$db" ); };
if ($@) {
return " *Error - Problem handling $fortune_db $db* \n";
}
$ffile->read_header();
return ( $ffile->get_random_fortune() );
}
Expand Down Expand Up @@ -218,23 +229,29 @@ sub _FORTUNE_LIST {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$fortune_bin -m .* %DATABASE|F% ",
DATABASE => "$db" );
$output =~ s/\n/<br \/>/g; # Newlines become breaks
$output =~ s/<br \/>%<br \/>/\n<li>/g; # Percents become list entries
$output =~ s/<li>$//g; # Remove trailing entry
return "<ul><li>" . $output . "\n </ul>\n";
$output =~ s/\n/<br \/>/g; # Newlines become breaks
$output =~ s/<br \/>%<br \/>/\n<li>/g; # Percents become list entries
$output =~ s/<li>$//g; # Remove trailing entry
return "<ul><li>" . $output . "\n </ul>\n";
}
else {
my $ffile = new Fortune( "$fortune_db" . "$db" );
my $ffile = undef;
eval { $ffile = new Fortune( "$fortune_db" . "$db" ); };
if ($@) {
return " *Error - Problem handling $fortune_db $db* \n";
}
$ffile->read_header();
my $num_fortunes = $ffile->num_fortunes ();
&Foswiki::Func::writeDebug( "FortunePlugin - " . $num_fortunes . $fortune_db . $db );
my $num_fortunes = $ffile->num_fortunes();
&Foswiki::Func::writeDebug(
"FortunePlugin - " . $num_fortunes . $fortune_db . $db );
my $output = "<ul>";
for (my $i = 0; $i < $num_fortunes; $i++) {
&Foswiki::Func::writeDebug( "FortunePlugin - " . $i . " = " . $ffile->read_fortune($i) );
$output .= "<li>" . $ffile->read_fortune ($i) ;
}
$output .= "\n</ul>\n";
return $output ;
for ( my $i = 0 ; $i < $num_fortunes ; $i++ ) {
&Foswiki::Func::writeDebug(
"FortunePlugin - " . $i . " = " . $ffile->read_fortune($i) );
$output .= "<li>" . $ffile->read_fortune($i);
}
$output .= "\n</ul>\n";
return $output;
}

}
Expand All @@ -257,13 +274,32 @@ to count the number of fortunes in the file.
sub _FORTUNE_DB_LIST {
my ( $session, $params, $theTopic, $theWeb ) = @_;

#SMELL: "fortune -f" sends the results to stderr. So Sandbox cannot be used.
#SMELL: "fortune -f" sends the results to stderr. So Sandbox cannot be used.

my $output = undef;
if ($fortune_bin) {
my $output = `$fortune_bin -f 2>&1`;
return "<pre>" . $output . "\n </pre>\n";
$output = `$fortune_bin -f 2>&1`;
return "<pre>" . $output . "\n </pre>\n";
}
else {
opendir( DIR, $fortune_db )
|| die "<ERR> Can't find directory --> $fortune_db !";
my @fdbs = grep { /\.dat$/ } readdir(DIR);
@fdbs = sort (@fdbs);
$output = "\n| *Fortune Count* | *Database Name* |\n";
foreach my $fdb (@fdbs) {
$fdb = substr( $fdb, 0, -4 );
my $ffile = undef;
eval { $ffile = new Fortune( "$fortune_db" . "$fdb" ); };
if ($@) {
return " *Error - Problem handling $fortune_db.$fdb* \n";
}
$ffile->read_header();
$output .= "| " . $ffile->num_fortunes() . " | " . $fdb . " |\n";
}
$output .= "\n</ul>\n";
return $output;
}

}

1;
Expand Down
2 changes: 2 additions & 0 deletions lib/Foswiki/Plugins/FortunePlugin/MANIFEST
@@ -1,4 +1,6 @@
data/System/FortunePlugin.txt 0644
lib/Foswiki/Plugins/FortunePlugin.pm 0644
lib/Foswiki/Plugins/FortunePlugin/Config.spec 0644
pub/System/FortunePlugin/foswiki 0644
pub/System/FortunePlugin/foswiki.dat 0644

2 changes: 1 addition & 1 deletion lib/Foswiki/Plugins/FortunePlugin/build.pl
Expand Up @@ -30,7 +30,7 @@ package BuildBuild;

sub new {
my $class = shift;
return bless( $class->SUPER::new("EmptyPlugin"), $class );
return bless( $class->SUPER::new("FortunePlugin"), $class );
}

# Example: Override the build target
Expand Down
23 changes: 23 additions & 0 deletions pub/System/FortunePlugin/foswiki
@@ -0,0 +1,23 @@
Today there are a variety of wikis, one of them being the wiki you are presently looking at.
%
Foswiki can be extended with hundreds of plugins
%
In the WYSIWYG Editor, click the pick-axe symbol to switch to the raw editor
%
Subscribing to WebNotify will enable Foswiki to send you details of changes made on topics.
%
The breadcrumb displayed with a particular Foswiki topic is constructed with a topic's Parent.
Use the "More Topic Actions" link if you want to change the topic parent.
%
You can disable automatic linking of WikiWords by surrounding the text with &lt;noautolink> and &lt;/noautolink>
%
The breadcrumb of a topic shows you page hierarchy. It is constructed using a topic's parent setting, following the parents up to the web level.
%
To encourage collaboration, instead of restricting edit, allow anyone to edit topics, and use WebNotify and revision controls to deal with undesired changes.
%
If you need a list of topics, considering using the %SEARCH macro to automatically populate the list.
%
To disable the WYSIWYG editor on a topic, add the setting TINYMCEPLUGIN_DISABLE = 1 to the topic.
%
You can customize Foswiki by overriding settings in your UserName topic. For example, if you want to disable the WYSIWYG editor for all of your edits, set TINYMCEPLUGIN_DISABLE = 1 in your user topic.
%
Binary file added pub/System/FortunePlugin/foswiki.dat
Binary file not shown.
9 changes: 0 additions & 9 deletions test/unit/FortunePlugin/FortunePluginSuite.pm

This file was deleted.

36 changes: 0 additions & 36 deletions test/unit/FortunePlugin/FortunePluginTests.pm

This file was deleted.

0 comments on commit e8c7874

Please sign in to comment.