Skip to content

Commit

Permalink
StCR Import: Improve skipped subs count and logging routines.
Browse files Browse the repository at this point in the history
  • Loading branch information
raamdev committed Dec 17, 2015
1 parent 2936887 commit 53169ed
Showing 1 changed file with 94 additions and 44 deletions.
138 changes: 94 additions & 44 deletions comment-mail-pro/includes/classes/import-stcr.php
Expand Up @@ -57,19 +57,26 @@ class import_stcr extends abs_base
protected $total_imported_subs;

/**
* @var boolean Has more posts to import?
* @var integer Total created subs.
*
* @since 141111 First documented version.
*/
protected $has_more_posts_to_import;
protected $total_created_subs;

/**
* @var integer Total skipped subscriptions during import.
*
* @since 15xxxx Fixing confusing StCR import count
* @since 15xxxx Improving StCR import count results
*/
protected $total_skipped_subs;

/**
* @var boolean Has more posts to import?
*
* @since 141111 First documented version.
*/
protected $has_more_posts_to_import;

/**
* Class constructor.
*
Expand Down Expand Up @@ -106,7 +113,7 @@ public function __construct(array $request_args = array())
$this->unimported_post_ids = array_slice($this->unimported_post_ids, 0, $this->max_post_ids_limit);
}
$this->imported_post_ids = array(); // Initialize.
$this->total_imported_post_ids = $this->total_imported_subs = $this->total_skipped_subs = 0;
$this->total_imported_post_ids = $this->total_imported_subs = $this->total_created_subs = $this->total_skipped_subs = 0;

$this->maybe_import(); // Handle importation.
}
Expand Down Expand Up @@ -218,36 +225,53 @@ protected function maybe_import_sub($post_id, \stdClass $sub)
$sub_inserter = new sub_inserter($sub_insert_data);
if($sub_inserter->did_insert()){
$this->total_imported_subs++;
$this->total_created_subs++;
} else {
$this->log_failure('Failed to insert All Comments subscription', $sub_insert_data);
$this->log_failure('Failed to insert an All Comments (Y) subscription', $sub_insert_data);
$this->total_skipped_subs++;
}
}
else # Otherwise, specific comment(s) only; i.e. "Replies Only".
{
foreach($this->sub_comment_ids($post_id, $sub->email) as $_comment_id)
{
$_sub_insert_data = array(
'post_id' => $post_id,
'comment_id' => $_comment_id,

'status' => 'subscribed',
'deliver' => 'asap',

'fname' => $sub->fname,
'email' => $sub->email,
$_sub_comment_ids = $this->sub_comment_ids($post_id, $sub->email);

if(!empty($_sub_comment_ids)) {
/*
* This is where the behavior of Comment Mail and StCR diverge when it comes to how they store subscriptions.
* StCR only stores one (1) `R` subscription per email per Post ID, while Comment Mail creates a Replies Only subscription
* for each comment the user has posted on a given Post ID. That means the Total StCR Subscriptions imported will
* likely be much lower than the total subscriptions created by Comment Mail. See also: http://bit.ly/1QtwEWO
*
* Note how we count imported subs outside of this foreach loop, but we count created subs inside the foreach loop.
*/
$this->total_imported_subs++;

'insertion_time' => $sub->time,
);
$_sub_inserter = new sub_inserter($_sub_insert_data);
if($_sub_inserter->did_insert()) {
$this->total_imported_subs++;
} else {
$this->log_failure('Failed to insert Replies Only subscription', $_sub_insert_data);
$this->total_skipped_subs++;
foreach ($_sub_comment_ids as $_comment_id) {
$_sub_insert_data = array(
'post_id' => $post_id,
'comment_id' => $_comment_id,

'status' => 'subscribed',
'deliver' => 'asap',

'fname' => $sub->fname,
'email' => $sub->email,

'insertion_time' => $sub->time,
);
$_sub_inserter = new sub_inserter($_sub_insert_data);
if ($_sub_inserter->did_insert()) {
$this->total_created_subs++;
} else {
$this->log_failure('Failed to import Replies Only (R) subscription', $_sub_insert_data);
$this->total_skipped_subs++;
}
}
} else { // No comments associated with $sub->email were found for $post_id
$this->log_failure('Failed to import Replies Only (R) subscription', array('reason'=>'Associated comment has been deleted, trashed, or marked as spam', 'post_id'=>$post_id, 'email'=>$sub->email));
$this->total_skipped_subs++;
}
unset($_comment_id, $_sub_insert_data, $_sub_inserter); // Housekeeping.
unset($_comment_id, $_sub_insert_data, $_sub_inserter, $_sub_comment_ids); // Housekeeping.
}
}

Expand Down Expand Up @@ -297,25 +321,25 @@ protected function stcr_subs_for_post($post_id)
$_email = trim(strtolower($_email));

if(!$_email || strpos($_email, '@', 1) === FALSE || !is_email($_email)) {
$this->log_failure('Invalid Email Address ('.$this->plugin->utils_db->wp->postmeta.'.meta_id = '.$_result->meta_id.'): '.$_email);
$this->log_failure('Invalid Email Address', array($this->plugin->utils_db->wp->postmeta.'.meta_id'=>$_result->meta_id, 'email'=>$_email));
continue; // Invalid email address.
}

// Original format: `2013-03-11 01:31:01|R`.
if(!$_result->meta_value || strpos($_result->meta_value, '|', 1) === FALSE){
$this->log_failure('Invalid meta data ('.$this->plugin->utils_db->wp->postmeta.'.meta_id = '.$_result->meta_id.'): '.$_result->meta_value);
$this->log_failure('Invalid meta data', array($this->plugin->utils_db->wp->postmeta.'.meta_id'=>$_result->meta_id,'meta_value'=>$_result->meta_value));
continue; // Invalid meta data.
}

list($_local_datetime, $_status) = explode('|', $_result->meta_value);

if(!($_time = strtotime($_local_datetime))) {
$this->log_failure('Date not strtotime() compatible ('.$this->plugin->utils_db->wp->postmeta.'.meta_id = '.$_result->meta_id.'): '.$_local_datetime);
$this->log_failure('Date not strtotime() compatible', array($this->plugin->utils_db->wp->postmeta.'.meta_id'=>$_result->meta_id, 'date'=>$_local_datetime));
continue; // Not `strtotime()` compatible.
}

if(($_time = $_time + (get_option('gmt_offset') * 3600)) < 1){
$this->log_failure('Unable to convert date to UTC timestamp ('.$this->plugin->utils_db->wp->postmeta.'.meta_id = '.$_result->meta_id.'): '.$_time);
$this->log_failure('Unable to convert date to UTC timestamp', array($this->plugin->utils_db->wp->postmeta.'.meta_id'=>$_result->meta_id, 'date'=>$_time));
continue; // Unable to convert date to UTC timestamp.
}

Expand All @@ -324,20 +348,20 @@ protected function stcr_subs_for_post($post_id)
// An `R` indicates they want notifications for replies only.
// A `C` indicates "suspended" or "unconfirmed".
if($_status !== 'Y' && $_status !== 'R') {// Active?
$this->log_failure('Ignoring this subscription; not an active status ('.$this->plugin->utils_db->wp->postmeta.'.meta_id = '.$_result->meta_id.'): '.$_status);
$this->log_failure('Ignoring this subscription; not an active status (Y or R)', array($this->plugin->utils_db->wp->postmeta.'.meta_id'=>$_result->meta_id, 'status'=>$_status));
continue; // Not an active subscriber.
}

if(!isset($subs[$_email]) || ($_status === 'R' && $subs[$_email]->status === 'Y')) {
if(isset($subs[$_email]) && $_status === 'R' && $subs[$_email]->status === 'Y'){
$this->total_skipped_subs++; // We're giving precedence to an `R` subscription and skipping a `Y` subscription
if(isset($subs[$_email]) && $_status === 'R' && $subs[$_email]->status === 'Y') {
$this->total_skipped_subs++; // We're overwriting a `Y` subscription with an `R` subscription below
}
// Give precedence to any subscription that chose to receive "Replies Only".
// See: <https://github.com/websharks/comment-mail/issues/7#issuecomment-57252200>
$subs[$_email] = (object)array('fname' => $this->plugin->utils_string->first_name('', $_email),
'email' => $_email, 'time' => $_time, 'status' => $_status);
} else {
$this->total_skipped_subs++; // We already have an `R` or `Y` subscription for this Post ID
$subs[$_email] = (object)array(
'fname' => $this->plugin->utils_string->first_name('', $_email),
'email' => $_email, 'time' => $_time, 'status' => $_status
);
}
}
unset($_result, $_email, $_local_datetime, $_status); // Housekeeping.
Expand Down Expand Up @@ -472,18 +496,22 @@ protected function parent_output_status($child_status_url)
' </script>'."\n";

$status .= ' <script type="text/javascript">'."\n";
$status .= ' function updateCounters(childTotalPostIds, childTotalSubs, childTotalSkippedSubs)'."\n".
$status .= ' function updateCounters(childTotalPostIds, childTotalSubs, childTotalSkippedSubs, childTotalCreatedSubs)'."\n".
' {'."\n".
' var $totalImportedPostIds = $("#total-imported-post-ids");'."\n".
' var $totalImportedSubs = $("#total-imported-subs");'."\n".
' var $totalSkippedSubs = $("#total-skipped-subs");'."\n".
' var $totalCreatedSubs = $("#total-created-subs");'."\n".

' $totalImportedPostIds.html(Number($totalImportedPostIds.text()) + Number(childTotalPostIds));'."\n".
' $totalImportedSubs.html(Number($totalImportedSubs.text()) + Number(childTotalSubs));'."\n".
' $totalSkippedSubs.html(Number($totalSkippedSubs.text()) + Number(childTotalSkippedSubs));'."\n".
' $totalCreatedSubs.html(Number($totalCreatedSubs.text()) + Number(childTotalCreatedSubs));'."\n".
' }'."\n";
$status .= ' function importComplete()'."\n".
$status .= ' function importComplete(additionalSkippedSubs)'."\n".
' {'."\n".
' var $totalSkippedSubs = $("#total-skipped-subs");'."\n".
' $totalSkippedSubs.html(Number($totalSkippedSubs.text()) + Number(additionalSkippedSubs));'."\n".
' $("#importing").remove();'."\n". // Removing importing div/animation.
' $("body").append("<div>'.sprintf(__('<strong>Import complete!<strong> (<a href=\'%1$s\' target=\'_parent\'>view list of all subscriptions</a>)', $this->plugin->text_domain), esc_attr($this->plugin->utils_url->subs_menu_page_only())).'</div>");'."\n".
' }'."\n";
Expand All @@ -501,8 +529,9 @@ protected function parent_output_status($child_status_url)
' </div>'."\n";

$status .= ' <code id="total-imported-post-ids">'.esc_html($this->total_imported_post_ids).'</code> '.__('post IDs', $this->plugin->text_domain).';'.
' <code id="total-imported-subs">'.esc_html($this->total_imported_subs).'</code> '.__('subscriptions', $this->plugin->text_domain).' '.
' (<code id="total-skipped-subs">'.esc_html($this->total_skipped_subs).'</code> '.__('skipped', $this->plugin->text_domain).').'."\n";
' <code id="total-imported-subs">'.esc_html($this->total_imported_subs).'</code> '.__('subscriptions', $this->plugin->text_domain).
' (<code id="total-skipped-subs">'.esc_html($this->total_skipped_subs).'</code> '.__('skipped', $this->plugin->text_domain).';'.
' <code id="total-created-subs">'.esc_html($this->total_created_subs).'</code> '.__('created', $this->plugin->text_domain).').'."\n";

if($this->has_more_posts_to_import) // Import will contiue w/ child processes?
$status .= ' <iframe src="'.esc_attr((string)$child_status_url).'" style="width:1px; height:1px; border:0; visibility:hidden;"></iframe>';
Expand Down Expand Up @@ -533,7 +562,7 @@ protected function child_output_status()
$status .= ' <meta charset="UTF-8" />'."\n";

$status .= ' <script type="text/javascript">'."\n";
$status .= ' parent.updateCounters('.$this->total_imported_post_ids.', '.$this->total_imported_subs.', '.$this->total_skipped_subs.');'."\n";
$status .= ' parent.updateCounters('.$this->total_imported_post_ids.', '.$this->total_imported_subs.', '.$this->total_skipped_subs.', '.$this->total_created_subs.');'."\n";
$status .= ' </script>'."\n";

if($this->has_more_posts_to_import)
Expand All @@ -542,7 +571,7 @@ protected function child_output_status()
else // Import complete; signal the parent output status window.
{
$status .= ' <script type="text/javascript">'."\n";
$status .= ' parent.importComplete();'."\n";
$status .= ' parent.importComplete('.$this->total_subs_with_invalid_post_ids().');'."\n";
$status .= ' </script>'."\n";
}
$status .= ' </head>'."\n"; // End `<head>`.
Expand Down Expand Up @@ -611,6 +640,28 @@ public static function delete_post_meta_keys()
$plugin->plugin->utils_db->wp->query($sql);
}

/**
* Count StCR subscriptions that belong to Post IDs that no longer exist or are no longer published
*
* @since 15xxxx Improving StCR import count results
*
* @return int Number of subscriptions the importer will skip due to non-existent Post IDs
*
* @note This routine is used to update the total number of skipped subscriptions, as the import routine only processes subscriptions for posts that exist.
*/

protected function total_subs_with_invalid_post_ids()
{
$valid_post_ids = // All valid Post IDs
"SELECT DISTINCT `ID` FROM `".esc_sql($this->plugin->utils_db->wp->posts)."`";

$sql = "SELECT COUNT(*) as `count` FROM `".esc_sql($this->plugin->utils_db->wp->postmeta)."`".
" WHERE `meta_key` LIKE '%\\_stcr@\\_%'".
" AND `post_id` NOT IN (".$valid_post_ids.")"; // StCR subs that belong to invalid Post IDs

return (int)$this->plugin->utils_db->wp->get_var($sql);
}

/**
* Log StCR import failures.
*
Expand All @@ -630,8 +681,7 @@ protected function log_failure($msg, $details = array())
throw new \Exception(sprintf(__('StCR import log file is NOT writable: `%1$s`. Please set permissions to `644` (or higher). `666` might be needed in some cases.', $this->plugin->text_domain), $log_file));
}

$log_entry = date(DATE_RFC822)."\n";
$log_entry .= $msg."\n";
$log_entry = $msg."\n";
foreach ($details as $key => $val) {
$log_entry .= $key.': '.$val."\n";
}
Expand Down

0 comments on commit 53169ed

Please sign in to comment.