From 90a0fee735d961124cec1ff5a9da397303ebb28d Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Sun, 8 Sep 2024 17:49:31 +1000 Subject: [PATCH 1/6] Script to update pledge test page from data in spl-data repo. --- update-pledges.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 update-pledges.sh diff --git a/update-pledges.sh b/update-pledges.sh new file mode 100644 index 0000000..1942745 --- /dev/null +++ b/update-pledges.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +#wp post list --post_type=page +#wp post get 426 --field=content > current-homepage +#wp post get 1409 --field=content > movie-homepage +#wp post create --post_type=page --post_title="test_pledge" movie-homepage + +wp post update 1803 ../spl-data/movie-homepage From ca0bab50719dd4d1c9a566d9f6af08b2a1c54e8e Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Sun, 8 Sep 2024 19:26:09 +1000 Subject: [PATCH 2/6] Collect data for pledge rotation. --- .gitignore | 1 + pledge-update/main.php | 50 ++++++++++++++++++++++++++++++++++++++++++ update-pledges.sh | 16 +++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 pledge-update/main.php mode change 100644 => 100755 update-pledges.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/pledge-update/main.php b/pledge-update/main.php new file mode 100644 index 0000000..ee009e7 --- /dev/null +++ b/pledge-update/main.php @@ -0,0 +1,50 @@ + $file) { + $config_file = dirname($file)."/config.json"; + $config_string = file_get_contents($config_file); + + if ($config_string !== FALSE) { + $config = json_decode($config_string, true); + } else { + error_log('Error opening config.json'); + exit(1); + } + + if (($handle = fopen($file, "r")) !== FALSE) { + $headers = fgetcsv($handle); + while (($data = fgetcsv($handle)) !== FALSE) { + $candidate = []; + $candidate['Pledge'] = 'n'; + foreach ($headers as $key => $value) { + $candidate[$value] = $data[$key]; + } + $candidate['Council'] = $config['councilName']; + $candidateData[] = $candidate; + } + fclose($handle); + } else { + error_log('Error opening candidates file'); + exit(1); + } +} + +$pledgeCandidates = array_filter($candidateData, function ($candidate) { + return $candidate['Pledge'] === 'y'; +}); + +print_r($pledgeCandidates); + +exit(0); diff --git a/update-pledges.sh b/update-pledges.sh old mode 100644 new mode 100755 index 1942745..6ccf4b4 --- a/update-pledges.sh +++ b/update-pledges.sh @@ -1,8 +1,22 @@ #!/bin/bash +# If this runs as a cron job - might want to limit the number of revisions wordpress stores: +# wp-config.php: +# define( 'WP_POST_REVISIONS', 3 ); + #wp post list --post_type=page #wp post get 426 --field=content > current-homepage #wp post get 1409 --field=content > movie-homepage #wp post create --post_type=page --post_title="test_pledge" movie-homepage +#wp post update 1803 ../spl-data/movie-homepage -wp post update 1803 ../spl-data/movie-homepage +DATA_PATH="../spl-data" + +candidates_files=() +for folder in "$DATA_PATH"/*; do + if test -f "$folder"/candidates.csv; then + candidates_files+=("$folder"/candidates.csv) + fi +done + +php pledge-update/main.php --candidates-files "${candidates_files[*]}" From 18910638a1bd3d08fa597267e4825fcbf9f17af2 Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Sun, 8 Sep 2024 22:25:14 +1000 Subject: [PATCH 3/6] Select 9 random candidates for pledge area and generate sed commands. --- pledge-update/main.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pledge-update/main.php b/pledge-update/main.php index ee009e7..c434e13 100644 --- a/pledge-update/main.php +++ b/pledge-update/main.php @@ -19,7 +19,7 @@ foreach ($files as $key => $file) { if ($config_string !== FALSE) { $config = json_decode($config_string, true); } else { - error_log('Error opening config.json'); + error_log("Error opening config.json."); exit(1); } @@ -32,6 +32,7 @@ foreach ($files as $key => $file) { $candidate[$value] = $data[$key]; } $candidate['Council'] = $config['councilName']; + $candidate['Path'] = dirname($file); $candidateData[] = $candidate; } fclose($handle); @@ -41,10 +42,39 @@ foreach ($files as $key => $file) { } } +/* Select people who have taken the pledge */ $pledgeCandidates = array_filter($candidateData, function ($candidate) { return $candidate['Pledge'] === 'y'; }); -print_r($pledgeCandidates); +/* Select 9 random candidates */ +$pledgeKeys = array_rand($pledgeCandidates, 9); +shuffle($pledgeKeys); + +$i = 0; +foreach ($pledgeKeys as $key) { + $media_desc = $pledgeCandidates[$key]['Path']."/". + $pledgeCandidates[$key]['Picture'].".json"; + $media_string = file_get_contents($media_desc); + + if ($media_string !== FALSE) { + $media = json_decode($media_string, true); + } else { + error_log("Error opening image descriptor."); + exit(1); + } + + $image_url = $media['url']; + + echo "s|pledge_img_".$i."|".$image_url."|\n"; + + echo "s|pledge_string_".$i."|"; + echo $pledgeCandidates[$key]['Candidate Name']. + " (". + $pledgeCandidates[$key]['Council']. + ") has taken the pledge!|\n"; + + $i++; +} exit(0); From b0985b6d605717114c446a65cd0dd363d188480d Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Tue, 10 Sep 2024 23:26:00 +1000 Subject: [PATCH 4/6] Apply sed script. --- pledge-update/main.php | 2 ++ update-pledges.sh | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pledge-update/main.php b/pledge-update/main.php index c434e13..1b7c68c 100644 --- a/pledge-update/main.php +++ b/pledge-update/main.php @@ -65,8 +65,10 @@ foreach ($pledgeKeys as $key) { } $image_url = $media['url']; + $image_id = $media['id']; echo "s|pledge_img_".$i."|".$image_url."|\n"; + echo "s|pledge_id_".$i."|".$image_id."|\n"; echo "s|pledge_string_".$i."|"; echo $pledgeCandidates[$key]['Candidate Name']. diff --git a/update-pledges.sh b/update-pledges.sh index 6ccf4b4..e07bca2 100755 --- a/update-pledges.sh +++ b/update-pledges.sh @@ -19,4 +19,8 @@ for folder in "$DATA_PATH"/*; do fi done -php pledge-update/main.php --candidates-files "${candidates_files[*]}" +pledge_sed=$(php pledge-update/main.php --candidates-files "${candidates_files[*]}") + +content=$(sed "$pledge_sed" ../spl-data/movie-homepage) + +echo wp post update 1803 $content From 1ab14ba8c90ea991401b3697b10ff31fc3a25c5d Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Sat, 14 Sep 2024 14:40:52 +1000 Subject: [PATCH 5/6] Separate file for pledge data. --- pledge-update/main.php | 36 +++++++++++++++++++++++++++--------- update-pledges.sh | 11 ++++++----- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pledge-update/main.php b/pledge-update/main.php index 1b7c68c..594084c 100644 --- a/pledge-update/main.php +++ b/pledge-update/main.php @@ -1,17 +1,17 @@ $file) { $config_file = dirname($file)."/config.json"; $config_string = file_get_contents($config_file); @@ -23,7 +23,9 @@ foreach ($files as $key => $file) { exit(1); } - if (($handle = fopen($file, "r")) !== FALSE) { + $candidate_file = dirname($file)."/candidates.csv"; + + if (($handle = fopen($candidate_file, "r")) !== FALSE) { $headers = fgetcsv($handle); while (($data = fgetcsv($handle)) !== FALSE) { $candidate = []; @@ -33,17 +35,33 @@ foreach ($files as $key => $file) { } $candidate['Council'] = $config['councilName']; $candidate['Path'] = dirname($file); - $candidateData[] = $candidate; + $candidate_data[$candidate['Candidate Name']] = $candidate; } fclose($handle); } else { error_log('Error opening candidates file'); exit(1); } + + if (($handle = fopen($file, "r")) !== FALSE) { + $headers = fgetcsv($handle); + while (($data = fgetcsv($handle)) !== FALSE) { + $candidate = []; + foreach ($headers as $key => $value) { + $candidate[$value] = $data[$key]; + } + $candidate_data[$candidate['Candidate Name']]['Pledge'] = + $candidate['Pledge']; + } + fclose($handle); + } else { + error_log('Error opening pledges file'); + exit(1); + } } /* Select people who have taken the pledge */ -$pledgeCandidates = array_filter($candidateData, function ($candidate) { +$pledgeCandidates = array_filter($candidate_data, function ($candidate) { return $candidate['Pledge'] === 'y'; }); diff --git a/update-pledges.sh b/update-pledges.sh index e07bca2..a8db05a 100755 --- a/update-pledges.sh +++ b/update-pledges.sh @@ -1,6 +1,7 @@ #!/bin/bash -# If this runs as a cron job - might want to limit the number of revisions wordpress stores: +# If this runs as a cron job - might want to limit the number of revisions +# wordpress stores: # wp-config.php: # define( 'WP_POST_REVISIONS', 3 ); @@ -12,14 +13,14 @@ DATA_PATH="../spl-data" -candidates_files=() +pledges_files=() for folder in "$DATA_PATH"/*; do - if test -f "$folder"/candidates.csv; then - candidates_files+=("$folder"/candidates.csv) + if test -f "$folder"/pledges.csv; then + pledges_files+=("$folder"/pledges.csv) fi done -pledge_sed=$(php pledge-update/main.php --candidates-files "${candidates_files[*]}") +pledge_sed=$(php pledge-update/main.php --pledges-files "${pledges_files[*]}") content=$(sed "$pledge_sed" ../spl-data/movie-homepage) From b46e331a53eab56c3121fbb395411aa59da0699d Mon Sep 17 00:00:00 2001 From: Kim Taylor Date: Sat, 14 Sep 2024 15:41:13 +1000 Subject: [PATCH 6/6] Fix update command. --- update-pledges.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/update-pledges.sh b/update-pledges.sh index a8db05a..9283f8a 100755 --- a/update-pledges.sh +++ b/update-pledges.sh @@ -24,4 +24,4 @@ pledge_sed=$(php pledge-update/main.php --pledges-files "${pledges_files[*]}") content=$(sed "$pledge_sed" ../spl-data/movie-homepage) -echo wp post update 1803 $content +wp post update 1803 --post_content="$content"