| 1 | package CGI::Wiki::Simple::Plugin; | ||
| 2 | |||
| 3 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 0 | ▶ | use CGI::Wiki; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use CGI::Wiki::Simple; | ||
| 7 | use Carp qw(croak); | ||
| 8 | use Digest::MD5 qw( md5_hex ); | ||
| 9 | |||
| 10 | use vars qw($VERSION); | ||
| 11 | |||
| 12 | $VERSION = 0.09; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | CGI::Wiki::Simple::Plugin - Base class for CGI::Wiki::Simple plugins. | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | This is the base class for special interactive Wiki nodes where the content | ||
| 21 | is produced programmatically, like the LatestChanges page and the AllNodes | ||
| 22 | page. A plugin subclass implements | ||
| 23 | more or less the same methods as a CGI::Wiki::Store - a later refactoring | ||
| 24 | might convert all Plugin-subclasses to CGI::Wiki::Store subclasses or vice-versa. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | =for example begin | ||
| 29 | |||
| 30 | package CGI::Wiki::Simple::Plugin::MyPlugin; | ||
| 31 | use strict; | ||
| 32 | use Carp qw(croak); | ||
| 33 | use CGI::Wiki::Simple::Plugin( name => 'MyPlugin' ); | ||
| 34 | |||
| 35 | sub retrieve_node_data { | ||
| 36 | my ($wiki) = shift; | ||
| 37 | |||
| 38 | my %args = scalar @_ == 1 ? ( name => $_[0] ) : @_; | ||
| 39 | croak "No valid node name supplied" | ||
| 40 | unless $args{name}; | ||
| 41 | |||
| 42 | # $args{name} is the node name | ||
| 43 | # $args{version} is the node version, if no version is passed, means current | ||
| 44 | # ... now actually retrieve the content ... | ||
| 45 | my @results = ("Hello world",0,""); | ||
| 46 | |||
| 47 | my %data; | ||
| 48 | @data{ qw( content version last_modified ) } = @results; | ||
| 49 | $data{checksum} = md5_hex($data{content}); | ||
| 50 | return wantarray ? %data : $data{content}; | ||
| 51 | }; | ||
| 52 | |||
| 53 | # Alternatively, if your plugin can handle more than one node : | ||
| 54 | package CGI::Wiki::Simple::Plugin::MyMultiNodePlugin; | ||
| 55 | use strict; | ||
| 56 | use CGI::Wiki::Simple::Plugin (); # No automatic import | ||
| 57 | |||
| 58 | sub import { | ||
| 59 | my ($module,@nodenames) = @_; | ||
| 60 | CGI::Wiki::Simple::Plugin::register_nodes(module => $module, names => [@nodenames]); | ||
| 61 | }; | ||
| 62 | |||
| 63 | =for example end | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub import { | ||
| 68 | my ($class,%args) = @_; | ||
| 69 | my ($module) = caller; | ||
| 70 | my %names; | ||
| 71 | |||
| 72 | for (qw(name names)) { | ||
| 73 | if (exists $args{$_}) { | ||
| 74 | if (ref $args{$_}) { | ||
| 75 | for (@{$args{$_}}) { | ||
| 76 | $names{$_} = 1 | ||
| 77 | }; | ||
| 78 | } else { | ||
| 79 | $names{$args{$_}} = 1; | ||
| 80 | }; | ||
| 81 | }; | ||
| 82 | }; | ||
| 83 | |||
| 84 | register_nodes(module => $module, names => [sort keys %names]); | ||
| 85 | }; | ||
| 86 | |||
| 87 | sub register_nodes { | ||
| 88 | my (%args) = @_; | ||
| 89 | my ($module) = $args{module}; | ||
| 90 | my (%names); | ||
| 91 | |||
| 92 | for (qw(name names)) { | ||
| 93 | if (exists $args{$_}) { | ||
| 94 | if (ref $args{$_}) { | ||
| 95 | for (@{$args{$_}}) { | ||
| 96 | $names{$_} = 1 | ||
| 97 | }; | ||
| 98 | } else { | ||
| 99 | $names{$args{$_}} = 1; | ||
| 100 | }; | ||
| 101 | }; | ||
| 102 | }; | ||
| 103 | my @names = keys %names; | ||
| 104 | croak "Need the node name as which to install $module" | ||
| 105 | unless @names; | ||
| 106 | |||
| 107 | # Install our callback to the plugin | ||
| 108 | no strict 'refs'; | ||
| 109 | my $handler = $args{code} || \&{"${module}::retrieve_node"}; | ||
| 110 | |||
| 111 | for (@names) { | ||
| 112 | $CGI::Wiki::Simple::magic_node{$_} = sub { | ||
| 113 | my $wiki = shift; | ||
| 114 | my %args = scalar @_ == 1 ? ( name => $_[0] ) : @_; | ||
| 115 | $args{wiki} = $wiki; | ||
| 116 | croak "No valid node name supplied" unless $args{name}; | ||
| 117 | my @results = $handler->( %args ); | ||
| 118 | @results = ("", 0, "") unless scalar @results; | ||
| 119 | my %data; | ||
| 120 | @data{ qw( content version last_modified ) } = @results; | ||
| 121 | $data{checksum} = md5_hex($data{content}); | ||
| 122 | return wantarray ? %data : $data{content}; | ||
| 123 | }; | ||
| 124 | }; | ||
| 125 | }; | ||
| 126 | |||
| 127 | 1; | ||