-
Notifications
You must be signed in to change notification settings - Fork 0
/
se.pl
executable file
·512 lines (405 loc) · 13 KB
/
se.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#!/usr/bin/perl -w
#=[ Alpha ]====================================================================
#
# se.pl - Query an SQLite3 database imported from a StackExchange XML dump and
# display the questions and answers in a human-readable format.
#
# This code should be considered in an 'alpha' state and is missing features,
# intelligent architecture and design, and probable bug fixes. My apologies.
#
#===============================================================[ AJC 2019 ]===
#======================================
# Mandatory Modules
#======================================
use HTML::FormatText::WithLinks;
use List::MoreUtils qw(uniq);
use List::Flatten;
use Getopt::Long;
use Data::Dumper;
use DBD::SQLite;
use Text::Wrap;
use warnings;
use strict;
use Encode;
use POSIX;
use DBI;
#======================================
# Constants and Global Variables
#======================================
$Text::Wrap::unexpand = 0;
my $dbpath = "/home/josefcub/scripts/se-db/";
my $dbname = "stackoverflow";
my $dbext = ".db";
#======================================
# Initialization Code
#======================================
#
# Process the command line appropriately.
#
my $allflag;
my $bodysearch;
my $help;
my $listflag;
my $nomungeflag;
my $questionsearch;
my $titlesearch;
GetOptions(
'all|a' => \$allflag,
'body|b=s' => \$bodysearch,
'database|d=s' => \$dbname,
'help|h' => \$help,
'list|l' => \$listflag,
'nomunge|n' => \$nomungeflag,
'question|q=s' => \$questionsearch,
'title|s=s' => \$titlesearch,
) || die printUsage();
printUsage() if defined $help;
# Get the database set up and connected.
my $dbh = DBI->connect(
"dbi:SQLite:$dbpath$dbname$dbext",
"",
"",
{ RaiseError => 1 },
) or die $DBI::errstr;
#======================================
# Useful Subroutines
#======================================
#####
#
# printUsage - Prints out a useful help message.
#
# COMMAND-LINE ARGUMENTS
# -h prints out a usage summary
#
#####
sub printUsage {
my $name = $0;
print << "ENDUSAGE";
Usage: $name [options]
--all -a Print out all available results
--body -b <string> Searches the bodies of questions
--database -d <dbname> Searches a specific database
--list -l Lists available SE databases
--help -h Print this message
--nomunge -n Doesn't alter or remove HTML
--question -q <ID> return a question and answers
--title -t <string> Searches only the titles of questions
ENDUSAGE
exit(0)
}
######
#
# subhtml - Do bad practices to HTML.
#
# This is neither the smart way,
# the Perl way, nor the right way
# to handle this.
#
######
sub subhtml {
my $data = shift;
if (not $nomungeflag and $data) {
# Handle specific tags
$data =~ s/<LI>/\n\ \ \*\ /gsi;
$data =~ s/<STRONG>/\*/gsi;
$data =~ s/<\/STRONG>/\*/gsi;
$data =~ s/<BLOCKQUOTE>/---------------------\n/gsi;
$data =~ s/<\/BLOCKQUOTE>/---------------------\n/gsi;
$data =~ s/<PRE><CODE>/---------------------\n/gsi;
$data =~ s/<\/CODE><\/PRE>/---------------------\n/gsi;
# Handle the rest of the HTML tags
$data =~ s/<[^>]*>//gs;
# Handle HTML entities
$data =~ s/\ //gsi;
$data =~ s/\</</gsi;
$data =~ s/\>/>/gsi;
}
return($data);
}
######
#
# searchtitles - Search titles in database for specified string
#
######
sub searchtitles {
my $search = shift;
# Fetch our search results from the database
my $stmt = $dbh->prepare("SELECT rowid,Title,Body from posts_search WHERE Title MATCH '$search';");
$stmt->execute() or die $DBI::errstr;
my @results = $stmt->fetchall_arrayref();
my $retcount = $stmt->rows();
$stmt->finish();
# Search results empty
if ($retcount == 0) {
print "There were no results found for '$search'.\n";
exit(-1);
}
# Search results header
$Text::Wrap::columns = 71;
print ("Number\t| Article Title\n");
print "="x80 . "\n";
# The results themselves.
if ($allflag) {
for (my $counter = 0;$counter < $retcount;$counter++) {
if (not defined $results[0][$counter][0]) { next }
# make sure my body is ready
my @body = split("\n", $results[0][$counter][2]);
push @body, [$1] while $body[0] =~ s/<[^>]*>//gs;
my $body = $body[0];
# Pretty formatting.
my $numbertabs = "\t\t";
if ($results[0][$counter][0] > 9999999) {
$numbertabs = "\t";
}
print $results[0][$counter][0] . "$numbertabs| " . wrap("","\t\t| ",$results[0][$counter][1]) . "\n";
print "\t\t" . "-"x64 . "\n";
print "\t\t| " . wrap("","\t\t| ", $body) . "\n";
if ($counter < $retcount - 1) { print "-"x80 . "\n" }
}
} else {
for (my $counter = 0;$counter < 10;$counter++) {
# Pretty formatting.
my $numbertabs = "\t\t";
if (defined $results[0][$counter][0] and $results[0][$counter][0] > 9999999) {
$numbertabs = "\t";
}
if (not defined $results[0][$counter][0]) { next }
print $results[0][$counter][0] . "$numbertabs| " . wrap("","\t\t| ",$results[0][$counter][1]) . "\n";
if ($counter < 9 and $counter < $retcount - 1) { print "-"x80 . "\n" }
}
}
# Finish out the footer
print "="x80 . "\n";
if ($retcount < 10 or $allflag) {
if ($retcount > 1) {
print "\nAll $retcount results shown for '$search'.\n";
} else {
print "One result shown for '$search'.\n";
}
} else {
print "\nShowing 10 results of $retcount maximum for '$search'.\n";
}
}
######
#
# searchbodies - Search titles in database for specified string
#
######
sub searchbodies {
my $search = shift;
# Fetch our search results from the database
my $stmt = $dbh->prepare("SELECT rowid,Title,Body from posts_search WHERE Body MATCH '$search';");
$stmt->execute() or die $DBI::errstr;
my @results = $stmt->fetchall_arrayref();
my $retcount = $stmt->rows();
$stmt->finish();
$dbh->disconnect();
# Search results empty
if ($retcount == 0) {
print "There were no results found for '$search'.\n";
exit(-1);
}
# Search results header
$Text::Wrap::columns = 71;
print ("Number\t| Article Title and Match\n");
print "="x80 . "\n";
# The results themselves.
if ($allflag) {
for (my $counter = 0;$counter < $retcount;$counter++) {
# make sure my body is redy
my @body = split("\n", $results[0][$counter][2]);
# munge search term for grep
my @searchterms = join("|", split(" ", $search));
# Pull out the first matching line and remove any HTML.
my @foo = grep(/@searchterms/i, @body);
push @foo, [$1] while $foo[0] =~ s/<[^>]*>//gs;
my $foo = $foo[0];
# Pretty formatting.
my $numbertabs = "\t\t";
if ($results[0][$counter][0] > 9999999) {
$numbertabs = "\t";
}
if (not defined $results[0][$counter][0]) { next }
print $results[0][$counter][0] . "$numbertabs| " . wrap("","\t\t| ",$results[0][$counter][1]) . "\n";
print "\t\t" . "-"x64 . "\n";
print "\t\t| " . wrap("","\t\t| ", $foo) . "\n";
if ($counter < $retcount - 1) { print "-"x80 . "\n" }
}
} else {
for (my $counter = 0;$counter < 10;$counter++) {
if (not defined $results[0][$counter][0]) { next }
# make sure my body is redy
my @body = split("\n", $results[0][$counter][2]);
# munge search term for grep
my @searchterms = join("|", split(" ", $search));
# Pull out the first matching line and remove any HTML.
my @foo = grep(/@searchterms/i, @body);
if ($foo[0]) {
push @foo, [$1] while $foo[0] =~ s/<[^>]*>//gs;
} else {
$foo[0] = "";
}
my $foo = $foo[0];
# Pretty formatting.
my $numbertabs = "\t\t";
if ($results[0][$counter][0] > 9999999) {
$numbertabs = "\t";
}
# Display foo with the article title for context
print $results[0][$counter][0] . "$numbertabs| " . wrap("","\t\t| ",$results[0][$counter][1]) . "\n";
print "\t\t" . "-"x64 . "\n";
print "\t\t| " . wrap("","\t\t| ", $foo) . "\n";
if ($counter < 9 and $counter < $retcount - 1) { print "-"x80 . "\n" }
}
}
# Finish out the footer
print "="x80 . "\n";
if ($retcount < 10 or $allflag) {
if ($retcount > 1) {
print "\nAll $retcount results shown for '$search'.\n";
} else {
print "One result shown for '$search'.\n";
}
} else {
print "\nShowing 10 results of $retcount maximum for '$search'.\n";
}
}
#####
#
# showquestion - Shows the question referenced by number.
#
#####
sub showquestion {
my $question = shift;
$Text::Wrap::columns = 91;
# Fetch the post itself, and any comments
my $stmt = $dbh->prepare("SELECT posts.Title, posts.Body, comments.Text, posts.ParentId FROM posts LEFT JOIN comments ON posts.Id = comments.PostId WHERE posts.Id='$question';");
$stmt->execute() or die $DBI::errstr;
my @post = flat $stmt->fetchall_arrayref();
my $postcount = $stmt->rows();
# Stop here if it doesn't exist.
if ($postcount < 1) {
print "\nPost '$question' not found.\n";
exit(-1);
}
# If this is a reply, bring up the parent instead.
if ($post[0][3]) { print "Redirecting from reply #$question to parent question #$post[0][3].\n";
showquestion($post[0][3]);
exit(0);
}
# Pretty formatting begins.
print "="x100 . "\n";
# Make the correct URL
my $url = "";
if ($dbname eq "stackoverflow" or $dbname eq "superuser" or $dbname eq "serverfault") {
$url = "https://$dbname.com/questions/$question";
} else {
$url = "https://$dbname.stackexchange.com/questions/$question";
}
print "URL\t| $url\n";
print "-"x100 . "\n";
my $f = HTML::FormatText::WithLinks->new(
before_link => "",
after_link => " [%n]"
);
# Fix unicode and filter out garbage.
my $qtext = Encode::decode("utf8", $post[0][1]);
$qtext =~ tr/\x00-\x7f//cd;
# Parse the text
my $setext = $f->parse($qtext);
# Since you can pull a reply, but not a comment, by number, we need to be
# ready and handle this edge case. Also, replies don't have title text, so
if ( not $post[0][3]) {
print "Title\t| " . wrap("","\t| ", subhtml($post[0][0])) . "\n";
}
print "-"x100 . "\n";
print "Body\t| " . wrap("","\t| ", $setext);
print "-"x100 . "\n";
$Text::Wrap::columns = 83;
# Comments are broken out, if they exist.
for (my $counter = 0;$counter <= $#post;$counter++) {
my $comment = $post[$counter];
if (@$comment[2]) {
# Label the block
if ($counter == 0) {
print "Comment(s)\t| " . wrap("","\t\t| ", subhtml(@$comment[2])) . "\n";
} else {
print "\t\t| " . wrap("","\t\t| ", subhtml(@$comment[2])) . "\n";
}
print "\t\t|". "-"x83 . "\n";
}
}
# Now for the replies and any existing comments.
$stmt = $dbh->prepare("SELECT posts.Body, comments.Text FROM posts LEFT JOIN comments ON posts.Id = comments.PostId WHERE posts.parentId = '$question' AND posts.PostTypeID = 2 ORDER BY posts.Score DESC;");
$stmt->execute() or die $DBI::errstr;
my @reply = flat $stmt->fetchall_arrayref();
my $replycount = $stmt->rows();
my $oldreply = "";
foreach my $comment (@reply) {
# for (my $counter = 0;$counter <= $#post;$counter++) {
# my $comment = $post[$counter];
# Reply, and not comment
if (@$comment[0] ne $oldreply) {
# Fix unicode and filter out garbage.
my $qtext = Encode::decode("utf8", @$comment[0]);
$qtext =~ tr/\x00-\x7f//cd;
# Parse the text
my $setext = $f->parse($qtext);
$Text::Wrap::columns = 91;
print "-"x100 . "\n";
print "Reply\t| " . wrap("","\t| ", $setext);
print "-"x100 . "\n";
if (@$comment[1]) {
$Text::Wrap::columns = 83;
print "Comment(s)\t| " . wrap("","\t\t| ", subhtml(@$comment[1])) . "\n";
print "\t\t|". "-"x83 . "\n";
}
$oldreply = @$comment[0];
} else {
# The rest of the comments
$Text::Wrap::columns = 83;
print "\t\t| " . wrap("","\t\t| ", subhtml(@$comment[1])) . "\n";
print "\t\t|". "-"x83 . "\n";
}
}
print "="x100 . "\n";
}
#======================================
# Main Execution Block
#======================================
if ($listflag) {
opendir my($dh), $dbpath or die "Couldn't open dir '$dbpath': $!";
my @files = readdir $dh;
if ($#files == 0) {
print "No databases available in '$dbpath'. Please configure this script and try again.\n";
exit(-1);
}
print "Available databases\n";
print "-------------------\n";
for (my $counter = 0;$counter <= $#files;$counter++) {
$files[$counter] =~ s/.db//g;
if ($files[$counter] ne ".." and $files[$counter] ne ".") {
print " $files[$counter]\n";
}
}
print "-------------------\n";
print "Default database is:\n";
print " $dbname\n";
exit(0);
}
if ($titlesearch) {
searchtitles($titlesearch);
$dbh->disconnect();
exit(0);
}
if ($bodysearch) {
searchbodies($bodysearch);
exit(0);
}
if ($questionsearch) {
showquestion($questionsearch);
$dbh->disconnect();
exit(0);
}
# If we somehow fall through here...
printUsage();