diff --git a/classes/db/DAOResultFactory.inc.php b/classes/db/DAOResultFactory.inc.php index 58d7d994187..d5aa0d30b75 100644 --- a/classes/db/DAOResultFactory.inc.php +++ b/classes/db/DAOResultFactory.inc.php @@ -16,6 +16,7 @@ import('lib.pkp.classes.core.ItemIterator'); +import('lib.pkp.classes.db.DAOResultIterator'); class DAOResultFactory extends ItemIterator { /** @var DAO The DAO used to create objects */ @@ -222,6 +223,14 @@ function toArray() { return $returner; } + /** + * Return an Iterator for this DAOResultFactory. + * @return Iterator + */ + function toIterator() { + return new DAOResultIterator($this); + } + /** * Convert this iterator to an associative array by database ID. * @return array diff --git a/classes/db/DAOResultIterator.inc.php b/classes/db/DAOResultIterator.inc.php new file mode 100644 index 00000000000..1787dff425a --- /dev/null +++ b/classes/db/DAOResultIterator.inc.php @@ -0,0 +1,79 @@ +_resultFactory = $resultFactory; + $this->_current = $this->_resultFactory->next(); + } + + /** + * @copydoc Iterator::current + */ + public function current() { + return $this->_current; + } + + /** + * Return the 0-based index for the current object. + * Note that this is NOT the DataObject's ID -- for that, call + * getId() on the current element. + * @return int|null + */ + public function key() { + if (!$this->_current) return null; + return $this->_i; + return $this->_current->getId(); + } + + /** + * @copydoc Iterator::next() + */ + public function next() { + $this->_current = $this->_resultFactory->next(); + $this->_i++; + } + + /** + * Rewind the DAOResultFactory to the begining. WARNING that this + * operation is not arbitrarily supported -- it can only be called + * before the first call to `next()`. + */ + public function rewind() { + if ($this->_i != 0) throw new Exception('DAOResultIterator currently does not support rewind() once iteration has started.'); + } + + /** + * @copydoc Iterator::valid() + */ + public function valid() { + return ($this->_current !== null); + } +} + diff --git a/classes/publication/PKPPublicationDAO.inc.php b/classes/publication/PKPPublicationDAO.inc.php index 765b23579de..99eaf2f2708 100644 --- a/classes/publication/PKPPublicationDAO.inc.php +++ b/classes/publication/PKPPublicationDAO.inc.php @@ -61,7 +61,9 @@ public function _fromRow($primaryRow) { $publication = parent::_fromRow($primaryRow); // Get authors - $publication->setData('authors', Services::get('author')->getMany(['publicationIds' => $publication->getId()])); + $publication->setData('authors', iterator_to_array( + Services::get('author')->getMany(['publicationIds' => $publication->getId()]) + )); // Get controlled vocab metadata $submissionKeywordDao = DAORegistry::getDAO('SubmissionKeywordDAO'); diff --git a/classes/services/PKPAuthorService.inc.php b/classes/services/PKPAuthorService.inc.php index f894ded35b1..f4b0ed24e21 100644 --- a/classes/services/PKPAuthorService.inc.php +++ b/classes/services/PKPAuthorService.inc.php @@ -46,7 +46,7 @@ public function get($authorId) { * @option int count * @option int offset * } - * @return array + * @return Iterator */ public function getMany($args = array()) { $authorQB = $this->_getQueryBuilder($args); @@ -56,7 +56,7 @@ public function getMany($args = array()) { $result = $authorDao->retrieveRange($authorQO->toSql(), $authorQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $authorDao, '_fromRow'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** diff --git a/classes/services/PKPContextService.inc.php b/classes/services/PKPContextService.inc.php index 1f69f573997..fcf3dd7c118 100644 --- a/classes/services/PKPContextService.inc.php +++ b/classes/services/PKPContextService.inc.php @@ -59,7 +59,7 @@ public function get($contextId) { * @option int count * @option int offset * } - * @return array + * @return Iterator */ public function getMany($args = array()) { $contextListQB = $this->_getQueryBuilder($args); @@ -69,7 +69,7 @@ public function getMany($args = array()) { $result = $contextDao->retrieveRange($contextListQO->toSql(), $contextListQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $contextDao, '_fromRow'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** diff --git a/classes/services/PKPEmailTemplateService.inc.php b/classes/services/PKPEmailTemplateService.inc.php index 6c071fb2162..822049b3d7d 100644 --- a/classes/services/PKPEmailTemplateService.inc.php +++ b/classes/services/PKPEmailTemplateService.inc.php @@ -78,7 +78,7 @@ public function getByKey($contextId, $key) { * @option int count * @option int offset * } - * @return array + * @return Iterator */ public function getMany($args = array()) { $emailTemplateQB = $this->_getQueryBuilder($args); @@ -88,7 +88,7 @@ public function getMany($args = array()) { $result = $emailTemplateDao->retrieveRange($emailTemplateQueryParts[0], $emailTemplateQueryParts[1], $range); $queryResults = new DAOResultFactory($result, $emailTemplateDao, '_fromRow'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** @@ -322,7 +322,7 @@ public function restoreDefaults($contextId) { $result = $emailTemplateDao->retrieve($emailTemplateQO->toSql(), $emailTemplateQO->getBindings()); $queryResults = new DAOResultFactory($result, $emailTemplateDao, '_fromRow'); $deletedKeys = []; - foreach ($queryResults->toArray() as $emailTemplate) { + while ($emailTemplate = $queryResults->next()) { $deletedKeys[] = $emailTemplate->getData('key'); $this->delete($emailTemplate); } diff --git a/classes/services/PKPPublicationService.inc.php b/classes/services/PKPPublicationService.inc.php index c776a80c1ca..fcdcd33f5bd 100644 --- a/classes/services/PKPPublicationService.inc.php +++ b/classes/services/PKPPublicationService.inc.php @@ -46,7 +46,7 @@ public function get($publicationId) { * @option int count * @option int offset * } - * @return array + * @return Iterator */ public function getMany($args = []) { $publicationQB = $this->_getQueryBuilder($args); @@ -56,7 +56,7 @@ public function getMany($args = []) { $result = $publicationDao->retrieveRange($publicationQO->toSql(), $publicationQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $publicationDao, '_fromRow'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** diff --git a/classes/services/PKPSubmissionService.inc.php b/classes/services/PKPSubmissionService.inc.php index b1ebb1a5e2e..fefd739055e 100644 --- a/classes/services/PKPSubmissionService.inc.php +++ b/classes/services/PKPSubmissionService.inc.php @@ -53,7 +53,7 @@ public function get($submissionId) { * @option string searchPhrase * @option int count * @option int offset - * @return array + * @return Iterator */ public function getMany($args = array()) { $submissionListQB = $this->_getQueryBuilder($args); @@ -63,7 +63,7 @@ public function getMany($args = array()) { $result = $submissionDao->retrieveRange($submissionListQO->toSql(), $submissionListQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $submissionDao, '_fromRow'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** @@ -154,7 +154,7 @@ public function getProperties($submission, $props, $args = null) { function($publication) use ($args) { return Services::get('publication')->getFullProperties($publication, $args); }, - (array) $submission->getData('publications') + $submission->getData('publications') ); break; case 'reviewAssignments': @@ -367,9 +367,9 @@ public function getPropertyStages($submission, $stageIds = null) { $submission->getId(), $stageId, $request->getUser()->getId() // Current user restriction should prevent unauthorized access - )->toArray(); + ); - foreach ($queries as $query) { + while ($query = $queries->next()) { $stage['queries'][] = array( 'id' => (int) $query->getId(), 'assocType' => (int) $query->getAssocType(), @@ -616,11 +616,11 @@ public function canCurrentUserDelete($submission) { * Get review rounds for a submission * * @param $submission Submission - * @return array + * @return Iterator */ public function getReviewRounds($submission) { $reviewRoundDao = DAORegistry::getDAO('ReviewRoundDAO'); - return $reviewRoundDao->getBySubmissionId($submission->getId())->toArray(); + return $reviewRoundDao->getBySubmissionId($submission->getId())->toIterator(); } /** @@ -742,8 +742,8 @@ public function delete($submission) { * @return boolean */ public function canUserEditMetadata($submissionId, $userId) { - $stageAssignments = DAORegistry::getDAO('StageAssignmentDAO')->getBySubmissionAndUserIdAndStageId($submissionId, $userId, null)->toArray(); - foreach($stageAssignments as $stageAssignment) { + $stageAssignments = DAORegistry::getDAO('StageAssignmentDAO')->getBySubmissionAndUserIdAndStageId($submissionId, $userId, null); + while ($stageAssignment = $stageAssignments->next()) { if ($stageAssignment->getCanChangeMetadata()) { return true; } diff --git a/classes/services/PKPUserService.inc.php b/classes/services/PKPUserService.inc.php index f8a8f822fee..ac712507abb 100644 --- a/classes/services/PKPUserService.inc.php +++ b/classes/services/PKPUserService.inc.php @@ -52,7 +52,7 @@ public function get($userId) { * @option string searchPhrase * @option int count * @option int offset - * @return array + * @return Iterator */ public function getMany($args = array()) { $userListQB = $this->_getQueryBuilder($args); @@ -62,7 +62,7 @@ public function getMany($args = array()) { $result = $userDao->retrieveRange($userListQO->toSql(), $userListQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $userDao, '_returnUserFromRowWithData'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** @@ -133,7 +133,7 @@ public function getReviewers($args = array()) { $result = $userDao->retrieveRange($userListQO->toSql(), $userListQO->getBindings(), $range); $queryResults = new DAOResultFactory($result, $userDao, '_returnUserFromRowWithReviewerStats'); - return $queryResults->toArray(); + return $queryResults->toIterator(); } /** diff --git a/classes/submission/PKPSubmissionDAO.inc.php b/classes/submission/PKPSubmissionDAO.inc.php index db7809e9a77..d2176c6883e 100644 --- a/classes/submission/PKPSubmissionDAO.inc.php +++ b/classes/submission/PKPSubmissionDAO.inc.php @@ -94,7 +94,9 @@ abstract function newDataObject(); */ function _fromRow($row) { $submission = parent::_fromRow($row); - $submission->setData('publications', Services::get('publication')->getMany(['submissionIds' => $submission->getId()])); + $submission->setData('publications', iterator_to_array( + Services::get('publication')->getMany(['submissionIds' => $submission->getId()]) + )); return $submission; } diff --git a/controllers/grid/settings/sections/form/PKPSectionForm.inc.php b/controllers/grid/settings/sections/form/PKPSectionForm.inc.php index c0e0b404391..67715ee9c93 100644 --- a/controllers/grid/settings/sections/form/PKPSectionForm.inc.php +++ b/controllers/grid/settings/sections/form/PKPSectionForm.inc.php @@ -87,15 +87,9 @@ public function _getAssignedSubEditorIds($sectionId, $contextId) { 'assignedToSection' => $sectionId, )); - if (empty($subEditors)) { - return array(); - } - - $subEditorIds = array_map(function($subEditor) { + return array_map(function($subEditor) { return (int) $subEditor->getId(); - }, $subEditors); - - return $subEditorIds; + }, iterator_to_array($subEditors)); } /** diff --git a/controllers/grid/users/reviewer/form/ReviewerForm.inc.php b/controllers/grid/users/reviewer/form/ReviewerForm.inc.php index 4e08198d468..852d20c4c46 100644 --- a/controllers/grid/users/reviewer/form/ReviewerForm.inc.php +++ b/controllers/grid/users/reviewer/form/ReviewerForm.inc.php @@ -263,12 +263,13 @@ function fetch($request, $template = null, $display = false) { ]); $customTemplateKeys = array_map(function($emailTemplate) { return $emailTemplate->getData('key'); - }, $customTemplates); + }, iterator_to_array($customTemplates)); $templateKeys = array_merge($templateKeys, $customTemplateKeys); break; } } + $templates = array(); foreach ($templateKeys as $templateKey) { $thisTemplate = new SubmissionMailTemplate($submission, $templateKey, null, null, null, false); $thisTemplate->assignParams(array()); diff --git a/controllers/grid/users/stageParticipant/form/PKPStageParticipantNotifyForm.inc.php b/controllers/grid/users/stageParticipant/form/PKPStageParticipantNotifyForm.inc.php index 2ad916a98f8..c9ce157d4fe 100644 --- a/controllers/grid/users/stageParticipant/form/PKPStageParticipantNotifyForm.inc.php +++ b/controllers/grid/users/stageParticipant/form/PKPStageParticipantNotifyForm.inc.php @@ -79,7 +79,7 @@ function fetch($request, $template = null, $display = false) { ]); $customTemplateKeys = array_map(function($emailTemplate) { return $emailTemplate->getData('key'); - }, $customTemplates); + }, iterator_to_array($customTemplates)); $templateKeys = array_merge($templateKeys, $customTemplateKeys); break; } @@ -90,6 +90,7 @@ function fetch($request, $template = null, $display = false) { if (array_key_exists($currentStageId, $stageTemplates)) { $templateKeys = array_merge($templateKeys, $stageTemplates[$currentStageId]); } + $templates = array(); foreach ($templateKeys as $templateKey) { $thisTemplate = $this->_getMailTemplate($submission, $templateKey); $thisTemplate->assignParams(array());