-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql_queries.php
More file actions
224 lines (196 loc) · 7.14 KB
/
Copy pathsql_queries.php
File metadata and controls
224 lines (196 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Query layer — every value is BOUND, never concatenated into SQL.
*
* The old version built query strings with "..." . $var . "...". Because
* poem.php, poet.php and year.php pass $_GET straight in, a visitor could end
* their value with a quote and have the rest read as SQL. See the git history
* of this file.
*
* Each function now takes the mysqli handle, runs a prepared statement, and
* returns plain arrays. Call sites use foreach instead of ->fetch_assoc()
* loops.
*
* $genre is 'poem' or 'story' and comes from section.php, which is how the
* poetry site and the prose site at /mer-vi share one implementation.
*/
// ---------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------
function db_all(mysqli $db, string $sql, string $types = '', ...$args): array
{
$st = $db->prepare($sql);
if ($st === false) {
error_log('prepare failed: ' . $db->error . ' :: ' . $sql);
return [];
}
if ($types !== '') {
$st->bind_param($types, ...$args);
}
$st->execute();
$res = $st->get_result();
$rows = $res ? $res->fetch_all(MYSQLI_ASSOC) : [];
$st->close();
return $rows;
}
function db_row(mysqli $db, string $sql, string $types = '', ...$args): ?array
{
$rows = db_all($db, $sql, $types, ...$args);
return $rows[0] ?? null;
}
function db_count(mysqli $db, string $sql, string $types = '', ...$args): int
{
return count(db_all($db, $sql, $types, ...$args));
}
// ---------------------------------------------------------------------
// poem.php
// ---------------------------------------------------------------------
/** A work plus every translation of it. */
function poem_sql(mysqli $db, string $poem, string $genre): array
{
return db_all($db,
"SELECT *, poem.poet AS ogpoet, poem.source AS ogsource,
trans.source AS tsource, trans.poet AS tpoet,
trans.translator AS ttranslator
FROM poem LEFT JOIN trans ON poem.poem = trans.poem
WHERE poem.poem = ? AND poem.genre = ?
ORDER BY trans.lang = 'eng' DESC",
'ss', $poem, $genre);
}
function poet_sql(mysqli $db, string $poet_eng): ?array
{
return db_row($db, "SELECT * FROM poet WHERE name_e = ?", 's', $poet_eng);
}
function biolinks_sql(mysqli $db, string $poet_eng): array
{
return db_all($db, "SELECT * FROM bio_links WHERE poet = ?", 's', $poet_eng);
}
function poemlinks_sql(mysqli $db, string $poem): array
{
return db_all($db, "SELECT * FROM poem_links WHERE poem = ? AND type = 'poem'",
's', $poem);
}
function contextlinks_sql(mysqli $db, string $poem): array
{
return db_all($db, "SELECT * FROM poem_links WHERE poem = ? AND type = 'context'",
's', $poem);
}
// ---------------------------------------------------------------------
// index.php
// ---------------------------------------------------------------------
function poem_list_yid(mysqli $db, string $genre): array
{
return db_all($db,
"SELECT title_y, poet, img, poem FROM poem
WHERE public IS TRUE AND genre = ? ORDER BY title_y",
's', $genre);
}
function poem_list_eng(mysqli $db, string $genre): array
{
return db_all($db,
"SELECT *, poem.poet AS poet_e
FROM poem LEFT JOIN trans ON poem.poem = trans.poem
WHERE genre = ? AND public IS TRUE AND trans.lang = 'eng'
ORDER BY title",
's', $genre);
}
function poet_yid_sql(mysqli $db, string $poet_eng): ?string
{
$r = db_row($db, "SELECT name_y FROM poet WHERE name_e = ?", 's', $poet_eng);
return $r['name_y'] ?? null;
}
function poet_list_yid(mysqli $db): array
{
return db_all($db, "SELECT name_y, name_e, img FROM poet ORDER BY name_y");
}
function poet_list_eng(mysqli $db): array
{
return db_all($db, "SELECT name_y, name_e, img FROM poet ORDER BY name_e");
}
function count_poems(mysqli $db, string $poet_eng, string $genre): int
{
return db_count($db,
"SELECT poem FROM poem WHERE poet = ? AND public IS TRUE AND genre = ?",
'ss', $poet_eng, $genre);
}
function poem_year_list(mysqli $db, string $genre): array
{
return db_all($db,
"SELECT DISTINCT YEAR(date) AS y FROM poem
WHERE public IS TRUE AND genre = ? ORDER BY date",
's', $genre);
}
// ---------------------------------------------------------------------
// year.php
// ---------------------------------------------------------------------
function poems_by_year(mysqli $db, int $year, string $genre): array
{
return db_all($db,
"SELECT * FROM poem
WHERE YEAR(date) = ? AND public IS TRUE AND genre = ?
ORDER BY title_y",
'is', $year, $genre);
}
function poem_year_eng(mysqli $db, int $year, string $genre): array
{
return db_all($db,
"SELECT trans.title, poem.poet, poem.poem, poem.img
FROM poem LEFT JOIN trans ON poem.poem = trans.poem
WHERE YEAR(date) = ? AND genre = ? AND public IS TRUE
AND trans.lang = 'eng'
ORDER BY title",
'is', $year, $genre);
}
// ---------------------------------------------------------------------
// poet.php
// ---------------------------------------------------------------------
function poet_check(mysqli $db, string $poet, string $genre): int
{
return db_count($db,
"SELECT poem FROM poem WHERE poet = ? AND public IS TRUE AND genre = ?",
'ss', $poet, $genre);
}
function poet_poem_yid_list(mysqli $db, string $poet, string $genre): array
{
return db_all($db,
"SELECT title_y, poet, img, poem FROM poem
WHERE poet = ? AND public IS TRUE AND genre = ?
ORDER BY title_y",
'ss', $poet, $genre);
}
function poet_poem_eng_list(mysqli $db, string $poet, string $genre): array
{
return db_all($db,
"SELECT *, poem.poet AS poet_e
FROM poem LEFT JOIN trans ON poem.poem = trans.poem
WHERE genre = ? AND public IS TRUE AND trans.lang = 'eng'
AND poem.poet = ?
ORDER BY title",
'ss', $genre, $poet);
}
// ---------------------------------------------------------------------
// yud_browse.php (Broderzon's Yud sequence)
// ---------------------------------------------------------------------
/**
* NOTE: the old $yud_eng filtered on `title_e`, which is not a column on any
* table — poem has title_y, trans has title. MySQL would have rejected the
* query outright, so the English half of this page has been failing. Changed
* to trans.title, which is plainly what was meant. Worth eyeballing the page.
*/
function yud_eng(mysqli $db): array
{
return db_all($db,
"SELECT trans.title, poem.poet, poem.poem, poem.img
FROM poem LEFT JOIN trans ON poem.poem = trans.poem
WHERE poem.genre = 'poem' AND public IS TRUE AND trans.lang = 'eng'
AND poem.poet = 'Moyshe Broderzon' AND trans.title LIKE '%Yud%'
ORDER BY trans.title");
}
function yud_yid(mysqli $db): array
{
return db_all($db,
"SELECT title_y, poet, img, poem FROM poem
WHERE poet = 'Moyshe Broderzon' AND title_y LIKE '%י [%'
AND public IS TRUE
ORDER BY title_y");
}