Search::Sitemap::Ping - Notify search engines of sitemap updates


Search-Sitemap documentation Contained in the Search-Sitemap distribution.

Index


Code Index:

NAME

Top

Search::Sitemap::Ping - Notify search engines of sitemap updates

SYNOPSIS

Top

  use Search::Sitemap::Ping;

  my $ping = Search::Sitemap::Ping->new(
    'http://www.jasonkohles.com/sitemap.gz',
  );

  $ping->submit;

  for my $url ( $ping->urls ) {
      print "$url\n";
      for my $engine ( $ping->engines ) {
          printf( "    %25s %s\n", $engine, $ping->status( $url, $engine ) );
      }
  }

DESCRIPTION

Top

This module makes it easy to notify search engines that your sitemaps, or sitemap indexes, have been updated. See Search::Sitemap and Search::Sitemap::Index for tools to help you create sitemaps and indexes.

METHODS

Top

new

Create a new Search::Sitemap::Ping object.

add_url( @urls )

Add one or more urls to the list of URLs to submit.

urls

Return the list of urls that will be (or were) submitted.

add_engine( @engines )

Add one or more search engines to the list of search engines to submit to.

engines

Return the list of search engines that will be (or were) submitted to.

submit

Submit the urls to the search engines, returns the number of successful submissions. This module uses LWP::UserAgent for the web-based submissions, and will honor proxy settings in the environment. See LWP::UserAgent for more information.

status( $url [, $engine ] )

Returns the status of the indicated submission. The URL must be specified, If an engine is specified it will return just the status of the submission to that engine, otherwise it will return a hashref of the engines that the url will be (or was) submitted to, and the status for each one.

The status may be one of:

* undef or empty string

Not submitted yet.

* 'SUCCESS'

Succesfully submitted. Note that this just means it was successfully transferred to the search engine, if there are problems in the file the search engine may reject it later when it attempts to use it.

* HTTP Error String

In case of an error, the error string will be provided as the status.

MODULE HOME PAGE

Top

The home page of this module is http://www.jasonkohles.com/software/Search-Sitemap. This is where you can always find the latest version, development versions, and bug reports. You will also find a link there to report bugs.

SEE ALSO

Top

Search::Sitemap

AUTHOR

Top

Jason Kohles, <email@jasonkohles.com>

COPYRIGHT AND LICENSE

Top


Search-Sitemap documentation Contained in the Search-Sitemap distribution.

package Search::Sitemap::Ping;
use strict; use warnings;
our $VERSION = '2.13';
our $AUTHORITY = 'cpan:JASONK';
use Moose;
use Search::Sitemap::Pinger;
use Class::Trigger qw(
    progress success failure
    before_submit after_submit
    before_engine after_engine
);
use MooseX::Types::Moose qw( ArrayRef );
use namespace::clean -except => [qw( meta add_trigger call_trigger )];

has 'urls'  => (
    is          => 'rw',
    isa         => ArrayRef,
    lazy        => 1,
    auto_deref  => 1,
    default     => sub { [] },
);

has 'engines'   => (
    is          => 'rw',
    isa         => ArrayRef['Search::Sitemap::Pinger'],
    auto_deref  => 1,
    lazy        => 1,
    default     => sub { [
        map { $_->new } Search::Sitemap::Pinger->ALL_PINGERS
    ] },
);

sub BUILDARGS {
    my $class = shift;
    my @urls = ();
    while ( @_ && $_[0] =~ m{^https?://} ) { push( @urls, shift ) }
    my $args = $class->SUPER::BUILDARGS( @_ );
    push( @{ $args->{ 'urls' } ||= [] }, @urls );
    return $args;
}

sub submit {
    my $self = shift;

    $self->call_trigger( 'before_submit' );
    my $total = @{ $self->urls } * @{ $self->engines };
    my $attempt = 0;
    my $success = 0;
    my $failure = 0;
    my $progress = sub {
        my $percent = sprintf( '%.02f', ( $attempt / $total ) * 100 );
        $self->call_trigger( 'progress',
            $percent, $total, $attempt, $success, $failure
        );
    };
    $progress->();
    for my $engine ( $self->engines ) {
        my @urls = $self->urls;
        $self->call_trigger( 'before_engine', $engine, \@urls );
        next unless @urls;
        $engine->submit( sub {
            my ( $status, $url, $msg ) = @_;
            $attempt++;
            if ( $status eq 'success' ) {
                $success++;
            } else {
                $failure++;
            }
            unless ( $progress->() ) {
                if ( $status eq 'failure' ) {
                    warn "Submitting $url to $engine failed: $msg\n";
                }
            }
        }, @urls );
        $self->call_trigger( 'after_engine', $engine );
    }
    $self->call_trigger( 'after_submit' );
}

__PACKAGE__->meta->make_immutable;
1;
__END__