Revert batch processing on bulk actions - too buggy

- minor fixes and cleanup
This commit is contained in:
Jonathan Labreuille
2016-05-27 14:07:54 +02:00
parent 3c46a5b434
commit 8292e9a744
4 changed files with 41 additions and 61 deletions

View File

@ -121,7 +121,7 @@ const bulk_actions = [
onSuccess: function(response) {
MailPoet.Notice.success(
MailPoet.I18n.t('multipleSubscribersMovedToList')
.replace('%$1d', ~~response.subscribers)
.replace('%$1d', ~~(response.subscribers))
.replace('%$2s', response.segment)
);
}

View File

@ -5,7 +5,6 @@ if(!defined('ABSPATH')) exit;
class Model extends \Sudzy\ValidModel {
protected $_errors;
const MAX_BATCH_SIZE = 200;
function __construct() {
$this->_errors = array();
@ -91,7 +90,7 @@ class Model extends \Sudzy\ValidModel {
}
function restore() {
return $this->set_expr('deleted_at', 'NULl')->save();
return $this->set_expr('deleted_at', 'NULL')->save();
}
static function bulkRestore($orm) {
@ -99,7 +98,7 @@ class Model extends \Sudzy\ValidModel {
return self::bulkAction($orm, function($ids) use($model) {
self::rawExecute(join(' ', array(
'UPDATE `'.$model::$_table.'`',
'SET `deleted_at`=NULL',
'SET `deleted_at` = NULL',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($ids)), ',').')'
)),
$ids
@ -112,48 +111,21 @@ class Model extends \Sudzy\ValidModel {
if($total === 0) return false;
$affected_rows = 0;
$rows = $orm->select(static::$_table.'.id')
->offset(null)
->limit(null)
->findArray();
if($total > self::MAX_BATCH_SIZE) {
for(
$i = 0, $batches = ceil($total / self::MAX_BATCH_SIZE);
$i < $batches;
$i++
) {
$rows = $orm->select('id')
->offset($i * self::MAX_BATCH_SIZE)
->limit(self::MAX_BATCH_SIZE)
->findArray();
$ids = array_map(function($model) {
return (int)$model['id'];
}, $rows);
$ids = array_map(function($model) {
return (int)$model['id'];
}, $rows);
if($callback !== false) {
$callback($ids);
}
// increment number of affected rows
$affected_rows += $orm->get_last_statement()->rowCount();
}
} else {
$rows = $orm->select(static::$_table.'.id')
->offset(null)
->limit(null)
->findArray();
$ids = array_map(function($model) {
return (int)$model['id'];
}, $rows);
if($callback !== false) {
$callback($ids);
}
// get number of affected rows
$affected_rows = $orm->get_last_statement()->rowCount();
if($callback !== false) {
$callback($ids);
}
return $affected_rows;
// get number of affected rows
return $orm->get_last_statement()->rowCount();
}
function duplicate($data = array()) {

View File

@ -544,13 +544,13 @@ class Subscriber extends Model {
if($segment === false) return false;
$subscribers_count = parent::bulkAction($orm,
function($subscriber_ids) use($segment) {
SubscriberSegment::deleteManySubscriptions(
$subscriber_ids, array($segment->id)
);
}
);
$subscribers_count = $orm->count();
parent::bulkAction($orm, function($subscriber_ids) use($segment) {
SubscriberSegment::deleteManySubscriptions(
$subscriber_ids, array($segment->id)
);
});
return array(
'subscribers' => $subscribers_count,
@ -590,7 +590,9 @@ class Subscriber extends Model {
self::rawExecute(join(' ', array(
'UPDATE `'.self::$_table.'`',
'SET `deleted_at` = NOW()',
'WHERE `id` IN ('.rtrim(str_repeat('?,', count($subscriber_ids)), ',').')',
'WHERE `id` IN ('.
rtrim(str_repeat('?,', count($subscriber_ids)), ',')
.')',
'AND `wp_user_id` IS NULL'
)),
$subscriber_ids

View File

@ -95,18 +95,24 @@ class SubscriberSegment extends Model {
}
// create many subscriptions to each segment
foreach($segment_ids as $segment_id) {
$query = array(
'INSERT IGNORE INTO `'.self::$_table.'`',
'(`subscriber_id`, `segment_id`, `status`)',
'VALUES '.rtrim(str_repeat(
"(?, ".(int)$segment_id.", '".Subscriber::STATUS_SUBSCRIBED."'), ",
count($subscriber_ids)
), ', ')
);
self::rawExecute(join(' ', $query), $subscriber_ids);
$values = array();
$row_count = 0;
foreach($segment_ids as &$segment_id) {
foreach($subscriber_ids as &$subscriber_id) {
$values[] = (int)$subscriber_id;
$values[] = (int)$segment_id;
$values[] = Subscriber::STATUS_SUBSCRIBED;
$row_count++;
}
}
$query = array(
'INSERT IGNORE INTO `'.self::$_table.'`',
'(`subscriber_id`, `segment_id`, `status`)',
'VALUES '.rtrim(str_repeat('(?, ?, ?), ', $row_count), ', ')
);
self::rawExecute(join(' ', $query), $values);
return true;
}