| 1 | package DBIx::dbMan::Extension::Extensions; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Text::FormatTable; | ||
| 6 | |||
| 7 | our $VERSION = '0.06'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | sub IDENTIFICATION { return "000001-000008-000006"; } | ||
| 12 | |||
| 13 | sub preference { return 0; } | ||
| 14 | |||
| 15 | sub known_actions { return [ qw/EXTENSION/ ]; } | ||
| 16 | |||
| 17 | sub menu { | ||
| 18 | my $obj = shift; | ||
| 19 | |||
| 20 | my @ext_reload = (); | ||
| 21 | my @ext_unload = (); | ||
| 22 | for my $ext (sort { my $na = $a; my $nb = $b; | ||
| 23 | $na =~ s/=.*$//; $na =~ s/^.*:://; | ||
| 24 | $nb =~ s/=.*$//; $nb =~ s/^.*:://; $na cmp $nb; } | ||
| 25 | @{$obj->{-core}->{extensions}}) { | ||
| 26 | $_ = $ext; s/=.*$//; s/^.*:://; | ||
| 27 | push @ext_reload, | ||
| 28 | { label => $_, action => { action => 'EXTENSION', | ||
| 29 | operation => 'reload', what => $_ } }; | ||
| 30 | push @ext_unload, | ||
| 31 | { label => $_, action => { action => 'EXTENSION', | ||
| 32 | operation => 'unload', what => $_ } }; | ||
| 33 | } | ||
| 34 | |||
| 35 | return ( { label => 'dbMan', submenu => [ | ||
| 36 | { label => 'Extensions', submenu => [ | ||
| 37 | { label => 'Show', preference => 50, submenu => [ | ||
| 38 | { label => 'Sort by name', preference => 2, | ||
| 39 | action => { action => 'EXTENSION', operation => 'show', | ||
| 40 | sort => 'name' } }, | ||
| 41 | { label => 'Sort by id', preference => 1, | ||
| 42 | action => { action => 'EXTENSION', operation => 'show', | ||
| 43 | sort => 'id' } }, | ||
| 44 | { label => 'Sort by preference', preference => 3, | ||
| 45 | action => { action => 'EXTENSION', operation => 'show', | ||
| 46 | sort => 'preference' } }, | ||
| 47 | ] }, | ||
| 48 | { separator => 1, preference => 25 }, | ||
| 49 | { label => 'Reload', preference => 2, submenu => \@ext_reload }, | ||
| 50 | { label => 'Unload', preference => 1, submenu => \@ext_unload }, | ||
| 51 | ] } ] } ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub handle_action { | ||
| 55 | my ($obj,%action) = @_; | ||
| 56 | |||
| 57 | $action{processed} = 1; | ||
| 58 | if ($action{action} eq 'EXTENSION') { | ||
| 59 | if ($action{operation} eq 'show') { | ||
| 60 | my $table = new Text::FormatTable '| r | l | l |'; | ||
| 61 | $table->rule; | ||
| 62 | $table->head('PRI','NAME','IDENTIFICATION'); | ||
| 63 | $table->rule; | ||
| 64 | my $sort_crit = $action{sort}; | ||
| 65 | for my $ext (sort { | ||
| 66 | if ($sort_crit eq 'id') { | ||
| 67 | $a->IDENTIFICATION | ||
| 68 | cmp $b->IDENTIFICATION; | ||
| 69 | } elsif ($sort_crit eq 'name') { | ||
| 70 | my $na = $a; my $nb = $b; | ||
| 71 | $na =~ s/=.*$//; | ||
| 72 | $na =~ s/^.*:://; | ||
| 73 | $nb =~ s/=.*$//; | ||
| 74 | $nb =~ s/^.*:://; | ||
| 75 | $na cmp $nb; | ||
| 76 | } else { | ||
| 77 | $b->preference | ||
| 78 | <=> $a->preference; | ||
| 79 | } | ||
| 80 | } @{$obj->{-core}->{extensions}}) { | ||
| 81 | my $name = $ext; | ||
| 82 | $name =~ s/=.*$//; $name =~ s/^.*:://; | ||
| 83 | my $id = $ext->IDENTIFICATION; | ||
| 84 | $table->row($ext->preference,$name,$id); | ||
| 85 | } | ||
| 86 | $table->rule; | ||
| 87 | |||
| 88 | $action{action} = 'OUTPUT'; | ||
| 89 | $action{output} = $table->render($obj->{-interface}->render_size); | ||
| 90 | } elsif ($action{operation} =~ /^(un|re)load$/) { | ||
| 91 | $action{action} = 'NONE'; | ||
| 92 | my $i = 0; my $unload = undef; my $name = ''; | ||
| 93 | for my $ext (@{$obj->{-core}->{extensions}}) { | ||
| 94 | $name = $ext; $name =~ s/=.*$//; $name =~ s/^.*:://; | ||
| 95 | if (lc $name eq lc $action{what}) { $unload = $i; last; } | ||
| 96 | ++$i; | ||
| 97 | } | ||
| 98 | if (defined $unload) { | ||
| 99 | $obj->{-core}->{extensions}->[$unload]->done(); | ||
| 100 | undef $obj->{-core}->{extensions}->[$unload]; | ||
| 101 | splice @{$obj->{-core}->{extensions}},$unload,1; | ||
| 102 | $action{action} = 'OUTPUT'; | ||
| 103 | $action{output} = "Extension $name unloaded.\n" if $action{operation} eq 'unload'; | ||
| 104 | $obj->{-interface}->rebuild_menu(); | ||
| 105 | } else { | ||
| 106 | $obj->{-interface}->error("Extension $action{what} not exists."); | ||
| 107 | return %action; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | if ($action{operation} =~ /^(re)?load$/) { | ||
| 111 | $action{action} = 'NONE'; | ||
| 112 | my %loaded = (); | ||
| 113 | for my $ext (@{$obj->{-core}->{extensions}}) { | ||
| 114 | my $id = $ext->IDENTIFICATION; | ||
| 115 | $id =~ s/-[^-]+$//; ++$loaded{$id}; | ||
| 116 | } | ||
| 117 | |||
| 118 | my $name = $action{what}; | ||
| 119 | my %candidates = (); | ||
| 120 | for my $dir ($obj->{-core}->extensions_directories) { | ||
| 121 | opendir D,$dir; | ||
| 122 | for (grep /\.pm$/,readdir D) { | ||
| 123 | next unless lc $_ eq lc $action{what}.".pm"; | ||
| 124 | eval { require "$dir/$_"; }; | ||
| 125 | next if $@; | ||
| 126 | my $oldslash = $/; | ||
| 127 | undef $/; | ||
| 128 | if (open F,"$dir/$_") { | ||
| 129 | eval <F>; | ||
| 130 | close F; | ||
| 131 | } | ||
| 132 | delete $INC{"$dir/$_.pm"}; | ||
| 133 | eval { require "$dir/$_"; }; | ||
| 134 | $/ = $oldslash; | ||
| 135 | next if $@; | ||
| 136 | s/\.pm$//; | ||
| 137 | my $candidate = "DBIx::dbMan::Extension::".$_; | ||
| 138 | $name = $_; | ||
| 139 | my ($low,$high) = ('',''); | ||
| 140 | eval { ($low,$high) = $candidate->for_version(); }; | ||
| 141 | next if $low and $DBIx::dbMan::VERSION < $low; | ||
| 142 | next if $high and $DBIx::dbMan::VERSION > $high; | ||
| 143 | my $id = ''; | ||
| 144 | eval { $id = $candidate->IDENTIFICATION(); }; | ||
| 145 | next unless $id or $@; | ||
| 146 | my ($ident,$ver) = ($id =~ /^(.*)-(.*)$/); | ||
| 147 | next if $ident eq '000001-000001'; # not ID | ||
| 148 | if (exists $loaded{$ident}) { | ||
| 149 | $obj->{-interface}->error("Extension $name already loaded."); | ||
| 150 | return %action; | ||
| 151 | } | ||
| 152 | delete $INC{"$dir/$_.pm"}; | ||
| 153 | if (exists $candidates{$ident}) { | ||
| 154 | next if $candidates{$ident}->{-ver} > $ver; | ||
| 155 | } | ||
| 156 | $candidates{$ident} = | ||
| 157 | { -file => "$dir/$_.pm", -candidate => $candidate, -ver => $ver }; | ||
| 158 | }; | ||
| 159 | closedir D; | ||
| 160 | } | ||
| 161 | if (keys %candidates) { | ||
| 162 | my %extensions = (); | ||
| 163 | for my $candidate (keys %candidates) { | ||
| 164 | my $ext = undef; | ||
| 165 | eval { | ||
| 166 | require $candidates{$candidate}->{-file}; | ||
| 167 | $ext = $candidates{$candidate}->{-candidate}->new( | ||
| 168 | -config => $obj->{-core}->{config}, | ||
| 169 | -interface => $obj->{-core}->{interface}, | ||
| 170 | -dbi => $obj->{-core}->{dbi}, | ||
| 171 | -core => $obj->{-core}, | ||
| 172 | -mempool => $obj->{-core}->{mempool}); | ||
| 173 | die unless $ext->load_ok(); | ||
| 174 | }; | ||
| 175 | if (defined $ext and not $@) { | ||
| 176 | my $preference = 0; | ||
| 177 | eval { $preference = $ext->preference(); }; | ||
| 178 | $ext->{'___sort_criteria___'} = $preference.'_'.$obj->{extension_iterator}; | ||
| 179 | ++$ext->{'___need_init___'}; | ||
| 180 | $extensions{$preference.'_'.$obj->{-core}->{extension_iterator}} = $ext; | ||
| 181 | ++$obj->{-core}->{extension_iterator}; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | for my $ext (@{$obj->{-core}->{extensions}}) { | ||
| 186 | my $sc = $ext->{___sort_criteria___}; | ||
| 187 | $extensions{$sc} = $ext; | ||
| 188 | } | ||
| 189 | |||
| 190 | $obj->{-core}->{extensions} = []; | ||
| 191 | for (sort { | ||
| 192 | my ($fa,$sa,$fb,$sb) = split /_/,($a.'_'.$b); | ||
| 193 | if ($fa == $fb) { $sa <=> $sb; } else { $fb <=> $fa }; | ||
| 194 | } keys %extensions) { | ||
| 195 | push @{$obj->{-core}->{extensions}},$extensions{$_}; | ||
| 196 | $extensions{$_}->init() if $extensions{$_}->{'___need_init___'}; | ||
| 197 | delete $extensions{$_}->{'___need_init___'}; | ||
| 198 | } | ||
| 199 | |||
| 200 | $action{action} = 'OUTPUT'; | ||
| 201 | $action{output} = "Extension $name $action{operation}ed successfully.\n"; | ||
| 202 | $obj->{-interface}->rebuild_menu(); | ||
| 203 | } else { | ||
| 204 | $obj->{-interface}->error("Extension $action{what} not found."); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | return %action; | ||
| 210 | } |