public function initialSync($processInCli = true) { $stepSize = 100; $format = self::_optimalImportExportFormat($this->getProxyModel(), $this->getSourceModel()); $count = $this->_sourceModel->countRows(); $progress = null; if (PHP_SAPI == 'cli' && $processInCli) { $c = new Zend_ProgressBar_Adapter_Console(); $c->setElements(array(Zend_ProgressBar_Adapter_Console::ELEMENT_PERCENT, Zend_ProgressBar_Adapter_Console::ELEMENT_BAR, Zend_ProgressBar_Adapter_Console::ELEMENT_ETA, Zend_ProgressBar_Adapter_Console::ELEMENT_TEXT)); $progress = new Zend_ProgressBar($c, 0, $count); } $startTime = microtime(true); $this->getProxyModel()->deleteRows(array()); //alles löschen for ($offset = 0; $offset < $count; $offset += $stepSize) { $s = new Kwf_Model_Select(); $s->limit($stepSize, $offset); /* $data = $this->_sourceModel->export($format, $s); $this->getProxyModel()->import($format, $data); */ //warning: slow code ahead foreach ($this->_sourceModel->getRows($s) as $row) { $data = $row->toArray(); $newRow = $this->createRow($data); foreach ($this->getDependentModels() as $rule => $depModel) { if ($depModel instanceof Kwf_Model_RowsSubModel_MirrorCacheSimple) { //dieser code könne vielleicht im Kwf_Model_RowsSubModel_MirrorCacheSimple liegen $m = $depModel->getSourceModel(); $ref = $m->getReferenceByModelClass(get_class($this), null); $select = new Kwf_Model_Select(); $select->whereEquals($ref['column'], $row->{$this->getPrimaryKey()}); $childRows = $m->getRows($select); foreach ($childRows as $childRow) { $newCRow = $newRow->createChildRow($rule, $childRow->toArray()); } } } $newRow->save(); } unset($row); unset($newRow); unset($newCRow); unset($childRows); unset($childRow); foreach (self::getInstances() as $m) { $m->clearRows(); } //echo round(memory_get_usage()/(1024*1024), 3)."MB\n"; if ($progress) { $text = round(($offset + $stepSize) / (microtime(true) - $startTime)) . ' rows/sec'; $progress->next($stepSize, $text); } } }
private function _getExportData($onlyShowIn, $calcEstimatedMemUsageType, $memoryLimitMb = 0) { if (!isset($this->_model)) { $rowSet = $this->_fetchData(null, null, null); $countRows = count($rowSet); } else { $sel = $this->_getSelect(); if (is_null($sel)) { return array(); } //TODO: dieser code sollte in _getOrder liegen $order = $this->_defaultOrder; if ($this->getRequest()->getParam('sort')) { $order['field'] = $this->getRequest()->getParam('sort'); } if ($this->_getParam("direction") && $this->_getParam('direction') != 'undefined') { $order['direction'] = $this->_getParam('direction'); } $order = $this->_getOrder($order); if ($order) { $sel->order($order); } $countRows = $this->_model->countRows($sel); $rowSet = $this->_model->getRows($sel); } if ($rowSet && $countRows) { $this->_progressBar = new Zend_ProgressBar(new Kwf_Util_ProgressBar_Adapter_Cache($this->_getParam('progressNum')), 0, $countRows * 1.05 * 3); // Index 0 reserved for column headers $exportData = array(0 => array()); $estimatedMemoryUsage = memory_get_usage(); $memForRows = array(); $rowLenghtes = array(); $columns = $columnsHeader = array(); foreach ($rowSet as $row) { $rowBeginMemUsage = memory_get_usage(); if (is_array($row)) { // wenn _fetchData() überschrieben wurde $row = (object) $row; } if (!$this->_hasPermissions($row, 'load')) { throw new Kwf_Exception("You don't have the permissions to load this row"); } $columns = $columnsHeader = array(); foreach ($this->_columns as $column) { if (!($column->getShowIn() & $onlyShowIn)) { continue; } $currentColumnHeader = $column->getHeader(); if (!is_null($currentColumnHeader)) { $columnsHeader[] = (string) $currentColumnHeader; $colVal = $column->load($row, Kwf_Grid_Column::ROLE_EXPORT, array()); $setTypeTo = 'string'; if ($column->getType()) { if ($column->getType() == 'boolean' || $column->getType() == 'bool') { $setTypeTo = 'bool'; } if ($column->getType() == 'integer' || $column->getType() == 'int') { $setTypeTo = 'int'; } if ($column->getType() == 'double' || $column->getType() == 'float') { $setTypeTo = 'float'; } if ($column->getType() == 'null') { $setTypeTo = 'null'; } } if ($setTypeTo == 'bool') { if ($colVal) { $colVal = trlKwf('Yes'); } else { $colVal = trlKwf('No'); } } else { settype($colVal, $setTypeTo); } $columns[] = $colVal; } } $exportData[] = $columns; // zum berechnen des geschätzten speicherverbrauchs if ($memoryLimitMb) { if (count($rowLenghtes) == 40) { // text length $estimatedMemoryUsage += array_sum($rowLenghtes) / count($rowLenghtes) * $countRows; // daten sammeln in dieser schleife hier $estimatedMemoryUsage += array_sum($memForRows) / count($memForRows) * $countRows; // xls export if ($calcEstimatedMemUsageType == 'xls') { $estimatedMemoryUsage += 1400 * $countRows * count($columns); } /** * TODO: Calculating for csv */ if ($estimatedMemoryUsage / 1024 / 1024 > $memoryLimitMb) { throw new Kwf_Exception_Client(trlKwf("Too many rows to export. Try exporting two times with fewer rows.")); } } if (count($rowLenghtes) < 41) { $memForRows[] = memory_get_usage() - $rowBeginMemUsage; $rowLenghtes[] = strlen(implode('', $columns)); } } $this->_progressBar->next(2, trlKwf('Collecting data')); } $exportData[0] = $columnsHeader; return $exportData; } else { $this->_progressBar = new Zend_ProgressBar(new Kwf_Util_ProgressBar_Adapter_Cache($this->_getParam('progressNum')), 0, 4); } return array(); }
public function rewind() { $this->_rowset = $this->_model->getRows($this->_select); }