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