1<?php // vim: ft=php et 2 3/* 4 This code is used to pregenerate the events listing displayed 5 on the PHP.net homepage. It gets the filename 6 of the CSV where the info is already fetched, and generates the 7 PHP code ready to be included to $outfile 8*/ 9 10function pregenerate_events($csvfile, $outfile, $months = 2) 11{ 12 // Try to open output file for reading 13 $out = @fopen("$outfile~", "w"); 14 if (!$out) { die("unable to pregenerate events list to '$outfile~'"); } 15 16 // Read in events CSV file 17 $csv = @fopen($csvfile, "r"); 18 if (!$csv) { die("unable to open $csvfile for reading"); } 19 20 // Current month number, current category and categories list 21 $cm = $ccat = 0; 22 $cats = ['unknown', 'User Group Events', 'Conferences', 'Training']; 23 24 // Event duplication check hash 25 $seen = []; 26 27 // Start output file with PHP code 28 fwrite( 29 $out, 30 "<?php\n\$RSIDEBAR_DATA = <<<END_RSIDEBAR_DATA\n" . 31 "<h4>Upcoming Events <a href=\"/submit-event.php\">[add]</a></h4>\n" 32 ); 33 34 $content = ""; $buffer = []; 35 $endts = strtotime("+$months months"); 36 $endm = date("n", $endts); 37 $endy = date("Y", $endts); 38 39 // While we can read the events file 40 while (TRUE) { 41 42 // Get information event elements from file 43 $elems = fgetcsv($csv, 8192); 44 if ($elems === FALSE) { break; } 45 list($d, $m, $y, $ccode, $desc, $id, , , , , , , $cat) = $elems; 46 // Skip events way in the future 47 if (($endy == $y && $endm <= $m) || $endy < $y) { continue; } 48 49 // Fgetcsv() returns an array with a single null element 50 // for a blank line, which we need to skip 51 if ($d === NULL) { continue; } 52 53 // If the month number changed 54 if ($cm != (int) $m) { 55 56 // Update current month information 57 $cm = (int) $m; 58 59 // Buffer month header 60 $headline = '<h4 class="eventmonth">' . 61 strftime('%B', mktime(12, 0, 0, $cm, $d, $y)) . 62 "</h4>\n"; 63 $buffer[$headline] = []; 64 65 // We have not seen any events in this month 66 $seen = []; 67 68 // New category header needed 69 $ccat = 0; 70 } 71 72 // Start new category with a category header 73 if ($ccat != (int) $cat) { 74 if($ccat) { 75 // "buffer" the current content before we move onto next category 76 $buffer[$headline][$ccat] = $content; 77 } 78 79 $content = '<h4>' . $cats[$cat] . "</h4>\n"; 80 $ccat = $cat; 81 } 82 83 // There is no event with this description in this month already seen 84 if (!isset($seen[$desc])) { 85 if ($m < 10) { 86 $m = "0$m"; 87 } 88 if ($d < 10) { 89 $d = "0$d"; 90 } 91 92 // Add event to the "buffer" 93 $content .= "<span class=\"event_$ccode vevent\"><abbr title=\"$y-$m-$d\" class=\"dtstart\">$d</abbr>. <a href=\"/cal.php?id=$id\" class=\"summary\">" . 94 htmlspecialchars(stripslashes($desc), ENT_QUOTES,'UTF-8') . 95 "</a></span><br />\n"; 96 97 // Set seen flag 98 $seen[$desc] = TRUE; 99 } 100 } 101 // Add the last category to the "buffer" 102 $buffer[$headline][$ccat] = $content; 103 104 // Organize the output 105 $order = [ 106 2, // Conferences 107 1, // User Group Events 108 3, // Training 109 0, // Unkown 110 ]; 111 112 foreach($buffer as $headline => $categories) { 113 // Prevent empty listing 114 if(empty($categories)) { 115 continue; 116 } 117 // Start month with a header 118 fwrite($out, $headline); 119 120 foreach($order as $category) { 121 if (!isset($buffer[$headline][$category])) { 122 continue; 123 } 124 fwrite($out, $buffer[$headline][$category]); 125 } 126 } 127 128 // End heredoc string 129 fwrite($out, "END_RSIDEBAR_DATA;\n"); 130 131 // Close files (all events displayed) 132 fclose($csv); fclose($out); 133 134 // If we don't have new data, delete file 135 if (!@filesize("$outfile~")) { 136 echo "'$outfile~' was empty, skipping\n"; 137 unlink("$outfile~"); 138 return; 139 } 140 141 // Replace real file with temporary file 142 return rename("$outfile~", $outfile); 143} 144?> 145