From 1acad14f7474bf046f800cf2a7e104c8f5a1de49 Mon Sep 17 00:00:00 2001 From: vicchi Date: Mon, 25 Feb 2013 15:30:37 +0000 Subject: [PATCH] Add configuration support --- checkins/tools/config.php.sample | 8 ++ checkins/tools/extract-checkins.php | 145 ++++++++++++++++++++++++++++ checkins/tools/extract.php | 98 ------------------- 3 files changed, 153 insertions(+), 98 deletions(-) create mode 100644 checkins/tools/extract-checkins.php delete mode 100644 checkins/tools/extract.php diff --git a/checkins/tools/config.php.sample b/checkins/tools/config.php.sample index ed091c6..42965ba 100644 --- a/checkins/tools/config.php.sample +++ b/checkins/tools/config.php.sample @@ -1,8 +1,16 @@ \ No newline at end of file diff --git a/checkins/tools/extract-checkins.php b/checkins/tools/extract-checkins.php new file mode 100644 index 0000000..1ae9ab3 --- /dev/null +++ b/checkins/tools/extract-checkins.php @@ -0,0 +1,145 @@ +connect_errno) { + echo 'Failed to connect to MySQL: (' . $sqli->connect_errno . ') ' . $sqli->connect_error; + exit; +} + +$res = $sqli->query($CHECKIN_SQL); +if (FALSE === $res) { + echo 'Query failed' . PHP_EOL; + if ($sqli->errno) { + echo 'Failed on query ' . $sql . ': (' . $sqli->errno . ') ' . $sqli->error . PHP_EOL; + exit; + } +} + +while ($row = $res->fetch_assoc()) { + $venue_id = $row['venue_id']; + if (!empty($venue_id)) { + if (array_key_exists($venue_id, $venues)) { + $venues[$venue_id]++; + } + else { + $venues[$venue_id] = 1; + } + } +} + +$res->free(); + +$geo['type'] = 'FeatureCollection'; +$maxvcount = 0; + +foreach ($venues as $vid => $vcount) { + if ($vcount > $maxvcount) { + $maxvcount = $vcount; + } + + $sql = sprintf($VENUE_SQL, $vid); + $res = $sqli->query($sql); + if ($sqli->errno) { + echo 'Failed on query ' . $sql . ': (' . $sqli->errno . ') ' . $sqli->error; + exit; + } + + $row = $res->fetch_assoc(); + $json = json_decode($row['data']); + if (NULL === $json) { + echo 'Failed to decode JSON (' . json_last_error() . ')'; + switch (json_last_error()) { + case JSON_ERROR_NONE: + echo ' - No errors'; + break; + case JSON_ERROR_DEPTH: + echo ' - Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + echo ' - Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + echo ' - Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + echo ' - Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + echo ' - Unknown error'; + break; + } + echo PHP_EOL; + exit; + } + $skip = false; + if (count($json->categories) == 0) { + $skip = true; + } + else { + $pos = strpos($json->categories[0]->name, '(private)'); + if ($pos !== false) { + $skip = true; + } + } + + if (!$skip) { + $geometry = array( + 'type' => 'Point', + 'coordinates' => array($json->location->lng, $json->location->lat) + ); + + $properties = array( + 'name' => $json->name, + 'venue' => $vid, + 'checkins' => $vcount + ); + + $feature = array( + 'type' => 'Feature', + 'geometry' => $geometry, + 'properties' => $properties + ); + + $features[] = $feature; + } + + $res->free(); +} + +$sqli->close(); + +$geo['max-checkins'] = $maxvcount; +$geo['features'] = $features; +$geo['time-stamp'] = date(DATE_COOKIE, time()); + +$pos = strpos(phpversion(), '5.4'); +if (0 === $pos) { + $geojson = json_encode($geo, JSON_PRETTY_PRINT); +} +else { + $geojson = json_encode($geo); +} + +$contents = array(); +$contents[] = 'var checkins = '; +$contents[] = $geojson; +$contents[] = ';'; +file_put_contents('../js/checkins-geojson.js', implode('', $contents)); + +?> \ No newline at end of file diff --git a/checkins/tools/extract.php b/checkins/tools/extract.php deleted file mode 100644 index 59c5e0d..0000000 --- a/checkins/tools/extract.php +++ /dev/null @@ -1,98 +0,0 @@ -connect_errno) { - echo 'Failed to connect to MySQL: (' . $sqli->connect_errno . ') ' . $sqli->connect_error; - exit; -} - -$sql = 'SELECT id, venue_id FROM PrivatesquareCheckins WHERE user_id = 1'; - -$res = $sqli->query($sql); -if ($sqli->connect_errno) { - echo 'Failed on query ' . $sql . ': (' . $sqli->connect_errno . ') ' . $sqli->connect_error; - exit; -} - -$venues = array (); - -while ($row = $res->fetch_assoc()) { - if (array_key_exists($row['venue_id'], $venues)) { - $venues[$row['venue_id']]++; - } - else { - $venues[$row['venue_id']] = 1; - } -} - -$res->free(); - -$geo = array(); -$geo['type'] = 'FeatureCollection'; -$features = array (); -$maxvcount = 0; - -foreach ($venues as $vid => $vcount) { - if ($vcount > $maxvcount) { - $maxvcount = $vcount; - } - $sql = 'SELECT data FROM FoursquareVenues WHERE venue_id = \'' . $vid . '\';'; - $res = $sqli->query($sql); - if ($sqli->connect_errno) { - echo 'Failed on query ' . $sql . ': (' . $sqli->connect_errno . ') ' . $sqli->connect_error; - exit; - } - $row = $res->fetch_assoc(); - $json = json_decode($row['data']); - - $skip = false; - if (count($json->categories) == 0) { - $skip = true; - } - else { - $pos = strpos($json->categories[0]->name, '(private)'); - if ($pos !== false) { - $skip = true; - } - } - - if (!$skip) { - $geometry = array( - 'type' => 'Point', - 'coordinates' => array($json->location->lng, $json->location->lat) - ); - - $properties = array( - 'name' => $json->name, - 'venue' => $vid, - 'checkins' => $vcount - ); - - $feature = array( - 'type' => 'Feature', - 'geometry' => $geometry, - 'properties' => $properties - ); - - $features[] = $feature; - } - - $res->free(); -} - -$sqli->close(); - -$geo['max-checkins'] = $maxvcount; -$geo['features'] = $features; -$geo['time-stamp'] = date(DATE_COOKIE, time()); -$geojson = json_encode($geo, JSON_PRETTY_PRINT); - -$contents = array(); -$contents[] = 'var checkins = '; -$contents[] = $geojson; -$contents[] = ';'; -file_put_contents('../js/checkins-geojson.js', implode('', $contents)); - -?> \ No newline at end of file