BBS-DiscuzX2

 view release on metacpan or  search on metacpan

lib/BBS/DiscuzX2.pm  view on Meta::CPAN

#ABSTRACT: DISCUZ X2 帖子处理

=pod

=encoding utf8

=head1 NAME

BBS::DiscuzX2

=over 

=back

=head1 DESCRIPTION 

Discuz X2 贴子处理器

=over 

=back

=head1 SYNOPSIS

   注意:数据库中的表名前缀固定为pre_

=over 

=back

=head1 FUNCTION

=head2 init_db_handler

    #初始化

    my $bbs = BBS::DiscuzX2->new();

    #初始化后台数据库连接

    #dp_port / db_charset 也可不填

    $bbs->init_db_handler(

        db_host => 'xxx.xxx.xxx.xxx',

        db_port => 3306, 

        db_user => 'xxx',

        db_passwd => 'xxx',

        db_name => 'xxx',

        db_charset => 'utf8', 
    );

=over

=back

=head2 create_user

    #后台新建论坛用户

    #如果passwd未指定,则采用default_passwd

    #group_id 为用户所在群组,如果未指定,则采用default_group_id

    #mail/user_ip可不填

    $bbs->{db_handler}{default_passwd} = 'ashaxj';

    $bbs->{db_handler}{default_group_id} = 10;

    my $uid = $bbs->create_user({

        user => 'xxx',

        passwd => 'xxx',

        group_id => 10, 

        mail => 'xxx@xxx.xxx',

        user_ip => 'xxx.xxx.xxx.xxx', 

    });

=over

=back

=head2 load_thread

    #从后台向 版块10 载入一个贴子

    my $data = {

        fid => 10, 

        floors => [

            {   poster => 'abc', subject => 'test', dateline => '2013-03-05 11:20:00', 

                message => 'just a test', user_ip => '123.123.123.123', 

                is_html => 0, is_bbcode => 1, 

            }, 

            {   poster => 'def', dateline => '2013-03-05 11:21:00', 

                message => 'just a test reply', user_ip => '222.222.222.222', 

lib/BBS/DiscuzX2.pm  view on Meta::CPAN

            'save' => '',
            'uploadalbum' => '',
        ],	
    );

    return unless($res->is_success);

    my ($tid, $pid) = $res->as_string =~ m[action=edit&fid=\d+&tid=(\d+)&pid=(\d+)&]s;
    return {
        tid => $tid,
        pid => $pid, 
        response => $res,
    };	
}

#sub append_thread {
#	my ($self, $data) = @_;
#	my $url = "$self->{site}forum.php?mod=misc&action=postappend&tid=$data->{tid}&pid=$data->{pid}&extra=&postappendsubmit=yes&infloat=yes";
#	my $res = $self->{browser}->post($url,
#		[
#			'formhash' => $self->{formhash},
#			'postappendmessage' => $data->{message}, 
#			'handlekey' => 'postappend',
#		],
#	);
#
#	return unless($res->is_success);
#	return $res;
#}

sub delete_thread {
    my ($self, $data) = @_;
    my $url = "$self->{site}forum.php?mod=post&action=edit&extra=&editsubmit=yes";
    my $referer = "$self->{site}forum.php?mod=post&action=edit&fid=$data->{fid}&tid=$data->{tid}&pid=$data->{pid}&page=1";
    $self->{browser}->add_header('Referer' => $referer);
    my $res = $self->{browser}->post($url,
        [
            fid	=> $data->{fid}, 
            tid	=> $data->{tid}, 
            pid	=> $data->{pid}, 
            formhash	=> $self->{formhash},
            allownoticeauthor	=> $data->{notice_author} // 1, 
            delattachop	=> $data->{del_attach} || 0, 
            delete	=> 1,
        ],
    );

    return unless($res->is_success);
    return 1;
}

sub init_db_handler {
    my ($self, %db_opt) = @_;
    $db_opt{db_port} ||= 3306;

    my $dsn      = "DBI:mysql:host=$db_opt{db_host};port=$db_opt{db_port};database=$db_opt{db_name}";
    $self->{db_handler} = BBS::DiscuzX2::DB->new(
        connect_info => [ $dsn, $db_opt{db_user}, $db_opt{db_passwd} ]
    );

    if($db_opt{db_charset}){
        $self->{db_handler}->do("SET character_set_client='$db_opt{db_charset}'");
        $self->{db_handler}->do("SET character_set_connection='$db_opt{db_charset}'");
        $self->{db_handler}->do("SET character_set_results='$db_opt{db_charset}'");
    }

    for my $k (qw/default_passwd default_group_id/){
        next unless(exists $db_opt{$k});
        $self->{db_handler}{$k} = $db_opt{$k};
    }

    $self;
}

sub create_user {
    my ($self,$data) = @_;
    $self->{db_handler}->create_user($data);
}

sub load_thread {
    my ($self,$data) = @_;
    $self->{db_handler}->load_thread($data);
}

1;



( run in 0.580 second using v1.01-cache-2.11-cpan-6aa56a78535 )