/
user_messages.php
115 lines (103 loc) · 4.65 KB
/
user_messages.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
<?php
declare(strict_types=1);
require_once 'constants.php'; // @codeCoverageIgnore
function html_echo(string $text, string $alternate_text=''): void {
if (!TRAVIS || defined("TRAVIS_PRINT")) {
echo HTML_OUTPUT ? $text : $alternate_text; // @codeCoverageIgnore
}
}
function user_notice(string $symbol, string $class, string $text): void {
if (!TRAVIS || defined("TRAVIS_PRINT")) {
// @codeCoverageIgnoreStart
if (defined('BIG_JOB_MODE') && in_array($class, ["boring", "removed", "added", "changed", "subsubitem", "subitem"], true)) {
echo '.'; // Echo something to keep the code alive, but not so much to overfill the cache
return;
}
// These are split over three lines to avoid creating a single long string during error conditions - which could blow out the memory
echo "\n " . (HTML_OUTPUT ? "<span class='$class'>" : "") . $symbol;
if (defined('BIG_JOB_MODE') && strlen($text) > 1000) { // No one looks at this anyway - long ones are often URLs in zotero errors
echo "HUGE amount of text NOT printed";
bot_debug_log("HUGE amount of text NOT printed. Here is a bit: " . substr($text, 0, 500));
} else {
echo $text;
}
echo HTML_OUTPUT ? "</span>" : "";
// @codeCoverageIgnoreEnd
}
}
function report_phase(string $text): void { user_notice("\n>", "phase", $text); }
function report_action(string $text): void { user_notice(">", "subitem", $text); }
function report_info(string $text): void { user_notice(" >", "subsubitem", $text); }
function report_inaction(string $text): void { user_notice(" .", "boring", $text); }
function report_warning(string $text): void { user_notice(" !", "warning", $text); }
function report_modification(string $text): void { user_notice(" ~", "changed", $text); }
function report_add(string $text): void { user_notice(" +", "added", $text); }
function report_forget(string $text): void { user_notice(" -", "removed", $text); }
function report_inline(string $text): void {
if ((!TRAVIS && defined('BIG_JOB_MODE')) || defined("TRAVIS_PRINT")) {
echo " $text";
}
}
// call report_warning to give users a message before we die
function report_error(string $text): never {
// @codeCoverageIgnoreStart
if (TRAVIS) {
trigger_error($text); // Stop this test now
} elseif (function_exists('bot_debug_log')) {
bot_debug_log($text); // Code logfile, if defined
report_warning($text); // To the user
} else {
report_warning($text); // To the user
trigger_error($text); // System Logfile
}
exit; // @codeCoverageIgnoreEnd
}
function report_minor_error(string $text): void { // For things we want to error in tests, but continue on Wikipedia
// @codeCoverageIgnoreStart
if (!HTML_OUTPUT) { // command line and TRAVIS
report_error($text);
} else {
bot_debug_log($text);
report_warning($text);
}
// @codeCoverageIgnoreEnd
}
function quietly(callable $function, string $text): void { // Stuff suppressed when running on the command line
if (HTML_OUTPUT || TRAVIS) {
$function($text);
}
}
// special flags to mark this function as making all untrustworthy input magically safe to output
function echoable(?string $string): string {
/**
* @psalm-taint-escape html
* @psalm-taint-escape has_quotes
*/
$string = (string) $string;
return HTML_OUTPUT ? htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401) : $string;
}
function pubmed_link(string $identifier, string $pm): string {
return HTML_OUTPUT
? '<a href="https://www.ncbi.nlm.nih.gov/pubmed/' . urlencode($pm) . '" target="_blank">' . strtoupper($identifier) . ' ' . echoable($pm) . "</a>" // @codeCoverageIgnore
: strtoupper($identifier) . ' ' . echoable($pm);
}
function bibcode_link(string $id): string {
return HTML_OUTPUT
? '<a href="https://ui.adsabs.harvard.edu/abs/' . urlencode($id) . '" target="_blank">' . echoable($id) . '</a>' // @codeCoverageIgnore
: echoable($id);
}
function doi_link(string $doi): string {
return HTML_OUTPUT
? '<a href="https://dx.doi.org/' . doi_encode(urldecode($doi)) . '" target="_blank">' . echoable($doi) . '</a>' // @codeCoverageIgnore
: echoable($doi);
}
function jstor_link(string $id): string {
return HTML_OUTPUT
? '<a href="https://www.jstor.org/citation/ris/' . urlencode($id) . '" target="_blank">JSTOR ' . echoable($id) . '</a>' // @codeCoverageIgnore
: "JSTOR " . echoable($id);
}
function wiki_link(string $page): string {
return HTML_OUTPUT
? '<a href="' . WIKI_ROOT . '?title=' . urlencode(str_replace(' ', '_', $page)) . '" target="_blank">Wikipedia page: ' . echoable($page) . '</a>' // @codeCoverageIgnore
: "Wikipedia page : " . echoable($page);
}