diff --git a/lib/Subscribers/ImportExport/Export/DefaultSubscribersGetter.php b/lib/Subscribers/ImportExport/Export/DefaultSubscribersGetter.php deleted file mode 100644 index 505ade191e..0000000000 --- a/lib/Subscribers/ImportExport/Export/DefaultSubscribersGetter.php +++ /dev/null @@ -1,80 +0,0 @@ -getSubscribersWithoutSegment = (array_search(0, $segmentsIds) !== false); - } - - protected function filter($subscribers) { - $subscribers = $subscribers - ->selectMany( - [ - 'list_status' => SubscriberSegment::$_table . '.status', - ] - ) - ->left_outer_join( - SubscriberSegment::$_table, - [ - Subscriber::$_table . '.id', - '=', - SubscriberSegment::$_table . '.subscriber_id', - ] - ) - ->left_outer_join( - Segment::$_table, - [ - Segment::$_table . '.id', - '=', - SubscriberSegment::$_table . '.segment_id', - ] - ) - ->groupBy(Segment::$_table . '.id'); - - if ($this->getSubscribersWithoutSegment !== false) { - // if there are subscribers who do not belong to any segment, use - // a CASE function to group them under "Not In Segment" - $subscribers = $subscribers - ->selectExpr( - 'MAX(CASE WHEN ' . Segment::$_table . '.name IS NOT NULL ' . - 'THEN ' . Segment::$_table . '.name ' . - 'ELSE "' . WPFunctions::get()->__('Not In Segment', 'mailpoet') . '" END) as segment_name' - ) - ->whereRaw( - SubscriberSegment::$_table . '.segment_id IN (' . - rtrim(str_repeat('?,', count($this->segmentsIds)), ',') . ') ' . - 'OR ' . SubscriberSegment::$_table . '.segment_id IS NULL ', - $this->segmentsIds - ); - } else { - // if all subscribers belong to at least one segment, select the segment name - $subscribers = $subscribers - ->selectExpr('MAX(' . Segment::$_table . '.name) as segment_name') - ->whereIn(SubscriberSegment::$_table . '.segment_id', $this->segmentsIds); - } - - $subscribers = $subscribers - ->offset($this->offset) - ->limit($this->batchSize) - ->findArray(); - - return $subscribers; - } -} diff --git a/lib/Subscribers/ImportExport/Export/DynamicSubscribersGetter.php b/lib/Subscribers/ImportExport/Export/DynamicSubscribersGetter.php deleted file mode 100644 index 3943991ada..0000000000 --- a/lib/Subscribers/ImportExport/Export/DynamicSubscribersGetter.php +++ /dev/null @@ -1,77 +0,0 @@ -get(SingleSegmentLoader::class); - } - $this->dynamicSegmentsLoader = $dynamicSegmentsLoader; - } - - public function reset() { - parent::reset(); - $this->segmentIndex = 0; - } - - protected function filter($subscribers) { - $segmentId = $this->segmentsIds[$this->segmentIndex]; - - $filters = $this->dynamicSegmentsLoader->load($segmentId)->getFilters(); - - if (!is_array($filters) || empty($filters)) { - return []; - } - - $segment = Segment::findOne($segmentId); - if (!$segment instanceof Segment) { - return []; - } - $name = $segment->name; - - foreach ($filters as $filter) { - $subscribers = $filter->toSql($subscribers); - } - - return $subscribers - ->selectMany([ - 'list_status' => Subscriber::$_table . '.status', - ]) - ->selectExpr("'" . $name . "' AS segment_name") - ->offset($this->offset) - ->limit($this->batchSize) - ->findArray(); - } - - public function get() { - if ($this->segmentIndex >= count($this->segmentsIds)) { - $this->finished = true; - } - - $subscribers = parent::get(); - - if ($subscribers !== false && count($subscribers) < $this->batchSize) { - $this->segmentIndex ++; - $this->offset = 0; - $this->finished = false; - } - - return $subscribers; - } -} diff --git a/lib/Subscribers/ImportExport/Export/SubscribersGetter.php b/lib/Subscribers/ImportExport/Export/SubscribersGetter.php deleted file mode 100644 index d5e6c9ba60..0000000000 --- a/lib/Subscribers/ImportExport/Export/SubscribersGetter.php +++ /dev/null @@ -1,76 +0,0 @@ -segmentsIds = $segmentsIds; - $this->batchSize = $batchSize; - $this->reset(); - } - - public function reset() { - $this->offset = 0; - $this->finished = false; - } - - /** - * Initialize the query by selecting fields and ignoring trashed subscribers. - * - * @return ORM - */ - protected function select() { - return Subscriber::selectMany( - 'first_name', - 'last_name', - 'email', - 'subscribed_ip', - [ - 'global_status' => Subscriber::$_table . '.status', - ] - ) - ->filter('filterWithCustomFieldsForExport') - ->groupBy(Subscriber::$_table . '.id') - ->whereNull(Subscriber::$_table . '.deleted_at'); - } - - /** - * Filters the subscribers query based on the segments, offset and batch size. - * - * @param ORM $subscribers - * @return array - */ - abstract protected function filter($subscribers); - - /** - * Gets the next batch of subscribers or `false` if no more! - */ - public function get() { - if ($this->finished) { - return false; - } - - $subscribers = $this->select(); - $subscribers = $this->filter($subscribers); - - $this->offset += $this->batchSize; - - if (count($subscribers) < $this->batchSize) { - $this->finished = true; - } - - return $subscribers; - } -} diff --git a/tests/integration/Subscribers/ImportExport/Export/DefaultSubscribersGetterTest.php b/tests/integration/Subscribers/ImportExport/Export/DefaultSubscribersGetterTest.php deleted file mode 100644 index fe52a2ea72..0000000000 --- a/tests/integration/Subscribers/ImportExport/Export/DefaultSubscribersGetterTest.php +++ /dev/null @@ -1,282 +0,0 @@ -subscriberFields = [ - 'first_name' => 'First name', - 'last_name' => 'Last name', - 'email' => 'Email', - 1 => 'Country', - ]; - - $this->subscribersData = [ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 1 => 'Brazil', - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - ], - [ - 'first_name' => 'Paul', - 'last_name' => 'Newman', - 'email' => 'paul@newman.com', - ], - ]; - - $this->customFieldsData = [ - [ - 'name' => 'Country', - 'type' => 'text', - ], - ]; - - $this->segmentsData = [ - [ - 'name' => 'Newspapers', - ], - [ - 'name' => 'Journals', - ], - ]; - - foreach ($this->subscribersData as $subscriber) { - if (isset($subscriber[1])) { - unset($subscriber[1]); - } - $entity = Subscriber::create(); - $entity->hydrate($subscriber); - $entity->save(); - } - - foreach ($this->segmentsData as $segment) { - $entity = Segment::create(); - $entity->hydrate($segment); - $entity->save(); - } - - foreach ($this->customFieldsData as $customField) { - $entity = CustomField::create(); - $entity->hydrate($customField); - $entity->save(); - } - - $entity = SubscriberCustomField::create(); - $entity->subscriberId = 2; - $entity->customFieldId = 1; - $entity->value = $this->subscribersData[1][1]; - $entity->save(); - $entity = SubscriberSegment::create(); - $entity->subscriberId = 1; - $entity->segmentId = 1; - $entity->status = Subscriber::STATUS_UNSUBSCRIBED; - $entity->save(); - $entity = SubscriberSegment::create(); - $entity->subscriberId = 1; - $entity->segmentId = 2; - $entity->save(); - $entity = SubscriberSegment::create(); - $entity->subscriberId = 2; - $entity->segmentId = 1; - $entity->save(); - $entity = SubscriberSegment::create(); - $entity->subscriberId = 3; - $entity->segmentId = 2; - $entity->save(); - } - - protected function filterSubscribersData($subscribers) { - return array_map(function($subscriber) { - $data = []; - foreach ($subscriber as $key => $value) { - if (in_array($key, [ - 'first_name', 'last_name', 'email', 'global_status', - 'status', 'list_status', 'segment_name', 1, - ])) - $data[$key] = $value; - } - return $data; - }, $subscribers); - } - - public function testItGetsSubscribersInOneSegment() { - $getter = new DefaultSubscribersGetter([1], 10); - $subscribers = $getter->get(); - expect($this->filterSubscribersData($subscribers))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNSUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - ]); - - expect($getter->get())->equals(false); - } - - public function testItGetsSubscribersInMultipleSegments() { - $getter = new DefaultSubscribersGetter([1, 2], 10); - $subscribers = $getter->get(); - expect($this->filterSubscribersData($subscribers))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNSUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Journals', - 1 => null, - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($getter->get())->equals(false); - } - - public function testItGetsSubscribersInBatches() { - $getter = new DefaultSubscribersGetter([1, 2], 2); - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNSUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($getter->get())->equals([]); - expect($getter->get())->equals(false); - } - - public function testItGetsSubscribersWithoutSegment() { - $getter = new DefaultSubscribersGetter([0], 10); - $subscribers = $getter->get(); - expect($this->filterSubscribersData($subscribers))->equals([ - [ - 'first_name' => 'Paul', - 'last_name' => 'Newman', - 'email' => 'paul@newman.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => null, - 'segment_name' => 'Not In Segment', - 1 => null, - ], - ]); - } - - public function _after() { - ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); - ORM::raw_execute('TRUNCATE ' . Segment::$_table); - ORM::raw_execute('TRUNCATE ' . SubscriberSegment::$_table); - ORM::raw_execute('TRUNCATE ' . CustomField::$_table); - ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table); - } -} diff --git a/tests/integration/Subscribers/ImportExport/Export/DynamicSubscribersGetterTest.php b/tests/integration/Subscribers/ImportExport/Export/DynamicSubscribersGetterTest.php deleted file mode 100644 index fa2f2172d0..0000000000 --- a/tests/integration/Subscribers/ImportExport/Export/DynamicSubscribersGetterTest.php +++ /dev/null @@ -1,291 +0,0 @@ -subscriberFields = [ - 'first_name' => 'First name', - 'last_name' => 'Last name', - 'email' => 'Email', - 1 => 'Country', - ]; - - $this->subscribersData = [ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 1 => 'Brazil', - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - ], - [ - 'first_name' => 'Paul', - 'last_name' => 'Newman', - 'email' => 'paul@newman.com', - ], - ]; - - $this->customFieldsData = [ - [ - 'name' => 'Country', - 'type' => 'text', - ], - ]; - - $this->segmentsData = [ - [ - 'name' => 'Newspapers', - ], - [ - 'name' => 'Journals', - ], - ]; - - foreach ($this->subscribersData as $subscriber) { - if (isset($subscriber[1])) { - unset($subscriber[1]); - } - $entity = Subscriber::create(); - $entity->hydrate($subscriber); - $entity->save(); - } - - foreach ($this->customFieldsData as $customField) { - $entity = CustomField::create(); - $entity->hydrate($customField); - $entity->save(); - } - - foreach ($this->segmentsData as $segment) { - $entity = Segment::create(); - $entity->hydrate($segment); - $entity->save(); - } - - $entity = SubscriberCustomField::create(); - $entity->subscriberId = 2; - $entity->customFieldId = 1; - $entity->value = $this->subscribersData[1][1]; - $entity->save(); - - $this->singleSegmentLoader = $this->createMock(SingleSegmentLoader::class); - $this->singleSegmentLoader->method('load') - ->willReturnCallback(function($segmentId) { - $segment = DynamicSegment::create(); - $segment->name = 'Dynamic'; - if ($segmentId == 1) { - $segment->setFilters([new \DynamicSegmentFilter([1, 2])]); - } else if ($segmentId == 2) { - $segment->setFilters([new \DynamicSegmentFilter([1, 3, 4])]); - } - return $segment; - }); - } - - protected function filterSubscribersData($subscribers) { - return array_map(function($subscriber) { - $data = []; - foreach ($subscriber as $key => $value) { - if (in_array($key, [ - 'first_name', 'last_name', 'email', 'global_status', - 'status', 'list_status', 'segment_name', 1, - ])) - $data[$key] = $value; - } - return $data; - }, $subscribers); - } - - public function testItGetsSubscribersInOneSegment() { - $getter = new DynamicSubscribersGetter([1], 10, $this->singleSegmentLoader); - $subscribers = $getter->get(); - expect($this->filterSubscribersData($subscribers))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - ]); - - expect($getter->get())->equals(false); - } - - public function testItGetsSubscribersInMultipleSegments() { - $getter = new DynamicSubscribersGetter([1, 2], 10, $this->singleSegmentLoader); - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - ]); - - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - [ - 'first_name' => 'Paul', - 'last_name' => 'Newman', - 'email' => 'paul@newman.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($getter->get())->equals(false); - } - - public function testItGetsSubscribersInBatches() { - $getter = new DynamicSubscribersGetter([1, 2], 2, $this->singleSegmentLoader); - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Newspapers', - 1 => null, - ], - [ - 'first_name' => 'Mary', - 'last_name' => 'Jane', - 'email' => 'mary@jane.com', - 'status' => Subscriber::STATUS_SUBSCRIBED, - 'global_status' => Subscriber::STATUS_SUBSCRIBED, - 'list_status' => Subscriber::STATUS_SUBSCRIBED, - 'segment_name' => 'Newspapers', - 1 => 'Brazil', - ], - ]); - - expect($this->filterSubscribersData($getter->get()))->equals([]); - - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Adam', - 'last_name' => 'Smith', - 'email' => 'adam@smith.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - [ - 'first_name' => 'John', - 'last_name' => 'Kookoo', - 'email' => 'john@kookoo.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($this->filterSubscribersData($getter->get()))->equals([ - [ - 'first_name' => 'Paul', - 'last_name' => 'Newman', - 'email' => 'paul@newman.com', - 'status' => Subscriber::STATUS_UNCONFIRMED, - 'global_status' => Subscriber::STATUS_UNCONFIRMED, - 'list_status' => Subscriber::STATUS_UNCONFIRMED, - 'segment_name' => 'Journals', - 1 => null, - ], - ]); - - expect($getter->get())->equals(false); - } - - public function _after() { - ORM::raw_execute('TRUNCATE ' . Subscriber::$_table); - ORM::raw_execute('TRUNCATE ' . Segment::$_table); - ORM::raw_execute('TRUNCATE ' . CustomField::$_table); - ORM::raw_execute('TRUNCATE ' . SubscriberCustomField::$_table); - } -}