Update error messages, add mod.json mode

main
Pk11 2 years ago
parent 9e2bfd4319
commit b19ce3f726

@ -28,6 +28,8 @@
<dd>The maximum words to include. Default 100.</dd> <dd>The maximum words to include. Default 100.</dd>
<dt>include</dt> <dt>include</dt>
<dd>Columns to include, comma separated list. Default: id, solution, print_date, days_since_launch, editor. Additional: access_count. When only one column is included the json will be a flat array, no objects.</dd> <dd>Columns to include, comma separated list. Default: id, solution, print_date, days_since_launch, editor. Additional: access_count. When only one column is included the json will be a flat array, no objects.</dd>
<dt>mode</dt>
<dd>When set to 'mod.json', all other variables will be ignored and it will return a full <a href="/words.php?mode=mod.json">mod.json</a> file.</dd>
</dl> </dl>
<h2>Share</h2> <h2>Share</h2>

@ -72,7 +72,7 @@ function get_words($date, $limit, $include) {
if(pg_num_rows($res) == 0) { if(pg_num_rows($res) == 0) {
return [ return [
'status' => 'ERROR', 'status' => 'ERROR',
'message' => 'Date too late' 'message' => 'Already up to date'
]; ];
} }
@ -110,7 +110,7 @@ function add_words($datestamp) {
// Add word to database // Add word to database
$query = 'INSERT INTO words (id, solution, print_date, days_since_launch, editor) ' . $query = 'INSERT INTO words (id, solution, print_date, days_since_launch, editor) ' .
'VALUES ($1, $2, $3, $4, $5)'; 'VALUES ($1, UPPER($2), $3, $4, $5)';
$params = [ $params = [
$word['id'], $word['id'],
$word['solution'], $word['solution'],
@ -135,46 +135,54 @@ function add_words($datestamp) {
$dbc = pg_connect("host=$DB_HOST dbname=$DB_NAME user=$DB_USER password=$DB_PASSWORD") $dbc = pg_connect("host=$DB_HOST dbname=$DB_NAME user=$DB_USER password=$DB_PASSWORD")
or die('Could not connect: ' . pg_last_error()); or die('Could not connect: ' . pg_last_error());
// Check get vars // Check for words once a day
$date = $_GET['date']; $res = pg_query('SELECT EXTRACT(EPOCH FROM time) AS time FROM update') or die('Query failed: ' . pg_last_error());
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 100; $cur_time = time();
$include = isset($_GET['include']) ? $_GET['include'] : 'id,solution,print_date,days_since_launch,editor'; $last_time = pg_fetch_array($res)['time'];
if($cur_time > $last_time + (60 * 60 * 24)) {
add_words($cur_time);
if(!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) { $query = 'UPDATE update SET time=TO_TIMESTAMP($1)';
pg_query_params($query, [$cur_time]) or die('Query failed: ' . pg_last_error());
}
// Get words
if($_GET['mode'] == 'mod.json') {
header('Content-Disposition: attachment; filename="mod.json"');
echo json_encode([
'words' => [
'order' => get_words('2021-06-19', 100000, 'id')
]
]);
} else {
// Check get vars
$date = $_GET['date'];
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 100;
$include = isset($_GET['include']) ? $_GET['include'] : 'id,solution,print_date,days_since_launch,editor';
if(!preg_match('/^\d{4}-\d{2}-\d{2}$/', $date)) {
die(json_encode([ die(json_encode([
'status' => 'ERROR', 'status' => 'ERROR',
'message' => 'Invalid date format (YYYY-MM-DD)' 'message' => 'Invalid date format (YYYY-MM-DD)'
])); ]));
} else if($date < '2021-06-19') { } else if($date < '2021-06-19') {
die(json_encode([ die(json_encode([
'status' => 'ERROR', 'status' => 'ERROR',
'message' => 'Date too early' 'message' => 'Date too early'
])); ]));
} else if($limit <= 0) { } else if($limit <= 0) {
die(json_encode([ die(json_encode([
'status' => 'ERROR', 'status' => 'ERROR',
'message' => 'Invalid limit' 'message' => 'Invalid limit'
])); ]));
} else if(!preg_match('/^[a-z,_]+$/', $include)) { } else if(!preg_match('/^[a-z,_]+$/', $include)) {
die(json_encode([ die(json_encode([
'status' => 'ERROR', 'status' => 'ERROR',
'message' => 'Invalid include' 'message' => 'Invalid include'
])); ]));
} }
// Check for words once a day
$res = pg_query('SELECT EXTRACT(EPOCH FROM time) AS time FROM update') or die('Query failed: ' . pg_last_error());
$cur_time = time();
$last_time = pg_fetch_array($res)['time'];
if($cur_time > $last_time + (60 * 60 * 24)) {
add_words($cur_time);
$query = 'UPDATE update SET time=TO_TIMESTAMP($1)'; echo json_encode(get_words($date, $limit, $include));
pg_query_params($query, [$cur_time]) or die('Query failed: ' . pg_last_error());
} }
// Get words
echo json_encode(get_words($date, $limit, $include));
pg_close($dbc); pg_close($dbc);

Loading…
Cancel
Save