From 8ec8da0491ad89604700b3e29a227966f6d84ba1 Mon Sep 17 00:00:00 2001 From: Perl Tidy Date: Wed, 5 Dec 2018 15:38:52 -0500 Subject: no bug - reformat all the code using the new perltidy rules --- extensions/SiteMapIndex/lib/Constants.pm | 8 +- extensions/SiteMapIndex/lib/Util.pm | 207 ++++++++++++++++--------------- 2 files changed, 111 insertions(+), 104 deletions(-) (limited to 'extensions/SiteMapIndex/lib') diff --git a/extensions/SiteMapIndex/lib/Constants.pm b/extensions/SiteMapIndex/lib/Constants.pm index 4f404c8b1..bd098d16a 100644 --- a/extensions/SiteMapIndex/lib/Constants.pm +++ b/extensions/SiteMapIndex/lib/Constants.pm @@ -27,10 +27,10 @@ use warnings; use base qw(Exporter); our @EXPORT = qw( - SITEMAP_AGE - SITEMAP_MAX - SITEMAP_DELAY - SITEMAP_URL + SITEMAP_AGE + SITEMAP_MAX + SITEMAP_DELAY + SITEMAP_URL ); # This is the amount of hours a sitemap index and it's files are considered diff --git a/extensions/SiteMapIndex/lib/Util.pm b/extensions/SiteMapIndex/lib/Util.pm index 4519461b4..fb945e324 100644 --- a/extensions/SiteMapIndex/lib/Util.pm +++ b/extensions/SiteMapIndex/lib/Util.pm @@ -28,8 +28,8 @@ use warnings; use base qw(Exporter); our @EXPORT = qw( - generate_sitemap - bug_is_ok_to_index + generate_sitemap + bug_is_ok_to_index ); use Bugzilla::Extension::SiteMapIndex::Constants; @@ -41,169 +41,176 @@ use Scalar::Util qw(blessed); use IO::Compress::Gzip qw(gzip $GzipError); sub too_young_date { - my $hours_ago = DateTime->now(time_zone => Bugzilla->local_timezone); - $hours_ago->subtract(hours => SITEMAP_DELAY); - return $hours_ago; + my $hours_ago = DateTime->now(time_zone => Bugzilla->local_timezone); + $hours_ago->subtract(hours => SITEMAP_DELAY); + return $hours_ago; } sub bug_is_ok_to_index { - my ($bug) = @_; - return 1 unless blessed($bug) && $bug->isa('Bugzilla::Bug') && !$bug->{error}; - my $creation_ts = datetime_from($bug->creation_ts); - return ($creation_ts && $creation_ts lt too_young_date()) ? 1 : 0; + my ($bug) = @_; + return 1 unless blessed($bug) && $bug->isa('Bugzilla::Bug') && !$bug->{error}; + my $creation_ts = datetime_from($bug->creation_ts); + return ($creation_ts && $creation_ts lt too_young_date()) ? 1 : 0; } # We put two things in the Sitemap: a list of Browse links for products, # and links to bugs. sub generate_sitemap { - my ($extension_name) = @_; - - # If file is less than SITEMAP_AGE hours old, then read in and send to caller. - # If greater, then regenerate and send the new version. - my $index_file = bz_locations->{'datadir'} . "/$extension_name/sitemap_index.xml"; - if (-e $index_file) { - my $index_mtime = (stat($index_file))[9]; - my $index_hours = sprintf("%d", (time() - $index_mtime) / 60 / 60); # in hours - if ($index_hours < SITEMAP_AGE) { - my $index_fh = new IO::File($index_file, 'r'); - $index_fh || die "Could not open current sitemap index: $!"; - my $index_xml; - { local $/; $index_xml = <$index_fh> } - $index_fh->close() || die "Could not close current sitemap index: $!"; - - return $index_xml; - } + my ($extension_name) = @_; + + # If file is less than SITEMAP_AGE hours old, then read in and send to caller. + # If greater, then regenerate and send the new version. + my $index_file + = bz_locations->{'datadir'} . "/$extension_name/sitemap_index.xml"; + if (-e $index_file) { + my $index_mtime = (stat($index_file))[9]; + my $index_hours = sprintf("%d", (time() - $index_mtime) / 60 / 60); # in hours + if ($index_hours < SITEMAP_AGE) { + my $index_fh = new IO::File($index_file, 'r'); + $index_fh || die "Could not open current sitemap index: $!"; + my $index_xml; + { local $/; $index_xml = <$index_fh> } + $index_fh->close() || die "Could not close current sitemap index: $!"; + + return $index_xml; } - - # Set the atime and mtime of the index file to the current time - # in case another request is made before we finish. - utime(undef, undef, $index_file); - - # Sitemaps must never contain private data. - Bugzilla->logout_request(); - my $user = Bugzilla->user; - my $products = $user->get_accessible_products; - - my $num_bugs = SITEMAP_MAX - scalar(@$products); - # We do this date math outside of the database because databases - # usually do better with a straight comparison value. - my $hours_ago = too_young_date(); - - # We don't use Bugzilla::Bug objects, because this could be a tremendous - # amount of data, and we only want a little. Also, we only display - # bugs that are not in any group. We show the last $num_bugs - # most-recently-updated bugs. - my $dbh = Bugzilla->dbh; - my $bug_sth = $dbh->prepare( - 'SELECT bugs.bug_id, bugs.delta_ts + } + + # Set the atime and mtime of the index file to the current time + # in case another request is made before we finish. + utime(undef, undef, $index_file); + + # Sitemaps must never contain private data. + Bugzilla->logout_request(); + my $user = Bugzilla->user; + my $products = $user->get_accessible_products; + + my $num_bugs = SITEMAP_MAX - scalar(@$products); + + # We do this date math outside of the database because databases + # usually do better with a straight comparison value. + my $hours_ago = too_young_date(); + + # We don't use Bugzilla::Bug objects, because this could be a tremendous + # amount of data, and we only want a little. Also, we only display + # bugs that are not in any group. We show the last $num_bugs + # most-recently-updated bugs. + my $dbh = Bugzilla->dbh; + my $bug_sth = $dbh->prepare( + 'SELECT bugs.bug_id, bugs.delta_ts FROM bugs LEFT JOIN bug_group_map ON bugs.bug_id = bug_group_map.bug_id WHERE bug_group_map.bug_id IS NULL AND creation_ts < ? - ' . $dbh->sql_limit($num_bugs, '?')); + ' . $dbh->sql_limit($num_bugs, '?') + ); - my $filecount = 1; - my $filelist = []; - my $offset = 0; + my $filecount = 1; + my $filelist = []; + my $offset = 0; - while (1) { - my $bugs = []; + while (1) { + my $bugs = []; - $bug_sth->execute($hours_ago, $offset); + $bug_sth->execute($hours_ago, $offset); - while (my ($bug_id, $delta_ts) = $bug_sth->fetchrow_array()) { - push(@$bugs, { bug_id => $bug_id, delta_ts => $delta_ts }); - } + while (my ($bug_id, $delta_ts) = $bug_sth->fetchrow_array()) { + push(@$bugs, {bug_id => $bug_id, delta_ts => $delta_ts}); + } - last if !@$bugs; + last if !@$bugs; - # We only need the product links in the first sitemap file - $products = [] if $filecount > 1; + # We only need the product links in the first sitemap file + $products = [] if $filecount > 1; - push(@$filelist, _generate_sitemap_file($extension_name, $filecount, $products, $bugs)); + push(@$filelist, + _generate_sitemap_file($extension_name, $filecount, $products, $bugs)); - $filecount++; - $offset += $num_bugs; - } + $filecount++; + $offset += $num_bugs; + } - # Generate index file - return _generate_sitemap_index($extension_name, $filelist); + # Generate index file + return _generate_sitemap_index($extension_name, $filelist); } sub _generate_sitemap_index { - my ($extension_name, $filelist) = @_; + my ($extension_name, $filelist) = @_; - my $dbh = Bugzilla->dbh; - my $timestamp = $dbh->selectrow_array( - "SELECT " . $dbh->sql_date_format('NOW()', '%Y-%m-%d')); + my $dbh = Bugzilla->dbh; + my $timestamp = $dbh->selectrow_array( + "SELECT " . $dbh->sql_date_format('NOW()', '%Y-%m-%d')); - my $index_xml = < END - foreach my $filename (@$filelist) { - $index_xml .= " + foreach my $filename (@$filelist) { + $index_xml .= " - " . Bugzilla->localconfig->{urlbase} . "data/$extension_name/$filename + " + . Bugzilla->localconfig->{urlbase} . "data/$extension_name/$filename $timestamp "; - } + } - $index_xml .= < END - my $index_file = bz_locations->{'datadir'} . "/$extension_name/sitemap_index.xml"; - my $index_fh = new IO::File($index_file, 'w'); - $index_fh || die "Could not open new sitemap index: $!"; - print $index_fh $index_xml; - $index_fh->close() || die "Could not close new sitemap index: $!"; + my $index_file + = bz_locations->{'datadir'} . "/$extension_name/sitemap_index.xml"; + my $index_fh = new IO::File($index_file, 'w'); + $index_fh || die "Could not open new sitemap index: $!"; + print $index_fh $index_xml; + $index_fh->close() || die "Could not close new sitemap index: $!"; - return $index_xml; + return $index_xml; } sub _generate_sitemap_file { - my ($extension_name, $filecount, $products, $bugs) = @_; + my ($extension_name, $filecount, $products, $bugs) = @_; - my $bug_url = Bugzilla->localconfig->{urlbase} . 'show_bug.cgi?id='; - my $product_url = Bugzilla->localconfig->{urlbase} . 'describecomponents.cgi?product='; + my $bug_url = Bugzilla->localconfig->{urlbase} . 'show_bug.cgi?id='; + my $product_url + = Bugzilla->localconfig->{urlbase} . 'describecomponents.cgi?product='; - my $sitemap_xml = < END - foreach my $product (@$products) { - $sitemap_xml .= " + foreach my $product (@$products) { + $sitemap_xml .= " " . $product_url . url_quote($product->name) . " daily 0.4 "; - } + } - foreach my $bug (@$bugs) { - $sitemap_xml .= " + foreach my $bug (@$bugs) { + $sitemap_xml .= " " . $bug_url . $bug->{bug_id} . " " . datetime_from($bug->{delta_ts}, 'UTC')->iso8601 . 'Z' . " "; - } + } - $sitemap_xml .= < END - # Write the compressed sitemap data to a file in the cgi root so that they can - # be accessed by the search engines. - my $filename = "sitemap$filecount.xml.gz"; - gzip \$sitemap_xml => bz_locations->{'datadir'} . "/$extension_name/$filename" - || die "gzip failed: $GzipError\n"; + # Write the compressed sitemap data to a file in the cgi root so that they can + # be accessed by the search engines. + my $filename = "sitemap$filecount.xml.gz"; + gzip \$sitemap_xml => bz_locations->{'datadir'} . "/$extension_name/$filename" + || die "gzip failed: $GzipError\n"; - return $filename; + return $filename; } 1; -- cgit v1.2.3-24-g4f1b