Guest User

Untitled

a guest
Jul 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. <?php
  2. // Original by: Philippe Dagenais-Pérusse
  3.  
  4. // Get the files to parse
  5. if ($argc) { // Command-line interface
  6. $files = array_slice($argv, 1);
  7. }
  8. else {
  9. $files = array();
  10. if ($handle = opendir($path)) {
  11. while (($file = readdir($handle)) !== FALSE) {
  12. if ($file != '.' && $file != '..' && strstr($file, '.pdf')) {
  13. $files[$file] = $_SERVER['DOCUMENT_ROOT'] . '/glaces/lib/files/' . $file;
  14. }
  15. }
  16. closedir($handle);
  17. }
  18. }
  19.  
  20. $headers = array(
  21. 1 => 'OO', // Ouverte: oui
  22. 2 => 'ON', // Ouverte: non
  23. 3 => 'DO', // Déblayée: oui
  24. 4 => 'DN', // Déblayée: non
  25. 5 => 'AO', // Arrosée: oui
  26. 6 => 'AN', // Arrosée: non
  27. 7 => 'CE', // Condition: excellente
  28. 8 => 'CB', // Condition: bonne
  29. 9 => 'CM', // Condition: mauvaise
  30. );
  31.  
  32. // The first text line on which a rink name appears
  33. $first = 13;
  34.  
  35. // If the status line is neither empty nor "X", parsing has failed
  36. function parsable($value) {
  37. return $value == '' || $value == 'X';
  38. }
  39.  
  40. $boroughs = array();
  41. foreach ($files as $basename => $file) {
  42. // Collect all text lines from the PDF
  43. $lines = array();
  44. foreach (file($file) as $line_no => $line) {
  45. if (preg_match('/Tj/', $line)) {
  46. $lines[] = trim(substr(str_replace(') Tj', '', stripslashes($line)), 1));
  47. }
  48. }
  49.  
  50. // Get the borough data
  51. $borough = array(
  52. 'Fichier' => $basename, // File
  53. 'MAJ-Fichier' => filemtime($file), // Mise à jour du fichier / File updated at
  54. 'Region' => $lines[8], // Arrondissement / Borough
  55. 'MAJ' => $lines[0], // Mise à jour / Updated at
  56. 'Remarques' => '', // Remarks
  57. 'Patinoires' => array(), // Rinks
  58. 'INTEGRITE' => 'Oui', // Parseable
  59. );
  60.  
  61. // Get the borough remarks
  62. $index = array_search($file == 'L29_79.pdf' ? 'Remarques' : 'Oui', $lines);
  63. for ($i = 1; $i <= 5; $i++) {
  64. if ($lines[$index - $i] == '' || $lines[$index - $i] == 'X' || is_numeric($lines[$index - $i])) break;
  65. $borough['Remarques'] = $lines[$index - $i] . ' ' . $borough['Remarques'];
  66. }
  67.  
  68. // Nombre total de patinoires / Total number of rinks
  69. $total = $lines[array_search('Entretenues par les citoyens', $lines) - 1];
  70.  
  71. // Get the rinks data
  72. for ($i = 0; $i < $total; $i++) {
  73. $index = array_search('Grand total', $lines);
  74. $rink = array(
  75. 'Nom' => $lines[$first + $i],
  76. 'RO' => $lines[$index + $i + 1], // Resurfacée: oui
  77. 'RN' => $lines[$index + $i + 1 + $total], // Resurfacée: non
  78. );
  79. if (!parsable($lines[$index + $i + 1]) || !parsable($lines[$index + $i + 1 + $total])) {
  80. $borough['INTEGRITE'] = 'Non';
  81. }
  82.  
  83. foreach ($headers as $offset => $key) {
  84. $index = $first + $i + ($total * $offset);
  85. $rink[$key] = $lines[$index];
  86. if (!parsable($lines[$index])) {
  87. $borough['INTEGRITE'] = 'Non';
  88. }
  89. }
  90.  
  91. $borough['Patinoires'][] = $rink;
  92. }
  93.  
  94. $boroughs[] = $borough;
  95. }
  96.  
  97. function cmp($a, $b) {
  98. return strcmp($a['Region'], $b['Region']);
  99. }
  100. usort($boroughs, 'cmp'); // Order by borough
Add Comment
Please, Sign In to add comment