major changes for post insert handling and geo caching
This commit is contained in:
parent
372b30a2b3
commit
469a295dff
|
@ -98,16 +98,20 @@ function gcal_import_activate()
|
|||
VALUES('ov-freising', '/tmp/neufahrn.ics', '1')");
|
||||
|
||||
// CREATE geocaching table if it does not exist already.
|
||||
// the location field will be used only during development and debugging, and will be omitted in production.
|
||||
$table = $wpdb->prefix.GCAL_GEO_TABLE;
|
||||
$query = "CREATE TABLE IF NOT EXISTS $table (
|
||||
id INT(9) NOT NULL AUTO_INCREMENT,
|
||||
gcal_geo_location VARCHAR(128) NOT NULL,
|
||||
gcal_geo_hash VARCHAR(40) NOT NULL,
|
||||
gcal_geo_lat VARCHAR(20) NOT NULL,
|
||||
gcal_geo_lon VARCHAR(20) NOT NULL,
|
||||
gcal_geo_timestamp DATETIME NOT NULL,
|
||||
UNIQUE KEY id (id)
|
||||
);";
|
||||
$wpdb->query($query);
|
||||
|
||||
$table = $wpdb->prefix.GCAL_TABLE;
|
||||
// do it once now! Won't work if the table hasn't been populated yet.
|
||||
$result = $wpdb->query("SELECT gcal_category FROM $table");
|
||||
if ($result != 0) {
|
||||
|
@ -158,7 +162,8 @@ function gcal_import_uninstall()
|
|||
global $wpdb;
|
||||
$table = $wpdb->prefix.GCAL_TABLE;
|
||||
$wpdb->query("DROP TABLE $table");
|
||||
$wpdb->query("DROP TABLE gcal_import_geocache");
|
||||
$table = $wpdb->prefix.GCAL_GEO_TABLE;
|
||||
$wpdb->query("DROP TABLE $table");
|
||||
error_log ("gcal_import_uninstall finished");
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
function gcal_import_geocity($location) {
|
||||
|
||||
|
@ -33,13 +34,14 @@ function gcal_import_geocode($location) {
|
|||
|
||||
// we try to cache results as we will need many times the same results especially for recurring events.
|
||||
// we will use a hash for the location because the hash has a fixed length, while the location has not.
|
||||
// TODO: This table will grow indefinitely over time, so we should add a timestamp field and remove
|
||||
// This table will grow indefinitely over time, so we need to add a timestamp field and remove
|
||||
// entries that are older than, say, 30 days each time.
|
||||
// this will also cope with Google subtly changing location strings in Maps over time.
|
||||
// new entries will thus replace outdated ones over time.
|
||||
global $wpdb;
|
||||
// CREATE table if it does not exist already.
|
||||
$table = $wpdb->prefix.GCAL_GEO_TABLE;
|
||||
/*
|
||||
// CREATE table if it does not exist already.
|
||||
$query = "CREATE TABLE IF NOT EXISTS $table (
|
||||
id INT(9) NOT NULL AUTO_INCREMENT,
|
||||
gcal_geo_hash VARCHAR(40) NOT NULL,
|
||||
|
@ -49,12 +51,13 @@ function gcal_import_geocode($location) {
|
|||
UNIQUE KEY id (id)
|
||||
);";
|
||||
$wpdb->query($query);
|
||||
*/
|
||||
|
||||
$hash = hash ('md5', $location);
|
||||
$query = "SELECT gcal_geo_lat, gcal_geo_lon FROM $table WHERE gcal_geo_hash = $hash";
|
||||
$result = $wpdb->get_results($query);
|
||||
if ( ! empty ($result) ) {
|
||||
return array ($result[0], $result[1]);
|
||||
return ($result);
|
||||
} else {
|
||||
|
||||
// do the housekeeping first, before we create a new caching entry.
|
||||
|
@ -62,25 +65,21 @@ function gcal_import_geocode($location) {
|
|||
$query = "DELETE FROM $table WHERE gcal_geo_timestamp < $outdated";
|
||||
$wpdb->query($query);
|
||||
|
||||
// OK so we need to work on the rate limiting.
|
||||
return array ('48.3124161', '11.6637297');
|
||||
|
||||
$attempts = 0;
|
||||
$success = false;
|
||||
// let's be a mobile Firefox Klar browser just for fun.
|
||||
$opts = array('http' =>
|
||||
array(
|
||||
'method' => "GET",
|
||||
'header' => "User-Agent: Mozilla/5.0 (Android 7.0; Mobile; rv:62.0) Gecko/62.0 Firefox/62.0",
|
||||
'header' => "User-Agent: Mozilla/5.0 (Android 7.0; Mobile; rv:62.0) Gecko/62.0 Firefox/62.0",
|
||||
)
|
||||
);
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
|
||||
// we'll need to be easy with GMaps in order no to get a 429 Too Many Requests.
|
||||
while ($success == false && $attempts < 3) {
|
||||
// @ = 'ignore_errors' => TRUE
|
||||
$url = 'https://maps.google.com/maps?q=' . urlencode ($location);
|
||||
$result = file_get_contents ($url, false, $context);
|
||||
// see if we were too fast (429 Too Many Requests)
|
||||
if (429 == getHttpCode($http_response_header)) {
|
||||
time.sleep(2);
|
||||
error_log ("got a HTTP 429 Too Many Requests on $url");
|
||||
|
@ -98,8 +97,11 @@ function gcal_import_geocode($location) {
|
|||
// and now we need to look for:
|
||||
$pattern = '#www.google.com/maps/preview/place/[^/]+/@([\d\.]+),([\d\.]+),.*#';
|
||||
preg_match ($pattern, $result, $matches);
|
||||
|
||||
// do the caching now.
|
||||
// $wpdb_insert does all the sanitizing for us.
|
||||
$wpdb->insert($table, array(
|
||||
'gcal_geo_location' => substr( $location, 0, 128 ),
|
||||
'gcal_geo_hash' => $hash,
|
||||
'gcal_geo_lat' => $matches[1],
|
||||
'gcal_geo_lon' => $matches[2],
|
||||
|
@ -110,22 +112,19 @@ function gcal_import_geocode($location) {
|
|||
// and return the result:
|
||||
return array ($matches[1], $matches[2]);
|
||||
}
|
||||
// limit requests per second
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function gcal_import_do_import($category, $link) {
|
||||
|
||||
$gcal_category = 'Neufahrn';
|
||||
// global $_POST;
|
||||
$post = array();
|
||||
|
||||
require_once dirname (__FILE__) . '/icalparser/src/IcalParser.php';
|
||||
require_once dirname (__FILE__) . '/icalparser/src/Recurrence.php';
|
||||
require_once dirname (__FILE__) . '/icalparser/src/Freq.php';
|
||||
require_once dirname (__FILE__) . '/icalparser/src/WindowsTimezones.php';
|
||||
require_once dirname (__FILE__) . '/../../icalparser/src/IcalParser.php';
|
||||
require_once dirname (__FILE__) . '/../../icalparser/src/Recurrence.php';
|
||||
require_once dirname (__FILE__) . '/../../icalparser/src/Freq.php';
|
||||
require_once dirname (__FILE__) . '/../../icalparser/src/WindowsTimezones.php';
|
||||
|
||||
$cal = new \om\IcalParser();
|
||||
$results = $cal->parseFile($link);
|
||||
|
@ -138,42 +137,71 @@ function gcal_import_do_import($category, $link) {
|
|||
wp_set_auth_cookie( $user_id );
|
||||
}
|
||||
foreach ($cal->getSortedEvents() as $r) {
|
||||
// create a per-post nonce.
|
||||
// $gcal_nonce = wp_create_nonce();
|
||||
$gcal_nonce = 0x8d538f9a;
|
||||
echo sprintf(' <li>%s - %s</li>' . PHP_EOL, $r['DTSTART']->format('j.n.Y'), $r['SUMMARY']);
|
||||
// TODO: wenn DTEND in der Vergangenheit liegt, nicht mehr posten. Next.
|
||||
$post['ID'] = 0;
|
||||
$post['post_author'] = $user_id; // always admin to avoid permission things.
|
||||
// wenn DTEND in der Vergangenheit liegt, nicht mehr posten. Next.
|
||||
if (DateTime($r['DTEND']) < DateTime('NOW')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The zeitstempel. No idea what it's for, but kal3000 seems to use it.
|
||||
$wpc_from = $r['DTSTART']->format(d.m.Y H:i);
|
||||
// code borrowed from kal3000_termine_save_postdata which will not be invoked.
|
||||
$zeitstempel = strftime( strToTime( $wpc_from ) );
|
||||
if(!$zeitstempel) {
|
||||
// strftime doesn't seem to work, so let's get creative
|
||||
preg_match("/([0-9]{1,2}).\s(\w{1,})\s([0-9]{4})\s([0-9]{2}):([0-9]{2})/", $wpc_from, $zeitstempel);
|
||||
|
||||
$month_number = "";
|
||||
for($i=1;$i<=12;$i++){
|
||||
if(strtolower(date_i18n("F", mktime(0, 0, 0, $i, 1, 0))) == strtolower($zeitstempel[2])){
|
||||
$month_number = $i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$zeit = mktime($zeitstempel[4], $zeitstempel[5], 0, $month_number, $zeitstempel[1], $zeitstempel[3]);
|
||||
$zeitstempel = date_i18n('U', $zeit);
|
||||
}
|
||||
|
||||
// geocoden
|
||||
$my_latlon = gcal_import_geocode($r['LOCATION']);
|
||||
|
||||
// create a default form
|
||||
$post = get_default_post_to_edit ('termine');
|
||||
|
||||
// and fill in the post form
|
||||
$post['post_content'] = $r['DESCRIPTION'];
|
||||
$post['post_title'] = $r['SUMMARY'];
|
||||
// create an excerpt for the overview page ([wpcalendar kat=...])
|
||||
if (strlen ($r['DESCRIPTION']) > 160) {
|
||||
$post['post_excerpt'] = substr ($r['DESCRIPTION'], 0, 160) . ' ...'; // first 160 chars of DESCRIPTION plus ' ...'
|
||||
} else {
|
||||
$post['post_excerpt'] = $r['DESCRIPTION'];
|
||||
}
|
||||
$post['post_status'] = 'published';
|
||||
$post['post_category'] = $category; // muss noch hierher.
|
||||
$post['post_name'] = $r['DTSTART']->format('Y-m-d-H-i') . '-' . urlencode($r['SUMMARY']) ; // sanitized title
|
||||
$post['post_type'] = 'termine';
|
||||
$post['post_category'] = $category;
|
||||
// sanitized title. We will add a timestamp to enable recurring events
|
||||
// this is not handled properly by wp_insert_post - recurring events would all have the same post_name.
|
||||
$post['post_name'] = $r['DTSTART']->format('Y-m-d-H-i') . '-' . strtolower( urlencode($r['SUMMARY']) ) ;
|
||||
$post['visibility'] = 'public';
|
||||
$post['wpc_from'] = $r['DTSTART']; // muss noch ins richtige Format.
|
||||
$post['wpc_until'] = $r['DTEND'];
|
||||
// Jetzt den ort zerlegen und geocoden
|
||||
$post['geocity'] = gcal_import_geocity($r['LOCATION']);
|
||||
$post['geoshow'] = gcal_import_geoshow($r['LOCATION']);
|
||||
// geocoden
|
||||
$my_latlon = gcal_import_geocode($r['LOCATION']);
|
||||
$post['wpc_lat'] = $my_latlon[0];
|
||||
$post['wpc_lon'] = $my_latlon[1];
|
||||
$post['wpc_zoom'] = 10;
|
||||
$post['wpcalendar_noncename'] = $gcal_nonce; //
|
||||
// TODO: Jetzt alles nach $_POST kopieren
|
||||
// crappy global var ...
|
||||
// $post_id = wp_write_post ();
|
||||
// error handling, wenn Fehler
|
||||
// add_post_meta ($post_id, '_gcal_category', '$category'); // gemient ist die aus dem GCal abgeleitete cat.
|
||||
$file = dirname (__FILE__) . '/wp_write_post.txt';
|
||||
|
||||
// now the wpcalendar metas.
|
||||
$postmeta = array(
|
||||
_wpcal_from => $r['DTSTART']->format(d.m.Y H:i),
|
||||
_bis => $r['DTEND']->format(d.m.Y H:i),
|
||||
_geocity => gcal_import_geocity($r['LOCATION']),
|
||||
_geoshow => gcal_import_geoshow($r['LOCATION']),
|
||||
_lat => $my_latlon[0],
|
||||
_lon => $my_latlon[1],
|
||||
_zoom = 10,
|
||||
_veranstalter = '';
|
||||
_veranstalterlnk = '',
|
||||
_zeitstempel = $zeitstempel,
|
||||
_gcal_category => $category,
|
||||
);
|
||||
$post['meta_input'] = $postmeta;
|
||||
$post_id = wp_insert_post( $post, false );
|
||||
// debug
|
||||
$file = dirname (__FILE__) . '/' . $post['post_name'] . '.txt';
|
||||
file_put_contents ( $file, var_export ($post, TRUE) );
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue