Compare commits
6 Commits
f1aca43042
...
8a1983a0f3
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a1983a0f3 | |||
| 5257ca24dc | |||
| 397fdb1ede | |||
| e9a7de83cd | |||
| 4d9d633676 | |||
| 9d5ba8bd38 |
@@ -1,3 +0,0 @@
|
||||
Council,Ward,Candidate Name,Rating
|
||||
Brimbank,Harvester,Joe Blogs,1
|
||||
Brimbank,Harvester,Jane Doe,5
|
||||
|
3
data/greater-geelong/candidates.csv
Normal file
3
data/greater-geelong/candidates.csv
Normal file
@@ -0,0 +1,3 @@
|
||||
Council,Ward,Candidate Name,Rating
|
||||
Greater Geelong,Kardinia,Joe Blogs,1
|
||||
Greater Geelong,You Yangs,Jane Doe,5
|
||||
|
@@ -8,7 +8,11 @@
|
||||
# The "slug" field must be a string.
|
||||
# The "councilName" field must be a string.
|
||||
# The "wardNames" field must be a list of strings.
|
||||
JSON_FILE="council_names.json"
|
||||
COUNCILS_FILE="council_names.json"
|
||||
|
||||
# The folder containing data for each council.
|
||||
# Includes the list of candidates and any media.
|
||||
DATA_PATH="data"
|
||||
|
||||
# Controls the flags that are passed to every usage of the wp command.
|
||||
WP_FLAGS="--allow-root --path=/var/www/html"
|
||||
@@ -21,7 +25,9 @@ function create_or_update_page() {
|
||||
|
||||
slug=$(echo "$council_block" | jq -r '.slug')
|
||||
|
||||
content=$(echo "$council_block" | jq -c | php php-template/main.php --council-file "php://stdin" --candidates-file "candidates/$slug.csv")
|
||||
jq -n '[inputs | { (input_filename | sub("\\.json$"; "") | sub("^.+/"; "")): . }] | reduce .[] as $item ({}; . + $item)' "$DATA_PATH"/$slug/*.json > "$DATA_PATH"/$slug/media.json
|
||||
|
||||
content=$(echo "$council_block" | jq -c | php php-template/main.php --council-file "php://stdin" --candidates-file "$DATA_PATH"/$slug/candidates.csv --media-file "$DATA_PATH"/$slug/media.json )
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
|
||||
@@ -36,10 +42,12 @@ function create_or_update_page() {
|
||||
else
|
||||
echo "Failed to generate page content for $short_name"
|
||||
fi
|
||||
|
||||
rm "$DATA_PATH"/$slug/media.json
|
||||
}
|
||||
|
||||
# Read JSON data
|
||||
data=$(cat "$JSON_FILE")
|
||||
# Read council data
|
||||
data=$(cat "$COUNCILS_FILE")
|
||||
|
||||
# Get all page IDs in one go because the wp command is pretty slow
|
||||
wp_posts=$(wp post list --post_type=page --format=json $WP_FLAGS)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
require_once "page_renderer.php";
|
||||
|
||||
$options = getopt("", ["council-file:", "candidates-file:"]);
|
||||
$options = getopt("", ["council-file:", "candidates-file:", "media-file:"]);
|
||||
|
||||
if (isset($options['council-file'])) {
|
||||
$councilFileContents = file_get_contents($options['council-file']);
|
||||
@@ -50,8 +50,17 @@ if (empty($candidateData)) {
|
||||
error_log("Failed to load any candidates for " . $councilData['shortName']);
|
||||
}
|
||||
|
||||
if (isset($options['media-file'])) {
|
||||
$mediaFileContents = file_get_contents($options['media-file']);
|
||||
} else {
|
||||
error_log("Error: Missing required option '--media-file'.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$mediaData = json_decode($mediaFileContents, true);
|
||||
|
||||
$renderer = new SPLPageRenderer();
|
||||
$pageContent = $renderer->renderCouncilPage($councilData, $candidateData);
|
||||
$pageContent = $renderer->renderCouncilPage($councilData, $candidateData, $mediaData);
|
||||
if ($pageContent === null) {
|
||||
exit(2);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
<?php
|
||||
|
||||
class SPLPageRenderer {
|
||||
public function renderCouncilPage($config, $candidates) {
|
||||
public function renderCouncilPage($config, $candidates, $media) {
|
||||
ob_start();
|
||||
|
||||
set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
||||
// Clear any output if an error occurred
|
||||
ob_clean();
|
||||
$didError = false;
|
||||
|
||||
set_error_handler(function($errno, $errstr, $errfile, $errline) use(&$didError) {
|
||||
$didError = true;
|
||||
error_log("Error: $errstr in $errfile on line $errline");
|
||||
return true; // Prevent default error handling
|
||||
});
|
||||
@@ -17,8 +18,8 @@ class SPLPageRenderer {
|
||||
|
||||
$content = ob_get_clean();
|
||||
|
||||
// Explictly return null if we didn't generate any content
|
||||
if (!empty($content)) {
|
||||
// Explictly return null if we didn't generate any content or if there was an error
|
||||
if (!empty($content) && !$didError) {
|
||||
return $content;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
@@ -2,6 +2,12 @@
|
||||
<p><?php echo $config['councilName']; ?></p>
|
||||
<!-- /wp:paragraph -->
|
||||
|
||||
<?php if (isset($media["header.jpg"])): ?>
|
||||
<!-- wp:image {"id":<?php echo $media["header.jpg"]['id']; ?>,"aspectRatio":"16/9","scale":"cover","sizeSlug":"full","linkDestination":"none"} -->
|
||||
<figure class="wp-block-image size-full"><img src="<?php echo $media["header.jpg"]['url']; ?>" alt="" class="wp-image-<?php echo $media["header.jpg"]['id']; ?>" style="aspect-ratio:16/9;object-fit:cover"/></figure>
|
||||
<!-- /wp:image -->
|
||||
<?php endif ?>
|
||||
|
||||
<?php foreach ($config['wardNames'] as $index => $wardName): ?>
|
||||
<!-- wp:heading {"className":"is-style-default"} -->
|
||||
<h2 class="wp-block-heading is-style-default" id="<?php echo strtolower(str_replace(' ', '-', $wardName)); ?>"><?php echo $wardName; ?></h2>
|
||||
|
||||
26
upload-media.sh
Normal file
26
upload-media.sh
Normal file
@@ -0,0 +1,26 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Tries to upload media to wordpress and stores a json file with the ID and url of the media.
|
||||
|
||||
# This script uses the jq, and wp commands, make sure they are installed before running this script.
|
||||
|
||||
# Additionally, make sure the wp-cli/restful package is installed in the wp command (via "wp package install wp-cli/restful")
|
||||
|
||||
# Controls the flags that are passed to every usage of the wp command.
|
||||
WP_FLAGS="--allow-root --path=/var/www/html"
|
||||
|
||||
media_path="$1"
|
||||
|
||||
if test -f "$media_path"; then
|
||||
if test -f "$media_path.json"; then
|
||||
echo "Found $media_path.json, skipping uploading media."
|
||||
else
|
||||
echo "Could not find $media_path.json, uploading media!"
|
||||
id=$(wp media import "$media_path" --porcelain $WP_FLAGS)
|
||||
url=$(wp rest attachment get "$id" --field=source_url $WP_FLAGS)
|
||||
jq -n --arg id $id --arg url "$url" '{"id": $id, "url": $url}' > "$media_path.json"
|
||||
cat "$media_path.json"
|
||||
fi
|
||||
else
|
||||
echo "Could not find $media_path"
|
||||
fi
|
||||
Reference in New Issue
Block a user