From: <gem...@li...> - 2013-04-15 10:30:30
|
Revision: 1228 http://sourceforge.net/p/gemstracker/code/1228 Author: mennodekker Date: 2013-04-15 10:30:26 +0000 (Mon, 15 Apr 2013) Log Message: ----------- Made batch export a little more generic Modified Paths: -------------- trunk/library/classes/Gems/Export/Excel.php trunk/library/classes/Gems/Export/ExportAbstract.php trunk/library/classes/Gems/Export/ExportBatchInterface.php Added Paths: ----------- trunk/library/classes/Gems/Task/Export/ExportCommand.php Removed Paths: ------------- trunk/library/classes/Gems/Task/Export/Finalize.php trunk/library/classes/Gems/Task/Export/Step.php Modified: trunk/library/classes/Gems/Export/Excel.php =================================================================== --- trunk/library/classes/Gems/Export/Excel.php 2013-04-11 15:43:08 UTC (rev 1227) +++ trunk/library/classes/Gems/Export/Excel.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -139,9 +139,15 @@ /** * This method handles the export with the given options * - * The method takes care of rendering the right script by using $this->export->controller to - * access the controller object. - * + * We open the file and add tasks to the batch to export in steps of 500 records. + * This should be small enough to not run out of time/memory. + * + * We make use of the Export_ExportCommand to forward calls to this class. + * Extra methods in this class are + * handleExportBatchStep Exports the records + * and + * handleExportBatchFinalize Write the footer to the file + * * @param Gems_Task_TaskRunnerBatch $batch The batch to start * @param array $filter The filter to use * @param string $language The language used / to use for the export @@ -238,17 +244,18 @@ do { $filter['limit'] = $step; $filter['offset'] = $current; - $batch->addTask('Export_Step', $data['type'], $filter, $language, $data); + $batch->addTask('Export_ExportCommand', $data['type'], 'handleExportBatchStep', $data, $filter, $language); $current = $current + $step; } while ($current < $answerCount); - $batch->addTask('Export_Finalize', $data['type']); + $batch->addTask('Export_ExportCommand', $data['type'], 'handleExportBatchFinalize'); return; } - public function handleExportBatchStep($batch, $data, $filter, $language) + public function handleExportBatchStep($data, $filter, $language) { + $batch = $this->_batch; $files = $batch->getMessage('file', array()); $survey = $this->loader->getTracker()->getSurvey($data['sid']); $answers = $survey->getRawTokenAnswerRows($filter); @@ -283,10 +290,10 @@ fclose($f); } - public function handleExportBatchFinalize($batch, $data) + public function handleExportBatchFinalize() { - $files = $batch->getMessage('file', array()); - $batch->setMessage('export-progress', $this->_('Export finished')); + $files = $this->_batch->getMessage('file', array()); + $this->_batch->setMessage('export-progress', $this->_('Export finished')); $f = fopen(GEMS_ROOT_DIR . '/var/tmp/' . $files['file'], 'a'); fwrite($f, ' </tbody> </table> Modified: trunk/library/classes/Gems/Export/ExportAbstract.php =================================================================== --- trunk/library/classes/Gems/Export/ExportAbstract.php 2013-04-11 15:43:08 UTC (rev 1227) +++ trunk/library/classes/Gems/Export/ExportAbstract.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -35,9 +35,10 @@ */ /** - * Short description for ExportAbstract + * This is a helper class to make implementing exports easier * - * Long description for class ExportAbstract (if any)... + * The setBatch method will only be used when running batch exports. To do so + * you must implement the Gems_Export_ExportBatchInterface * * @package Gems * @subpackage Export @@ -46,8 +47,15 @@ * @since Class available since version 1.5 */ abstract class Gems_Export_ExportAbstract extends Gems_Loader_TargetLoaderAbstract implements Gems_Export_ExportInterface -{ +{ /** + * Holds the current batch if there is any + * + * @var Gems_Task_TaskRunnerBatch + */ + protected $_batch = null; + + /** * Variable needed to access the controller functions * * $this->export->controller @@ -87,4 +95,16 @@ { return $this->translate->getAdapter()->_($text, $locale); } + + /** + * Set the batch to be used by this source + * + * Use $this->hasBatch to check for existence + * + * @param Gems_Task_TaskRunnerBatch $batch + */ + public function setBatch(Gems_Task_TaskRunnerBatch $batch) + { + $this->_batch = $batch; + } } \ No newline at end of file Modified: trunk/library/classes/Gems/Export/ExportBatchInterface.php =================================================================== --- trunk/library/classes/Gems/Export/ExportBatchInterface.php 2013-04-11 15:43:08 UTC (rev 1227) +++ trunk/library/classes/Gems/Export/ExportBatchInterface.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -47,7 +47,7 @@ interface Gems_Export_ExportBatchInterface extends Gems_Export_ExportInterface { /** - * This method handles setting up all needed stept for the batch export + * This method handles setting up all needed steps for the batch export * * Normally this will initialize the file to download and set up as much * steps as needed and the final job to close the file. @@ -58,23 +58,13 @@ * @param array $data The formdata */ public function handleExportBatch($batch, $filter, $language, $data); - + /** - * Executes a step in exporting the data, this should be as large as possible - * without hitting max request time limits - * - * @param Gems_Task_TaskRunnerBatch $batch The batch to start - * @param array $data The formdata - * @param array $filter The filter to use - * @param string $language The language used / to use for the export - */ - public function handleExportBatchStep($batch, $data, $filter, $language); - - /** - * Final step in batch export, perform cleanup / finalize the file - * + * Set the batch to be used by this source + * + * Use $this->hasBatch to check for existence + * * @param Gems_Task_TaskRunnerBatch $batch - * @param array $data */ - public function handleExportBatchFinalize($batch, $data); + public function setBatch(Gems_Task_TaskRunnerBatch $batch); } \ No newline at end of file Added: trunk/library/classes/Gems/Task/Export/ExportCommand.php =================================================================== --- trunk/library/classes/Gems/Task/Export/ExportCommand.php (rev 0) +++ trunk/library/classes/Gems/Task/Export/ExportCommand.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -0,0 +1,58 @@ +<?php +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @package Gems + * @subpackage Task_Export + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id$ + */ + +/** + * Executes any command in an Export class for a given $exportType + * + * @package Gems + * @subpackage Task_Tracker + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.5.3 + */ +class Gems_Task_Export_ExportCommand extends Gems_Task_TaskAbstract +{ + public function execute($exportType = null, $command = null) + { + $params = array_slice(func_get_args(), 2); + $export = $this->loader->getExport()->getExport($exportType); + $export->setBatch($this->_batch); + + if ($messages = call_user_func_array(array($export, $command), $params)) { + foreach ($messages as $message) { + $this->_batch->addMessage($command . ': ' . $message); + } + } + } +} \ No newline at end of file Deleted: trunk/library/classes/Gems/Task/Export/Finalize.php =================================================================== --- trunk/library/classes/Gems/Task/Export/Finalize.php 2013-04-11 15:43:08 UTC (rev 1227) +++ trunk/library/classes/Gems/Task/Export/Finalize.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -1,54 +0,0 @@ -<?php -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * @package Gems - * @subpackage Task_Export - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @version $Id$ - */ - -/** - * Perform one step of results export - * - * @package Gems - * @subpackage Task_Export - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @since Class available since version 1.6.1 - */ -class Gems_Task_Export_Finalize extends Gems_Task_TaskAbstract { - - public function execute($exportType = null, $filter = array(), $language = null, $data = array()) - { - // $filter now has a limit and offset - $export = $this->loader->getExport()->getExport($exportType); - $export->handleExportBatchFinalize($this->_batch, $data); - - return; - } -} \ No newline at end of file Deleted: trunk/library/classes/Gems/Task/Export/Step.php =================================================================== --- trunk/library/classes/Gems/Task/Export/Step.php 2013-04-11 15:43:08 UTC (rev 1227) +++ trunk/library/classes/Gems/Task/Export/Step.php 2013-04-15 10:30:26 UTC (rev 1228) @@ -1,54 +0,0 @@ -<?php -/** - * Copyright (c) 2011, Erasmus MC - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the name of Erasmus MC nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * @package Gems - * @subpackage Task_Export - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @version $Id$ - */ - -/** - * Perform one step of results export - * - * @package Gems - * @subpackage Task_Export - * @copyright Copyright (c) 2011 Erasmus MC - * @license New BSD License - * @since Class available since version 1.6.1 - */ -class Gems_Task_Export_Step extends Gems_Task_TaskAbstract { - - public function execute($exportType = null, $filter = array(), $language = null, $data = array()) - { - // $filter now has a limit and offset - $export = $this->loader->getExport()->getExport($exportType); - $export->handleExportBatchStep($this->_batch, $data, $filter, $language); - - return; - } -} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |