-
Notifications
You must be signed in to change notification settings - Fork 1
/
DOItools.php
2275 lines (2163 loc) · 91.1 KB
/
DOItools.php
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?
$bot = new Snoopy();
define("wikiroot", "http://en.wikipedia.org/w/index.php?");
define("api", "http://en.wikipedia.org/w/api.php");
if ($linkto2) echo "\n// included DOItools2 & initialised \$bot\n";
define("doiRegexp", "(10\.\d{4}(/|%2F)..([^\s\|\"\?&>]|&l?g?t;|<[^\s\|\"\?&]*>))(?=[\s\|\"\?]|</)"); //Note: if a DOI is superceded by a </span>, it will pick up this tag. Workaround: Replace </ with \s</ in string to search.
define("timelimit", $fastMode?4:($slow_mode?15:10));
define("early", 8000);//Characters into the literated text of an article in which a DOI is considered "early".
define("siciRegExp", "~(\d{4}-\d{4})\((\d{4})(\d\d)?(\d\d)?\)(\d+):?([+\d]*)[<\[](\d+)::?\w+[>\]]2\.0\.CO;2~");
function list_parameters () { // Lists the parameters in order.
return Array(
"null",
"author", "author1", "last", "last1", "first", "first1", "authorlink", "authorlink1", "author1-link",
"coauthors", "author2", "last2", "first2", "authorlink2", "author2-link",
"author3", "last3", "first3", "authorlink3", "author3-link",
"author4", "last4", "first4", "authorlink4", "author4-link",
"author5", "last5", "first5", "authorlink5", "author5-link",
"author6", "last6", "first6", "authorlink6", "author6-link",
"author7", "last7", "first7", "authorlink7", "author7-link",
"author8", "last8", "first8", "authorlink8", "author8-link",
"author9", "last9", "first9", "authorlink9", "author9-link",
"author10", "last10", "first10", "authorlink10", "author10-link",
"author11", "last11", "first11", "authorlink11", "author11-link",
"author12", "last12", "first12", "authorlink12", "author12-link",
"author13", "last13", "first13", "authorlink13", "author13-link",
"author14", "last14", "first14", "authorlink14", "author14-link",
"author15", "last15", "first15", "authorlink15", "author15-link",
"author16", "last16", "first16", "authorlink16", "author16-link",
"author17", "last17", "first17", "authorlink17", "author17-link",
"author18", "last18", "first18", "authorlink18", "author18-link",
"author19", "last19", "first19", "authorlink19", "author19-link",
"author20", "last20", "first20", "authorlink20", "author20-link",
"author21", "last21", "first21", "authorlink21", "author21-link",
"author22", "last22", "first22", "authorlink22", "author22-link",
"author23", "last23", "first23", "authorlink23", "author23-link",
"author24", "last24", "first24", "authorlink24", "author24-link",
"author25", "last25", "first25", "authorlink25", "author25-link",
"author26", "last26", "first26", "authorlink26", "author26-link",
"author27", "last27", "first27", "authorlink27", "author27-link",
"author28", "last28", "first28", "authorlink28", "author28-link",
"author29", "last29", "first29", "authorlink29", "author29-link",
"author30", "last30", "first30", "authorlink30", "author30-link",
"author31", "last31", "first31", "authorlink31", "author31-link",
"author32", "last32", "first32", "authorlink32", "author32-link",
"author33", "last33", "first33", "authorlink33", "author33-link",
"author34", "last34", "first34", "authorlink34", "author34-link",
"author35", "last35", "first35", "authorlink35", "author35-link",
"author36", "last36", "first36", "authorlink36", "author36-link",
"author37", "last37", "first37", "authorlink37", "author37-link",
"author38", "last38", "first38", "authorlink38", "author38-link",
"author39", "last39", "first39", "authorlink39", "author39-link",
"author40", "last40", "first40", "authorlink40", "author40-link",
"author41", "last41", "first41", "authorlink41", "author41-link",
"author42", "last42", "first42", "authorlink42", "author42-link",
"author43", "last43", "first43", "authorlink43", "author43-link",
"author44", "last44", "first44", "authorlink44", "author44-link",
"author45", "last45", "first45", "authorlink45", "author45-link",
"author46", "last46", "first46", "authorlink46", "author46-link",
"author47", "last47", "first47", "authorlink47", "author47-link",
"author48", "last48", "first48", "authorlink48", "author48-link",
"author49", "last49", "first49", "authorlink49", "author49-link",
"author50", "last50", "first50", "authorlink50", "author50-link",
"author51", "last51", "first51", "authorlink51", "author51-link",
"author52", "last52", "first52", "authorlink52", "author52-link",
"author53", "last53", "first53", "authorlink53", "author53-link",
"author54", "last54", "first54", "authorlink54", "author54-link",
"author55", "last55", "first55", "authorlink55", "author55-link",
"author56", "last56", "first56", "authorlink56", "author56-link",
"author57", "last57", "first57", "authorlink57", "author57-link",
"author58", "last58", "first58", "authorlink58", "author58-link",
"author59", "last59", "first59", "authorlink59", "author59-link",
"author60", "last60", "first60", "authorlink60", "author60-link",
"author61", "last61", "first61", "authorlink61", "author61-link",
"author62", "last62", "first62", "authorlink62", "author62-link",
"author63", "last63", "first63", "authorlink63", "author63-link",
"author64", "last64", "first64", "authorlink64", "author64-link",
"author65", "last65", "first65", "authorlink65", "author65-link",
"author66", "last66", "first66", "authorlink66", "author66-link",
"author67", "last67", "first67", "authorlink67", "author67-link",
"author68", "last68", "first68", "authorlink68", "author68-link",
"author69", "last69", "first69", "authorlink69", "author69-link",
"author70", "last70", "first70", "authorlink70", "author70-link",
"author71", "last71", "first71", "authorlink71", "author71-link",
"author72", "last72", "first72", "authorlink72", "author72-link",
"author73", "last73", "first73", "authorlink73", "author73-link",
"author74", "last74", "first74", "authorlink74", "author74-link",
"author75", "last75", "first75", "authorlink75", "author75-link",
"author76", "last76", "first76", "authorlink76", "author76-link",
"author77", "last77", "first77", "authorlink77", "author77-link",
"author78", "last78", "first78", "authorlink78", "author78-link",
"author79", "last79", "first79", "authorlink79", "author79-link",
"author80", "last80", "first80", "authorlink80", "author80-link",
"author81", "last81", "first81", "authorlink81", "author81-link",
"author82", "last82", "first82", "authorlink82", "author82-link",
"author83", "last83", "first83", "authorlink83", "author83-link",
"author84", "last84", "first84", "authorlink84", "author84-link",
"author85", "last85", "first85", "authorlink85", "author85-link",
"author86", "last86", "first86", "authorlink86", "author86-link",
"author87", "last87", "first87", "authorlink87", "author87-link",
"author88", "last88", "first88", "authorlink88", "author88-link",
"author89", "last89", "first89", "authorlink89", "author89-link",
"author90", "last90", "first90", "authorlink90", "author90-link",
"author91", "last91", "first91", "authorlink91", "author91-link",
"author92", "last92", "first92", "authorlink92", "author92-link",
"author93", "last93", "first93", "authorlink93", "author93-link",
"author94", "last94", "first94", "authorlink94", "author94-link",
"author95", "last95", "first95", "authorlink95", "author95-link",
"author96", "last96", "first96", "authorlink96", "author96-link",
"author97", "last97", "first97", "authorlink97", "author97-link",
"author98", "last98", "first98", "authorlink98", "author98-link",
"author99", "last99", "first99", "authorlink99", "author99-link",
"editor", "editor1",
"editor-last", "editor1-last",
"editor-first", "editor1-first",
"editor-link", "editor1-link",
"editor2", "editor2-author", "editor2-first", "editor2-link",
"editor3", "editor3-author", "editor3-first", "editor3-link",
"editor4", "editor4-author", "editor4-first", "editor4-link",
"others",
"chapter", "trans_chapter", "chapterurl",
"title", "trans_title", "language",
"url",
"archiveurl",
"archivedate",
"format",
"accessdate",
"edition",
"series",
"journal",
"volume",
"issue",
"page",
"pages",
"nopp",
"publisher",
"location",
"date",
"origyear",
"year",
"month",
"location",
"language",
"isbn",
"issn",
"oclc",
"pmid", "pmc",
"doi",
"doi_brokendate", "doi_inactivedate",
"bibcode",
"id",
"quote",
"ref",
"laysummary",
"laydate",
"separator",
"postscript",
"authorauthoramp",
);
}
global $dontCap, $unCapped;
// Remember to enclose any word in spaces.
// $dontCap is a global array of strings that should not be capitalized in their titlecase format; $unCapped is their correct capitalization
$dontCap = array(' and Then ', ' Of ',' The ',' And ',' An ',' Or ',' Nor ',' But ',' Is ',' If ',' Then ',' Else ',' When', 'At ',' From ',' By ',' On ',' Off ',' For ',' In ',' Over ',' To ',' Into ',' With ',' U S A ',' Usa ',' Et ');
$unCapped = array(' and then ', ' of ',' the ',' and ',' an ',' or ',' nor ',' but ',' is ',' if ',' then ',' else ',' when', 'at ',' from ',' by ',' on ',' off ',' for ',' in ',' over ',' to ',' into ',' with ',' U S A ',' USA ',' et ');
// journal acrynyms which should be capitilised are downloaded from User:Citation_bot/capitalisation_exclusions
$bot->fetch(wikiroot . "title=" . urlencode('User:Citation_bot/capitalisation_exclusions') . "&action=raw");
if (preg_match_all('~\n\*\s*(.+)~', $bot->results, $dontCaps)) {
foreach ($dontCaps[1] as $o) {
$unCapped[] = ' ' . trim($o) . ' ';
// dontCap is a global array of strings that should not be capitalized in their titlecase format; $unCapped is their correct capitalization
$dontCap[] = ' '
. trim(
(strlen(str_replace(array("[", "]"), "", trim($o))) > 6)
? mb_convert_case($o, MB_CASE_TITLE, "UTF-8")
:$o
)
. ' ';
}
}
/** Returns revision number */
function revisionID() {
global $last_revision_id;
if ($last_revision_id) return $last_revision_id;
$svnid = '$Rev: 432 $';
$scid = substr($svnid, 6);
$thisRevId = intval(substr($scid, 0, strlen($scid) - 2));
return expandFnsRevId() > $thisRefId ? expandFnsRevId() : $thisRevId;
$repos_handle = svn_repos_open('~/citation-bot');
return svn_fs_youngest_rev($repos_handle);
}
function bubble_p ($a, $b) {
return ($a["weight"] > $b["weight"]) ? 1 : -1;
}
function is($key){
global $p;
return ("" != trim($p[$key][0])) ? true : false;
}
function dbg($array, $key = false) {
if(myIP())
echo "<pre>" . str_replace("<", "<", $key ? print_r(array($key=>$array),1) : print_r($array,1)), "</pre>";
else echo "<p>Debug mode active</p>";
}
function myIP() {
switch ($_SERVER["REMOTE_ADDR"]){
case "1":
case "":
case "86.6.164.132":
case "99.232.120.132":
case "192.75.204.31":
return true;
default: return false;
}
}
/*underTwoAuthors
* Return true if 0 or 1 author in $author; false otherwise
*/
function underTwoAuthors($author) {
$author = str_replace(array (" '", "et al"), "", $author);
$chars = count_chars(trim($author));
if ($chars[ord(";")] > 0 || $chars[ord(" ")] > 2 || $chars[ord(",")] > 1) {
return false;
}
return true;
}
/* jrTest - tests a name for a Junior appelation
* Input: $name - the name to be tested
* Output: array ($name without Jr, if $name ends in Jr, Jr)
*/
function jrTest($name) {
$junior = (substr($name, -3) == " Jr")?" Jr":false;
if ($junior) {
$name = substr($name, 0, -3);
} else {
$junior = (substr($name, -4) == " Jr.")?" Jr.":false;
if ($junior) {
$name = substr($name, 0, -4);
}
}
if (substr($name, -1) == ",") {
$name = substr($name, 0, -1);
}
return array($name, $junior);
}
function nothingMissing($journal){
global $authors_missing;
if (!(is("pages") || is("page"))
|| (preg_match('~no.+no|n/a|in press|none~', $p['pages'][0] . $p['page'][0]))) {
return false;
}
return ( is($journal)
&& is("volume")
&& is("issue")
&& $noPagesMissing
&& is("title")
&& (is("date") || is("year"))
&& (is("author2") || is("author2"))
&& (!$authors_missing && (is("author") || is("author1")))
);
}
function get_data_from_pubmed($identifier = "pmid") {
global $p;
echo "\n - Checking " . strtoupper($identifier) . ' ' . $p[$identifier][0] . ' for more details [DOItools.php/get_data_from_pubmed]';
$details = pmArticleDetails($p[$identifier][0], $identifier);
foreach ($details as $key => $value) {
if (if_null_set($key, $value) && $key == 'pmid') {
// PMC search is limited but will at least return a PMID.
get_data_from_pubmed('pmid');
} else if ($identifier == 'pmc' && $key == 'title') {
// this will only be called with $identifier=pmc if a PMC id has just been discovered in a fragmentary citation.
if_null_set ('title', $value); // According to a previous comment, it may sometimes be necessary to forcedly set the title in cite webs. I've not implemented this; I'll wait to see whether it causes problems.
}
}
if (false && !is("url")) { // TODO: BUGGY - CHECK PMID DATABASES, and see other occurrence above
if (!is('pmc')) {
$url = pmFullTextUrl($p["pmid"][0]);
} else {
unset ($p['url']);
}
if ($url) {
set ("url", $url);
}
}
}
function expand_from_crossref ($crossRef, $editing_cite_doi_template, $silence = false) {
if ($silence) {
ob_start();
}
global $p, $doiCrossRef, $jstor_redirect, $priorP;
if (substr($p["doi"][0], 3, 4) == "2307") {
echo "\n - Populating from JSTOR database: ";
if (get_data_from_jstor($p["doi"][0])) {
$crossRef = crossRefData($p["doi"][0]);
if (!$crossRef) {
// JSTOR's UID is not registered as a DOI, meaning that there is another (correct - DOIs should be unique) DOI, issued by the publisher.
if ($editing_cite_doi_template) {
$jstor_redirect = $p["doi"][0];
}
unset ($p["doi"][0]);
$crossRef = crossRefDoi($p["title"][0], $p["journal"][0], is("author1")?$p["author1"][0]:$p["author"][0]
, $p["year"][0], $p["volume"][0], get_first_page($p), get_last_page($p), $p["issn"][0], null);
}
} else {
echo "not found in JSTOR?";
}
} else {
// Not a JSTOR doi, use CrossRef
$crossRef = $crossRef?$crossRef:crossRefData(urlencode(trim($p["doi"][0])));
}
if ($crossRef) {
echo "\n - Checking CrossRef for more details [DOItools.php/expand_from_crossref]";
if ($editing_cite_doi_template) {
$doiCrossRef = $crossRef;
}
if ($crossRef->volume_title && !is('journal')) {
if_null_set("chapter", $crossRef->article_title);
if (strtolower($p["title"][0]) == strtolower($crossRef->article_title)) {
unset($p['title'][0]);
}
if_null_set('title', $crossRef->volume_title);
} else {
if_null_set('title', $crossRef->article_title);
}
if_null_set('series', $crossRef->series_title);
if_null_set("year", $crossRef->year);
if (!is("editor") && !is("editor1") && !is("editor-last") && !is("editor1-last")
&& $crossRef->contributors->contributor) {
foreach ($crossRef->contributors->contributor as $author) {
if ($author["contributor_role"] == "editor") {
++$ed_i;
if ($ed_i < 5) {
if_null_set("editor$ed_i-last", formatSurname($author->surname));
if_null_set("editor$ed_i-first", formatForename($author->given_name));
}
} else {
++$au_i;
if_null_set("last$au_i", formatSurname($author->surname));
if_null_set("first$au_i", formatForename($author->given_name));
}
}
}
if_null_set("doi", $crossRef->doi);
if_null_set("isbn", $crossRef->isbn);
if ($jstor_redirect) {
global $jstor_redirect_target;
$jstor_redirect_target = $crossRef->doi;
}
if_null_set("journal", $crossRef->journal_title);
if ($crossRef->volume > 0) {
if_null_set("volume", $crossRef->volume);
}
if ((integer) $crossRef->issue > 1) {
// "1" may refer to a journal without issue numbers,
// e.g. 10.1146/annurev.fl.23.010191.001111, as well as a genuine issue 1. Best ignore.
if_null_set("issue", $crossRef->issue);
}
if (!is("page")) if_null_set("pages", $crossRef->first_page
. ($crossRef->last_page && ($crossRef->first_page != $crossRef->last_page)
? "-" . $crossRef->last_page //replaced by an endash later in script
: "") );
echo " (ok)";
searchForPmid();
} else {
echo "\n - No CrossRef record found :-(";
}
if ($silence) {
ob_end_clean();
}
return $crossRef;
}
// text must be text that contains a SICI. It could be an entire citation.
function get_data_from_sici($text) {
if (preg_match(siciRegExp, urldecode($text), $sici)) {
if (!is($journal) && !is("issn")) set("issn", $sici[1]);
#if (!is ("year") && !is("month") && $sici[3]) set("month", date("M", mktime(0, 0, 0, $sici[3], 1, 2005)));
if (!is("year")) set("year", $sici[2]);
#if (!is("day") && is("month") && $sici[4]) set ("day", $sici[4]);
if (!is("volume")) set("volume", 1*$sici[5]);
if (!is("issue") && $sici[6]) set("issue", 1*$sici[6]);
if (!is("pages") && !is("page")) set("pages", 1*$sici[7]);
return true;
} else return false;
}
function get_data_from_adsabs() {
global $p;
$url_root = "http://adsabs.harvard.edu/cgi-bin/abs_connect?data_type=XML&";
if (is("bibcode")) {
$xml = simplexml_load_file($url_root . "bibcode=" . urlencode($p["bibcode"][0]));
} elseif (is("doi")) {
$xml = simplexml_load_file($url_root . "doi=" . urlencode($p["doi"][0]));
} elseif (is("title")) {
$xml = simplexml_load_file($url_root . "title=" . urlencode('"' . $p["title"][0] . '"'));
$inTitle = str_replace(array(" ", "\n", "\r"), "", (mb_strtolower($xml->record->title)));
$dbTitle = str_replace(array(" ", "\n", "\r"), "", (mb_strtolower($p["title"][0])));
if (
(strlen($inTitle) > 254 || strlen(dbTitle) > 254)
? strlen($inTitle) != strlen($dbTitle) || similar_text($inTitle, $dbTitle)/strlen($inTitle) < 0.98
: levenshtein($inTitle, $dbTitle) > 3
) {
echo "\n Similar title not found in database";
return false;
}
}
if ($xml["retrieved"] != 1 && is("journal")) {
// try partial search using bibcode components:
$xml = simplexml_load_file($url_root
. "year=" . $p["year"][0]
. "&volume=" . $p["volume"][0]
. "&page=" . ($p["pages"] ? $p["pages"][0] : $p["page"][0])
);
$journal_string = explode(",", (string) $xml->record->journal);
$journal_fuzzyer = "~\bof\b|\bthe\b|\ba\beedings\b|\W~";
if (strpos(mb_strtolower(preg_replace($journal_fuzzyer, "", $p["journal"][0])),
mb_strtolower(preg_replace($journal_fuzzyer, "", $journal_string[0]))) === FALSE) {
echo "\n Match for pagination but database journal \"{$journal_string[0]}\" didn't match \"journal = {$p["journal"][0]}\".";
return false;
}
}
if ($xml["retrieved"] == 1) {
if_null_set("bibcode", (string) $xml->record->bibcode);
if_null_set("title", (string) $xml->record->title);
foreach ($xml->record->author as $author) {
if_null_set("author" . ++$i, $author);
}
$journal_string = explode(",", (string) $xml->record->journal);
$journal_start = mb_strtolower($journal_string[0]);
if_null_set("volume", (string) $xml->record->volume);
if_null_set("issue", (string) $xml->record->issue);
if_null_set("year", preg_replace("~\D~", "", (string) $xml->record->pubdate));
if_null_set("pages", (string) $xml->record->page);
if (preg_match("~\bthesis\b~ui", $journal_start)) {}
elseif (substr($journal_start, 0, 6) == "eprint") {
if (substr($journal_start, 7, 6) == "arxiv:") {
if (if_null_set("arxiv", substr($journal_start, 13))) { // nothingMissing will return FALSE as no journal!
get_data_from_arxiv(substr($journal_start, 13));
}
} else {
$p["id"][0] .= " " . substr($journal_start, 13);
}
} else {
if_null_set("journal", $journal_string[0]);
}
if (if_null_set("doi", (string) $xml->record->DOI) && nothingMissing("journal")) {
get_data_from_doi();
}
return true;
} else {
return false;
}
}
function expand_from_doi($a, $b, $c = false, $DEPRECATED = TRUE) {
print "\n\n !! ==== \n\n USING DUD FN DOI! \n\n";
expand_from_crossref($a, $b, $c);
}
function get_data_from_doi($doi, $silence) {
global $editing_cite_doi_template;
$crossRef = crossRefData($doi);
if ($crossRef)
return expand_from_crossref($crossRef, $editing_cite_doi_template, $silence);
else if (substr(trim($doi), 0, 8) == '10.2307/')
return get_data_from_jstor(substr(trim($doi), 8));
else return false;
}
function getDataFromArxiv($a, $DEPRECATED = TRUE) {
print "\n\n !! ==== \n\n USING DUD FN ARXIV! \n\n";
return get_data_from_arxiv($a);
}
function expand_from_pubmed($DEPRECATED = TRUE) {
print "\n\n !! ==== \n\n USING DUD FN PMID! \n\n";
return get_data_from_pubmed();
}
function getInfoFromISBN($DEPRECATED = TRUE) {
print "\n\n !! ==== \n\n USING DUD FN ISBN! \n\n";
return get_data_from_isbn($a);
}
function get_data_from_arxiv($a) {
$xml = simplexml_load_string(
preg_replace("~(</?)(\w+):([^>]*>)~", "$1$2$3", file_get_contents("http://export.arxiv.org/api/query?start=0&max_results=1&id_list=$a"))
);
if ($xml) {
global $p;
foreach ($xml->entry->author as $auth) {
$i++;
$name = $auth->name;
if (preg_match("~(.+\.)(.+?)$~", $name, $names)) {
if_null_set("last$i", $names[2]); // I previously had "author$i", which prevented "first$i" from being null-set
if_null_set("first$i", $names[1]);
// If there's a newline before the forename,, remove it so it displays alongside the surname.
if (strpos($p["first$i"], "\n" !== false)) {
$p["first$i"][1] = " | ";
}
}
elseif (trim($p['author'][0]) == "") {
if_null_set("author$i", $name);
}
}
if_null_set("title", (string)$xml->entry->title);
if_null_set("class", (string)$xml->entry->category["term"]);
if_null_set("author", substr($authors, 2));
if (if_null_set("doi", (string) $xml->entry->arxivdoi) && !nothingMissing("journal")) {
get_data_from_doi((string) $xml->entry->arxivdoi);
}
if ($xml->entry->arxivjournal_ref) {
$journal_data = (string) $xml->entry->arxivjournal_ref;
if (preg_match("~(\(?([12]\d{3})\)?).*?$~", $journal_data, $match)) {
$journal_data = str_replace($match[1], "", $journal_data);
if_null_set("year", $match[2]);
}
if (preg_match("~\w?\d+-\w?\d+~", $journal_data, $match)) {
$journal_data = str_replace($match[0], "", $journal_data);
if_null_set("pages", str_replace("--", en_dash, $match[0]));
}
if (preg_match("~(\d+)(?:\D+(\d+))?~", $journal_data, $match)) {
if_null_set("volume", $match[1]);
if_null_set("issue", $match[2]);
$journal_data = preg_replace("~[\s:,;]*$~", "",
str_replace(array($match[1], $match[2]), "", $journal_data));
}
if_null_set("journal", $journal_data);
} else {
if_null_set("year", date("Y", strtotime((string)$xml->entry->published)));
}
return true;
}
return false;
}
function get_data_from_jstor($jid) {
$jstor_url = "http://dfr.jstor.org/sru/?operation=searchRetrieve&query=dc.identifier%3D%22$jid%22&version=1.1";
$data = @file_get_contents ($jstor_url);
$xml = simplexml_load_string(str_replace(":", "___", $data));
if ($xml->srw___numberOfRecords == 1) {
$data = $xml->srw___records->srw___record->srw___recordData;
global $p;
if (trim(substr($p["doi"][0], 0, 7)) == "10.2307" || is("jstor")) {
if (strpos($p["url"][0], "jstor.org")) {
unset($p["url"]);
if_null_set("jstor", substr($jid, 8));
}
}
if (preg_match("~(pp\. )?(\w*\d+.*)~", $data->dc___coverage, $match)) {
if_null_set("pages", str_replace("___", ":", $match[2]));
}
foreach ($data->dc___creator as $author) {
$i++;
$oAuthor = formatAuthor(str_replace("___", ":", $author));
$oAuthor = explode(", ", $oAuthor);
$first = str_replace(" .", "",
preg_replace("~(\w)\w*\W*((\w)\w*\W+)?((\w)\w*\W+)?((\w)\w*)?~",
"$1. $3. $5. $7.", $oAuthor[1]));
if_null_set("last$i", $oAuthor[0]);
if_null_set("first$i", $first);
}
if_null_set("title", (string) str_replace("___", ":", $data->dc___title)) ;
if (preg_match("~(.*),\s+Vol\.\s+([^,]+)(, No\. (\S+))?~", str_replace("___", ":", $data->dc___relation), $match)) {
if_null_set("journal", str_replace("___", ":", $match[1]));
if_null_set("volume", $match[2]);
if_null_set("issue", $match[4]);
$handled_data = true;
} else {
if (preg_match("~Vol\.___\s*([\w\d]+)~", $data->dc___relation, $match)) {
if_null_set("volume", $match[1]);
$handled_data = true;
}
if (preg_match("~No\.___\s*([\w\d]+)~", $data->dc___relation, $match)) {
if_null_set("issue", $match[1]);
$handled_data = true;
}
if (preg_match("~JOURNAL___\s*([\w\d\s]+)~", $data->dc___relation, $match)) {
if_null_set("journal", str_replace("___", ":", $match[1]));
$handled_data = true;
}
}
if (!$handled_data) {
echo "unhandled data: $data->dc___relation";
}
/* -- JSTOR's publisher field is often dodgily formatted.
if (preg_match("~[^/;]*~", $data->dc___publisher, str_replace("___", ":", $match))) {
if_null_set("publisher", $match[0]);
}*/
if (preg_match ("~\d{4}~", $data->dc___date[0], $match)) {
if_null_set("year", str_replace("___", ":", $match[0]));
}
return true;
} else {
echo $xml->srw___numberOfRecords . " records obtained. ";
return false;
}
}
function crossRefData($doi) {
global $crossRefId;
$url = "http://www.crossref.org/openurl/?pid=$crossRefId&id=doi:$doi&noredirect=true";
$xml = @simplexml_load_file($url);
if ($xml) {
$result = $xml->query_result->body->query;
} else {
echo "Error loading CrossRef file from DOI $doi! <br>\n";
return false;
}
return ($result["status"]=="resolved")?$result:false;
}
function crossRefDoi($title, $journal, $author, $year, $volume, $startpage, $endpage, $issn, $url1, $debug = false ){
global $priorP;
$input = array($title, $journal, $author, $year, $volume, $startpage, $endpage, $issn, $url1);
if ($input == $priorP['crossref']) {
echo "\n * Data not changed since last CrossRef search.";
return false;
} else {
$priorP['crossref'] = $input;
global $crossRefId;
if ($journal || $issn) {
$url = "http://www.crossref.org/openurl/?noredirect=true&pid=$crossRefId"
. ($title ? "&atitle=" . urlencode(deWikify($title)) : "")
. ($author ? "&aulast=" . urlencode($author) : '')
. ($startpage ? "&spage=" . urlencode($startpage) : '')
. ($endpage > $startpage ? "&epage=" . urlencode($endpage) : '')
. ($year ? "&date=" . urlencode(preg_replace("~([12]\d{3}).*~", "$1", $year)) : '')
. ($volume ? "&volume=" . urlencode($volume) : '')
. ($issn ? "&issn=$issn" : ($journal ? "&title=" . urlencode(deWikify($journal)) : ''));
if (!($result = @simplexml_load_file($url)->query_result->body->query)){
echo "\n * Error loading simpleXML file from CrossRef.";
}
else if ($result['status'] == 'malformed') {
echo "\n * Cannot search CrossRef: " . $result->msg;
}
else if ($result["status"] == "resolved") {
return $result;
}
}
if ($url1) {
$url = "http://www.crossref.org/openurl/?url_ver=Z39.88-2004&req_dat=$crossRefId&rft_id=info:http://" . urlencode(str_replace(Array("http://", "&noredirect=true"), Array("", ""), urldecode($url1)));
if (!($result = @simplexml_load_file($url)->query_result->body->query)) echo "\n xxx Error loading simpleXML file from CrossRef via URL. ";
if ($debug) echo $url . "<BR>";
if ($result["status"]=="resolved") return $result;
echo "URL search failed. Trying other parameters... ";
}
global $fastMode;
if ($fastMode || !$author || !($journal || $issn) ) return;
// If fail, try again with fewer constraints...
echo "Full search failed. Dropping author & endpage... ";
$url = "http://www.crossref.org/openurl/?noredirect=true&pid=$crossRefId";
if ($title) $url .= "&atitle=" . urlencode(deWikify($title));
if ($issn) $url .= "&issn=$issn"; elseif ($journal) $url .= "&title=" . urlencode(deWikify($journal));
if ($year) $url .= "&date=" . urlencode($year);
if ($volume) $url .= "&volume=" . urlencode($volume);
if ($startpage) $url .= "&spage=" . urlencode($startpage);
if (!($result = @simplexml_load_file($url)->query_result->body->query)) {
echo "\n * Error loading simpleXML file from CrossRef.";
}
else if ($result['status'] == 'malformed') {
echo "\n * Cannot search CrossRef: " . $result->msg;
} else if ($result["status"]=="resolved") {
echo " Successful!";
return $result;
}
}
}
function textToSearchKey($key){
switch (mb_strtolower($key)){
case "doi": return "AID";
case "author": case "author1": return "Author";
case "author": case "author1": return "Author";
case "issue": return "Issue";
case "journal": return "Journal";
case "pages": case "page": return "Pagination";
case "date": case "year": return "Publication Date";
## Formatting: YYY/MM/DD Publication Date [DP]
case "title": return "Title";
case "pmid": return "PMID";
case "volume": return "Volume";
##Text Words [TW] ; Title/Abstract [TIAB]
}
return false;
}
/* pmSearch
*
* Searches pubmed based on terms provided in an array.
* Provide an array of wikipedia parameters which exist in $p, and this function will construct a Pubmed seach query and
* return the results as array (first result, # of results)
* If $check_for_errors is true, it will return 'fasle' on errors returned by pubmed
*/
function pmSearch($p, $terms, $check_for_errors = false) {
foreach ($terms as $term) {
$key = textToSearchKey($term);
if ($key && trim($p[$term][0]) != "") {
$query .= " AND (" . str_replace("%E2%80%93", "-", urlencode($p[$term][0])) . "[$key])";
}
}
$query = substr($query, 5);
$url = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&tool=DOIbot&[email protected]&term=$query";
$xml = simplexml_load_file($url);
if ($check_for_errors && $xml->ErrorList) {
echo $xml->ErrorList->PhraseNotFound
? " no results."
: "\n - Errors detected in PMID search (" . print_r($xml->ErrorList, 1) . "); abandoned.";
return array(null, 0);
}
return $xml?array((string)$xml->IdList->Id[0], (string)$xml->Count):array(null, 0);// first results; number of results
}
/* pmSearchResults
*
* Performs a search based on article data, using the DOI preferentially, and failing that, the rest of the article details.
* Returns an array:
* [0] => PMID of first matching result
* [1] => total number of results
*
*/
function pmSearchResults($p){
if ($p) {
if ($p['doi'][0]) {
$results = pmSearch($p, array("doi"), true);
if ($results[1] == 1) return $results;
}
// If we've got this far, the DOI was unproductive or there was no DOI.
if (is("journal") && is("volume") && is("pages")) {
$results = pmSearch($p, array("journal", "volume", "issue", "pages"));
if ($results[1] == 1) return $results;
}
if (is("title") && (is("author") || is("author") || is("author1") || is("author1"))) {
$results = pmSearch($p, array("title", "author", "author", "author1", "author1"));
if ($results[1] == 1) return $results;
if ($results[1] > 1) {
$results = pmSearch($p, array("title", "author", "author", "author1", "author1", "year", "date"));
if ($results[1] == 1) return $results;
if ($results[1] > 1) {
$results = pmSearch($p, array("title", "author", "author", "author1", "author1", "year", "date", "volume", "issue"));
if ($results[1] == 1) return $results;
}
}
}
}
}
function searchForPmid() {
global $p, $priorP;
if ($p == $priorP['pmid']) {
echo "\n - No changes since last PubMed search.";
return false;
} else {
echo "\n - Searching PubMed... ";
$results = (pmSearchResults($p));
if ($results[1] == 1) {
if_null_set('pmid', $results[0]);
$details = pmArticleDetails($results[0]);
echo " 1 result found; updating citation";
foreach ($details as $key=>$value) {
if_null_set ($key, $value);
}
if (!is('doi')) {
// PMID search succeeded but didn't throw up a new DOI. Try CrossRef again.
echo "\n - Looking for DOI in CrossRef database with new information ... ";
$crossRef = crossRefDoi(trim($p["title"][0]), trim($p[$journal][0]),
get_first_author($p), trim($p["year"][0]), trim($p["volume"][0]),
get_first_page($p), get_last_page($p), trim($p["issn"][0]), trim($p["url"][0]));
if ($crossRef) {
$p["doi"][0] = $crossRef->doi;
echo "Match found: " . $p["doi"][0];
} else {
echo "no match.";
}
}
} else {
echo " nothing found.";
if (mb_strtolower(substr($citation[$cit_i+2], 0, 8)) == "citation" && !is("journal")) {
// Check for ISBN, but only if it's a citation. We should not risk a false positive by searching for an ISBN for a journal article!
echo "\n - Checking for ISBN";
$isbnToStartWith = isset($p["isbn"]);
if (!isset($p["isbn"][0]) && is("title")) set("isbn", findISBN( $p["title"][0], $p["author"][0] . " " . $p["last"][0] . $p["last1"][0]));
else echo "\n Already has an ISBN. ";
if (!$isbnToStartWith && !$p["isbn"][0]) {
unset($p["isbn"]);
} else {
// getInfoFromISBN(); // Too buggy. Disabled.
}
}
}
}
$priorP['pmid'] = $p;
}
function pmArticleDetails($pmid, $id = "pmid"){
$result = Array();
$xml = simplexml_load_file("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?tool=DOIbot&[email protected]&db=" . (($id == "pmid")?"pubmed":"pmc") . "&id=$pmid");
// Debugging URL : view-source:http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&tool=DOIbot&[email protected]&id=
foreach($xml->DocSum->Item as $item) {
if (preg_match("~10\.\d{4}/[^\s\"']*~", $item, $match)) $result["doi"] = $match[0];
switch ($item["Name"]) {
case "Title": $result["title"] = str_replace(array("[", "]"), "",(string) $item);
break; case "PubDate": preg_match("~(\d+)\s*(\w*)~", $item, $match);
$result["year"] = (string) $match[1];
// $result["month"] = (string) $match[2]; DISABLED BY EUBLIDES
break; case "FullJournalName": $result["journal"] = (string) $item;
break; case "Volume": $result["volume"] = (string) $item;
break; case "Issue": $result["issue"] = (string) $item;
break; case "Pages": $result["pages"] = (string) $item;
break; case "PmId": $result["pmid"] = (string) $item;
break; case "ISSN": // $result["issn"] = (string) $item; DISABLED BY EUBLIDES
break; case "AuthorList":
$i = 0;
foreach ($item->Item as $subItem) {
$i++;
if (authorIsHuman((string) $subItem)) {
$jr_test = jrTest($subItem);
$subItem = $jr_test[0];
$junior = $jr_test[1];
if (preg_match("~(.*) (\w+)$~", $subItem, $names)) {
$result["last$i"] = formatSurname($names[1]) . $junior;
$result["first$i"] = formatForename($names[2]);
}
} else {
// We probably have a committee or similar. Just use 'author$i'.
$result["author$i"] = (string) $subItem;
}
}
break; case "LangList":
break; // Disabled at the request of EUBULIDES
/*
foreach ($item->Item as $subItem) {
if ($subItem["Name"] == "Lang" && $subItem != "English" && $subItem != "Undetermined") {
$result ["language"] = (string) $subItem;
if ($result['title']) {
$result['trans_title'] = $result['title'];
unset ($result['title']);
}
}
}
break; */case "ArticleIds":
foreach ($item->Item as $subItem) {
switch ($subItem["Name"]) {
case "pubmed":
preg_match("~\d+~", (string) $subItem, $match);
$result["pmid"] = $match[0];
break;
case "pmc":
preg_match("~\d+~", (string) $subItem, $match);
$result["pmc"] = $match[0];
break;
case "doi": case "pii":
if (preg_match("~10\.\d{4}/[^\s\"']*~", (string) $subItem, $match)) {
$result["doi"] = $match[0];
}
break;
default:
if (preg_match("~10\.\d{4}/[^\s\"']*~", (string) $subItem, $match)) {
$result["doi"] = $match[0];
}
break;
}
}
break;
}
}
return $result;
}
function pmExpand($p, $id){
$details = pmArticleDetails($p[$id][0], $id);
foreach($details as $key=>$value) $p[$key][0] = $value;
}
function pmFullTextUrl($pmid){
$xml = simplexml_load_file("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&id=$pmid&cmd=llinks&tool=DOIbot&[email protected]");
if ($xml) {
foreach ($xml->LinkSet->IdUrlList->IdUrlSet->ObjUrl as $url){
foreach ($url->Attribute as $attrib) $requiredFound = strpos($url->Attribute, "required")?true:$requiredFound;
if (!$requiredFound) return (string) $url->Url;
}
}
}
function google_book_expansion() {
global $p;
if (is("url") && preg_match("~books\.google\.[\w\.]+/.*\bid=([\w\d\-]+)~", $p["url"][0], $gid)) {
$url = $p["url"][0];
if (strpos($url, "#")) {
$url_parts = explode("#", $url);
$url = $url_parts[0];
$hash = "#" . $url_parts[1];
}
$url_parts = explode("&", str_replace("?", "&", $url));
$url = "http://books.google.com/?id=" . $gid[1];
foreach ($url_parts as $part) {
$part_start = explode("=", $part);
switch ($part_start[0]) {
case "dq": case "pg": case "lpg": case "q": case "printsec": case "cd": case "vq":
$url .= "&" . $part;
// TODO: vq takes precedence over dq > q. Only use one of the above.
case "id":
break; // Don't "remove redundant"
case "as": case "useragent": case "as_brr": case "source": case "hl":
case "ei": case "ots": case "sig": case "source": case "lr":
case "as_brr": case "sa": case "oi": case "ct": case "client": // List of parameters known to be safe to remove
default:
echo "\n - $part";
$removed_redundant++;
}
}
if ($removed_redundant > 1) { // http:// is counted as 1 parameter
$p["url"][0] = $url . $hash;
}
google_book_details($gid[1]);
return true;
}
return false;
}
function google_book_details ($gid) {
$google_book_url = "http://books.google.com/books/feeds/volumes/$gid";
$simplified_xml = str_replace(":", "___", file_get_contents($google_book_url));
$xml = simplexml_load_string($simplified_xml);
if ($xml->dc___title[1]) {
if_null_set("title", str_replace("___", ":", $xml->dc___title[0] . ": " . $xml->dc___title[1]));
} else {
if_null_set("title", str_replace("___", ":", $xml->title));
}
/* Possibly contains dud information on occasion
if_null_set("publisher", str_replace("___", ":", $xml->dc___publisher));
*/
foreach ($xml->dc___identifier as $ident) {
if (preg_match("~isbn.*?([\d\-]{9}[\d\-]+)~i", (string) $ident, $match)) {
$isbn = $match[1];
}
}
if_null_set("isbn", $isbn);
// Don't set 'pages' parameter, as this refers to the CITED pages, not the page count of the book.
$i = null;
if (!is("editor") && !is("editor1") && !is("editor1-last") && !is("editor-last")
&& !is("author") && !is("author1") && !is("last") && !is("last1")
&& !is("publisher")) { // Too many errors in gBook database to add to existing data. Only add if blank.
foreach ($xml->dc___creator as $author) {
$i++;
if_null_set("author$i", formatAuthor(str_replace("___", ":", $author)));
}
}
if_null_set("date", $xml->dc___date);
}
function findISBN ($title, $auth = false) {
global $isbnKey, $over_isbn_limit;
// TODO: implement over_isbn_limit based on &results=keystats in API
if (!$over_isbn_limit) {
$title = trim($title); $auth = trim($auth);
$xml = simplexml_load_file("http://isbndb.com/api/books.xml?access_key=$isbnKey&index1=combined&value1=" . urlencode($title . " " . $auth));
if ($xml->BookList["total_results"] == 1) return (string) $xml->BookList->BookData["isbn"];
if ($auth && $title) {
return $xml->BookList["total_results"] > 0
? (string) $xml->BookList->BookData["isbn"]
: false;
}
} else return false;
}
function get_data_from_isbn() {
global $p, $isbnKey;
$params = array("author"=>"author", "year"=>"year", "publisher"=>"publisher", "location"=>"city", "title"=>"title"/*, "oclc"=>"oclcnum"*/);
if (is("author") || is("first") || is("author1") ||
is("editor") || is("editor-last") || is("editor-last1") || is("editor1-last")
|| is("author") || is("author1")) unset($params["author"]);
if (is("publisher")) {
unset($params["location"]);
unset($params["publisher"]);
}
if (is("title")) unset ($params["title"]);
if (is("year")||is("date")) unset ($params["year"]);
if (is("location")) unset ($params["location"]);
foreach ($params as $null) $missingInfo = true;
if ($missingInfo) $xml = simplexml_load_file("http://xisbn.worldcat.org/webservices/xid/isbn/" . str_replace(array("-", " "), "", $p["isbn"][0]) . "?method=getMetadata&fl=*&format=xml");#&ai=Wikipedia_doibot");
if ($xml["stat"] == "ok") {
foreach ($params as $key => $value) {