Skip to content

Commit

Permalink
pkp#5122 Support Iteration for DAO results
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Oct 3, 2019
1 parent 3877b85 commit cafe5d9
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ protected function _processSearchPhrase($searchPhrase, $submissionIds = []) {

$searchPhraseSubmissionIds = array_map(function($submission) {
return $submission->getId();
}, $submissions);
}, iterator_to_array($submissions));

if (!empty($submissionIds)) {
$submissionIds = array_intersect($submissionIds, $searchPhraseSubmissionIds);
Expand Down
9 changes: 9 additions & 0 deletions classes/db/DAOResultFactory.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions classes/db/DAOResultIterator.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* @file classes/db/DAOResultIterator.inc.php
*
* Copyright (c) 2014-2019 Simon Fraser University
* Copyright (c) 2000-2019 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class DAOResultIterator
* @ingroup db
*
* @brief Wrapper around a DAOResultFactory providing a proper PHP Iterator implementation
*/


class DAOResultIterator implements Iterator {
/** @var DAOResultFactory */
var $_resultFactory;

/** @var DataObject Current return value data object. */
var $_current = null;

/** @var $_i int 0-based index of current data object. */
var $_i = 0;

/**
* Create an Iterator for the specified DAOResultFactory.
* @param $itemIterator ItemIterator
*/
public function __construct($resultFactory) {
$this->_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);
}
}

4 changes: 3 additions & 1 deletion classes/publication/PKPPublicationDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions classes/services/PKPAuthorService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions classes/services/PKPContextService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions classes/services/PKPEmailTemplateService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions classes/services/PKPPublicationService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down
18 changes: 9 additions & 9 deletions classes/services/PKPSubmissionService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions classes/services/PKPUserService.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion classes/submission/PKPSubmissionDAO.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 2 additions & 8 deletions controllers/grid/settings/sections/form/PKPSectionForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion controllers/grid/users/reviewer/form/ReviewerForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/data/PKPStatsTest.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function generateUsageStats() {
// created.
// See: https://github.com/pkp/pkp-lib/issues/4880
array_filter(
Services::get('submission')->getMany(['contextId' => 1, 'status' => STATUS_PUBLISHED]),
iterator_to_array(Services::get('submission')->getMany(['contextId' => 1, 'status' => STATUS_PUBLISHED])),
function($submission) {
return $submission->getCurrentPublication();
}
Expand Down

0 comments on commit cafe5d9

Please sign in to comment.