/** * Tries to deduce a town from postal code and if that fails from content. * Stores (in object, not db) and returns the town */ public function detectTown() { if ($this->postalCode !== null && $this->postalCode != "") { $address = Notification::get_address($this->postalCode); if ($address['success']) { $this->town = $address['resource']['town']; return $this->town; } } $towns = array(); $db = Database::getConnection(); if ($result = $db->query("SELECT name FROM towns")) { foreach ($result as $townarr) { if (preg_match("#\\b(" . $townarr['name'] . ")\\b#i", $this->content)) { //If isolated word (\b is non word character, like begin of string or ","), /i for non-case-sensitive $towns[] = $townarr['name']; } } $result->close(); } if (!empty($towns)) { $this->town = implode("||", $towns); } else { $this->town = ""; } return $this->town; }
$notifications = array(); while ($stmt->fetch()) { $notifications[] = array('id' => $id, 'postal' => $postal); } $stmt->close(); fwrite(STDOUT, count($notifications) . " notifications retrieved\n"); $i = 0; $added = 0; //for each notification without postal with town, try to find the street in the content. If found find postal code belonging to this town and street. foreach ($notifications as $notification) { // if($i<10){ fwrite(STDOUT, "Iteration " . $i . " (id " . $notification['id'] . ", pc " . $notification['postal'] . "): "); // } else { // continue; // } $address = Notification::get_address($notification['postal']); $success = $address['success'] ? "yes" : "no"; fwrite(STDOUT, "success: " . $success); if ($address['success']) { $added++; $lat = $address['resource']['latitude']; $long = $address['resource']['longitude']; $coordinates = "{lat: " . $lat . ", lng: " . $long . "}"; fwrite(STDOUT, $coordinates); $stmt = $db->prepare("UPDATE notifications SET coordinates = ? WHERE id = ?"); $stmt->bind_param("si", $coordinates, $notification['id']); if ($stmt->execute()) { $added++; } else { fwrite(STDOUT, "error: " . $db->error); }
/** * Split and store Notification objects */ private function indexNotifications($rawNotifications) { $alreadyStored = 0; if ($rawNotifications == null || empty($rawNotifications)) { return false; } foreach ($rawNotifications as $raw) { $raw = explode("</tr>", $raw); $row1 = explode("</td>", $raw[0]); $date = str_replace("<tr><td class=\"DT\">", "", $row1[0]); $time = explode(">", $row1[1])[1]; $type = explode(">", $row1[2])[1]; $region = explode(">", $row1[3])[1]; $content = explode(">", $row1[4])[1]; preg_match_all('/\\b[0-9]{4}\\s?[a-zA-Z]{2}\\b/', $content, $postals); $postal = !empty($postals[0]) ? $postals[0][0] : ""; // TEST GEOCODE --------------------------------------------------------------------------------------------- //fwrite(STDOUT, "Postal: ".$postal.", strlen: ".strlen($postal)."\n"); // Test output $coords = ""; if (strlen($postal) == 6) { $address = Notification::get_address($postal); if ($address['success']) { $coords = "{lat: " . $address['resource']['latitude'] . ", lng: " . $address['resource']['longitude'] . "}"; } fwrite(STDOUT, $coords . "\n"); // Test output } // END TEST ------------------------------------------------------------------------------------------------- $notification = new Notification($date, $time, $type, $region, $postal, $content, $coords); if (!$notification->isActualNotification()) { continue; //Skip current iteration, this notification won't be stored } if (count($raw) >= 4) { for ($i = 1; $i < count($raw) - 2; $i++) { $capContent = explode("<", explode(">", $raw[$i])[10])[0]; $capCode = explode(" ", $capContent)[0]; $cc = new Capcode($capCode, $capContent); $notification->addCapCode($cc); } } if ($notification->existsInDatabase()) { $alreadyStored++; continue; // Skip detectTown() and store() } if ($notification->detectTown() == "") { //echo "No town detected (no postal code and no town in content)\n"; fwrite(STDOUT, "No town detected (no postal code and no town in content)\n"); // for running on CLI } $notification->cluster(); //sets the cluster this notification belongs to, if any. Call after detectTown if (!$notification->store()) { //echo '<span style="color: blue;">Notification was already in database! Nothing stored.</span><br/>'; fwrite(STDOUT, "Notification was already in database! Nothing stored.\n"); // for CLI } // $notification->printNotification(); echo "<hr>"; //TODO: remove, just for testing } return $alreadyStored; }