DiscuzX2.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 108 CC 7 SCAR 18.3
CC
7
Cov
0%
login
12.0
init_db_handler
12.0
post_thread
6.0
CRAP 6.2
1 #ABSTRACT: DISCUZ X2 帖子处理
2  
3 =pod
4  
5 =encoding utf8
6  
7 =head1 NAME
8  
9 BBS::DiscuzX2
10  
11 =over
12  
13 =back
14  
15 =head1 DESCRIPTION
16  
17 Discuz X2 贴子处理器
18  
19 =over
20  
21 =back
22  
23 =head1 SYNOPSIS
24  
25 注意:数据库中的表名前缀固定为pre_
26  
27 =over
28  
29 =back
30  
31 =head1 FUNCTION
32  
33 =head2 init_db_handler
34  
35 #初始化
36  
37 my $bbs = BBS::DiscuzX2->new();
38  
39 #初始化后台数据库连接
40  
41 #dp_port / db_charset 也可不填
42  
43 $bbs->init_db_handler(
44  
45 db_host => 'xxx.xxx.xxx.xxx',
46  
47 db_port => 3306,
48  
49 db_user => 'xxx',
50  
51 db_passwd => 'xxx',
52  
53 db_name => 'xxx',
54  
55 db_charset => 'utf8',
56 );
57  
58 =over
59  
60 =back
61  
62 =head2 create_user
63  
64 #后台新建论坛用户
65  
66 #如果passwd未指定,则采用default_passwd
67  
68 #group_id 为用户所在群组,如果未指定,则采用default_group_id
69  
70 #mail/user_ip可不填
71  
72 $bbs->{db_handler}{default_passwd} = 'ashaxj';
73  
74 $bbs->{db_handler}{default_group_id} = 10;
75  
76 my $uid = $bbs->create_user({
77  
78 user => 'xxx',
79  
80 passwd => 'xxx',
81  
82 group_id => 10,
83  
84 mail => 'xxx@xxx.xxx',
85  
86 user_ip => 'xxx.xxx.xxx.xxx',
87  
88 });
89  
90 =over
91  
92 =back
93  
94 =head2 load_thread
95  
96 #从后台向 版块10 载入一个贴子
97  
98 my $data = {
99  
100 fid => 10,
101  
102 floors => [
103  
104 { poster => 'abc', subject => 'test', dateline => '2013-03-05 11:20:00',
105  
106 message => 'just a test', user_ip => '123.123.123.123',
107  
108 is_html => 0, is_bbcode => 1,
109  
110 },
111  
112 { poster => 'def', dateline => '2013-03-05 11:21:00',
113  
114 message => 'just a test reply', user_ip => '222.222.222.222',
115  
116 },
117  
118 { poster => 'ghi', dateline => '2013-03-06 10:00:03',
119  
120 message => 'just a test reply update', user_ip => '202.202.202.202',
121 },
122  
123 ],
124  
125 };
126  
127 my $tid = $self->load_thread($data);
128  
129 =over
130  
131 =back
132  
133 =head2 init_browser
134  
135 #初始化浏览器
136  
137 $bbs->init_browser(
138  
139 'User-Agent' =>
140  
141 'Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0',
142  
143 );
144  
145 =over
146  
147 =back
148  
149 =head2 login
150  
151 #用户登录
152  
153 $bbs->login(
154  
155 site => 'http://127.0.0.1/discuz_x2/',
156  
157 user => 'xxx',
158  
159 passwd => 'xxx',
160  
161 );
162  
163 =over
164  
165 =back
166  
167 =head2 post_thread
168  
169 #在版块2发新帖
170  
171 my $r = $bbs->post_thread({
172  
173 fid => 2,
174  
175 subject => 'hello world',
176  
177 message => 'just a test ',
178  
179 });
180  
181 #$r->{tid}为贴子编号
182  
183 #$r->{pid}为贴子内容编号
184  
185 #$r->{res}为返回的html response
186  
187 =over
188  
189 =back
190  
191 =head2 delete_thread
192  
193 #删帖
194  
195 $bbs->delete_thread({
196  
197 fid => 2,
198  
199 tid => 6,
200  
201 pid => 9,
202  
203 });
204  
205 =over
206  
207 =back
208  
209 =over
210  
211 =back
212  
213 =cut
214  
215 use strict;
216 use warnings;
217 package BBS::DiscuzX2;
218 use Moo;
219 use BBS::DiscuzX2::DB;
220 use WWW::Mechanize;
221  
222 our $VERSION =0.01;
223  
224 sub init_browser {
225 my ($self, %opt) = @_;
226  
227 $self->{browser} = WWW::Mechanize->new(autocheck=>0);
228 while(my ($k, $v) = each %opt){
229 $self->{browser}->add_header($k => $v);
230 }
231  
232 $self;
233 }
234  
235 sub login {
236 my ($self, %opt) = @_;
237  
238 $self->{browser}->add_header('Referer' => $opt{site});
239 my $url = $opt{site}.'member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes&inajax=1';
240 my $data = [
241 username=>$opt{user},
242 password=>$opt{passwd},
243 fastloginfield=>'username',
244 quickforward=>'yes',
245 handlekey=>'ls',
246 ];
247 my $res = $self->{browser}->post($url, $data);
248 return unless($res->is_success);
249 $self->{site} = $opt{site};
250  
251 $res = $self->{browser}->get($opt{site}.'forum.php');
252 return unless($res->is_success);
253  
254 ($self->{formhash}) = $res->decoded_content =~ m/formhash=(\w+)">/s;
255 return 1;
256 }
257  
258 sub post_thread {
259 my ($self, $data) = @_;
260 my $url = "$self->{site}forum.php?mod=post&action=newthread&fid=$data->{fid}&extra=&topicsubmit=yes";
261 my $res = $self->{browser}->post($url,
262 [
263 'formhash' => $self->{formhash},
264 'message' => $data->{message},
265 'subject' => $data->{subject},
266 'usesig' => $data->{use_sig} // 1,
267 'allownoticeauthor' => $data->{notice_author} // 1,
268 'wysiwyg' => '1',
269 'newalbum' => '',
270 'posttime' => '',
271 'save' => '',
272 'uploadalbum' => '',
273 ],
274 );
275  
276 return unless($res->is_success);
277  
278 my ($tid, $pid) = $res->as_string =~ m[action=edit&fid=\d+&tid=(\d+)&pid=(\d+)&]s;
279 return {
280 tid => $tid,
281 pid => $pid,
282 response => $res,
283 };
284 }
285  
286 #sub append_thread {
287 # my ($self, $data) = @_;
288 # my $url = "$self->{site}forum.php?mod=misc&action=postappend&tid=$data->{tid}&pid=$data->{pid}&extra=&postappendsubmit=yes&infloat=yes";
289 # my $res = $self->{browser}->post($url,
290 # [
291 # 'formhash' => $self->{formhash},
292 # 'postappendmessage' => $data->{message},
293 # 'handlekey' => 'postappend',
294 # ],
295 # );
296 #
297 # return unless($res->is_success);
298 # return $res;
299 #}
300  
301 sub delete_thread {
302 my ($self, $data) = @_;
303 my $url = "$self->{site}forum.php?mod=post&action=edit&extra=&editsubmit=yes";
304 my $referer = "$self->{site}forum.php?mod=post&action=edit&fid=$data->{fid}&tid=$data->{tid}&pid=$data->{pid}&page=1";
305 $self->{browser}->add_header('Referer' => $referer);
306 my $res = $self->{browser}->post($url,
307 [
308 fid => $data->{fid},
309 tid => $data->{tid},
310 pid => $data->{pid},
311 formhash => $self->{formhash},
312 allownoticeauthor => $data->{notice_author} // 1,
313 delattachop => $data->{del_attach} || 0,
314 delete => 1,
315 ],
316 );
317  
318 return unless($res->is_success);
319 return 1;
320 }
321  
322 sub init_db_handler {
323 my ($self, %db_opt) = @_;
324 $db_opt{db_port} ||= 3306;
325  
326 my $dsn = "DBI:mysql:host=$db_opt{db_host};port=$db_opt{db_port};database=$db_opt{db_name}";
327 $self->{db_handler} = BBS::DiscuzX2::DB->new(
328 connect_info => [ $dsn, $db_opt{db_user}, $db_opt{db_passwd} ]
329 );
330  
331 if($db_opt{db_charset}){
332 $self->{db_handler}->do("SET character_set_client='$db_opt{db_charset}'");
333 $self->{db_handler}->do("SET character_set_connection='$db_opt{db_charset}'");
334 $self->{db_handler}->do("SET character_set_results='$db_opt{db_charset}'");
335 }
336  
337 for my $k (qw/default_passwd default_group_id/){
338 next unless(exists $db_opt{$k});
339 $self->{db_handler}{$k} = $db_opt{$k};
340 }
341  
342 $self;
343 }
344  
345 sub create_user {
346 my ($self,$data) = @_;
347 $self->{db_handler}->create_user($data);
348 }
349  
350 sub load_thread {
351 my ($self,$data) = @_;
352 $self->{db_handler}->load_thread($data);
353 }
354  
355 1;