/** * @param string $clusterIdentifier * @throws eZDBException */ public static function fill($clusterIdentifier) { $configPath = "extension/ezoscar/bin/php/seo/config.json"; $cli = eZCLI::instance(); $config = file_get_contents($configPath); if(!$config) { $cli->output("Bad filepath for config file"); eZExecution::cleanExit(); } $config = json_decode($config, true); $db = MMDB::instance(); $rows = $db->arrayQuery(sprintf("SELECT * FROM mm_seo WHERE cluster_identifier = '%s'", $clusterIdentifier)); $progressBar = new ezcConsoleProgressbar( new ezcConsoleOutput(), count($rows), array( 'emptyChar' => ' ', 'barChar' => '=' ) ); $progressBar->start(); foreach($rows as $row) { if(!array_key_exists($clusterIdentifier, $config)) { continue; } $fq = $config[$clusterIdentifier][$row["application_identifier"]]["filters"]; $solrBase = new eZSolrBase(); $params = array( 'indent' => 'on', 'q' => '"'.$row['keyword'].'"', 'fq' => $fq, 'start' => 0, 'rows' => 0, 'fl' => '', 'qt' => 'ezpublish', 'explainOther' => '', 'hl.fl' => '', ); $results = $solrBase->rawSolrRequest('/select', $params); $num_found = $results['response']['numFound']; $db->query(sprintf("UPDATE mm_seo SET nb_results=%d, speciality_url='%s', keyword_url='%s' WHERE id=%d", $num_found, self::sanitize($row["speciality"]), self::sanitize($row["keyword"]), $row["id"])); $progressBar->advance(); } $progressBar->finish(); $cli->output(); }
/** * Finishes progress output for class search phase. * * Also inserts some extra newlines to make the output clearer. * * @return void */ public function finishPhase2() { $this->tokenizerProgress->finish(); $this->output->outputLine(); $this->output->outputLine(); }
public function testFinish() { $out = new ezcConsoleOutput(); $bar = new ezcConsoleProgressbar($out, 10); $res = array(); for ($i = 0; $i < 5; ++$i) { ob_start(); $bar->advance(); $resTmp = ob_get_clean(); if (trim($resTmp) !== '') { $res[] = $resTmp; } } ob_start(); $bar->finish(); $res[] = ob_get_clean(); $refFile = dirname(__FILE__) . '/data/' . (ezcBaseFeatures::os() === "Windows" ? "windows/" : "posix/") . __FUNCTION__ . '.dat'; // Use the following line to regenerate test reference files // file_put_contents( $refFile, implode( PHP_EOL, $res ) ); $this->assertEquals(file_get_contents($refFile), implode(PHP_EOL, $res), 'Progressbar not correctly generated for ' . $refFile . '.'); }
$out->formats->red->color = "red"; // Create progress bar itself $progress = new ezcConsoleProgressbar($out, 100, array('step' => 5)); $progress->options->emptyChar = '-'; $progress->options->progressChar = $out->formatText('>', "red"); $progress->options->formatString = "Uploading file </tmp/foobar.tar.bz2>: %act%/%max% kb [%bar%]"; // Perform actions $i = 0; while ($i++ < 20) { // Do whatever you want to indicate progress for usleep(mt_rand(20000, 2000000)); // Advance the progressbar by one step ( uploading 5k per run ) $progress->advance(); } // Finish progress bar and jump to next line. $progress->finish(); $out->outputText("Successfully uploaded </tmp/foobar.tar.bz2>.\n", 'success'); /* OUTPUT: (sequential, will be printed into 1 line and updated in reallife) Uploading file </tmp/foobar.tar.bz2>: 5/100 kb [++#----------------------------------------------] Uploading file </tmp/foobar.tar.bz2>: 10/100 kb [+++++#-------------------------------------------] Uploading file </tmp/foobar.tar.bz2>: 15/100 kb [++++++++#----------------------------------------] Uploading file </tmp/foobar.tar.bz2>: 20/100 kb [+++++++++++#-------------------------------------] Uploading file </tmp/foobar.tar.bz2>: 25/100 kb [++++++++++++++#----------------------------------] Uploading file </tmp/foobar.tar.bz2>: 30/100 kb [+++++++++++++++++#-------------------------------] Uploading file </tmp/foobar.tar.bz2>: 35/100 kb [++++++++++++++++++++#----------------------------] Uploading file </tmp/foobar.tar.bz2>: 40/100 kb [+++++++++++++++++++++++#-------------------------] Uploading file </tmp/foobar.tar.bz2>: 45/100 kb [++++++++++++++++++++++++++#----------------------] Uploading file </tmp/foobar.tar.bz2>: 50/100 kb [+++++++++++++++++++++++++++++#-------------------] Uploading file </tmp/foobar.tar.bz2>: 55/100 kb [++++++++++++++++++++++++++++++++#----------------] Uploading file </tmp/foobar.tar.bz2>: 60/100 kb [+++++++++++++++++++++++++++++++++++#-------------]
$file = eZClusterFileHandler::instance( $filePath ); if ( $file->exists() ) // Delete the file physically { $file->delete(); eZImageFile::removeFilepath( $contentObjectAttributeID, $filePath ); eZDir::cleanupEmptyDirectories( $dirpath ); } // Delete the obsolete reference in the database $image->remove(); } $progressBar->advance(); } $progressBar->finish(); $output->outputLine(); } else { $output->outputText( 'No image file found !' ); $output->outputLine(); } $script->shutdown(); } catch(Exception $e) { $output->outputText( $e->getMessage(), 'error' ); $output->outputLine(); $script->shutdown( $e->getCode() );
/** * Clears view cache for imported content objects. * ObjectIDs are stored in 'ezpending_actions' table, with {@link SQLIContent::ACTION_CLEAR_CACHE} action */ public static function viewCacheClear() { $db = eZDB::instance(); $isCli = isset($_SERVER['argv']); $output = null; $progressBar = null; $i = 0; $conds = array('action' => SQLIContent::ACTION_CLEAR_CACHE); $limit = array('offset' => 0, 'length' => 50); $count = (int) eZPersistentObject::count(eZPendingActions::definition(), $conds); if ($isCli && $count > 0) { // Progress bar implementation $output = new ezcConsoleOutput(); $output->outputLine('Starting to clear view cache for imported objects...'); $progressBarOptions = array('emptyChar' => ' ', 'barChar' => '='); $progressBar = new ezcConsoleProgressbar($output, $count, $progressBarOptions); $progressBar->start(); } /* * To avoid fatal errors due to memory exhaustion, pending actions are fetched by packets */ do { $aObjectsToClear = eZPendingActions::fetchObjectList(eZPendingActions::definition(), null, $conds, null, $limit); $jMax = count($aObjectsToClear); if ($jMax > 0) { for ($j = 0; $j < $jMax; ++$j) { if ($isCli) { $progressBar->advance(); } $db->begin(); eZContentCacheManager::clearContentCacheIfNeeded((int) $aObjectsToClear[$j]->attribute('param')); $aObjectsToClear[$j]->remove(); $db->commit(); $i++; } } unset($aObjectsToClear); eZContentObject::clearCache(); if (eZINI::instance('site.ini')->variable('ContentSettings', 'StaticCache') == 'enabled') { $optionArray = array('iniFile' => 'site.ini', 'iniSection' => 'ContentSettings', 'iniVariable' => 'StaticCacheHandler'); $options = new ezpExtensionOptions($optionArray); $staticCacheHandler = eZExtension::getHandlerClass($options); $staticCacheHandler::executeActions(); } } while ($i < $count); if ($isCli && $count > 0) { $progressBar->finish(); $output->outputLine(); } }
/** * Starts to run the import * @param array( SQLIImportItem ) $aImportItems * @throws SQLIImportBaseException * @throws ezcConfigurationNoConfigException */ public function runImport(array $aImportItems) { // First check if an import is already running if (SQLIImportToken::importIsRunning()) { throw new SQLIImportBaseException('Another import is already running. Aborting...', SQLIImportBaseException::IMPORT_ALREADY_RUNNING); } $this->token = SQLIImportToken::registerNewImport(); $this->handlePerformanceSettings(); if (empty($aImportItems)) { // If no source handler is provided, consider processing all source handlers available $aImportItems = $this->importINI->variable('ImportSettings', 'AvailableSourceHandlers'); } // Process import items one by one for ($i = 0, $iMax = count($aImportItems); $i < $iMax; ++$i) { try { if (!$aImportItems[$i] instanceof SQLIImportItem) { throw new SQLIImportRuntimeException('Invalid import item !'); } // Update status for import item $aImportItems[$i]->setAttribute('status', SQLIImportItem::STATUS_RUNNING); $aImportItems[$i]->store(); $this->currentImportItem = $aImportItems[$i]; // First check if this handler has all needed configuration $handler = $aImportItems[$i]->attribute('handler'); $handlerSection = $handler . '-HandlerSettings'; if (!$this->importINI->hasSection($handlerSection)) { // Check INI Section throw new ezcConfigurationNoConfigException('Error : Handler "' . $handler . '" does not have proper config section in sqliimport.ini !'); } if (!$this->importINI->hasVariable($handlerSection, 'ClassName')) { // Check if ClassName is properly defined throw new ezcConfigurationNoConfigException('Error : ClassName not defined for "' . $handler . '" in sqliimport.ini !'); } // Default values $handlerClassName = $this->importINI->variable($handlerSection, 'ClassName'); $handlerEnabled = true; $debug = false; $defaultParentNodeID = $this->importINI->variable('ImportSettings', 'DefaultParentNodeID'); $streamTimeout = $this->importINI->variable('ImportSettings', 'StreamTimeout'); if ($this->importINI->hasVariable($handlerSection, 'Enabled')) { $handlerEnabled = $this->importINI->variable($handlerSection, 'Enabled') === 'true'; } if ($this->importINI->hasVariable($handlerSection, 'Debug')) { $debug = $this->importINI->variable($handlerSection, 'Debug') === 'enabled'; } if ($this->importINI->hasVariable($handlerSection, 'DefaultParentNodeID')) { $localParentNodeID = $this->importINI->variable($handlerSection, 'DefaultParentNodeID'); $defaultParentNodeID = is_int($localParentNodeID) ? (int) $localParentNode : $defaultParentNodeID; } if ($this->importINI->hasVariable($handlerSection, 'StreamTimeout')) { $streamTimeout = (int) $this->importINI->variable($handlerSection, 'StreamTimeout'); } // Check $defaultParentNodeID and throw an exception if not consistent $parentNode = eZContentObjectTreeNode::fetch($defaultParentNodeID, false, false); if (!$parentNode) { throw new SQLIImportRuntimeException('Error : invalid DefaultParentNodeID ( ' . $defaultParentNodeID . ' )'); } unset($parentNode); // Check handler class validity if (!class_exists($handlerClassName)) { throw new SQLIImportRuntimeException('Error : invalid handler class "' . $handlerClassName . '". Did you regenerate autolads ?'); } // #################################### // ##### IMPORT HANDLER PROCESSING // #################################### // Instantiate the handler with appropriate options and process it. // Handler must implement ISQLIImportHandler and extend SQLIImportAbstractHandler $handlerOptions = $aImportItems[$i]->attribute('options'); $importHandler = new $handlerClassName($handlerOptions); if (!$importHandler instanceof ISQLIImportHandler || !$importHandler instanceof SQLIImportAbstractHandler) { throw new SQLIImportRuntimeException('Error : invalid handler "' . $handlerClassName . '". Must implement ISQLIImportHandler and extend SQLIImportAbstractHandler.'); } $importHandler->handlerConfArray = $this->importINI->group($handlerSection); $importHandler->initialize(); // Get process length to calculate advancement percentage to track advancement $processLength = $importHandler->getProcessLength(); $percentageAdvancementStep = 100 / $processLength; $handlerName = $importHandler->getHandlerName(); $handlerIdentifier = $importHandler->getHandlerIdentifier(); // Progress bar implementation $progressBarOptions = array('emptyChar' => ' ', 'barChar' => '='); $progressBar = new ezcConsoleProgressbar($this->output, $processLength, $progressBarOptions); $progressBar->start(); $this->cli->warning('Now processing "' . $handlerName . '" handler.'); $isInterrupted = false; while ($row = $importHandler->getNextRow()) { try { $progressBar->advance(); $startTime = time(); $importHandler->process($row); } catch (Exception $e) { SQLIImportLogger::logError('An error occurred during "' . $handlerIdentifier . '" import process : ' . $e->getMessage()); } $aImportItems[$i]->updateProgress($percentageAdvancementStep, $importHandler->getProgressionNotes()); // Now calculate process time for this iteration $endTime = time(); $diffTime = $endTime - $startTime; $oldProcessTime = $aImportItems[$i]->attribute('process_time'); $aImportItems[$i]->setAttribute('process_time', $oldProcessTime + $diffTime); $aImportItems[$i]->store(array('process_time')); // Interruption handling if ($aImportItems[$i]->isInterrupted()) { $this->cli->notice(); SQLIImportLogger::logNotice('Interruption has been requested for current import ! Cleaning and aborting process...'); $isInterrupted = true; break; } } $importHandler->cleanup(); $progressBar->finish(); $this->cli->notice(); unset($importHandler); if (!$isInterrupted) { $aImportItems[$i]->setAttribute('status', SQLIImportItem::STATUS_COMPLETED); $aImportItems[$i]->setAttribute('percentage', 100); // Force percentage to 100% $aImportItems[$i]->store(); } // #################################### // ##### END IMPORT HANDLER PROCESSING // #################################### } catch (Exception $e) { SQLIImportLogger::logError($e->getMessage()); $aImportItems[$i]->setAttribute('status', SQLIImportItem::STATUS_FAILED); $aImportItems[$i]->store(); if (isset($importHandler)) { $importHandler->cleanup(); unset($importHandler); } continue; } } }
function spawnDownload($remoteFile, $localFile, $remoteFileSize) { $cmd = $this->getPathToPHP() . " ./bin/download.php " . $this->data['identifier'] . " " . $remoteFile . " " . $localFile . " > /dev/null 2>&1 &"; exec($cmd); // wait until the archive exist locally while (!file_exists($localFile)) { usleep(1000); } $output = new ezcConsoleOutput(); $bar = new ezcConsoleProgressbar($output, (int) $remoteFileSize); $currentStep = 0; // until the file is completely downloaded do { // get current size of local file $currentFileSize = $this->getFileSize($localFile); $steps = $currentFileSize - $currentStep; $bar->advance(true, $steps); $currentStep = $currentFileSize; // wait for half second usleep(500000); } while ($currentFileSize < $remoteFileSize); $bar->finish(); }
<?php require_once 'tutorial_autoload.php'; $output = new ezcConsoleOutput(); $output->formats->bar->color = 'blue'; $output->formats->bar->style = array('bold'); $options = array('emptyChar' => ' ', 'barChar' => '-', 'formatString' => '%fraction%% <' . $output->formatText('%bar%', 'bar') . '> Uploaded %act% / %max% kb', 'redrawFrequency' => 50); $bar = new ezcConsoleProgressbar($output, 1024, $options); for ($i = 0; $i < 1024; $i++) { $bar->advance(); usleep(mt_rand(200, 2000)); } $bar->finish(); $output->outputLine();
public static function addClassAttributes($class_identifier, $attributesInfo) { if (isset($class_identifier)) { $class = eZContentClass::fetchByIdentifier($class_identifier); } if (!is_object($class)) { return; } $classID = $class->attribute('id'); foreach ($attributesInfo as $attributeInfo) { $classAttributeIdentifier = $attributeInfo['identifier']; $classAttributeName = $attributeInfo['name']; $datatype = $attributeInfo['data_type_string']; $defaultValue = isset($attributeInfo['default_value']) ? $attributeInfo['default_value'] : false; $canTranslate = isset($attributeInfo['can_translate']) ? $attributeInfo['can_translate'] : 1; $isRequired = isset($attributeInfo['is_required']) ? $attributeInfo['is_required'] : 0; $isSearchable = isset($attributeInfo['is_searchable']) ? $attributeInfo['is_searchable'] : 1; $attrContent = isset($attributeInfo['content']) ? $attributeInfo['content'] : false; $attrCreateInfo = array('identifier' => $classAttributeIdentifier, 'name' => $classAttributeName, 'can_translate' => $canTranslate, 'is_required' => $isRequired, 'is_searchable' => $isSearchable); $newAttribute = eZContentClassAttribute::create($classID, $datatype, $attrCreateInfo); $dataType = $newAttribute->dataType(); $dataType->initializeClassAttribute($newAttribute); // not all datatype can have 'default_value'. do check here. if ($defaultValue !== false) { switch ($datatype) { case 'ezboolean': $newAttribute->setAttribute('data_int3', $defaultValue); break; default: break; } } if ($attrContent) { $newAttribute->setContent($attrContent); } // store attribute, update placement, etc... $attributes = $class->fetchAttributes(); $attributes[] = $newAttribute; // remove temporary version if ($newAttribute->attribute('id') !== null) { $newAttribute->remove(); } $newAttribute->setAttribute('version', eZContentClass::VERSION_STATUS_DEFINED); $newAttribute->setAttribute('placement', count($attributes)); $class->adjustAttributePlacements($attributes); foreach ($attributes as $attribute) { $attribute->storeDefined(); } // update objects $classAttributeID = $newAttribute->attribute('id'); $count = eZContentObject::fetchSameClassListCount($class->ID); $output = new ezcConsoleOutput(); $bar = new ezcConsoleProgressbar($output, (int) $count); $offset = 0; $limit = 50; while (true) { if ($offset > $count) { break; } $objects = eZContentObject::fetchSameClassList($classID, true, $offset, $limit); foreach ($objects as $object) { $contentobjectID = $object->attribute('id'); $objectVersions = $object->versions(); foreach ($objectVersions as $objectVersion) { $translations = $objectVersion->translations(false); $version = $objectVersion->attribute('version'); foreach ($translations as $translation) { $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentobjectID, $version); $objectAttribute->setAttribute('language_code', $translation); $objectAttribute->initialize(); $objectAttribute->store(); $objectAttribute->postInitialize(); } } $bar->advance(); } eZContentObject::clearCache(); $offset += $limit; } $bar->finish(); } }