replacing sha1 primary key with plain autoinc int to be able to have multiple entries for the same source/target combination; that is for the other change: adding update option, so when a post is updated the urls are re-mentioned
Peter Molnar hello@petermolnar.eu
Wed, 15 Jun 2016 14:21:59 +0000
2 files changed,
30 insertions(+),
16 deletions(-)
M
sender.php
→
sender.php
@@ -120,8 +120,10 @@ return false;
} if ( 'publish' != $new_status ) { - static::debug( "Not adding {$post->ID} to mention queue yet; not published" ); return false; + } + else { + static::debug( "Adding {$post->ID} to mention queue" ); } static::debug("Trying to get urls for #{$post->ID}", 6);@@ -169,7 +171,9 @@ // skip self-pings
if ( $s_domain == $t_domain ) continue; - $r = static::queue_add ( 'out', $source, $target, $post->post_type, $post->ID ); + + $post_updated = get_post_modified_time( 'U', false, $post, false ); + $r = static::queue_add ( 'out', $source, $target, $post->post_type, $post->ID, $post_updated ); if ( !$r ) static::debug( " tried adding post #{$post->ID}, url: {$target} to mention queue, but it didn't go well", 4);
M
wp-webmention-again.php
→
wp-webmention-again.php
@@ -117,7 +117,7 @@ $charset_collate .= " COLLATE {$wpdb->collate}";
} $db_command = "CREATE TABLE IF NOT EXISTS `{$dbname}` ( - `id` char(160) CHARACTER SET ascii NOT NULL, + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL, `direction` varchar(12) NOT NULL DEFAULT 'in', `tries` int(4) NOT NULL DEFAULT '0',@@ -216,7 +216,7 @@ *
* @return false|string - false on failure, inserted ID on success * */ - public static function queue_add ( $direction, $source, $target, $object = '', $object_id = 0 ) { + public static function queue_add ( $direction, $source, $target, $object = '', $object_id = 0, $epoch = false ) { global $wpdb; $dbname = $wpdb->prefix . static::tablename;@@ -225,15 +225,16 @@ $valid_directions = array ( 'in', 'out' );
if ( ! in_array ( $direction, $valid_directions ) ) return false; - $id = sha1($source . $target); + //$id = sha1($source . $target . ); + //$id = uniqid(); - if ( static::queue_exists ( $direction, $source, $target ) ) + if ( static::queue_exists ( $direction, $source, $target, $epoch ) ) return true; $q = $wpdb->prepare( "INSERT INTO `{$dbname}` - (`id`,`date`,`direction`, `tries`,`source`, `target`, `object_type`, `object_id`, `status`, `note` ) VALUES - ( '%s', NOW(), '%s', 0, '%s', '%s', '%s', %d, 0, '' );", - $id, $direction, $source, $target, $object, $object_id ); + (`date`,`direction`, `tries`,`source`, `target`, `object_type`, `object_id`, `status`, `note` ) VALUES + ( NOW(), '%s', 0, '%s', '%s', '%s', %d, 0, '' );", + $direction, $source, $target, $object, $object_id ); try { $req = $wpdb->query( $q );@@ -242,7 +243,7 @@ catch (Exception $e) {
static::debug('Something went wrong: ' . $e->getMessage(), 4); } - return $id; + return $wpdb->insert_id; } /**@@ -377,10 +378,11 @@ *
* @param string $direction - 'in' or 'out' * @param string $source - source URL * @param string $target - target URL + * @param int $epoch - epoch to compare date with; used for updates * * @return bool true on existing element, false on not found */ - public static function queue_exists ( $direction, $source, $target ) { + public static function queue_exists ( $direction, $source, $target, $epoch = false ) { global $wpdb; $dbname = $wpdb->prefix . static::tablename;@@ -390,19 +392,27 @@ $valid_directions = array ( 'in', 'out' );
if ( ! in_array ( $direction, $valid_directions ) ) return false; - $id = sha1($source . $target); - - $q = $wpdb->prepare( "SELECT date FROM `{$dbname}` WHERE `direction` = '%s' and `id` = '%s';", $direction, $id ); + $q = $wpdb->prepare( "SELECT date, status FROM `{$dbname}` WHERE `direction` = '%s' and `source` = '%s' and `target`='%s' ORDER BY date DESC LIMIT 1;", $direction, $source, $target ); try { - $req = $wpdb->get_results( $q ); + $req = $wpdb->get_row( $q ); } catch (Exception $e) { static::debug('Something went wrong: ' . $e->getMessage(), 4); } - if ( ! empty ( $req ) ) + if ( ! empty ( $req ) ) { + + if ( false != $epoch && 0 != $req->status ) { + $dbtime = strtotime( $req->date ); + + if ( $epoch > $dbtime ) { // the post was recently updated + return false; + } + } + return true; + } return false; }