| 1 | package App::lcpan::Cmd::related_mods; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | require App::lcpan; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 11 | 1 | our $DATE = '2023-09-26'; # DATE | |||||||||||||||||||||||||||
| 12 | 1 | our $DIST = 'App-lcpan'; # DIST | |||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '1.074'; # VERSION | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $SPEC{'handle_cmd'} = { | ||||||||||||||||||||||||||||
| 18 | v => 1.1, | ||||||||||||||||||||||||||||
| 19 | summary => 'List other modules related to module(s)', | ||||||||||||||||||||||||||||
| 20 | description => <<'_', | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | This subcommand lists other modules that might be related to the module(s) you | ||||||||||||||||||||||||||||
| 23 | specify. This is done by listing modules that tend be mentioned together in POD | ||||||||||||||||||||||||||||
| 24 | documentation. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | The downside of this approach is that the module(s) and the related modules must | ||||||||||||||||||||||||||||
| 27 | all already be mentioned together in POD documentations. You will not find a | ||||||||||||||||||||||||||||
| 28 | fresh new module that tries to be an improvement/alternative to an existing | ||||||||||||||||||||||||||||
| 29 | module, even if that new module mentions the old module a lot, simply because | ||||||||||||||||||||||||||||
| 30 | the new module has not been mentioned in other modules' PODs. Someone will need | ||||||||||||||||||||||||||||
| 31 | to create that POD(s) first. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The scoring/ranking still needs to be tuned. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | _ | ||||||||||||||||||||||||||||
| 36 | args => { | ||||||||||||||||||||||||||||
| 37 | %App::lcpan::common_args, | ||||||||||||||||||||||||||||
| 38 | %App::lcpan::mods_args, | ||||||||||||||||||||||||||||
| 39 | #%App::lcpan::detail_args, | ||||||||||||||||||||||||||||
| 40 | limit => { | ||||||||||||||||||||||||||||
| 41 | summary => 'Maximum number of modules to return', | ||||||||||||||||||||||||||||
| 42 | schema => ['int*', min=>0], | ||||||||||||||||||||||||||||
| 43 | default => 20, | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | with_scores => { | ||||||||||||||||||||||||||||
| 46 | summary => 'Return score-related fields', | ||||||||||||||||||||||||||||
| 47 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | with_content_paths => { | ||||||||||||||||||||||||||||
| 50 | summary => 'Return the list of content paths where the module and a related module are mentioned together', | ||||||||||||||||||||||||||||
| 51 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 52 | }, | ||||||||||||||||||||||||||||
| 53 | sort => { | ||||||||||||||||||||||||||||
| 54 | 5 | schema => ['array*', of=>['str*', in=>[map {($_,"-$_")} qw/score num_mentions num_mentions_together pct_mentions_together module/]], min_len=>1], | |||||||||||||||||||||||||||
| 55 | default => ['-score', '-num_mentions'], | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | skip_same_dist => { | ||||||||||||||||||||||||||||
| 58 | summary => 'Skip modules from the same distribution', | ||||||||||||||||||||||||||||
| 59 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 60 | tags => ['category:filtering'], | ||||||||||||||||||||||||||||
| 61 | }, | ||||||||||||||||||||||||||||
| 62 | submodules => { | ||||||||||||||||||||||||||||
| 63 | summary => 'Whether to include submodules', | ||||||||||||||||||||||||||||
| 64 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 65 | description => <<'_', | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | If set to true, will only show related submodules, e.g. `lcpan related-modules | ||||||||||||||||||||||||||||
| 68 | Foo::Bar` will only show `Foo::Bar::Baz`, `Foo::Bar::Quz`, and so on. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | If set to false, will only show related modules that are not submodules, e.g. | ||||||||||||||||||||||||||||
| 71 | `lcpan related-modules Foo::Bar` will show `Baz`, `Foo::Baz`, but not | ||||||||||||||||||||||||||||
| 72 | `Foo::Bar::Baz`. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | _ | ||||||||||||||||||||||||||||
| 75 | cmdline_aliases => { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | exclude_submodules => {is_flag=>1, summary=>"Equivalent to --no-submodules", code=>sub {$_[0]{submodules}=0}}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | include_submodules => {is_flag=>1, summary=>"Equivalent to --submodules", code=>sub {$_[0]{submodules}=1}}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||
| 80 | }, | ||||||||||||||||||||||||||||
| 81 | 1 | }; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub handle_cmd { | ||||||||||||||||||||||||||
|
Subroutine
handle_cmd: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | my $state = App::lcpan::_init(\%args, 'ro'); | |||||||||||||||||||||||||||
| 86 | 0 | my $dbh = $state->{dbh}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $modules = $args{modules}; | |||||||||||||||||||||||||||
| 89 | 0 | my $modules_s = join(",", map {$dbh->quote($_)} @$modules); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($args{with_content_paths} && @$modules > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args{"with_content_paths"} and @$modules > 1
MC/DC0%0/2
$args{"with_content_paths"} and @$modules > 1
$args{"with_content_paths"}
@$modules > 1
Truth table0%0/3
A and B
A= $args{"with_content_paths"}B= @$modules > 1
| |||||||||||||||||||||||||||||
| 92 | 0 | return [412, "Sorry, --with-content-paths currently works with only one specified module"]; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my $limit = $args{limit}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # number of mentions of target modules | ||||||||||||||||||||||||||||
| 98 | 0 | my ($num_mentions) = $dbh->selectrow_array( | |||||||||||||||||||||||||||
| 99 | "SELECT COUNT(*) FROM mention WHERE module_id IN (SELECT id FROM module m2 WHERE name IN ($modules_s))"); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | return [400, "No mentions for module(s)"] if $num_mentions < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | log_debug("num_mentions for %s: %d", $modules, $num_mentions); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my @join = ( | |||||||||||||||||||||||||||
| 106 | "LEFT JOIN module m2 ON mtn1.module_id=m2.id", | ||||||||||||||||||||||||||||
| 107 | "LEFT JOIN file f ON m2.file_id=f.id", | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my @where = ( | |||||||||||||||||||||||||||
| 111 | "mtn1.source_content_id IN (SELECT source_content_id FROM mention mtn2 WHERE module_id IN (SELECT id FROM module m2 WHERE name IN ($modules_s)))", | ||||||||||||||||||||||||||||
| 112 | "m2.name NOT IN ($modules_s)", | ||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my @dist_names; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($args{skip_same_dist}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | my $sth = $dbh->prepare( | |||||||||||||||||||||||||||
| 118 | "SELECT DISTINCT dist_name FROM file WHERE dist_name IS NOT NULL AND id IN (SELECT file_id FROM module WHERE name IN ($modules_s))"); | ||||||||||||||||||||||||||||
| 119 | 0 | $sth->execute; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | while (my ($dist_name) = $sth->fetchrow_array) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | push @dist_names, $dist_name; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | push @where, "f.dist_name NOT IN (".join(", ", map { $dbh->quote($_) } @dist_names).")"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($args{submodules}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 126 | 0 | for my $module (@$modules) { | |||||||||||||||||||||||||||
| 127 | 0 | push @where, "m2.name LIKE " . $dbh->quote("$module\::%"); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | ▶ | } elsif (defined $args{submodules} && !$args{submodules}) { | |||||||||||||||||||||||||||
|
Condition0%0/3
defined $args{"submodules"} and not $args{"submodules"}
MC/DC0%0/2
defined $args{"submodules"} and not $args{"submodules"}
defined $args{"submodules"}
not $args{"submodules"}
Truth table0%0/3
A and not(B)
A= defined $args{"submodules"}B= not $args{"submodules"}
| |||||||||||||||||||||||||||||
| 130 | 0 | for my $module (@$modules) { | |||||||||||||||||||||||||||
| 131 | 0 | push @where, "m2.name NOT LIKE " . $dbh->quote("$module\::%"); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my @order = map {/(-?)(.+)/; $2 . ($1 ? " DESC" : "")} @{$args{sort}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # sql parts, to make SQL statement readable | ||||||||||||||||||||||||||||
| 138 | 0 | my $sp_num_mentions = "SELECT COUNT(*) FROM mention mnt3 WHERE module_id=m2.id"; | |||||||||||||||||||||||||||
| 139 | 0 | my $sp_pct_mentions_together = "ROUND(100.0 * COUNT(*)/($sp_num_mentions), 2)"; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my $sql = "SELECT | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | m2.name module, | ||||||||||||||||||||||||||||
| 143 | m2.abstract abstract, | ||||||||||||||||||||||||||||
| 144 | ($sp_num_mentions) num_mentions, | ||||||||||||||||||||||||||||
| 145 | COUNT(*) num_mentions_together, | ||||||||||||||||||||||||||||
| 146 | ($sp_pct_mentions_together) pct_mentions_together, | ||||||||||||||||||||||||||||
| 147 | (COUNT(*) * COUNT(*) * ($sp_pct_mentions_together)) score, | ||||||||||||||||||||||||||||
| 148 | f.dist_name dist, | ||||||||||||||||||||||||||||
| 149 | m2.cpanid author | ||||||||||||||||||||||||||||
| 150 | FROM mention mtn1 | ||||||||||||||||||||||||||||
| 151 | ".join("\n", @join)." | ||||||||||||||||||||||||||||
| 152 | WHERE ".join(" AND ", @where)." | ||||||||||||||||||||||||||||
| 153 | GROUP BY m2.name | ||||||||||||||||||||||||||||
| 154 | ".(@order ? "\nORDER BY ".join(", ", @order) : "")." | ||||||||||||||||||||||||||||
| 155 | LIMIT $limit | ||||||||||||||||||||||||||||
| 156 | "; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | my $sql_with_content_paths; | |||||||||||||||||||||||||||
| 159 | my $sth_with_content_paths; | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if ($args{with_content_paths}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | $sql_with_content_paths = "SELECT | |||||||||||||||||||||||||||
| 162 | path | ||||||||||||||||||||||||||||
| 163 | FROM content c | ||||||||||||||||||||||||||||
| 164 | WHERE | ||||||||||||||||||||||||||||
| 165 | EXISTS(SELECT id FROM mention WHERE module_id=(SELECT id FROM module WHERE name=?) AND source_content_id=c.id) AND | ||||||||||||||||||||||||||||
| 166 | EXISTS(SELECT id FROM mention WHERE module_id=(SELECT id FROM module WHERE name=?) AND source_content_id=c.id) | ||||||||||||||||||||||||||||
| 167 | "; | ||||||||||||||||||||||||||||
| 168 | 0 | $sth_with_content_paths = $dbh->prepare($sql_with_content_paths); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | my @res; | |||||||||||||||||||||||||||
| 172 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 173 | 0 | $sth->execute(); | |||||||||||||||||||||||||||
| 174 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | unless ($args{with_scores}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | delete $row->{$_} for qw(num_mentions num_mentions_together pct_mentions_together score); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($args{with_content_paths}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | my @content_paths; | |||||||||||||||||||||||||||
| 180 | 0 | $sth_with_content_paths->execute($modules->[0], $row->{module}); | |||||||||||||||||||||||||||
| 181 | 0 | ▶ | while (my $row2 = $sth_with_content_paths->fetchrow_arrayref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | push @content_paths, $row2->[0]; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 0 | $sth_with_content_paths->finish; | |||||||||||||||||||||||||||
| 185 | 0 | $row->{content_paths} = \@content_paths; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | 0 | push @res, $row; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 0 | my $resmeta = {}; | |||||||||||||||||||||||||||
| 190 | 0 | $resmeta->{'table.fields'} = [qw/module abstract num_mentions num_mentions_together pct_mentions_together score dist author/]; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | [200, "OK", \@res, $resmeta]; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | 1; | |||||||||||||||||||||||||||
| 196 | # ABSTRACT: List other modules related to module(s) | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | __END__ | ||||||||||||||||||||||||||||