mysociety /
theyworkforyou
| 1 | #!/usr/bin/php -q |
||
| 2 | <?php |
||
| 3 | |||
| 4 | /* |
||
| 5 | * Uses a CSV file to update the policies table with |
||
| 6 | * details of the images used for the hero banners on |
||
| 7 | * the divisions pages. |
||
| 8 | * |
||
| 9 | * Doesn't fetch the images but assumes they've been |
||
| 10 | * added in www/docs/images/ as $policy_id.jpg |
||
| 11 | * |
||
| 12 | * Take the path to the CSV file with the details |
||
| 13 | * Columns should be: |
||
| 14 | * 1: policy id |
||
| 15 | * 3: title |
||
| 16 | * 4: description |
||
| 17 | * 6: source: url of the original image |
||
| 18 | * 7: image attribution - name of photographer |
||
| 19 | * 8: license URL - url of the CC licence of photo |
||
| 20 | * |
||
| 21 | */ |
||
| 22 | |||
| 23 | include '../www/includes/easyparliament/init.php'; |
||
| 24 | include_once INCLUDESPATH . 'easyparliament/member.php'; |
||
| 25 | |||
| 26 | $ARGV = $_SERVER['argv']; |
||
| 27 | $db = new ParlDB(); |
||
| 28 | |||
| 29 | $csvfile = $ARGV[1] ?? ''; |
||
| 30 | |||
| 31 | if (!$csvfile) { |
||
| 32 | print "Need a csv file with policy details\n"; |
||
| 33 | exit(1); |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!file_exists($csvfile)) { |
||
| 37 | print "$csvfile cannot be found\n"; |
||
| 38 | exit(1); |
||
| 39 | } |
||
| 40 | |||
| 41 | $file = fopen($csvfile, 'r'); |
||
| 42 | |||
| 43 | if (!$file) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 44 | print "failed to open $csvfile\n"; |
||
| 45 | exit(1); |
||
| 46 | } |
||
| 47 | |||
| 48 | $count = 0; |
||
| 49 | while (($policy = fgetcsv($file)) !== false) { |
||
| 50 | if (intval($policy[0])) { |
||
| 51 | $policy_id = $policy[0]; |
||
| 52 | $img_id = $policy[1] ? $policy[1] : $policy_id; |
||
| 53 | $title = $policy[2]; |
||
| 54 | $description = $policy[3]; |
||
| 55 | $attribution = $policy[6]; |
||
| 56 | $licence_url = $policy[7]; |
||
| 57 | $source = $policy[5]; |
||
| 58 | |||
| 59 | $q = $db->query( |
||
| 60 | "UPDATE policies SET |
||
| 61 | image = :image, image_source = :image_source, image_attrib = :image_attribution, |
||
| 62 | image_license_url = :license_url, title = :title, description = :description WHERE |
||
| 63 | policy_id = :policy_id |
||
| 64 | ", |
||
| 65 | [ |
||
| 66 | ':policy_id' => $policy_id, |
||
| 67 | ':title' => $title, |
||
| 68 | ':description' => $description, |
||
| 69 | ':image' => "/images/policies/" . $img_id . ".jpg", |
||
| 70 | ':image_source' => $source, |
||
| 71 | ':image_attribution' => $attribution, |
||
| 72 | ':license_url' => $licence_url] |
||
| 73 | ); |
||
| 74 | if ($q->success()) { |
||
| 75 | $count += $q->affected_rows(); |
||
| 76 | } else { |
||
| 77 | print "failed to update data for $policy_id\n"; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | // this will only give a count where the update changed something |
||
| 83 | // so if we run an update and all the values are the same then it |
||
| 84 | // won't count in this total |
||
| 85 | print "Updated details for $count policies\n"; |
||
| 86 |