added optional debugging and setting.

This commit is contained in:
Harald Milz 2019-04-08 10:43:28 +02:00
parent 3253ed6aa7
commit f2cd7d8a85
4 changed files with 51 additions and 20 deletions

View file

@ -65,6 +65,9 @@ function gcal_admin_init(){
add_settings_section('gcal_geocoding', 'Geocoding', 'gcal_geocoding_section_text', 'gcal'); add_settings_section('gcal_geocoding', 'Geocoding', 'gcal_geocoding_section_text', 'gcal');
add_settings_field('gcal_geocoding', 'Geocoding', 'gcal_geocoding_setting_string', 'gcal', 'gcal_geocoding'); add_settings_field('gcal_geocoding', 'Geocoding', 'gcal_geocoding_setting_string', 'gcal', 'gcal_geocoding');
add_settings_section('gcal_debugging', 'Debugging', 'gcal_debugging_section_text', 'gcal');
add_settings_field('gcal_debugging', 'Debugging', 'gcal_debugging_setting_string', 'gcal', 'gcal_debugging');
} }
@ -133,7 +136,7 @@ function gcal_geocoding_setting_string() {
), ),
array( array(
'option' => 'osm', 'option' => 'osm',
'name' => 'OpenStreetMap - in Entwicklung', 'name' => 'OpenStreetMap',
), ),
); );
@ -149,6 +152,22 @@ function gcal_geocoding_setting_string() {
} }
function gcal_debugging_section_text() {
?>
<p><b>Debugging aktivieren (landet in ${APACHE_LOG_DIR}/error.log).</b></p>
<?php
}
function gcal_debugging_setting_string($args) {
$options = get_option('gcal_options');
// example from https://code.tutsplus.com/tutorials/the-wordpress-settings-api-part-8-validation-sanitisation-and-input-i--wp-25361
echo '<input type="checkbox" id="gcal_debugging" name="gcal_options[gcal_debugging]" value="1"' . checked( 1, $options['gcal_debugging'], false ) . '>';
// actual logging is done by gcal_error_log()
}
function gcal_options_validate($input) { function gcal_options_validate($input) {
return $input; return $input;

View file

@ -126,7 +126,7 @@ Suchen: if ( isset ( $options['geocache']['hashx'] ) ) ...
'gcal_geo_lon' => $lon, 'gcal_geo_lon' => $lon,
'gcal_geo_timestamp' => time(), 'gcal_geo_timestamp' => time(),
)); ));
error_log ("INFO: geocoded and cached lat=$lat lon=$lon for location $location"); gcal_error_log ("INFO: geocoded and cached lat=$lat lon=$lon for location $location");
} }
// error handling? // error handling?
} }
@ -138,7 +138,7 @@ function gcal_import_geocode_official($location) {
$options = get_option('gcal_options'); $options = get_option('gcal_options');
$apikey = $options['apikey']; $apikey = $options['apikey'];
if ( empty ($apikey) ) { // ??? we should handle this in the admin frontend. if ( empty ($apikey) ) { // ??? we should handle this in the admin frontend.
error_log ("WARN: using Google official geocoding but provided no APIKEY"); gcal_error_log ("WARN: using Google official geocoding but provided no APIKEY");
return array ('',''); return array ('','');
} else { } else {
$location = urlencode($location); $location = urlencode($location);
@ -171,7 +171,7 @@ function gcal_import_geocode_osm($location) {
// https://wiki.openstreetmap.org/wiki/Nominatim // https://wiki.openstreetmap.org/wiki/Nominatim
// https://nominatim.openstreetmap.org/search?q=Hotel+Gumberger+Gasthof+GmbH&format=json' // https://nominatim.openstreetmap.org/search?q=Hotel+Gumberger+Gasthof+GmbH&format=json'
$location = urlencode($location); $location = urlencode($location);
error_log ("gcal_import_geocode_osm: location $location"); gcal_error_log ("gcal_import_geocode_osm: location $location");
// the main problem with Nominatim is that it doesn't understand GCal location information very well. // the main problem with Nominatim is that it doesn't understand GCal location information very well.
// we ought to cut off the location name and the country, i.e. zip code, city & street address only // we ought to cut off the location name and the country, i.e. zip code, city & street address only
$url = 'https://nominatim.openstreetmap.org/search?q="' . $location . '"&format=json'; $url = 'https://nominatim.openstreetmap.org/search?q="' . $location . '"&format=json';
@ -182,7 +182,7 @@ function gcal_import_geocode_osm($location) {
// https://www.php.net/manual/en/function.json-decode.php // https://www.php.net/manual/en/function.json-decode.php
$decoded = json_decode($json, true); $decoded = json_decode($json, true);
// TODO error handling // TODO error handling e.g. if we get no usable values.
/* /*
$file = dirname (__FILE__) . '/json-decoded.txt'; $file = dirname (__FILE__) . '/json-decoded.txt';
// should simply be ->lat and -> lon // should simply be ->lat and -> lon
@ -191,9 +191,7 @@ function gcal_import_geocode_osm($location) {
*/ */
$lat = $decoded['0']['lat']; $lat = $decoded['0']['lat'];
$lon = $decoded['0']['lon']; $lon = $decoded['0']['lon'];
/* gcal_error_log ("gcal_import_geocode_osm found lat=$lat lon=$lon loc $location");
error_log ("gcal_import_geocode_osm found lat=$lat lon=$lon loc $location");
*/
return array ($lat, $lon); return array ($lat, $lon);
} }
@ -216,9 +214,9 @@ function gcal_import_geocode_inofficial($location) {
} elseif (429 == $http_code) { } elseif (429 == $http_code) {
time.sleep(2); time.sleep(2);
++$attempts; ++$attempts;
error_log ("got $attempts HTTP 429 Too Many Requests on $url"); gcal_error_log ("INFO: got $attempts HTTP 429 Too Many Requests on $url");
} else { } else {
error_log ("Ärgerlicher HTTP Fehler $http_code"); gcal_error_log ("WARN: Unspecified HTTP error $http_code");
return array ('', ''); return array ('', '');
} }
} }

View file

@ -34,7 +34,7 @@ function gcal_import_worker() {
foreach($terms as $term){ foreach($terms as $term){
$unique_id = 'gcal_feed_' . $term->name; $unique_id = 'gcal_feed_' . $term->name;
if ( empty ( $options[$unique_id] ) || $options[$unique_id] == '' ) { if ( empty ( $options[$unique_id] ) || $options[$unique_id] == '' ) {
error_log ( "INFO: link for event category $term->name is not known; next"); gcal_error_log ( "INFO: link for event category $term->name is not known; next");
continue; continue;
} }
@ -66,7 +66,7 @@ The update and delete logic goes as follows:
} }
// now we process the current feed. // now we process the current feed.
$link = $options[$unique_id]; $link = $options[$unique_id];
error_log ("INFO: now importing event cat $term->name, link $link"); gcal_error_log ("INFO: now importing event cat $term->name, link $link");
gcal_import_do_import($term->name, $link); gcal_import_do_import($term->name, $link);
// look if there are any published event posts in the current event category which were not posted anew or updated (ie recent == false) // look if there are any published event posts in the current event category which were not posted anew or updated (ie recent == false)
@ -90,7 +90,7 @@ The update and delete logic goes as follows:
foreach( $post_ids as $post_id ) { foreach( $post_ids as $post_id ) {
$id = $post_id->ID; $id = $post_id->ID;
wp_trash_post( $id ); wp_trash_post( $id );
error_log ("Event post $id gelöscht."); gcal_error_log ("Event post $id gelöscht.");
} }
} }
@ -131,7 +131,7 @@ function gcal_import_do_import($category, $link) {
if ($r['DTEND'] < $now) { if ($r['DTEND'] < $now) {
continue; continue;
} else { } else {
error_log ("INFO: processing $summary on $dtstart"); gcal_error_log ("INFO: processing $summary on $dtstart");
} }
// The zeitstempel. No idea what it's for, but kal3000 seems to use it. // The zeitstempel. No idea what it's for, but kal3000 seems to use it.
@ -193,7 +193,7 @@ function gcal_import_do_import($category, $link) {
// create image attachment and associate with new post // create image attachment and associate with new post
$attach = $r['ATTACH']; $attach = $r['ATTACH'];
$summary = $r['SUMMARY']; $summary = $r['SUMMARY'];
error_log ("INFO: found attachment $attach for $summary"); gcal_error_log ("INFO: found attachment $attach for $summary");
} }
if ( isset ( $r['CLASS'] ) && 'PRIVATE' == $r['CLASS']) { if ( isset ( $r['CLASS'] ) && 'PRIVATE' == $r['CLASS']) {
@ -263,7 +263,7 @@ function gcal_import_do_import($category, $link) {
$post_id = wp_insert_post( $post ); $post_id = wp_insert_post( $post );
if ( is_wp_error( $post_id ) ) { if ( is_wp_error( $post_id ) ) {
$message = $post_id->get_error_message(); $message = $post_id->get_error_message();
error_log ( "WARN: $message" ); gcal_error_log ( "WARN: $message" );
} else { } else {
update_post_meta( $post_id, '_edit_last', $user_id ); update_post_meta( $post_id, '_edit_last', $user_id );
$now = time(); $now = time();
@ -271,7 +271,7 @@ function gcal_import_do_import($category, $link) {
update_post_meta( $post_id, '_edit_lock', $lock ); update_post_meta( $post_id, '_edit_lock', $lock );
// and assign the taxonomy type and event category. // and assign the taxonomy type and event category.
wp_set_object_terms( $post_id, $category, 'termine_type' ); wp_set_object_terms( $post_id, $category, 'termine_type' );
error_log ("INFO: posted new post $post_id"); gcal_error_log ("INFO: posted new post $post_id");
} }
} else { } else {
// good, the post exists already. // good, the post exists already.
@ -285,19 +285,19 @@ function gcal_import_do_import($category, $link) {
$post_id = wp_update_post( $post, false ); $post_id = wp_update_post( $post, false );
// and update the _created field // and update the _created field
update_post_meta ( $id, '_gcal_created', $lastmodified ); update_post_meta ( $id, '_gcal_created', $lastmodified );
error_log ("INFO: updated post $post_id"); gcal_error_log ("INFO: updated post $post_id");
} elseif ( $lastmodified == $created ) { } elseif ( $lastmodified == $created ) {
// it was not modified after we created it, so we only update the recent tag. // it was not modified after we created it, so we only update the recent tag.
} elseif ( $lastmodified < $created ) { } elseif ( $lastmodified < $created ) {
// iiiiek! A time reversal or a secret time machine! That should not happen! // iiiiek! A time reversal or a secret time machine! That should not happen!
error_log ("WARN: post $id last-modified : created $lastmodified < $created "); gcal_error_log ("WARN: post $id last-modified : created $lastmodified < $created ");
} }
// and set the event to recent = true no matter what. // and set the event to recent = true no matter what.
update_post_meta ( $id, '_gcal_recent', 'true' ); update_post_meta ( $id, '_gcal_recent', 'true' );
} }
} else { } else {
$file = dirname (__FILE__) . '/get_posts-' . $post->post_name . '.txt'; $file = dirname (__FILE__) . '/get_posts-' . $post->post_name . '.txt';
error_log ("WARN: hmmm, get_posts() did not return an array. Logging to $file"); gcal_error_log ("WARN: hmmm, get_posts() did not return an array. Logging to $file");
file_put_contents ($file, var_export ($post_ids, TRUE)); file_put_contents ($file, var_export ($post_ids, TRUE));
} }
// and on the next entry. // and on the next entry.

View file

@ -129,3 +129,17 @@ function gcal_import_uninstall()
register_uninstall_hook( __FILE__, 'gcal_import_uninstall' ); register_uninstall_hook( __FILE__, 'gcal_import_uninstall' );
/*
* Debug logging if debugging is activated
*
* @since 0.3.0
*/
function gcal_error_log($args) {
$options = get_option('gcal_options');
if ( isset ( gcal_options['gcal_debugging'] ) && '1' == gcal_options['gcal_debugging'] ) {
error_log ( $args );
}
}