blib/lib/Kwiki/SocialMap.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 105 CC 4 SCAR 13.9
CC
4
Cov
0%
render_socialmap
12.0
cleanup
6.0
register
2.0
CRAP 4.0
1 package Kwiki::SocialMap;
2  
3 =head1 NAME
4  
5 Kwiki::SocialMap - Display social relation of this kwiki site
6  
7 =head1 DESCRIPTION
8  
9 Please see L<Graph::SocialMap> to know something about Social Map.
10  
11 =head1 COPYRIGHT
12  
13 Copyright 2004 by Kang-min Liu <gugod@gugod.org>.
14  
15 This program is free software; you can redistribute it and/or
16 modify it under the same terms as Perl itself.
17  
18 See <http://www.perl.com/perl/misc/Artistic.html>
19  
20 =cut
21  
22 use strict;
23 use warnings;
24 use Kwiki::Plugin '-Base';
25 use mixin 'Kwiki::Installer';
26 use YAML;
27  
28 our $VERSION = '0.09';
29  
30 const class_id => 'socialmap';
31 const class_title => 'SocialMap Blocks';
32 const screen_template => 'site_socialmap_screen.html';
33  
34 sub register {
35 my $registry = shift;
36 $registry->add(wafl => socialmap => 'Kwiki::SocialMap::Wafl');
37 $registry->add(action => 'socialmap');
38 $registry->add(toolbar => 'socialmap_button',
39 template => 'socialmap_button.html');
40 }
41  
42 sub socialmap {
43 my $relation = $self->find_kwiki_social_relation;
44 $self->render_socialmap($relation);
45 $self->render_screen;
46 }
47  
48 sub find_kwiki_social_relation {
49 my $db = $self->hub->config->database_directory;
50 my $relation;
51 for my $page ($self->pages->all) {
52 my $history = $self->hub->load_class('archive')->history($page);
53 my @edit_by = map { $_->{edit_by} } @$history;
54 $relation->{$page->id} = \@edit_by;
55 }
56 return $relation;
57 }
58  
59 sub socialmap_file {
60 # XXX: always regen the graph. Not good.
61 my $path = $self->plugin_directory;
62 io->catfile($path, 'socialmap.png')->unlink;
63 return io->catfile($path,'socialmap.png')->assert;
64 }
65  
66 sub render_socialmap {
67 my $relation = shift;
68 my $gsmio = $self->socialmap_file;
69 my $gsm = Graph::SocialMap->new(-relation => $relation);
70 $gsm->save(-format=> 'png',-file=> $gsmio);
71 }
72  
73 package Kwiki::SocialMap::Wafl;
74 use base 'Spoon::Formatter::WaflBlock';
75 use Graph::SocialMap;
76 use Digest::MD5;
77  
78 sub to_html {
79 $self->cleanup;
80 $self->render_socialmap($self->units->[0]);
81 }
82  
83 # XXX: I think cleanup should be called ony once per-page.
84 # (If a page is modified, re-generate all the socialmap inside)
85 # but put it in here will make it be called once per-socialmap-block.
86 sub cleanup {
87 my $path = $self->hub->socialmap->plugin_directory;
88 my $page =$self->hub->pages->current;
89 my $page_id = $page->id;
90 for(<$path/$page_id/*.png>) {
91 my $mt = (stat($_))[9];
92 unlink($_) if $mt < $page->modified_time;
93 }
94 }
95  
96 # use md5 as filename because I don't want to regenerate all the graphs
97 # on every page rendering. That's totally a waste of time.
98 sub render_socialmap {
99 my $reldump = shift;
100 my $page = $self->hub->pages->current->id;
101 my $digest = Digest::MD5::md5_hex($reldump);
102 my $path = $self->hub->socialmap->plugin_directory;
103 my $file = io->catfile($path,$page,"$digest.png")->assert;
104 unless(-f "$file") {
105 $file->lock;
106 my $relation;
107 eval { $relation = YAML::Load($reldump) };
108 return qq{<span style="color: red;">Error: Input is not valide YAML. Please go back and edit it again</span>} if $@;
109 my $gsm = Graph::SocialMap->new(-relation => $relation);
110 $gsm->no_overlap(1);
111 $gsm->save(-format=> 'png',-file=> $file);
112 $file->unlock;
113 }
114 return qq{<img src="$file" />};
115 }
116  
117 1;
118 package Kwiki::SocialMap;
119 __DATA__