Compare commits

...

7 Commits

7 changed files with 155 additions and 25 deletions

19
copy-config.sh Normal file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
# Splits council_names.json into separate config.json files for the spl-data repo.
# This script uses the jq command, make sure it is installed before running this script.
data_path="$1"
if test -d "$data_path"; then
jq -c '.[] | .' ./council_names.json | while IFS=' ' read -r council_block; do
slug=$(echo "$council_block" | jq -r '.slug')
if ! test -d "$data_path"/"$slug"; then
mkdir "$data_path"/"$slug"
fi
echo "$council_block" | jq > "$data_path"/"$slug"/config.json
done
else
echo "Could not find $data_path"
fi

View File

@@ -0,0 +1,30 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/php
{
"name": "PHP",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/php:1-8.3",
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
// "customizations": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [8000],
// Use 'portsAttributes' to set default properties for specific forwarded ports. More info: https://code.visualstudio.com/docs/remote/devcontainerjson-reference.
"portsAttributes": {
"8000": {
"label": "Hello Remote World",
"onAutoForward": "notify"
}
}
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "sudo chmod a+x \"$(pwd)\" && sudo rm -rf /var/www/html && sudo ln -s \"$(pwd)\" /var/www/html"
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}

14
csv-normaliser/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch application",
"type": "php",
"request": "launch",
"program": "${workspaceFolder}/main.php",
"args": [],
"cwd": "${workspaceFolder}",
"port": 9000
}
]
}

69
csv-normaliser/main.php Normal file
View File

@@ -0,0 +1,69 @@
<?php
require_once "page_renderer.php";
$options = getopt("", ["council-file:", "candidates-file:", "media-file:"]);
if (isset($options['council-file'])) {
$councilFileContents = file_get_contents($options['council-file']);
} else {
error_log("Error: Missing required option '--council-file'.");
exit(1);
}
$councilData = json_decode($councilFileContents, true);
// Check for decoding errors
if (json_last_error() !== JSON_ERROR_NONE) {
error_log('Error decoding council file: ' . json_last_error_msg());
exit(1);
}
if (isset($options['candidates-file'])) {
$candidatesFile = $options['candidates-file'];
} else {
error_log("Error: Missing required option '--candidates-file'.");
exit(1);
}
// Convert CSV into an array of dictionaries. Use the header as the key in the dictionary.
$candidateData = [];
if (($handle = fopen($candidatesFile, "r")) !== FALSE) {
$headers = fgetcsv($handle);
while (($data = fgetcsv($handle)) !== FALSE) {
$candidate = [];
foreach ($headers as $key => $value) {
$candidate[$value] = $data[$key];
}
$candidateData[] = $candidate;
}
fclose($handle);
} else {
error_log('Error opening candidates file');
exit(1);
}
$candidateData = array_filter($candidateData, function ($candidate) use ($councilData) {
return isset($candidate["Council"]) && $candidate["Council"] === $councilData['shortName'];
});
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, $mediaData);
if ($pageContent === null) {
exit(2);
}
echo $pageContent;
exit(0);

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

@@ -1,3 +0,0 @@
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

@@ -2,17 +2,9 @@
# This script uses the jq, wp, and php commands, make sure they are installed before running this script. # This script uses the jq, wp, and php commands, make sure they are installed before running this script.
# The council_names.json file must contain a list of objects (one for each council).
# Each object must have the following fields: "shortName", "slug", "councilName", and "wardNames"
# The "shortName" field must be a string.
# The "slug" field must be a string.
# The "councilName" field must be a string.
# The "wardNames" field must be a list of strings.
COUNCILS_FILE="council_names.json"
# The folder containing data for each council. # The folder containing data for each council.
# Includes the list of candidates and any media. # Includes the list of candidates and any media.
DATA_PATH="data" DATA_PATH="../spl-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"
@@ -25,7 +17,19 @@ function create_or_update_page() {
slug=$(echo "$council_block" | jq -r '.slug') slug=$(echo "$council_block" | jq -r '.slug')
jq -n '[inputs | { (input_filename | sub("\\.json$"; "") | sub("^.+/"; "")): . }] | reduce .[] as $item ({}; . + $item)' "$DATA_PATH"/$slug/*.json > "$DATA_PATH"/$slug/media.json media_inputs=()
for file in "$DATA_PATH"/$slug/*.{jpeg,jpg,png,gif}.json; do
if test -f "$file"; then
media_inputs+=("$file")
fi
done
for file in "$DATA_PATH"/*.{jpeg,jpg,png,gif}.json; do
if test -f "$file"; then
media_inputs+=("$file")
fi
done
jq -n '[inputs | { (input_filename | sub("\\.json$"; "") | sub("^.+/"; "")): . }] | reduce .[] as $item ({}; . + $item)' "${media_inputs[@]}" > "$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 ) 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 )
@@ -46,19 +50,19 @@ function create_or_update_page() {
rm "$DATA_PATH"/$slug/media.json rm "$DATA_PATH"/$slug/media.json
} }
# Read council data
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)
selected_council="$1" selected_council="$1"
# Iterate over JSON objects # Iterate over folders in data path
jq -c '.[] | .' <<< "$data" | while IFS=' ' read -r council_block; do for folder in "$DATA_PATH"/*; do
short_name=$(echo "$council_block" | jq -r '.shortName') if test -f "$folder"/config.json; then
page_id=$(echo $wp_posts | jq '.[] | select(.post_title == "'"$short_name"'") | .ID' | head -n 1) council_block=$(cat "$folder"/config.json | jq -c)
if [ ! "$selected_council" ] || [ "$short_name" = "$selected_council" ]; then short_name=$(echo "$council_block" | jq -r '.shortName')
create_or_update_page "$council_block" "$page_id" page_id=$(echo $wp_posts | jq '.[] | select(.post_title == "'"$short_name"'") | .ID' | head -n 1)
if [ ! "$selected_council" ] || [ "$short_name" = "$selected_council" ]; then
create_or_update_page "$council_block" "$page_id"
fi
fi fi
done done