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) { onSuccess: function(response) {
MailPoet.Notice.success( MailPoet.Notice.success(
MailPoet.I18n.t('multipleSubscribersMovedToList') MailPoet.I18n.t('multipleSubscribersMovedToList')
.replace('%$1d', ~~response.subscribers) .replace('%$1d', ~~(response.subscribers))
.replace('%$2s', response.segment) .replace('%$2s', response.segment)
); );
} }

View File

@ -5,7 +5,6 @@ if(!defined('ABSPATH')) exit;
class Model extends \Sudzy\ValidModel { class Model extends \Sudzy\ValidModel {
protected $_errors; protected $_errors;
const MAX_BATCH_SIZE = 200;
function __construct() { function __construct() {
$this->_errors = array(); $this->_errors = array();
@ -91,7 +90,7 @@ class Model extends \Sudzy\ValidModel {
} }
function restore() { function restore() {
return $this->set_expr('deleted_at', 'NULl')->save(); return $this->set_expr('deleted_at', 'NULL')->save();
} }
static function bulkRestore($orm) { static function bulkRestore($orm) {
@ -112,31 +111,6 @@ class Model extends \Sudzy\ValidModel {
if($total === 0) return false; if($total === 0) return false;
$affected_rows = 0;
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);
if($callback !== false) {
$callback($ids);
}
// increment number of affected rows
$affected_rows += $orm->get_last_statement()->rowCount();
}
} else {
$rows = $orm->select(static::$_table.'.id') $rows = $orm->select(static::$_table.'.id')
->offset(null) ->offset(null)
->limit(null) ->limit(null)
@ -151,9 +125,7 @@ class Model extends \Sudzy\ValidModel {
} }
// get number of affected rows // get number of affected rows
$affected_rows = $orm->get_last_statement()->rowCount(); return $orm->get_last_statement()->rowCount();
}
return $affected_rows;
} }
function duplicate($data = array()) { function duplicate($data = array()) {

View File

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

View File

@ -95,17 +95,23 @@ class SubscriberSegment extends Model {
} }
// create many subscriptions to each segment // create many subscriptions to each segment
foreach($segment_ids as $segment_id) { $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( $query = array(
'INSERT IGNORE INTO `'.self::$_table.'`', 'INSERT IGNORE INTO `'.self::$_table.'`',
'(`subscriber_id`, `segment_id`, `status`)', '(`subscriber_id`, `segment_id`, `status`)',
'VALUES '.rtrim(str_repeat( 'VALUES '.rtrim(str_repeat('(?, ?, ?), ', $row_count), ', ')
"(?, ".(int)$segment_id.", '".Subscriber::STATUS_SUBSCRIBED."'), ",
count($subscriber_ids)
), ', ')
); );
self::rawExecute(join(' ', $query), $subscriber_ids); self::rawExecute(join(' ', $query), $values);
}
return true; return true;
} }