Foorum

 view release on metacpan or  search on metacpan

lib/Foorum/TheSchwartz/Worker/Scraper.pm  view on Meta::CPAN

                            formatter   => 'html',
                            post_on     => time(),
                            post_ip     => '127.0.0.1',
                            reply_to    => $reply_to,
                            forum_id    => $forum_id,
                            upload_id   => 0,
                        }
                    );
                    $is_changed = 1;

                    # if $reply_to == 0 means new topic
                    # then we use the first comment's comment_id as reply_to
                    $reply_to = $comment->comment_id if ( $reply_to == 0 );

                    # update $last_msg_id so that no need to run again
                    $last_msg_id = $content->{msg_id}
                        if ( $content->{msg_id} > $last_msg_id );
                }

                # clear cache
                my $cache_key
                    = "comment|object_type=topic|object_id=$topic_id";
                $cache->remove($cache_key);

            }
        }

        # update last_msg_id
        update_last_scraped_msg_id( $schema, "scraper-mailman-$name",
            $last_msg_id );

        # update threads|replies count for forum and user
        if ( $is_changed and $last_post_id ) {
            update_forum( $schema, $cache, $forum_id, $last_post_id );
            my $user
                = $schema->resultset('User')->get( { user_id => $user_id } );
            $schema->resultset('User')->update_threads_and_replies($user);
        }
    }

    error_log( $schema, 'info', $log_text );
    $job->completed();
}

sub get_last_scraped_msg_id {
    my ( $schema, $forum_id, $name ) = @_;

    my $count
        = $schema->resultset('Forum')->count( { forum_id => $forum_id } );
    return -1 unless ($count);    # forum non-exists

    $name = substr( $name, 0, 24 );
    my $rs = $schema->resultset('Variables')->search(
        {   type => 'log',
            name => $name
        }
    )->first;
    return $rs ? $rs->value : 0;
}

sub update_last_scraped_msg_id {
    my ( $schema, $name, $value ) = @_;

    $name = substr( $name, 0, 24 );
    $schema->resultset('Variables')->search(
        {   type => 'log',
            name => $name,
        }
    )->delete;
    $schema->resultset('Variables')->create(
        {   type  => 'log',
            name  => $name,
            value => $value
        }
    );
}

sub get_topic_or_create {
    my ( $schema, $forum_id, $title, $user_id, $replies_no ) = @_;

    # trim 'Re:\s+'
    foreach my $tre (@Re_s) {
        $title =~ s/^$tre\s+//isg;
    }

    my $topic = $schema->resultset('Topic')->search(
        {   title    => { 'LIKE', $title },
            forum_id => $forum_id,
        },
        { columns => ['topic_id'], }
    )->first;
    if ($topic) {
        my $rs = $schema->resultset('Comment')->search(
            {   object_type => 'topic',
                object_id   => $topic->topic_id,
            },
            {   order_by => 'post_on',
                rows     => 1,
                page     => 1,
                columns  => ['comment_id'],
            }
        )->first;
        if ($rs) {
            my $reply_to = $rs->comment_id;
            return ( $topic->topic_id, $reply_to );
        }
    }

    # or else, create one
    my $topic_title = encodeHTML($title);
    my $new_topic   = $schema->resultset('Topic')->create(
        {   forum_id         => $forum_id,
            title            => $topic_title,
            author_id        => $user_id,
            last_updator_id  => $user_id,
            last_update_date => time(),
            hit              => 0,
            total_replies    => $replies_no
        }
    );
    return ( $new_topic->topic_id, 0 );
}

sub update_forum {
    my ( $schema, $cache, $forum_id, $last_post_id ) = @_;

    my $forum
        = $schema->resultset('Forum')->count( { forum_id => $forum_id } );
    return unless ($forum);

    # update forum
    $schema->resultset('Forum')->search( { forum_id => $forum_id, } )
        ->update( { last_post_id => $last_post_id || 0, } );

    $cache->remove("forum|forum_id=$forum_id");
}

1;



( run in 0.688 second using v1.01-cache-2.11-cpan-5b529ec07f3 )