-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathZotero.php
2180 lines (2094 loc) · 119 KB
/
Zotero.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
<?php
declare(strict_types=1);
require_once 'constants.php'; // @codeCoverageIgnore
require_once 'Template.php'; // @codeCoverageIgnore
const MAGIC_STRING_URLS = 'CITATION_BOT_PLACEHOLDER_URL_POINTER_';
const CITOID_ZOTERO = "https://en.wikipedia.org/api/rest_v1/data/citation/zotero/";
const THESIS_TYPES = ['PhD', 'MS', 'MA', 'MFA', 'MBA', 'EdD', 'BSN', 'DMin', 'DDiv'];
const BAD_URL_STATUS = ['usurped', 'unfit', 'dead', 'deviated'];
/**
@param array<string> $_ids
@param array<Template> $templates
*/
function query_url_api(array $_ids, array &$templates): void { // Pointer to save memory
Zotero::query_url_api_class($templates);
}
final class Zotero {
private const ZOTERO_GIVE_UP = 5;
private const ZOTERO_SKIPS = 100;
private const ERROR_DONE = 'ERROR_DONE';
private static int $zotero_announced = 0;
private static CurlHandle $zotero_ch;
private static CurlHandle $ch_ieee;
private static CurlHandle $ch_jstor;
private static CurlHandle $ch_dx;
private static CurlHandle $ch_pmc;
private static CurlHandle $ch_doi;
private static CurlHandle $ch_pii;
private static int $zotero_failures_count = 0;
public static function create_ch_zotero(): void {
static $is_setup = false;
if ($is_setup) {
return;
}
$is_setup = true;
if (TRAVIS) {
$time = 3.0;
} else {
$time = 1.0; // @codeCoverageIgnore
}
self::$zotero_ch = bot_curl_init($time, [
CURLOPT_URL => CITOID_ZOTERO,
CURLOPT_HTTPHEADER => ['accept: application/json; charset=utf-8', 'Accept-Language: en-US,en,en-GB,en-CA', 'Cache-Control: no-cache, must-revalidate'],
]);
self::$ch_ieee = bot_curl_init($time, [CURLOPT_USERAGENT => 'curl']); // IEEE requires JavaScript, unless curl is specified
self::$ch_jstor = bot_curl_init($time, []);
self::$ch_dx = bot_curl_init($time, []);
self::$ch_pmc = bot_curl_init($time, []);
self::$ch_doi = bot_curl_init($time, []);
self::$ch_pii = bot_curl_init($time, []);
}
public static function block_zotero(): void {
self::$zotero_failures_count = 1000000;
}
public static function unblock_zotero(): void {
self::$zotero_failures_count = 0;
}
/**
@param array<Template> $templates
*/
public static function query_url_api_class(array &$templates): void { // Pointer to save memory
foreach ($templates as $template) {
if (preg_match('~pii/(S\d{16})(?:|\/|\?|\:|\&|\;)$~i', $template->get('url'), $matches)) { // PII
if ($template->blank('doi')) {
$doi = self::get_doi_from_pii($matches[1]);
if (doi_works($doi)) {
$template->add_if_new('doi', $doi);
}
}
unset($doi, $matches);
}
}
if (!SLOW_MODE) {
return; // @codeCoverageIgnore
}
self::$zotero_announced = 1;
foreach ($templates as $template) {
$doi = $template->get('doi');
if (!doi_active($doi)) { // Do not expand if DOI works with CrossRef
self::expand_by_zotero($template);
}
if ($template->has('biorxiv')) {
if ($template->blank('doi')) {
$template->add_if_new('doi', '10.1101/' . $template->get('biorxiv'));
expand_by_doi($template, true); // this data is better than zotero
} elseif (strstr($template->get('doi'), '10.1101') === false) {
expand_doi_with_dx($template, '10.1101/' . $template->get('biorxiv')); // dx data is better than zotero
self::expand_by_zotero($template, 'https://dx.doi.org/10.1101/' . $template->get('biorxiv')); // Rare case there is a different DOI
}
}
$doi = $template->get('doi'); // might have changed
if (!doi_active($doi)) {
if ($template->has('citeseerx')) {
self::expand_by_zotero($template, ' https://citeseerx.ist.psu.edu/viewdoc/summary?doi=' . $template->get('citeseerx'));
}
// Has a CAPCHA -- if ($template->has('jfm'))
// Has a CAPCHA -- if ($template->has('zbl'))
// Do NOT do MR -- it is a review not the article itself. Note that html does have doi, but do not use it.
if ($template->has('hdl')) {
self::expand_by_zotero($template, 'https://hdl.handle.net/' . $template->get('hdl'));
}
if ($template->has('osti')) {
self::expand_by_zotero($template, 'https://www.osti.gov/biblio/' . $template->get('osti'));
}
if ($template->has('rfc')) {
self::expand_by_zotero($template, 'https://tools.ietf.org/html/rfc' . $template->get('rfc'));
}
if ($template->has('ssrn')) {
self::expand_by_zotero($template, 'https://papers.ssrn.com/sol3/papers.cfm?abstract_id=' . $template->get('ssrn'));
}
}
$doi = $template->get('doi'); // might have changed
if (doi_works($doi)) {
if (!doi_active($doi) && !preg_match(REGEXP_DOI_ISSN_ONLY, $doi)) {
self::expand_by_zotero($template, 'https://dx.doi.org/' . $doi); // DOIs without meta-data
}
if ($template->blank('title') && stripos($doi, "10.1023/A:") === 0) {
self::expand_by_zotero($template, 'https://link.springer.com/article/' . $doi); // DOIs without title meta-data
}
}
}
}
/**
@param array<Template> $templates
*/
public static function query_ieee_webpages(array &$templates): void { // Pointer to save memory
foreach (['url', 'chapter-url', 'chapterurl'] as $kind) {
foreach ($templates as $template) {
set_time_limit(120);
/** @psalm-taint-escape ssrf */
$the_url = $template->get($kind);
if (preg_match("~^https://ieeexplore\.ieee\.org/document/(\d{5,})$~", $the_url, $matches_url)) {
curl_setopt(self::$ch_ieee, CURLOPT_URL, $the_url);
if ($template->blank('doi')) {
usleep(100000); // 0.10 seconds
$return = bot_curl_exec(self::$ch_ieee);
if ($return !== "" && preg_match_all('~"doi":"(10\.\d{4}/[^\s"]+)"~', $return, $matches, PREG_PATTERN_ORDER)) {
$dois = array_unique($matches[1]);
if (count($dois) === 1) {
if ($template->add_if_new('doi', $dois[0])) {
if (strpos($template->get('doi'), $matches_url[1]) !== false && doi_works($template->get('doi'))) {
// SEP 2020 $template->forget($kind); // It is one of those DOIs with the document number in it
}
}
}
}
} elseif (doi_works($template->get('doi'))) {
usleep(100000); // 0.10 seconds
$return = bot_curl_exec(self::$ch_ieee);
if ($return !== "" && strpos($return, "<title> - </title>") !== false) {
report_forget("Existing IEEE no longer works - dropping URL"); // @codeCoverageIgnore
$template->forget($kind); // @codeCoverageIgnore
}
}
}
}
}
}
/**
@param array<Template> $templates
*/
public static function drop_urls_that_match_dois(array &$templates): void { // Pointer to save memory
// Now that we have expanded URLs, try to lose them
foreach ($templates as $template) {
$doi = $template->get_without_comments_and_placeholders('doi');
if ($template->has('url')) {
$url = $template->get('url');
$url_kind = 'url';
} elseif ($template->has('chapter-url')) {
$url = $template->get('chapter-url');
$url_kind = 'chapter-url';
} elseif ($template->has('chapterurl')) {
$url = $template->get('chapterurl'); // @codeCoverageIgnore
$url_kind = 'chapterurl'; // @codeCoverageIgnore
} else {
$url = '';
$url_kind = '';
}
if ($doi && // IEEE code does not require "not incomplete"
$url &&
!preg_match(REGEXP_DOI_ISSN_ONLY, $doi) &&
$template->blank(DOI_BROKEN_ALIASES) &&
preg_match("~^https?://ieeexplore\.ieee\.org/document/\d{5,}/?$~", $url) && strpos($doi, '10.1109') === 0) {
// SEP 2020 report_forget("Existing IEEE resulting from equivalent DOI; dropping URL");
// SEP 2020 $template->forget($url_kind);
}
if ($doi &&
$url &&
!$template->profoundly_incomplete() &&
!preg_match(REGEXP_DOI_ISSN_ONLY, $doi) &&
(strpos($doi, '10.1093/') === false) &&
$template->blank(DOI_BROKEN_ALIASES)) {
set_time_limit(120);
if (str_ireplace(PROXY_HOSTS_TO_DROP, '', $url) !== $url && $template->get('doi-access') === 'free') {
report_forget("Existing proxy URL resulting from equivalent free DOI; dropping URL");
$template->forget($url_kind);
} elseif (str_ireplace(PROXY_HOSTS_TO_ALWAYS_DROP, '', $url) !== $url && $template->get('doi-access') === 'free') {
report_forget("Existing proxy URL resulting from equivalent free DOI; dropping URL");
$template->forget($url_kind);
} elseif (str_ireplace(PROXY_HOSTS_TO_ALWAYS_DROP, '', $url) !== $url && $template->blank(['archive-url', 'archiveurl'])) {
report_forget("Existing proxy URL resulting from equivalent DOI; fixing URL");
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif (preg_match('~www.sciencedirect.com/science/article/B[^/\-]*\-[^/\-]+\-[^/\-]+/~', $url)) {
report_forget("Existing Invalid ScienceDirect URL when DOI is present; fixing URL");
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif (preg_match('~www.sciencedirect.com/science/article/pii/\S{0,16}$~i', $url)) { // Too Short
report_forget("Existing Invalid ScienceDirect URL when DOI is present; fixing URL");
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif (preg_match('~www.springerlink.com/content~i', $url)) { // Dead website
report_forget("Existing Invalid Springer Link URL when DOI is present; fixing URL");
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif (str_ireplace('insights.ovid.com/pubmed', '', $url) !== $url && $template->has('pmid')) {
// SEP 2020 report_forget("Existing OVID URL resulting from equivalent PMID and DOI; dropping URL");
// SEP 2020 $template->forget($url_kind);
} elseif ($template->has('pmc') && str_ireplace('iopscience.iop.org', '', $url) !== $url) {
// SEP 2020 report_forget("Existing IOP URL resulting from equivalent DOI; dropping URL");
// SEP 2020 $template->forget($url_kind);;
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif (str_ireplace('wkhealth.com', '', $url) !== $url) {
report_forget("Existing Outdated WK Health URL resulting from equivalent DOI; fixing URL");
$template->set($url_kind, "https://dx.doi.org/" . doi_encode($doi));
} elseif ($template->has('pmc') && str_ireplace('bmj.com/cgi/pmidlookup', '', $url) !== $url && $template->has('pmid') && $template->get('doi-access') === 'free' && stripos($url, 'pdf') === false) {
report_forget("Existing The BMJ URL resulting from equivalent PMID and free DOI; dropping URL");
$template->forget($url_kind);
} elseif ($template->get('doi-access') === 'free' && $template->get('url-status') === 'dead' && $url_kind === 'url') {
report_forget("Existing free DOI; dropping dead URL");
$template->forget($url_kind);
} elseif (doi_works($template->get('doi')) &&
!preg_match(REGEXP_DOI_ISSN_ONLY, $template->get('doi')) &&
$url_kind !== '' &&
(str_ireplace(CANONICAL_PUBLISHER_URLS, '', $template->get($url_kind)) !== $template->get($url_kind)) &&
$template->has_good_free_copy() &&
(stripos($template->get($url_kind), 'pdf') === false)) {
report_forget("Existing canonical URL resulting in equivalent free DOI/pmc; dropping URL");
$template->forget($url_kind);
} elseif (stripos($url, 'pdf') === false && $template->get('doi-access') === 'free' && $template->has('pmc')) {
curl_setopt(self::$ch_dx, CURLOPT_URL, "https://dx.doi.org/" . doi_encode($doi));
$ch_return = bot_curl_exec(self::$ch_dx);
if (strlen($ch_return) > 50) { // Avoid bogus tiny pages
$redirectedUrl_doi = curl_getinfo(self::$ch_dx, CURLINFO_EFFECTIVE_URL); // Final URL
if (stripos($redirectedUrl_doi, 'cookie') !== false) {
break; // @codeCoverageIgnore
}
if (stripos($redirectedUrl_doi, 'denied') !== false) {
break; // @codeCoverageIgnore
}
$redirectedUrl_doi = self::url_simplify($redirectedUrl_doi);
$url_short = self::url_simplify($url);
if (preg_match('~^https?://.+/pii/?(S?\d{4}[^/]+)~i', $redirectedUrl_doi, $matches ) === 1 ) { // Grab PII numbers
$redirectedUrl_doi = $matches[1] ; // @codeCoverageIgnore
}
if (stripos($url_short, $redirectedUrl_doi) !== false ||
stripos($redirectedUrl_doi, $url_short) !== false) {
report_forget("Existing canonical URL resulting from equivalent free DOI; dropping URL");
$template->forget($url_kind);
} else { // See if $url redirects
/** @psalm-taint-escape ssrf */
$the_url = $url;
curl_setopt(self::$ch_doi, CURLOPT_URL, $the_url);
$ch_return = bot_curl_exec(self::$ch_doi);
if (strlen($ch_return) > 60) {
$redirectedUrl_url = curl_getinfo(self::$ch_doi, CURLINFO_EFFECTIVE_URL);
$redirectedUrl_url = self::url_simplify($redirectedUrl_url);
if (stripos($redirectedUrl_url, $redirectedUrl_doi) !== false ||
stripos($redirectedUrl_doi, $redirectedUrl_url) !== false) {
report_forget("Existing canonical URL resulting from equivalent free DOI; dropping URL");
$template->forget($url_kind);
}
}
}
}
unset($ch_return);
}
}
$url = $template->get($url_kind);
if ($url && !$template->profoundly_incomplete() && str_ireplace(PROXY_HOSTS_TO_ALWAYS_DROP, '', $url) !== $url) {
if (!$template->blank_other_than_comments('pmc')) {
report_forget("Existing proxy URL resulting from equivalent PMC; dropping URL");
$template->forget($url_kind);
}
}
}
}
private static function zotero_request(string $url): string {
set_time_limit(120);
if (self::$zotero_failures_count > self::ZOTERO_GIVE_UP) {
self::$zotero_failures_count -= 1;
if (self::$zotero_failures_count === self::ZOTERO_GIVE_UP) {
self::$zotero_failures_count = 0; // @codeCoverageIgnore
}
}
/** @psalm-taint-escape ssrf */
$the_url = CITOID_ZOTERO . urlencode($url);
curl_setopt(self::$zotero_ch, CURLOPT_URL, $the_url);
if (self::$zotero_failures_count > self::ZOTERO_GIVE_UP) {
return self::ERROR_DONE;
}
$delay = max(min(100000*(1+self::$zotero_failures_count), 10), 0); // 0.10 seconds delay, with paranoid bounds checks
usleep($delay);
$zotero_response = bot_curl_exec(self::$zotero_ch);
if ($zotero_response === '') {
sleep(2); // @codeCoverageIgnore
$zotero_response = bot_curl_exec(self::$zotero_ch); // @codeCoverageIgnore
}
if ($zotero_response === '') {
// @codeCoverageIgnoreStart
report_warning(curl_error(self::$zotero_ch) . " For URL: " . echoable($url));
if (strpos(curl_error(self::$zotero_ch), 'timed out after') !== false) {
self::$zotero_failures_count += 1;
if (self::$zotero_failures_count > self::ZOTERO_GIVE_UP) {
report_warning("Giving up on URL expansion for a while");
self::$zotero_failures_count += self::ZOTERO_SKIPS;
}
}
$zotero_response = self::ERROR_DONE;
// @codeCoverageIgnoreEnd
}
return $zotero_response;
}
public static function expand_by_zotero(Template $template, ?string $url = null): void {
$access_date = 0;
if (is_null($url)) {
if (in_array($template->get('url-status'), BAD_URL_STATUS, true)) {
return;
}
$access_date = (int) strtotime(tidy_date($template->get('accessdate') . ' ' . $template->get('access-date')));
$archive_date = (int) strtotime(tidy_date($template->get('archivedate') . ' ' . $template->get('archive-date')));
if ($access_date && $archive_date) {
$access_date = min($access_date, $archive_date); // Whichever was first
} elseif ($archive_date) {
$access_date = $archive_date;
}
if ($template->has('url')) {
$url = $template->get('url');
} elseif ($template->has('chapter-url')) {
$url = $template->get('chapter-url');
} elseif ($template->has('chapterurl')) {
$url = $template->get('chapterurl');
} else {
return;
}
if (preg_match('~^https?://(?:dx\.|)doi\.org~i', $url)) {
return;
}
if (preg_match('~^https?://semanticscholar\.org~i', $url)) {
return;
}
if (preg_match(REGEXP_BIBCODE, urldecode($url))) {
return;
}
if (preg_match("~^https?://citeseerx\.ist\.psu\.edu~i", $url)) {
return;
}
if (preg_match("~\barxiv\.org/.*(?:pdf|abs|ftp/arxiv/papers/\d{4})/(.+?)(?:\.pdf)?$~i", $url)) {
return;
}
}
if (!$template->profoundly_incomplete($url)) {
return; // Only risk unvetted data if there's little good data to sully
}
if (stripos($url, 'CITATION_BOT_PLACEHOLDER') !== false) {
return; // That's a bad url
}
// Clean up URLs
if (preg_match('~^(https?://(?:www\.|)nature\.com/articles/[a-zA-Z0-9\.]+)\.pdf(?:|\?.*)$~i', $url, $matches)) { // remove .PDF from Nature urls
$url = $matches[1]; // @codeCoverageIgnore
}
if (preg_match('~^(https?://(?:www\.|)mdpi\.com/.+)(?:/pdf\-vor|/pdf)$~', $url, $matches)) {
$url = $matches[1];
}
$bad_url = implode('|', ZOTERO_AVOID_REGEX);
if (preg_match("~^https?://(?:www\.|m\.|ftp\.|web\.|)(?:" . $bad_url . ")~i", $url)) {
return;
}
// Is it actually a URL. Zotero will search for non-url things too!
if (preg_match('~^https?://[^/]+/?$~', $url) === 1) {
return; // Just a host name
}
set_time_limit(120); // This can be slow
if (preg_match(REGEXP_IS_URL, $url) !== 1) {
return; // See https://mathiasbynens.be/demo/url-regex/ This regex is more exact than validator. We only spend time on this after quick and dirty check is passed
}
set_time_limit(120);
if (self::$zotero_announced === 1) {
report_action("Using Zotero translation server to retrieve details from URLs and identifiers");
self::$zotero_announced = 0;
}
$zotero_response = self::zotero_request($url);
self::process_zotero_response($zotero_response, $template, $url, $access_date);
return;
}
public static function process_zotero_response(string $zotero_response, Template $template, string $url, int $access_date): void {
if ($zotero_response === self::ERROR_DONE) {
return; // Error message already printed in zotero_request()
}
switch (trim($zotero_response)) {
case '':
report_info("Nothing returned for URL " . echoable($url));
return;
case 'Internal Server Error':
report_info("Internal server error with URL " . echoable($url));
return;
case 'Remote page not found':
report_info("Remote page not found for URL " . echoable($url));
return;
case 'No items returned from any translator':
report_info("Remote page not interpretable for URL " . echoable($url));
return;
case 'An error occurred during translation. Please check translation with the Zotero client.':
report_info("An error occurred during translation for URL " . echoable($url));
return;
}
if (strpos($zotero_response, '502 Bad Gateway') !== false) {
report_warning("Bad Gateway error for URL ". echoable($url));
return;
}
if (strpos($zotero_response, '503 Service Temporarily Unavailable') !== false) {
report_warning("Temporarily Unavailable error for URL " . echoable($url)); // @codeCoverageIgnore
return; // @codeCoverageIgnore
}
if (strpos($zotero_response, '<title>Wikimedia Error</title>') !== false) {
report_warning("Temporarily giving an error for URL " . echoable($url)); // @codeCoverageIgnore
return; // @codeCoverageIgnore
}
$zotero_data = @json_decode($zotero_response, false);
if (!isset($zotero_data)) {
report_warning("Could not parse JSON for URL ". echoable($url) . ": " . $zotero_response);
return;
} elseif (!is_array($zotero_data)) {
if (is_object($zotero_data)) {
$zotero_data = (array) $zotero_data;
} else {
report_warning("JSON did not parse correctly for URL ". echoable($url) . ": " . $zotero_response);
return;
}
}
if (!isset($zotero_data[0])) {
$result = $zotero_data;
} else {
$result = $zotero_data[0];
}
$result = (object) $result ;
if (empty($result->publicationTitle) && empty($result->bookTitle) && !isset($result->title)) {
if (!empty($result->subject)) {
$result->title = $result->subject;
} elseif (!empty($result->caseName)) {
$result->title = $result->caseName;
} elseif (!empty($result->nameOfAct)) {
$result->title = $result->nameOfAct;
}
}
if (!isset($result->title)) {
$the_url = substr(echoable(substr($url, 0, 500)), 0, 600); // Limit length
if (strpos($zotero_response, 'unknown_error') !== false) { // @codeCoverageIgnoreStart
report_info("Did not get a title for unknown reason from URL ". $the_url);
} elseif (strpos($zotero_response, 'The remote document is not in a supported format') !== false) {
report_info("Document type not supported (usually PDF) for URL ". $the_url);
} elseif (strpos($zotero_response, 'Unable to load URL') !== false) {
report_info("Zotero could not fetch anything for URL ". $the_url);
} elseif (strpos($zotero_response, 'Invalid host supplied') !== false) {
report_info("DNS lookup failed for URL ". $the_url);
} elseif (strpos($zotero_response, 'Unknown error') !== false) {
report_info("Did not get a title for unknown reason from URL ". $the_url);
} elseif (strpos($zotero_response, 'Unable to get any metadata from url') !== false) {
report_info("Did not get a title for unknown meta-data reason from URL ". $the_url);
} elseif (strpos($zotero_response, 'Maximum number of allowed redirects reached') !== false) {
report_info("Too many redirects for URL ". $the_url);
} else {
report_minor_error("For some odd reason (" . $zotero_response . ") we did not get a title for URL ". $the_url); // Odd Error
}
return; // @codeCoverageIgnoreEnd
}
if (substr(strtolower(trim($result->title)), 0, 9) === 'not found') {
report_info("Could not resolve URL " . echoable($url));
return;
}
if ($result->title === 'Newstream') {
report_info("No good meta-data from URL " . echoable($url));
return;
}
// Remove unused stuff
unset($result->abstractNote);
unset($result->version);
unset($result->accessDate);
unset($result->libraryCatalog);
unset($result->url);
unset($result->tags);
unset($result->key);
unset($result->websiteTitle);
unset($result->journalAbbreviation);
unset($result->ISSN);
unset($result->subject);
unset($result->caseName);
unset($result->nameOfAct);
unset($result->language);
unset($result->source);
if (isset($result->publicationTitle) && substr($result->publicationTitle, -2) === " |") {
$result->publicationTitle = substr($result->publicationTitle, 0, -2);
}
if (stripos($url, 'www.royal.uk') !== false || stripos($url, 'astanatimes.com') !== false) {
unset($result->creators); // @codeCoverageIgnore
unset($result->author); // @codeCoverageIgnore
}
if (stripos($url, 'theathletic.com') !== false) { // Returns NYT
unset($result->publicationTitle); // @codeCoverageIgnore
}
if (stripos($url, 'newsen.com') !== false) { // Includes title of article
$result->publicationTitle = 'Newsen';
}
if (stripos($url, '/x.com') !== false || stripos($url, 'twitter.com') !== false) {
$result->itemType = 'webpage'; // @codeCoverageIgnore
}
if (stripos($url, 'newrepublic.com') !== false) { // Bad data for all but first one
unset($result->creators['1']);
unset($result->author['1']);
}
if (stripos($url, 'flickr.') !== false) {
$result->itemType = 'webpage';
unset($result->publicationTitle); //Flickr is not a work
}
if (stripos($url, 'pressbooks.online.ucf.edu') !== false) {
$result->itemType = 'webpage';
unset($result->author); // They list themself
}
if (stripos($url, '.tumblr.com') !== false) { // Returns tumblr, and it is a sub-domain
unset($result->publicationTitle); // @codeCoverageIgnore
}
if (stripos($url, 'tumblr.com') !== false) {
$result->itemType = 'webpage'; // @codeCoverageIgnore
}
if (stripos($url, 'tate.org.uk') !== false) {
$result->itemType = 'webpage';
unset($result->creators);
unset($result->author);
}
if (stripos((string) @$result->publicationTitle, 'Extended Abstracts') !== false) { // https://research.vu.nl/en/publications/5a946ccf-5f5b-4cab-b47e-824508c4d709
unset($result->publicationTitle);
}
// Reject if we find more than 5 or more than 10% of the characters are �. This means that character
// set was not correct in Zotero and nothing is good. We allow a couple of � for German umlauts that arer easily fixable by humans.
// We also get a lot of % and $ if the encoding was something like iso-2022-jp and converted wrong
$bad_count = substr_count($result->title, '�') + mb_substr_count($result->title, '$') + mb_substr_count($result->title, '%');
$total_count = mb_strlen($result->title);
if (isset($result->bookTitle)) {
$bad_count += substr_count($result->bookTitle, '�') + mb_substr_count($result->bookTitle, '$') + mb_substr_count($result->bookTitle, '%');
$total_count += mb_strlen($result->bookTitle);
}
if (($bad_count > 5) || ($total_count > 1 && (($bad_count/$total_count) > 0.1))) {
report_info("Could parse unicode characters in " . echoable($url));
return;
}
report_info("Retrieved info from " . echoable($url));
// Verify that Zotero translation server did not think that this was a website and not a journal
if (strtolower(substr(trim($result->title), -9)) === ' on jstor') { // Not really "expanded", just add the title without " on jstor"
$template->add_if_new('title', substr(trim($result->title), 0, -9)); // @codeCoverageIgnore
return; // @codeCoverageIgnore
}
$test_data = '';
if (isset($result->bookTitle)) {
$test_data .= $result->bookTitle . ' ';
}
if (isset($result->title)) {
$test_data .= $result->title;
}
foreach (BAD_ZOTERO_TITLES as $bad_title ) {
if (mb_stripos($test_data, $bad_title) !== false) {
report_info("Received invalid title data for URL " . echoable($url . ": " . $test_data));
return;
}
}
if ($test_data === '404' || $test_data === '/404') {
return;
}
if (isset($result->bookTitle) && strtolower($result->bookTitle) === 'undefined') {
unset($result->bookTitle); // S2 without journals
}
if (isset($result->publicationTitle) && strtolower($result->publicationTitle) === 'undefined') {
unset($result->publicationTitle); // S2 without journals
}
if (isset($result->bookTitle)) {
foreach (array_merge(BAD_ACCEPTED_MANUSCRIPT_TITLES, IN_PRESS_ALIASES) as $bad_title ) {
if (str_i_same($result->bookTitle, $bad_title)) {
report_info("Received invalid book title data for URL " . echoable($url . ": " . $result->bookTitle));
return;
}
}
}
if (isset($result->title)) {
foreach (array_merge(BAD_ACCEPTED_MANUSCRIPT_TITLES, IN_PRESS_ALIASES) as $bad_title ) {
if (str_i_same($result->title, $bad_title)) {
report_info("Received invalid title data for URL ". echoable($url . ": " . $result->title));
return;
}
}
}
if (isset($result->publicationTitle)) {
foreach (array_merge(BAD_ACCEPTED_MANUSCRIPT_TITLES, IN_PRESS_ALIASES) as $bad_title ) {
if (str_i_same($result->publicationTitle, $bad_title)) {
report_info("Received invalid publication title data for URL ". echoable($url . ": " . $result->publicationTitle));
return;
}
}
// Specific bad data that is correctable
$tester = strtolower($result->publicationTitle);
if ($tester === 'nationalpost') {
$result->publicationTitle = 'National Post';
} elseif ($tester === 'financialpost') {
$result->publicationTitle = 'Financial Post';
} elseif ($tester === 'bloomberg.com') {
$result->publicationTitle = 'Bloomberg';
} elseif ($tester === 'radiofreeeurope/radioliberty') {
$result->publicationTitle = 'Radio Free Europe/Radio Liberty';
} elseif ($tester === 'advanced books') {
unset($result->issue);
unset($result->volume);
unset($result->pages);
unset($result->publicationTitle);
}
}
// Ignore junk website names
if (isset($result->publicationTitle) && preg_match('~^https?://([^/]+)~', $url, $hostname) === 1) {
$hostname = str_ireplace('www.', '', (string) $hostname[1]);
$pub_name = str_ireplace('www.', '', (string) $result->publicationTitle);
if (str_i_same($pub_name, $hostname)) {
unset($result->publicationTitle);
}
}
if (preg_match('~^([^\]]+)\|([^\]]+)\| ?THE DAILY STAR$~i', (string) @$result->title, $matches)) {
$result->title = $matches[1];
$result->publicationTitle = 'The Daily Star';
}
if (isset($result->extra)) { // [extra] => DOI: 10.1038/546031a has been seen in the wild
if (preg_match('~\sdoi:\s?([^\s]+)\s~i', ' ' . $result->extra . ' ', $matches)) {
if (!isset($result->DOI)) {
$result->DOI = trim($matches[1]);
}
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
}
if (preg_match('~\stype:\s?([^\s]+)\s~i', ' ' . $result->extra . ' ', $matches)) { // [extra] => type: dataset has been seen in the wild
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
}
if (preg_match('~\sPMID: (\d+)\s+PMCID: PMC(\d+)\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
$template->add_if_new('pmid', $matches[1]);
$template->add_if_new('pmc', $matches[2]);
}
if (preg_match('~\sPMID: (\d+), (\d+)\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
if ($matches[1] === $matches[2]) {
$template->add_if_new('pmid', $matches[1]);
}
}
if (preg_match('~\sPMID: (\d+)\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
$template->add_if_new('pmid', $matches[1]);
}
if (preg_match('~\sOCLC: (?:|ocn|ocm)(\d+)\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
$template->add_if_new('oclc', $matches[1]);
}
if (preg_match('~\sOpen Library ID: OL(\d+M)\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
$template->add_if_new('ol', $matches[1]);
}
// UNUSED stuff goes below
if (preg_match('~\sFormat: PDF\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
}
if (preg_match('~\sIMDb ID: ((?:tt|co|nm)\d+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
}
if (preg_match('~\s(original-date: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Google-Books-ID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(ISSN: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Page Version ID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Citation Key: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(number-of-pages: [ivx]+, \d+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(number-of-pages: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Version: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(RSLID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(QID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(National Archives Identifier: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Catalog Number: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(BMCR ID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(PubAg AGID: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(IP-\d+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(Accession Number: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\sADS Bibcode: (\d{4}\S{15})\s~i', ' ' . $result->extra . ' ', $matches)) {
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra));
$template->add_if_new('bibcode', $matches[1]);
}
if (preg_match('~\s(arXiv: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it - only comes from arXiv DOIs
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(INIS Reference Number: \d+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it - https://inis.iaea.org
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(ERIC Number: \S+)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~\s(\d+ cm\.)\s~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it - size of book
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
// These go at end since it is unbound on end often with linefeeds and such
if (preg_match('~submitted:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~event\-location:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it and it is long verbose
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Translated title:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~reviewed\-title:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Physical Description:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~BBK:[\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Place Manufactured: [\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Dimensions: [\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Category: [\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Credit: [\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Manufacturer: [\s\S]*$~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~pl., cartes, errata.+~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Post URL:.+~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~Reference Number:.+~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
if (preg_match('~jurisdiction:.+~i', ' ' . $result->extra . ' ', $matches)) { // We don't use it
$result->extra = trim(str_replace(trim($matches[0]), '', $result->extra)); // @codeCoverageIgnore
}
$result->extra = trim($result->extra);
if ($result->extra !== '') {
// TODO - check back later on report_minor_error("Unhandled extra data: " . echoable($result->extra) . ' FROM ' . echoable($url)); // @codeCoverageIgnore
}
}
if (isset($result->DOI) && $template->blank('doi')) {
if (preg_match('~^(?:https://|http://|)(?:dx\.|)doi\.org/(.+)$~i', $result->DOI, $matches)) {
$result->DOI = $matches[1];
}
$possible_doi = sanitize_doi($result->DOI);
if (doi_works($possible_doi)) {
$template->add_if_new('doi', $possible_doi);
expand_by_doi($template);
if (stripos($url, 'jstor')) {
check_doi_for_jstor($template->get('doi'), $template);
}
if (!$template->profoundly_incomplete()) {
return;
}
}
}
if (isset($result->date)) {
foreach (NO_DATE_WEBSITES as $bad_website ) {
if (stripos($url, $bad_website) !== false) {
unset($result->date);
break;
}
}
}
if (isset($result->ISBN)) {
$template->add_if_new('isbn', $result->ISBN);
}
if ($access_date && isset($result->date)) {
$new_date = strtotime(tidy_date((string) $result->date)); // One time got an integer
if ($new_date) { // can compare
if ($new_date > $access_date) {
report_info("URL appears to have changed since access-date " . echoable($url));
return;
}
}
}
if (str_i_same(substr((string) @$result->publicationTitle, 0, 4), 'http') ||
str_i_same(substr((string) @$result->bookTitle, 0, 4), 'http') ||
str_i_same(substr((string) @$result->title, 0, 4), 'http')) {
report_info("URL returned in Journal/Newpaper/Title/Chapter field for " . echoable($url)); // @codeCoverageIgnore
return; // @codeCoverageIgnore
}
if (isset($result->bookTitle)) {
$result->bookTitle = safe_preg_replace('~\s*\(pdf\)$~i', '', $result->bookTitle);
$result->bookTitle = safe_preg_replace('~^\(pdf\)\s*~i', '', $result->bookTitle);
$result->bookTitle = safe_preg_replace('~ \- ProQuest\.?~i', '', $result->bookTitle);
}
if (isset($result->title)) {
$result->title = safe_preg_replace('~\s*\(pdf\)$~i', '', (string) $result->title);
$result->title = safe_preg_replace('~^\(pdf\)\s*~i', '', $result->title);
$result->title = safe_preg_replace('~ \- ProQuest\.?~i', '', $result->title);
}
if (strpos($url, 'biodiversitylibrary.org') !== false) {
unset($result->publisher); // Not reliably set
}
if (isset($result->title) && $result->title === 'Cultural Advice' && strpos($url, 'edu.au') !== false) {
unset($result->title); // A warning, not a title
}
if ($template->has('title')) {
if (isset($result->title) && titles_are_similar($template->get('title'), (string) $result->title)) {
unset($result->title);
}
}
if ($template->has('chapter')) {
if (isset($result->title) && titles_are_similar($template->get('chapter'), (string) $result->title)) {
unset($result->title);
}
}
if (isset($result->bookTitle)) {
$template->add_if_new('title', (string) $result->bookTitle);
if (isset($result->title)) {
$template->add_if_new('chapter', (string) $result->title);
}
if (isset($result->publisher)) {
$template->add_if_new('publisher', (string) $result->publisher);
}
} else {
if (isset($result->title)){
$template->add_if_new('title', (string) $result->title);
}
if (isset($result->itemType) && ($result->itemType === 'book' || $result->itemType === 'bookSection')) {
if (isset($result->publisher)) {
$template->add_if_new('publisher', (string) $result->publisher);
}
}
}
if (isset($result->issue)) {
$template->add_if_new('issue', self::clean_volume((string) $result->issue));
}
if (isset($result->pages)) {
$pos_pages = (string) $result->pages;
if (preg_match('~\d~', $pos_pages) && !preg_match('~\d+\.\d+.\d+~', $pos_pages)) { // At least one number but not a dotted number from medRxiv
$pos_pages = str_ireplace(['σελ.', 'σελ ', 'pages ', 'page ', 'pages:', 'page:', 'pages', 'page'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], $pos_pages);
$pos_pages = trim($pos_pages);
$pos_pages = str_ireplace([' ', ' ', ' '], [' ', ' ', ' '], $pos_pages);
$template->add_if_new('pages', $pos_pages);
}
}
if (isset($result->itemType) && $result->itemType === 'newspaperArticle') {
if (isset($result->publicationTitle)) {
$new_title = (string) $result->publicationTitle;
if (in_array(strtolower($new_title), WORKS_ARE_PUBLISHERS, true)) {
$template->add_if_new('publisher', $new_title);
} elseif ($template->blank(WORK_ALIASES)) {
$template->add_if_new('work', $new_title);
} else {
$use_it = false;
foreach (WORK_ALIASES as $work_type) {
$test_it = substr($template->get($work_type), -4);
if (str_i_same($test_it, '.com') || str_i_same($test_it, '.org') || str_i_same($test_it, '.net')) {
$use_it = true;
}
}
if ($use_it) {
$template->add_if_new('work', $new_title);
}
}
}
} else {
if (isset($result->publicationTitle)) {
if ((!$template->has('title') || !$template->has('chapter')) && // Do not add if already has title and chapter
(stripos((string) $result->publicationTitle, ' edition') === false)) { // Do not add if "journal" includes "edition"
if (str_replace(NON_JOURNALS, '', (string) $result->publicationTitle) === (string) $result->publicationTitle) {
if (str_ireplace(NON_JOURNAL_WEBSITES, '', $url) === $url || $template->wikiname() === 'cite journal') {
if (str_ireplace(CANONICAL_PUBLISHER_URLS, '', $url) === $url && str_ireplace(JOURNAL_ARCHIVES_SITES, '', $url) === $url) {
if ($url !== '' && $url !== 'X' && str_ireplace(['digitalcommons', 'repository', 'scholarship', 'digitalcollection', 'dialnet.', 'handle.net', '.library.', 'dx.doi.org'], '', $url) === $url && str_ireplace(NON_JOURNAL_WEBSITES, '', $url) === $url) { // '' and 'X" are only in test suite
bot_debug_log('Possible journal URL: ' . $url);
}
$template->add_if_new('work', (string) $result->publicationTitle);
} else {
$template->add_if_new('journal', (string) $result->publicationTitle);
}
} else {
$template->add_if_new('work', (string) $result->publicationTitle);
}
}
}
}
}
if (isset($result->volume)) {
$template->add_if_new('volume', self::clean_volume((string) $result->volume));
}
if (isset($result->date) && strlen((string) $result->date)>3) {
$new_date = tidy_date((string) $result->date);
if (stripos($url, 'indiatimes') !== false) { // "re-posted" website all at once
$maybe_date = (int) strtotime($new_date);
$end_date1 = strtotime('10 January 2017');
$end_date2 = strtotime('21 January 2017');
if ($maybe_date > $end_date1 && $maybe_date < $end_date2) {
$new_date = '';
}
}
if ($new_date) {
$template->add_if_new('date', $new_date);
}
}
if (isset($result->series) && stripos($url, '.acm.org')===false) {
$template->add_if_new('series', (string) $result->series);
}
$i = 0;
while (isset($result->author[$i])) {