Repository navigation
Expand file tree
/
Copy pathNARDatabase.pm
More file actions
806 lines (595 loc) · 17.2 KB
/
Copy pathNARDatabase.pm
File metadata and controls
806 lines (595 loc) · 17.2 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package NARDatabase;
use strict;
use MediaWiki::API;
use HTML::WikiConverter;
use Data::Dumper;
warn "PARSE AUTHORS / REFERENCES!!!\n";
warn "\n\n";
## Set up the package
require Exporter;
our @ISA = qw( Exporter ); # Inherit from Exporter
our @EXPORT = qw( ); # Default symbols to export.
our @EXPORT_OK = # Symbols on request.
(
## Functions
"parseNarDatabaseHtml",
"tidyTitleForMediaWiki",
"tidyHtmlForMediaWiki",
"getNarDatabasePageText",
"printNarDatabasePageData",
#
"makeTheFucker",
"mwLogin",
"getPage",
"getPageData",
"getPageStatus",
"getPageRevisions",
"printPageRevisions",
"updatePage",
"checkPageForAutomaticUpdate",
"movePage",
"createRedirect"
## Variables
);
my $wc =
new HTML::WikiConverter( dialect => 'MediaWiki' );
# Parse the HTML text of a 'NAR Database page'. The function expects a
# file handle and optionally a 'verbose flag' for debugging. It
# returns an array reference with various data elements taken from the
# page. It does as much error checking as I could stomach. Note that
# some pages fail the error checks!
sub parseNarDatabaseHtml {
my $fh = shift;
my $verbose = shift || 0;
## Watch out for logic!
my ($bodyFlag, # Are we in the body of the page?
#
$titleFlag, # Do we have the database title?
$dbTitle, # The database title.
$dbTitleCode, # The database title 'code' (if any)
#
$noFlag, # Do we have the database No.?
$accnNo, # The database No.
#
$urlFlag, # Do we have the database URL(s)?
@url, # The database URL(s).
#
$authorFlag, # Do we have the authors?
$author, # The database authors (one line).
#
$authorAdrFlag, # Do we have the author address?
$authorAdr, # The author address (one line).
#
$contactFlag, # Do we have the database contact(s)?
@contact, # The database contact(s).
#
$H3Flag, # Are we in a H3 section?
$H3, # The H3 section we are in.
%H3, # The H3 data.
#
%cat, # The database category number / id.
%subCat, # The database sub-category number / id.
#
$abstractFlag, # Does the database have an abstract?
$abstract, # Abstract URL.
$abstractYear # Abstract year.
);
# Used for sanity checking (in theory)
my $divCounter = 0;
while(<$fh>){
chomp;
next if /^$/;
# Look for the body of the page
if(!$bodyFlag){
if (/^\<!-- start body --\>$/){
$bodyFlag = 1;
warn "body starting at $.\n"
if $verbose > 1;
}
else{
warn "IGNORING (1): -$_-\n"
if $verbose > 2;
}
next; # We won't continue until we find this!
}
## Now we are in the body of the page
# Keep track of div's (for debugging, in theory)
$divCounter++
if /\<div /;
# Look for the database title
if (!$titleFlag){
if (/^\<h1 class=\"summary\"\>(.*)\<\/h1\>$/){
$titleFlag = 1;
($dbTitle, $dbTitleCode) = parseTitle($1);
warn join("\t", "Title", $., $divCounter, $dbTitle), "\n"
if $verbose > 1;
}
else{
warn "IGNORING (2): -$_-\n"
if $verbose > 2;
}
next; # We won't continue until we find this element!
}
# Look for the database number
if (!$noFlag){
if (/^NAR Molecular Biology Database Collection entry number (\d+)$/){
$noFlag = 1;
$accnNo = $1;
warn join("\t", "No.", $., $divCounter, $accnNo), "\n"
if $verbose > 1;
}
else{
warn "IGNORING (3): -$_-\n"
if $verbose > 2;
}
next; # We won't continue until we find this element!
}
# Look for the database URL(s) line
if (!$urlFlag){
# Note: there can be more than one URL (separated by ' or ')!
if (/^(?:\<a href=\".*?\"\>.*?\<\/a\>(?: or )?)+$/){
$urlFlag = 1;
for (split(/ or /, $_)){
if (/\<a href=\"(.*?)\"\>(.*?)\<\/a\>/){
die "WHAT1? : \"$_\"\n"
if $1 ne $2;
push @url, $1;
warn join("\t", "URL", $., $divCounter, $1), "\n"
if $verbose > 1;
}
else{
die "WHAT2? : \"$_\"\n";
}
}
}
else{
warn "IGNORING (4): -$_-\n"
if $verbose > 2;
}
next; # We won't continue until we find this element!
}
# Look for the database author(s) line (optional)
if (!$authorFlag){
if (/^ \<strong\>(.*)\<\/strong\>$/){
$authorFlag = 1;
$author = $1;
warn join("\t", "Auth", $., $divCounter, $author), "\n"
if $verbose > 1;
next; # Done!
}
else{
# We should set the $authorFlag later when we are sure that it
# is missing so that we don't keep checking for it here.
warn "IGNORING (5): -$_-\n"
if $verbose > 2;
}
}
# Grab author address(es) line (one line)
if ($authorFlag && !$authorAdrFlag){
# It should be the next line after the author that isn't one of
# these four...
if (!/^ \<h3 / &&
!/^ \<div / &&
!/^ \<span / &&
!/^ \<\/div\>$/ &&
/^ (.+)$/){
$authorAdrFlag = 1;
$authorAdr = $1;
warn join("\t", "Address", $., $divCounter, $authorAdr), "\n"
if $verbose > 1;
}
else{
warn "IGNORING (6): -$_-\n"
if $verbose > 2;
}
next; # Given we have an author, we won't continue until we find
# this element!
}
# Look for contact email address(es) line (optional)
if (!$contactFlag){
# Note: there can be more than one contact (separated by ' or ')!
if (/^ \<span class=\"subhead\"\>Contact\<\/span\> (?:\<a href=\"MAILTO:.*?\"\>.*?\<\/a\>(?: or )?)+$/){
$contactFlag = 1;
for (split(/ or /, $_)){
if (/\<a href=\"MAILTO:(.*?)\"\>(.*?)\<\/a\>/){
die "WHAT3? : \"$_\"\n"
if $1 ne $2;
push @contact, $1;
warn join("\t", "Cont", $., $divCounter, $1), "\n"
if $verbose > 1;
}
else{
die "WHAT4? : \"$_\"\n";
}
}
next; # Done
}
else{
# We should set the $contactFlag later when we are sure that
# it is missing.
warn "IGNORING (7): -$_-\n"
if $verbose > 2;
}
}
# Grab the (standard?) 'H3' sections.
if(/^ \<h3 class=\"summary\"\>(.*)\<\/h3\>$/){
$H3Flag = 1;
$H3 = $1;
warn join("\t", "H3", $., $divCounter, $H3 ), "\n"
if $verbose > 1;
next; # Flag set... We could apply some logic here (and above).
}
elsif ($H3Flag){
## Leave the section
if (/^ \<\/div\>$/){
$H3Flag = 0;
}
# Or grab data (its a single line).
elsif (!/ \<div / && /^ (.*)$/){
$H3{$H3} = $1;
}
else{
warn "IGNORING (8): -$_-\n"
if $verbose > 2;
}
next; # Given we have a H3, we won't continue until we find this
# element!
}
# Grab category data
# My error checking (and logic) ran out of steam...
if(/^ Category\: \<a href\=\"\/nar\/database\/cat\/(\d+)\"\>(.*)\<\/a\>$/){
my $catNo = $1;
my $catId = $2;
warn join("\t", "Cat", $., $divCounter, $catNo, $catId ), "\n"
if $verbose > 1;
#die "double cat!\n" if defined($cat{$catNo});
$cat{$catNo} = $catId;
next; # Done
}
if(/^ Subcategory\: \<a href\=\"\/nar\/database\/subcat\/(\d+)\/(\d+)\"\>(.*)\<\/a\>$/){
my $catNo = $1;
my $subCatNo = $2;
my $subCatId = $3;
warn join("\t", "SubCat", $., $divCounter, $subCatNo, $subCatId ), "\n"
if $verbose > 1;
die "sucks!\n" unless defined($cat{$catNo});
die "double sub-cat!\n" if defined($subCat{$subCatNo});
$subCat{$catNo}{$subCatNo} = $subCatId;
next; # Done
}
## Grab abstract link (optional)
if (!$abstractFlag){
if (/^\<div class\=\"bodytext\">Go to the \<a href\=\"(.*)\"\>abstract\<\/a\> in the NAR (\d\d\d\d) Database Issue\.$/){
$abstractFlag = 1;
$abstract = $1;
$abstractYear = $2;
warn join("\t", "Abst", $., $divCounter, $abstractYear, $abstract), "\n"
if $verbose > 1;
next; # Done
}
}
# Are we done here?
if(/\<!-- end body --\>/){
last;
}
}
## Basic sanity check. Not that we really care about sections, but
## tests for 'failed' parsing.
unless (keys %H3){
die "$accnNo : no sections?\n"
}
return [$dbTitle, # The database Title.
$dbTitleCode, # The database title code (if any).
$accnNo, # The database Accession No.
\@url, # The database URL(s).
$author, # The database author(s) (one line).
$authorAdr, # The author address(es) (one line).
\@contact, # The database contact email(s).
\%H3, # The H3 data.
#
\%cat, # The category data.
\%subCat, # The sub-category data.
#
$abstract, # Abstract URL.
$abstractYear # Abstract year.
];
}
## Look for a 'database code' in the database title.
sub parseTitle {
my $title = shift;
if(0){}
elsif($title =~ /^(\S+) - (.*)$/ ||
$title =~ /^(\S+): (.*)$/ ){
return($2, $1);
}
elsif($title =~ /^(.*) \((\S+)\)$/ ){
return($1, $2);
}
else{
# The title does not appear to contain a database code.
return($title)
}
}
## Remove some common HTML annoyances
sub tidyHtmlForMediaWiki {
my $text = shift;
$text =~ s/<br \/>/\n\n/;
# Somhow this look ugly to me
#$text =~ s/®/®/g;
#$text =~ s/®/®/g;
# So...
$text =~ s/®//g;
$text =~ s/®//g;
$text =~ s/>/>/g;
$text =~ s/</</g;
$text =~ s/&/&/g;
$text =~ s/'/'/g;
$text =~ s/é/é/;
die "Found an un-handled HTML character code?\n$text\n"
if $text =~ m/&\S+;/;
return $text;
}
## Tidy up the database title for mediawiki
sub tidyTitleForMediaWiki {
my $text = shift;
# Remove HTML that we can't wikify in the title...
$text =~ s/<sup>(.+)<\/sup>/$1/;
$text =~ s/<\/?[iI]>//g;
# Somhow this look ugly to me
#$text =~ s/®/®/g;
#$text =~ s/®/®/g;
# So...
$text =~ s/®//g;
$text =~ s/®//g;
# But...
$text =~ s/é/é/g;
$text =~ s/é/é/g;
# And...
$text =~ s/à/à/g;
die "WTF?\n$text\n"
if
$text =~ m/&\S+;/ ||
$text =~ m/<|>/;
return $text;
}
### MediaWiki Subroutines
sub mwLogin {
my $mw = MediaWiki::API->new();
$mw->{config}->{api_url} = 'http://metadatabase.org/api.php';
# log in
$mw->login( { lgname => '<user>',
lgpassword => '<pass>' } )
|| die $mw->{error}->{code} . ': ' . $mw->{error}->{details};
## Make the above error code a bit more convenient...
$mw->{config}->{on_error} = sub {
print "Error code: ". $mw->{error}->{code}. "\n";
print "Details: ". $mw->{error}->{details}. "\n\n";
print $mw->{error}->{stacktrace}."\n";
die;
};
warn "logged in\n";
return $mw;
}
sub getPage {
my $title = shift;
my $mwApi = shift;
warn "getting '$title'\n";
return $mwApi->get_page({ title => $title });
}
## Query the MediaWiki page meta data and set a page status code
sub getPageData {
my $title = shift;
my $mwApi = shift;
my $result;
my $pageInfo =
$mwApi->api({ action => 'query',
titles => $title,
prop => 'info',
});
#print Dumper($pageInfo), "\n\n";
## We 'parse' the returned data structure using some Perl-ology
$pageInfo =
(values %{$pageInfo->{query}->{pages}})[0];
#print Dumper($pageInfo), "\n\n";
## Now we explicitly set a status code using several status
## flags. This ad-hock step simplifies lots of subsequent code.
if ( exists($pageInfo->{missing}) ){
$pageInfo->{status} = 'missing'
}
elsif ( exists($pageInfo->{redirect}) ){
$pageInfo->{status} = 'redirect'
}
elsif ( exists($pageInfo->{invalid}) ){
$pageInfo->{status} = 'invalid'
}
else{
$pageInfo->{status} = 'exists'
}
return $pageInfo
}
# The purpose of this function is to check if a given Page exists, is
# a redirect, or is invalid (the page title is not a valid MediaWiki
# page title).
#
# Returns: a 'status code' which is 'exists', 'redirect', 'invalid' or
# 'missing'.
sub getPageStatus {
my $title = shift;
my $mwApi = shift;
my $result;
## The real work is done here
my $pageInfo =
getPageData( $title, $mwApi );
return $pageInfo->{status};
}
## Print the revision history of a given page
sub printPageRevisions {
my $title = shift;
my $mwApi = shift;
my $revisions =
getPageRevisions($title, $mwApi);
## Print the revisions
print "revisions for $title\n";
for(my $i=0; $i<@$revisions; $i++){
print
"\t", $revisions->[$i]->{timestamp},
"\t", $revisions->[$i]->{user},
"\t", $revisions->[$i]->{comment} || 'no comment',
"\n";
}
}
sub getPageRevisions {
my $title = shift;
my $mwApi = shift;
my $pageStatus = getPageStatus($title, $mwApi);
## If the page has any status other than 'exists', we quit
if( $pageStatus ne 'exists' ){
warn " error : page '$title' is *$pageStatus*!\n\n";
return [];
}
my $revisions =
$mwApi->api({ action => 'query',
prop => 'revisions',
titles => $title,
rvlimit => 'max',
});
## Debugging
#print Dumper $revisions, "\n";
## We 'parse' the returned data structure using some Perl-ology
$revisions =
(values %{$revisions->{query}->{pages}})[0]->{revisions};
return $revisions;
}
## Sets to contents of the given page to the given text.
sub updatePage {
my $title = shift;
my $mwApi = shift;
my $text = shift;
my $summary = shift;
warn "updating '$title' with new text\n";
my $page = $mwApi->get_page( { title => $title } );
# to avoid edit conflicts
my $timestamp = $page->{timestamp};
$mwApi->edit({ action => 'edit',
title => $title,
basetimestamp => $timestamp,
text => $text,
bot => 'true',
summary => $summary || 'Set "summary"'
});
}
sub movePage {
my $mw = shift;
my $title = shift;
my $newTitle = shift;
my $force = shift || 0;
my $summary = shift;
my $titleStatus = getPageStatus($title, $mw);
my $newTitleStatus = getPageStatus($newTitle, $mw);
if ($titleStatus ne 'exists'){
warn "$title does not exist\n";
return 0;
}
if ($newTitleStatus ne 'missing'){
warn "$newTitle $newTitleStatus\n";
return 0
unless $force;
}
# move a page
$mw->edit( {
action => 'move',
from => $title,
to => $newTitle,
bot => 'true',
reason => $summary || 'Set "summary"'
} );
return 1;
}
sub createRedirect {
my $mwApi = shift;
my $fromTitle = shift;
my $toTitle = shift;
my $force = shift || 0;
my $summary = shift;
my $page =
$mwApi->get_page( { title => $fromTitle } );
# to avoid edit conflicts
my $timestamp = $page->{timestamp};
$mwApi->edit({ action => 'edit',
title => $fromTitle,
basetimestamp => $timestamp,
text => "#REDIRECT [[$toTitle]]",
bot => 'true',
summary => $summary || 'Set "summary"'
});
return 1;
}
# Using the data produced by "parseNarDatabaseHtml", we format the
# text for the 'NAR Database' component of the wiki. The function
# expects the output of "parseNarDatabaseHtml" and returns a string.
sub makeTheFucker {
my $data = shift;
my $text;
## Unwrap the data
my ($title, # The database title.
$titleCode, # The database title code (if any).
$accn_no, # The database accession No.
$url, # The database URL(s).
$author, # The database author(s) (one line).
$authorAdr, # The author address(es) (one line).
$contact, # The database contact email(s).
$H3, # The H3 data.
#
$cat, # The database category data.
$subCat, # The database sub-category data.
#
$abstract, # Abstract URL.
$abstractYear # Abstract year
) = @$data;
## Start building the template
$text = "{{Database entry\n";
$text .= "|name=$title\n";
$text .= "|code=". ($titleCode || ''). "\n";
$text .= "|nar id=$accn_no\n";
##
my %sections =
("Database Description" => "10 nar description",
"Recent Developments" => "20 nar developments",
"Acknowledgements" => "30 nar acknowledgements",
"References" => "40 nar references",
);
for (keys %{$H3}){
next if $sections{$_};
die "unknown section -$_-\n"
}
for my $section (sort {$sections{$a} cmp
$sections{$b} } keys %sections){
my $paramName = substr($sections{$section}, 3);
$text .= "|$paramName=";
if (defined($H3->{$section})){
my $t = $wc->html2wiki( $H3->{$section} );
## Sad but true...
$t = tidyHtmlForMediaWiki( $t );
$text .= "$t"
}
$text .= "\n";
}
$text .= "|nar authors=". ($author || ''). "\n";
$text .= "|nar author addresses=". ($authorAdr || ''). "\n";
$text .= "|homepage urls=". join(", ", @$url). "\n";
$text .= "|nar abstract urls=". ($abstract || ''). "\n";
$text .= "|nar abstract years=". ($abstractYear || ''). "\n";
$text .= "|contact emails=". join(", ", @$contact). "\n";
$text .= "|nar categories=".
join(", ", values(%$cat),
(map {values(%$_)} values(%$subCat))
). "\n";
$text .= "|metabase version=2.1.1\n";
$text .= "}}\n";
$text .= <<EO;
<!-- Add your text below -->
<!-- Add your text above -->
{{Page links}}
EO
}
1;