MetaCurator/Export.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 333 CC 34 SCAR 47.8
CC
34
Cov
0%
export_tree
240.0
build_dag_tree
182.0
gather_statistics
42.0
CRAP 118.6
1 package CPAN::MetaCurator::Export;
2  
3 use boolean;
4 use feature 'say';
5 use parent 'CPAN::MetaCurator::HTML';
6 use warnings qw(FATAL utf8); # Fatalize encoding glitches.
7  
8 use Data::Dumper::Concise; # For Dumper().
9 use DateTime::Tiny;
10  
11 use File::Slurper qw/read_lines write_text/;
12  
13 use File::Spec;
14  
15 use HTML::Escape 'escape_html';
16  
17 use Mew;
18  
19 use Switch::Declare; # For switch.
20 use Syntax::Keyword::Match; # For match.
21  
22 use Tree::DAG_Node;
23  
24 has -dag_nodetree_path => (Str, default => sub{return ''}, chained => 1);
25  
26 has jstree_html_path => (Str, default => sub{return ''}, chained => 1);
27  
28 our $leaf_id;
29 our %seen;
30  
31 our $VERSION = '1.34';
32  
33 # --------------------------------------------------
34  
35 sub build_dag_item
36 {
37 my($self, $item, $token) = @_;
38  
39 my(@components);
40 my($text, $type);
41  
42 @components = split(' - ', $token);
43 $text = ($#components < 1) ? $components[0] : $components[1];
44 $type = switch ($components[0])
45 {
46 case /^\[?\[?[A-Za-z]+\d?\d?\]?\]?$/ {'topic'}
47 case /^http/ {'uri'}
48 default {'text'}
49 };
50  
51 match ($type : eq)
52 {
53 case('topic') {
54 $$item{text} = ($components[0] =~ /^\[?\[?([A-Za-z]+\d?\d?)\]?\]?$/) ? $1 : $components[0];
55 $$item{text} = "[Topic] <button class='btn btn-info'>$$item{text}</button>"
56 }
57 case('uri') {$$item{text} = "<a href = '" . escape_html($components[0]) . "' target = '_blank'>$text</a>"}
58 case('text') {$$item{text} = $token}
59 }
60  
61 return Tree::DAG_Node -> new({name => $$item{text}, attributes => {id => ++$leaf_id, description => '', uri => $$item{text}} });
62  
63 } # End of build_dag_item.
64  
65 # --------------------------------------------------
66  
67 sub build_dag_tree
68 {
69 my($self, $daughter, $pad, $topic) = @_;
70 my(@lines) = split(/\n/, $$topic{text});
71 @lines = grep{length} map{s/^\s+//; s/:\s*$//; $_} @lines;
72 my($index) = -1;
73  
74 my(%inside, $item);
75 my($leaf, $line, $line_count);
76 my($module);
77 my(%node_type, $note, $note_count);
78 my(@pre_pre);
79 my($see_also_root);
80 my($token);
81  
82 $inside{pre_pre} = false;
83 $inside{see_also} = false;
84 $item = {description => '', href => '', id => 0, text => '', uri => ''};
85  
86 while ($index < $#lines)
87 {
88 $index++;
89  
90 $line = $lines[$index];
91 $token = ($line =~ /^o (.+)/) ? $1 : '';
92  
93 $self -> logger -> debug("Processing line $index: <$line>. token: $token");
94  
95 # If the topic is ChangeLog[12]:
96 # $token ne '':
97 # a. V 1.23 many times
98 # Else it's a normal topic:
99 # $token ne '':
100 # a. See also
101 # b. An acronym
102 # Otherwise:
103 # c. A description
104 # d. A href
105 # e. <pre>
106 # f. </pre>
107  
108 if ($token =~ /See also/)
109 {
110 $inside{see_also} = true;
111 $see_also_root = Tree::DAG_Node -> new({name => '&#8853; See also', attributes => {id => ++$leaf_id, description => '', uri => ''} });
112  
113 $daughter -> add_daughter($see_also_root);
114 }
115 elsif ($token)
116 {
117 $inside{see_also} = false;
118 $line_count = 0;
119 $module = $token;
120 $note_count = 0;
121  
122 $self -> gather_statistics(\%node_type, $pad, $module, $topic);
123  
124 if (! $seen{$module} && ($$topic{title} ne 'Acronyms') )
125 {
126 $seen{$module} = $self -> insert_hashref('modules', {name => $module});
127 }
128 }
129 elsif ($line =~ /<pre>/)
130 {
131 # Fix me. What happens if there are 2 sets of <pre>...</pre> within 1 topic?
132  
133 $inside{pre_pre} = true;
134 }
135 elsif ($line =~ m|</pre>|)
136 {
137 $inside{pre_pre} = false;
138 }
139 elsif ($inside{pre_pre})
140 {
141 }
142 else
143 {
144 $line_count++;
145  
146 $token = ($line =~ /^- (.+)/) ? $1 : '';
147  
148 if ($inside{see_also})
149 {
150 # Fix me. References to topics can be forward references.
151  
152 $leaf = $self -> build_dag_item($item, $token);
153  
154 $see_also_root -> add_daughter($leaf);
155 }
156 elsif ( ($line_count == 1) && ($$topic{title} !~ /Author|ChangeLog/) )
157 {
158 $$item{description} = $token;
159 }
160 elsif ( ($line_count == 2) && ($$topic{title} !~ /Author|ChangeLog/) )
161 {
162 $$item{text} = $token; # Module name.
163 $leaf = Tree::DAG_Node -> new({name => $module, attributes => {id => ++$leaf_id, description => $$item{description}, uri => $$item{text}} });
164  
165 $daughter -> add_daughter($leaf);
166 }
167 else
168 {
169 $note_count++;
170  
171 if ($note_count == 1)
172 {
173 $note = Tree::DAG_Node -> new({name => "&#8853; Notes for: $module", attributes => {id => ++$leaf_id, description => '', uri => ''} });
174  
175 $daughter -> add_daughter($note);
176 }
177  
178 $leaf = $self -> build_dag_item($item, $token);
179  
180 $note -> add_daughter($leaf);
181 }
182 }
183 }
184  
185 } # End of build_dag_tree.
186  
187 # -----------------------------------------------
188  
189 sub build_tree
190 {
191 my($self, $pad) = @_;
192 my($origin) = shift @{$$pad{topics} }; # I.e.: {id => 1, parent_id => 1, text => 'Root', title => 'MetaCurator'}.
193 $leaf_id = 0;
194 my($root) = Tree::DAG_Node -> new({name => $$origin{title}, attributes => {id => $leaf_id, description => 'Root'} });
195  
196 my($daughter);
197  
198 for my $topic (@{$$pad{topics} })
199 {
200 $daughter = Tree::DAG_Node -> new({name => $$topic{title}, attributes => {id => ++$leaf_id, description => 'Topic'} });
201  
202 $root -> add_daughter($daughter);
203  
204 $self -> build_dag_tree($daughter, $pad, $topic);
205 }
206  
207 return $root;
208  
209 } # End of build_tree.
210  
211 # --------------------------------------------------
212  
213 sub export_modules_table
214 {
215 my($self) = @_;
216  
217 $self -> init_config;
218 $self -> init_db;
219  
220 my($database_path) = File::Spec -> catfile($self -> home_path, $self -> database_path);
221 my($modules_csv_path) = File::Spec -> catfile($self -> home_path, $self -> output_path);
222  
223 $self -> logger -> info("Exporting modules table");
224 $self -> logger -> info("Reading: $database_path");
225 $self -> logger -> info("Writing: $modules_csv_path");
226  
227 my($command) = `echo ".h on\n.mode csv\nselect * from modules" | sqlite3 $database_path > $modules_csv_path`;
228 my($line_count) = `wc -l $modules_csv_path`;
229 my($module_count, $name) = split(' ', $line_count);
230 $module_count--; # Allow for header record.
231  
232 $self -> logger -> info("Output record count (excluding header): $module_count");
233  
234 } # End of export_modules_table.
235  
236 # -----------------------------------------------
237  
238 sub export_tree
239 {
240 my($self) = @_;
241  
242 $self -> init_config;
243 $self -> init_db;
244  
245 # Phase 1: Build the DAG_Node tree.
246  
247 my($pad) = $self -> build_pad;
248 my($root) = $self -> build_tree($pad);
249  
250 # Phase 2: Save the DAG_Node tree to disk.
251  
252 if ($self -> dag_nodetree_path)
253 {
254 write_text($self -> dag_nodetree_path, join("\n", @{$root -> tree2string}) . "\n");
255 }
256  
257 # Phase 3: Scan the DAG_Node tree to get the topic ids.
258 # These will be used to resolve forward references of topics.
259  
260 my($attributes);
261 my($id);
262 my($name);
263 my(%topic_id_map);
264  
265 $root -> walk_down
266 ({
267 callbackback => sub
268 {
269 my($node, $options) = @_;
270 $attributes = $node -> attributes;
271 $name = $node -> name;
272  
273 if ($$options{_depth} == 1) # Topics.
274 {
275 $topic_id_map{$name} = $$attributes{id};
276 }
277  
278 }, # End of callbackback.
279 _depth => 0,
280 });
281  
282 # Phase 4; Build the JS Tree.
283 # New style.
284  
285 my($description);
286 my(@list);
287 my($previous_depth);
288 my($uri);
289  
290 $root -> walk_down
291 ({
292 callback => sub
293 {
294 my($node, $options) = @_;
295 $attributes = $node -> attributes;
296 $name = $node -> name;
297  
298 if ($$options{_depth} == 0) # Root.
299 {
300 push @list, '<ul>'; # Open global li.
301 push @list, qq|<li data-jstree='{"opened": true}' id = '$$attributes{id}'>$name|;
302 push @list, '<ul>'; # Open ul for subtree below root.
303 }
304 elsif ($$options{_depth} == 1) # Topics.
305 {
306 push @list, '</li>' if ($previous_depth == 1); # Close li opened at this depth.
307 push @list, '</ul></li>' if ($previous_depth == 2); # Close ul & li opened in subtree below.
308 push @list, '</ul></li></ul>' if ($previous_depth == 3); # Close ul & li opened in subtree below. Eg: CssStuff with no entries below See also.
309 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$name|;
310 }
311 elsif ($$options{_depth} == 2) # Module name || 'Notes for ...' || 'See also'.
312 {
313 $$pad{count}{leaf}++;
314  
315 $description = $$attributes{description};
316 $uri = $$attributes{uri} || '#';
317 $uri = ($name =~ qr/Notes for|See also/) ? $name : "<a href = '" . escape_html($uri) . "' target = '_blank'>$name - $description</a>";
318  
319 push @list, '<ul>' if ($previous_depth == 1); # Open ul for subtree at this level.
320 push @list, '</li>' if ($previous_depth == 2); # Close li opened at this depth.
321 push @list, '</ul></li>' if ($previous_depth == 3); # Close ul & li opened in subtree below.
322 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$uri|;
323 }
324 elsif ($$options{_depth} == 3) # 'Notes for' entries || 'See also' entries.
325 {
326 push @list, '<ul>' if ($previous_depth == 2); # Open ul for subtree at this level.
327 push @list, qq|\t<li data-jstree='{"opened": false}' id = '$$attributes{id}'>$name</li>|;
328 }
329  
330 $previous_depth = $$options{_depth};
331  
332 return 1;
333  
334 }, # End of callback.
335 _depth => 0,
336 });
337  
338 push @list, '</ul>'; # Close ul for subtree opened at root.
339 push @list, '</li>'; # CLose li opened at root.
340 push @list, '</ul>'; # Close global li.
341  
342 # Phase 5: Build the web page which uses JSTree.
343 # And save it to html/cpan.metacurator.tree.html
344  
345 $$pad{jstree_html_path} = $self -> jstree_html_path;
346 my($header, $body, $footer) = $self -> build_html($pad); # Returns templates.
347 my($list) = join("\n", @list);
348 $body =~ s/!list!/$list/;
349  
350 for $_ (keys %{$$pad{count} })
351 {
352 $header =~ s/!$_!/$$pad{count}{$_}/;
353 }
354  
355 $self -> write_file($header, $body, $footer, $pad);
356 $self -> logger -> info("$_ count: $$pad{count}{$_}") for (sort keys %{$$pad{count} });
357  
358 return 0;
359  
360 } # End of export_tree.
361  
362 # --------------------------------------------------
363 # Some names might be acronyms & module names & topic names.
364 # Example: RSS.
365  
366 sub gather_statistics
367 {
368 my($self, $node_type, $pad, $token, $topic) = @_;
369  
370 $$node_type{acronym} = $$topic{title} eq 'Acronyms' ? true : false;
371 $$node_type{topic} = exists($$pad{topic_names}{$token}) ? true : false;
372 $$node_type{known} = exists($$pad{module_names}{$token}) ? true : false;
373  
374 $$pad{count}{acronym}++ if ($$node_type{acronym});
375 $$pad{count}{known}++ if ($$node_type{known});
376  
377 } # End of gather_statistics;
378  
379 # --------------------------------------------------
380  
381 sub write_file
382 {
383 my($self, $header, $body, $footer, $pad) = @_;
384 my($output_path) = File::Spec -> catfile($self -> home_path, $self -> jstree_html_path);
385  
386 # $$pad{encoding} has a ':' prefix, & the value is from the constants table, which is from
387 # /home/ron/perl.modules/CPAN-MetaCurator/data/cpan.metacurator.constants.csv.
388  
389 open(my $fh, ">$$pad{encoding}", $output_path);
390 print $fh $header, $body, $footer;
391 close $fh;
392  
393 $self -> logger -> info("Created $output_path. Encoding: $$pad{encoding}");
394  
395 } # End of write_file.
396  
397 # --------------------------------------------------
398  
399 1;
400  
401 =pod
402  
403 =head1 NAME
404  
405 CPAN::MetaCurator::Export - Manage the cpan.metacurator.sqlite database
406  
407 =head1 Machine-Readable Change Log
408  
409 The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>.
410  
411 =head1 Version Numbers
412  
413 Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions.
414  
415 =head1 Test data
416  
417 There is a mechanism to restrict processing to a tiny number of topics.
418  
419 To this end there is an option called test_topics_path, which takes a file name.
420 If this file is present it is read, & each line in it is assumed to be a topic name.
421  
422 Topics listed are wanted, & so the program skips processing any other topics.
423  
424 There is a special case. If the file is present but empty, or absent, all topics are deemed
425 to appear in the file & hence are processed.
426  
427 Default: /tmp/test.topics.txt.
428  
429 =head1 Support
430  
431 Email the author.
432  
433 =head1 Author
434  
435 L<CPAN::MetaCurator> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2025.
436  
437 My homepage: L<https://savage.net.au/>.
438  
439 =head1 Copyright
440  
441 Australian copyright (c) 2025, Ron Savage.
442  
443 All Programs of mine are 'OSI Certified Open Source Software';
444 you can redistribute them and/or modify them under the terms of
445 The Perl License, a copy of which is available at:
446 http://dev.perl.org/licenses/
447  
448 =cut