Compare commits

..

6 Commits

8 changed files with 65 additions and 15 deletions

View File

@@ -1,3 +0,0 @@
Council,Ward,Candidate Name,Rating
Brimbank,Harvester,Joe Blogs,1
Brimbank,Harvester,Jane Doe,5
1 Council Ward Candidate Name Rating
2 Brimbank Harvester Joe Blogs 1
3 Brimbank Harvester Jane Doe 5

View File

@@ -0,0 +1,3 @@
Council,Ward,Candidate Name,Rating
Greater Geelong,Kardinia,Joe Blogs,1
Greater Geelong,You Yangs,Jane Doe,5
1 Council Ward Candidate Name Rating
2 Greater Geelong Kardinia Joe Blogs 1
3 Greater Geelong You Yangs Jane Doe 5

View File

@@ -8,7 +8,11 @@
# The "slug" field must be a string. # The "slug" field must be a string.
# The "councilName" field must be a string. # The "councilName" field must be a string.
# The "wardNames" field must be a list of strings. # 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. # Controls the flags that are passed to every usage of the wp command.
WP_FLAGS="--allow-root --path=/var/www/html" WP_FLAGS="--allow-root --path=/var/www/html"
@@ -21,7 +25,9 @@ function create_or_update_page() {
slug=$(echo "$council_block" | jq -r '.slug') 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 if [ $? -eq 0 ]; then
@@ -36,10 +42,12 @@ function create_or_update_page() {
else else
echo "Failed to generate page content for $short_name" echo "Failed to generate page content for $short_name"
fi fi
rm "$DATA_PATH"/$slug/media.json
} }
# Read JSON data # Read council data
data=$(cat "$JSON_FILE") data=$(cat "$COUNCILS_FILE")
# Get all page IDs in one go because the wp command is pretty slow # 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) wp_posts=$(wp post list --post_type=page --format=json $WP_FLAGS)

View File

@@ -1,7 +1,7 @@
<?php <?php
require_once "page_renderer.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'])) { if (isset($options['council-file'])) {
$councilFileContents = file_get_contents($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']); 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(); $renderer = new SPLPageRenderer();
$pageContent = $renderer->renderCouncilPage($councilData, $candidateData); $pageContent = $renderer->renderCouncilPage($councilData, $candidateData, $mediaData);
if ($pageContent === null) { if ($pageContent === null) {
exit(2); exit(2);
} }

View File

@@ -1,12 +1,13 @@
<?php <?php
class SPLPageRenderer { class SPLPageRenderer {
public function renderCouncilPage($config, $candidates) { public function renderCouncilPage($config, $candidates, $media) {
ob_start(); ob_start();
set_error_handler(function($errno, $errstr, $errfile, $errline) { $didError = false;
// Clear any output if an error occurred
ob_clean(); set_error_handler(function($errno, $errstr, $errfile, $errline) use(&$didError) {
$didError = true;
error_log("Error: $errstr in $errfile on line $errline"); error_log("Error: $errstr in $errfile on line $errline");
return true; // Prevent default error handling return true; // Prevent default error handling
}); });
@@ -17,8 +18,8 @@ class SPLPageRenderer {
$content = ob_get_clean(); $content = ob_get_clean();
// Explictly return null if we didn't generate any content // Explictly return null if we didn't generate any content or if there was an error
if (!empty($content)) { if (!empty($content) && !$didError) {
return $content; return $content;
} else { } else {
return null; return null;

View File

@@ -2,6 +2,12 @@
<p><?php echo $config['councilName']; ?></p> <p><?php echo $config['councilName']; ?></p>
<!-- /wp:paragraph --> <!-- /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): ?> <?php foreach ($config['wardNames'] as $index => $wardName): ?>
<!-- wp:heading {"className":"is-style-default"} --> <!-- 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> <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
View 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