From c7c203a6c9521913aad33299abf824cc1ba53347 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Wed, 19 Jun 2024 15:09:52 +0200 Subject: [PATCH] use XML::XPath for parsing 00whois.xml XML::Simple is not recommended because it is hard to use correctly. We can use XML::XPath instead, and the code is actually simpler. --- cpanfile | 2 +- cpanfile.snapshot | 92 +++++++++++++---------------------- lib/MetaCPAN/Script/Author.pm | 21 +++----- 3 files changed, 41 insertions(+), 74 deletions(-) diff --git a/cpanfile b/cpanfile index ab37491dc..c05f4d27a 100644 --- a/cpanfile +++ b/cpanfile @@ -139,7 +139,7 @@ requires 'Types::URI'; requires 'Twitter::API', '1.0006'; requires 'URI', '5.10'; requires 'version', '0.9929'; -requires 'XML::Simple'; +requires 'XML::XPath'; requires 'YAML::XS', '0.83'; # Mojolicious::Plugin::OpenAPI YAML loading # test requirements diff --git a/cpanfile.snapshot b/cpanfile.snapshot index 616076bc1..90109c0b1 100644 --- a/cpanfile.snapshot +++ b/cpanfile.snapshot @@ -7226,17 +7226,6 @@ DISTRIBUTIONS Fcntl 0 URI 1.10 perl 5.008001 - XML-NamespaceSupport-1.12 - pathname: P/PE/PERIGRIN/XML-NamespaceSupport-1.12.tar.gz - provides: - XML::NamespaceSupport 1.12 - requirements: - ExtUtils::MakeMaker 6.17 - constant 0 - perl 5.006 - strict 0 - vars 0 - warnings 0 XML-Parser-2.47 pathname: T/TO/TODDR/XML-Parser-2.47.tar.gz provides: @@ -7251,54 +7240,39 @@ DISTRIBUTIONS ExtUtils::MakeMaker 0 LWP::UserAgent 0 perl 5.004050 - XML-SAX-1.02 - pathname: G/GR/GRANTM/XML-SAX-1.02.tar.gz - provides: - XML::SAX 1.02 - XML::SAX::DocumentLocator undef - XML::SAX::ParserFactory 1.02 - XML::SAX::PurePerl 1.02 - XML::SAX::PurePerl::DebugHandler undef - XML::SAX::PurePerl::Exception undef - XML::SAX::PurePerl::Productions undef - XML::SAX::PurePerl::Reader undef - XML::SAX::PurePerl::Reader::Stream undef - XML::SAX::PurePerl::Reader::String undef - XML::SAX::PurePerl::Reader::URI undef - requirements: - ExtUtils::MakeMaker 0 - File::Temp 0 - XML::NamespaceSupport 0.03 - XML::SAX::Base 1.05 - XML-SAX-Base-1.09 - pathname: G/GR/GRANTM/XML-SAX-Base-1.09.tar.gz - provides: - XML::SAX::Base 1.09 - XML::SAX::Base::NoHandler 1.09 - XML::SAX::Exception 1.09 - requirements: - ExtUtils::MakeMaker 0 - perl 5.008 - XML-SAX-Expat-0.51 - pathname: B/BJ/BJOERN/XML-SAX-Expat-0.51.tar.gz - provides: - XML::SAX::Expat 0.51 - requirements: - ExtUtils::MakeMaker 0 - XML::NamespaceSupport 0.03 - XML::Parser 2.27 - XML::SAX 0.03 - XML::SAX::Base 1.00 - XML-Simple-2.25 - pathname: G/GR/GRANTM/XML-Simple-2.25.tar.gz - provides: - XML::Simple 2.25 - requirements: - ExtUtils::MakeMaker 0 - XML::NamespaceSupport 1.04 - XML::SAX 0.15 - XML::SAX::Expat 0 - perl 5.008 + XML-XPath-1.48 + pathname: M/MA/MANWAR/XML-XPath-1.48.tar.gz + provides: + XML::XPath 1.48 + XML::XPath::Boolean 1.48 + XML::XPath::Builder 1.48 + XML::XPath::Expr 1.48 + XML::XPath::Function 1.48 + XML::XPath::Literal 1.48 + XML::XPath::LocationPath 1.48 + XML::XPath::Node 1.48 + XML::XPath::Node::Attribute 1.48 + XML::XPath::Node::AttributeImpl 1.48 + XML::XPath::Node::Comment 1.48 + XML::XPath::Node::Element 1.48 + XML::XPath::Node::Namespace 1.48 + XML::XPath::Node::PI 1.48 + XML::XPath::Node::Text 1.48 + XML::XPath::NodeSet 1.48 + XML::XPath::Number 1.48 + XML::XPath::Parser 1.48 + XML::XPath::PerlSAX 1.48 + XML::XPath::Root 1.48 + XML::XPath::Step 1.48 + XML::XPath::Variable 1.48 + XML::XPath::XMLParser 1.48 + requirements: + ExtUtils::MakeMaker 0 + Path::Tiny 0.076 + Scalar::Util 1.45 + Test::More 0 + XML::Parser 2.23 + perl 5.010001 XString-0.005 pathname: A/AT/ATOOMIC/XString-0.005.tar.gz provides: diff --git a/lib/MetaCPAN/Script/Author.pm b/lib/MetaCPAN/Script/Author.pm index 0c996dc37..5a093cabe 100644 --- a/lib/MetaCPAN/Script/Author.pm +++ b/lib/MetaCPAN/Script/Author.pm @@ -13,7 +13,7 @@ use Cpanel::JSON::XS qw( decode_json ); use Log::Contextual qw( :log :dlog ); use MetaCPAN::Document::Author (); use URI (); -use XML::Simple qw( XMLin ); +use XML::XPath (); use MetaCPAN::Types::TypeTiny qw( Str ); use MetaCPAN::Util qw(diff_struct); @@ -87,23 +87,16 @@ has whois_data => ( sub _build_whois_data { my $self = shift; - my $data = XMLin( - $self->author_fh, - ForceArray => 1, - SuppressEmpty => '', - NoAttr => 1, - KeyAttr => [], - ); my $whois_data = {}; - for my $author ( @{ $data->{cpanid} } ) { + my $xp = XML::XPath->new( filename => $self->author_fh ); + + for my $author ( $xp->find('/cpan-whois/cpanid')->get_nodelist ) { my $data = { - map { - my $content = $author->{$_}; - @$content == 1 - && !ref $content->[0] ? ( $_ => $content->[0] ) : (); - } keys %$author + map +( $_->getLocalName, $_->string_value ), + grep $_->isa('XML::XPath::Node::Element'), + $author->getChildNodes }; my $pauseid = $data->{id};