From 44a2d778b2308dfac634b032a65fcb3ad9ddeb00 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 13 Mar 2020 15:47:03 -0500 Subject: [PATCH] MigrationMoodle: Output buffering - refs BT#15992 --- plugin/migrationmoodle/admin.php | 3 + plugin/migrationmoodle/src/Task/BaseTask.php | 94 +++++++++----------- 2 files changed, 43 insertions(+), 54 deletions(-) diff --git a/plugin/migrationmoodle/admin.php b/plugin/migrationmoodle/admin.php index 19e2190760..f1e16c777c 100644 --- a/plugin/migrationmoodle/admin.php +++ b/plugin/migrationmoodle/admin.php @@ -6,6 +6,9 @@ use Chamilo\PluginBundle\MigrationMoodle\Task\BaseTask; $cidReset = true; +ini_set('memory_limit', -1); +ini_set('max_execution_time', 0); + require_once __DIR__.'/../../main/inc/global.inc.php'; api_protect_admin_script(true); diff --git a/plugin/migrationmoodle/src/Task/BaseTask.php b/plugin/migrationmoodle/src/Task/BaseTask.php index 29f08c1380..0284dfef9d 100644 --- a/plugin/migrationmoodle/src/Task/BaseTask.php +++ b/plugin/migrationmoodle/src/Task/BaseTask.php @@ -54,8 +54,6 @@ abstract class BaseTask $this->loader = $this->getLoader(); $this->calledClass = get_called_class(); - - $this->initMapLog(); } /** @@ -74,6 +72,42 @@ abstract class BaseTask abstract public function getLoadConfiguration(); public function execute() + { + $taskId = \Database::insert( + 'plugin_migrationmoodle_task', + ['name' => $this->getTaskName()] + ); + + $i = 0; + + foreach ($this->executeETL() as $hash => $ids) { + \Database::insert( + 'plugin_migrationmoodle_item', + [ + 'hash' => $hash, + 'extracted_id' => (int) $ids['extracted'], + 'loaded_id' => (int) $ids['loaded'], + 'task_id' => $taskId, + ] + ); + + echo "Data migrated: $hash".PHP_EOL; + + $i++; + + if ($i % 10 === 0) { + flush(); + ob_flush(); + } + } + + ob_end_flush(); + } + + /** + * @return \Generator + */ + private function executeETL() { foreach ($this->extractFiltered() as $extractedData) { try { @@ -92,36 +126,13 @@ abstract class BaseTask continue; } - try { - $hash = $this->saveMapLog($extractedData['id'], $loadedId); - - echo "Data migrated. $hash".PHP_EOL; - } catch (\Exception $exception) { - echo "Data migrated, but... \n"; - echo "\n".$exception->getTraceAsString(); - } + yield md5("{$extractedData['id']}@@$loadedId") => [ + 'extracted' => $extractedData['id'], + 'loaded' => $loadedId + ]; } } - /** - * @throws \Exception - */ - private function initMapLog() - { - $taskName = $this->getTaskName(); - - $id = \Database::insert( - 'plugin_migrationmoodle_task', - ['name' => $taskName] - ); - - if (empty($id)) { - throw new \Exception("Failed to save task ($taskName)."); - } - - $this->taskId = $id; - } - /** * @return \Generator */ @@ -140,31 +151,6 @@ abstract class BaseTask } } - /** - * @param int $extractedId - * @param int $loadedId - * - * @throws \Exception - * - * @return string - */ - private function saveMapLog($extractedId, $loadedId) - { - $hash = md5("$extractedId@@$loadedId"); - - \Database::insert( - 'plugin_migrationmoodle_item', - [ - 'hash' => $hash, - 'extracted_id' => $extractedId, - 'loaded_id' => $loadedId, - 'task_id' => $this->taskId, - ] - ); - - return $hash; - } - /** * @return ExtractorInterface */