Paket Locale wurde in eine eigene Datei verlegt.
[kivitendo-erp.git] / scripts / dbupgrade2_tool.pl
1 #!/usr/bin/perl
2
3 BEGIN {
4   if (! -d "bin" || ! -d "SL") {
5     print("This tool must be run from the Lx-Office ERP base directory.\n");
6     exit(1);
7   }
8 }
9
10 use DBI;
11 use Data::Dumper;
12 use Getopt::Long;
13
14 use SL::LXDebug;
15
16 $lxdebug = LXDebug->new();
17
18 use SL::Form;
19 use SL::Locale;
20 use SL::DBUpgrade2;
21
22 #######
23 #######
24 #######
25
26 sub show_help {
27   print("dbupgrade2_tool.pl [--list] [--tree] [--rtree] [--graphviz]\n" .
28         "                   [--nodepds] [--help]\n");
29 }
30
31 sub calc_rev_depends {
32   map({ $_->{"rev_depends"} = []; } values(%{$controls}));
33   foreach my $control (values(%{$controls})) {
34     map({ push(@{$controls->{$_}{"rev_depends"}}, $control->{"tag"}) }
35         @{$control->{"depends"}});
36   }
37 }
38
39 sub dump_list {
40   my @sorted_controls = sort_dbupdate_controls($controls);
41
42   print("LIST VIEW\n\n");
43   print("number tag depth priority\n");
44   $i = 0;
45   foreach (@sorted_controls) {
46     print("$i $_->{tag} $_->{depth} $_->{priority}\n");
47     $i++;
48   }
49
50   print("\n");
51 }
52
53 sub dump_node {
54   my ($tag, $depth) = @_;
55
56   print(" " x $depth . $tag . "\n");
57
58   my $c = $controls->{$tag};
59   my $num = scalar(@{$c->{"depends"}});
60   for (my $i = 0; $i < $num; $i++) {
61     dump_node($c->{"depends"}[$i], $depth + 1);
62   }
63 }
64
65 sub dump_tree {
66   print("TREE VIEW\n\n");
67
68   calc_rev_depends();
69
70   my @sorted_controls = sort_dbupdate_controls($controls);
71
72   foreach my $control (@sorted_controls) {
73     dump_node($control->{"tag"}, "") unless (@{$control->{"rev_depends"}});
74   }
75
76   print("\n");
77 }
78
79 sub dump_node_reverse {
80   my ($tag, $depth) = @_;
81
82   print(" " x $depth . $tag . "\n");
83
84   my $c = $controls->{$tag};
85   my $num = scalar(@{$c->{"rev_depends"}});
86   for (my $i = 0; $i < $num; $i++) {
87     dump_node_reverse($c->{"rev_depends"}[$i], $depth + 1);
88   }
89 }
90
91 sub dump_tree_reverse {
92   print("REVERSE TREE VIEW\n\n");
93
94   calc_rev_depends();
95
96   my @sorted_controls = sort_dbupdate_controls($controls);
97
98   foreach my $control (@sorted_controls) {
99     last if ($control->{"depth"} > 1);
100     dump_node_reverse($control->{"tag"}, "");
101   }
102
103   print("\n");
104 }
105
106 sub dump_graphviz {
107   print("GRAPHVIZ POSTCRIPT\n\n");
108   print("Output will be written to db_dependencies.ps\n");
109   $dot = "|dot -Tps ";
110   open(OUT, "${dot}> db_dependencies.ps");
111   print(OUT
112         "digraph db_dependencies {\n" .
113         "node [shape=box];\n");
114   my %ranks;
115   foreach my $c (values(%{$controls})) {
116     $ranks{$c->{"depth"}} = [] unless ($ranks{$c->{"depth"}});
117     push(@{$ranks{$c->{"depth"}}}, $c->{"tag"});
118   }
119   foreach (sort(keys(%ranks))) {
120     print(OUT "{ rank = same; " .
121           join("", map({ '"' . $_ . '"; ' } @{$ranks{$_}})) .
122           " }\n");
123   }
124   foreach my $c (values(%{$controls})) {
125     print(OUT "$c->{tag};\n");
126     foreach my $d (@{$c->{"depends"}}) {
127       print(OUT "$c->{tag} -> $d;\n");
128     }
129   }
130   print(OUT "}\n");
131   close(OUT);
132 }
133
134 sub dump_nodeps {
135   calc_rev_depends();
136
137   print("SCRIPTS NO OTHER SCRIPTS DEPEND ON\n\n" .
138         join("\n",
139              map({ $_->{"tag"} }
140                  grep({ !@{$_->{"rev_depends"}} }
141                       values(%{$controls})))) .
142         "\n\n");
143 }
144
145 #######
146 #######
147 #######
148
149 eval { require "lx-erp.conf"; };
150
151 $form = Form->new();
152 $locale = Locale->new("de", "login");
153
154 #######
155 #######
156 #######
157
158 my ($opt_list, $opt_tree, $opt_rtree, $opt_nodeps, $opt_graphviz, $opt_help);
159
160 GetOptions("list" => \$opt_list,
161            "tree" => \$opt_tree,
162            "rtree" => \$opt_rtree,
163            "nodeps" => \$opt_nodeps,
164            "graphviz" => \$opt_graphviz,
165            "help" => \$opt_help,
166   );
167
168 if ($opt_help) {
169   show_help();
170   exit(0);
171 }
172
173 $controls = parse_dbupdate_controls($form, "Pg");
174
175 if ($opt_list) {
176   dump_list();
177 }
178
179 if ($opt_tree) {
180   dump_tree();
181 }
182
183 if ($opt_rtree) {
184   dump_tree_reverse();
185 }
186
187 if ($opt_graphviz) {
188   dump_graphviz();
189 }
190
191 if ($opt_nodeps) {
192   dump_nodeps();
193 }