setRedrawFrequency() public method

Sets the redraw frequency.
public setRedrawFrequency ( integer | float $freq )
$freq integer | float The frequency in steps
コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function prepare()
 {
     $this->progress = new ProgressBar($this->output, $this->reader->count());
     $this->progress->setFormat($this->verbosity);
     $this->progress->setRedrawFrequency($this->redrawFrequency);
     $this->progress->start();
 }
コード例 #2
0
 /**
  * @phpcsSuppress SlevomatCodingStandard.Typehints.TypeHintDeclaration.missingParameterTypeHint
  * @param int $step
  */
 public function progressAdvance($step = 1)
 {
     if ($this->output->isDecorated() && $step > 0) {
         $stepTime = (time() - $this->progressBar->getStartTime()) / $step;
         if ($stepTime > 0 && $stepTime < 1) {
             $this->progressBar->setRedrawFrequency(1 / $stepTime);
         } else {
             $this->progressBar->setRedrawFrequency(1);
         }
     }
     $this->progressBar->setProgress($this->progressBar->getProgress() + $step);
 }
コード例 #3
0
 /**
  * Exports es index to provided file.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param array           $types
  * @param int             $chunkSize
  * @param OutputInterface $output
  */
 public function exportIndex(Manager $manager, $filename, $types, $chunkSize, OutputInterface $output)
 {
     $typesMapping = $manager->getMetadataCollector()->getMappings($manager->getConfig()['mappings']);
     $typesToExport = [];
     if ($types) {
         foreach ($types as $type) {
             if (!array_key_exists($type, $typesMapping)) {
                 throw new \InvalidArgumentException(sprintf('Type "%s" does not exist.', $type));
             }
             $typesToExport[] = $typesMapping[$type]['bundle'] . ':' . $typesMapping[$type]['class'];
         }
     } else {
         foreach ($typesMapping as $type => $typeConfig) {
             $typesToExport[] = $typeConfig['bundle'] . ':' . $typeConfig['class'];
         }
     }
     $repo = $manager->getRepository($typesToExport);
     $results = $this->getResults($repo, $chunkSize);
     $progress = new ProgressBar($output, $results->count());
     $progress->setRedrawFrequency(100);
     $progress->start();
     $metadata = ['count' => $results->count(), 'date' => date(\DateTime::ISO8601)];
     $writer = $this->getWriter($this->getFilePath($filename), $metadata);
     foreach ($results as $data) {
         $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
         $writer->push($doc);
         $progress->advance();
     }
     $writer->finalize();
     $progress->finish();
     $output->writeln('');
 }
コード例 #4
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $indexName = $input->getOption('index');
     $batchSize = $input->getOption('batch_size');
     $indexManager = $this->getIndexManager($indexName);
     $totalDocuments = $indexManager->getTotalEntities();
     $iterations = $this->getIterations($totalDocuments, $batchSize);
     $output->writeln(sprintf('<info>Reindexing</info> "%s"', $indexName));
     $output->writeln(sprintf('<comment>Total documents:</comment> %s', $totalDocuments));
     $output->writeln(sprintf('<comment>Batch size:</comment> %s', $batchSize));
     $output->writeln(sprintf('<comment>Iterations:</comment> %s', $iterations));
     $progress = new ProgressBar($output, $totalDocuments);
     $progress->setFormat('verbose');
     $progress->setRedrawFrequency($batchSize);
     $progress->start();
     $indexManager->purgeIndex();
     for ($i = 0; $i < $iterations; $i++) {
         $criteria = new Criteria();
         $criteria->setMaxResults($batchSize);
         $criteria->setFirstResult($i * $batchSize);
         $collection = $indexManager->getEntitiesCollection($criteria);
         $collection->map(function (EntityInterface $entity) use($indexManager, $progress) {
             $indexManager->addEntity($entity);
             $progress->advance();
         });
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln(sprintf('<info>Optimizing "%s"</info>', $indexName));
     $indexManager->optimizeIndex();
 }
コード例 #5
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var ImportAddressService $importAddressService */
     $importAddressService = $this->getHelper('container')->getByType('StreetApi\\Services\\ImportAddressService');
     $cityId = $input->getArgument('cityId');
     $xmlFile = simplexml_load_file($importAddressService->getRootDir() . '/../adresy.xml');
     if (!$xmlFile) {
         $output->writeln(PHP_EOL . '<error>Missing source file!</error>');
         return 1;
     }
     try {
         $output->writeLn('<info>Start importing addresses</info>');
         $totalCount = $xmlFile->count();
         $output->writeln(PHP_EOL . PHP_EOL . PHP_EOL . PHP_EOL);
         $progressBar = new ProgressBar($output, $totalCount);
         $progressBar->setFormat('%message%' . PHP_EOL . '%bar% %percent:3s% %' . PHP_EOL . 'count: %current%/%max%' . PHP_EOL . 'time:  %elapsed:6s%/%estimated:-6s%' . PHP_EOL);
         $progressBar->setBarCharacter('<info>■</info>');
         $progressBar->setEmptyBarCharacter(' ');
         $progressBar->setProgressCharacter('');
         $progressBar->setRedrawFrequency(ceil($totalCount / 100));
         $progressBar->start();
         $importAddressService->import($xmlFile, $progressBar, $cityId);
         $output->writeLn(PHP_EOL . '<info>Importing addresses finished</info>');
         return 0;
     } catch (\Exception $e) {
         $output->writeLn('<error>' . $e->getMessage() . '</error>');
         return 1;
     }
 }
コード例 #6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $desde = (int) $input->getArgument('desde');
     $cantidad = 100;
     $progress = null;
     $importador = new ImportadorAgentes($this->getContainer(), $this->getContainer()->get('doctrine')->getManager());
     $importador->Inicializar();
     $progress = new ProgressBar($output, $importador->ObtenerCantidadTotal());
     $progress->setRedrawFrequency(1);
     $progress->setMessage('Importando agentes...');
     $progress->start();
     $ResultadoFinal = new ResultadoImportacion($importador);
     while (true) {
         $resultado = $importador->Importar($desde, $cantidad);
         $ResultadoFinal->AgregarContadoresLote($resultado);
         if (!$resultado->HayMasRegistros()) {
             break;
         }
         $desde += $cantidad;
         $progress->advance($cantidad);
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln(' Se importaron   ' . $ResultadoFinal->RegistrosNuevos . ' registros nuevos.');
     $output->writeln(' Se actualizaron ' . $ResultadoFinal->RegistrosActualizados . ' registros.');
     $output->writeln(' Se ignoraron    ' . $ResultadoFinal->RegistrosIgnorados . ' registros.');
     $output->writeln('Importación finalizada, se procesaron ' . $ResultadoFinal->TotalRegistrosProcesados() . ' registros.');
 }
コード例 #7
0
 /**
  * Run a terminal command
  * @param  [array]         $command  [description]
  * @param  [path]          $directory [description]
  * @param  OutputInterface $output    [description]
  * @return [void]                     [description]
  */
 private function runProcess($command, $directory, $output, $alias)
 {
     $output->writeln('');
     if (is_array($command['line'])) {
         $commandLine = implode(' && ', $command['line']);
     } else {
         $commandLine = $command['line'];
     }
     $process = new Process($commandLine, $directory);
     $process->setTimeout(7600);
     $process->start();
     if ($output->isVerbose()) {
         $process->wait(function ($type, $buffer) {
             echo $buffer;
         });
     } else {
         $progress = new ProgressBar($output);
         $progress->setFormat("<comment>%message%</comment> [%bar%]");
         $progress->setMessage($command['title']);
         $progress->start();
         $progress->setRedrawFrequency(10000);
         while ($process->isRunning()) {
             $progress->advance();
         }
         $progress->finish();
         $progress->clear();
     }
     $output->writeln('');
     $output->write('<comment>' . $command['title'] . ' </comment><info>√ done</info>');
 }
コード例 #8
0
 private function reindex(string $locale, OutputInterface $output)
 {
     $totalEntities = $this->repository->getTotalCount();
     $iterations = $this->getIterations($totalEntities, $this->batchSize);
     $output->writeln(sprintf('<comment>Total entities:</comment> %s', $totalEntities));
     $output->writeln(sprintf('<comment>Batch size:</comment> %s', $this->batchSize));
     $output->writeln(sprintf('<comment>Iterations:</comment> %s', count($iterations)));
     $output->writeln(sprintf('<comment>Locale:</comment> %s', $locale));
     $output->writeln('<info>Flushing index</info>');
     $this->manager->flushIndex($locale);
     $progress = new ProgressBar($output, $totalEntities);
     $progress->setFormat('verbose');
     $progress->setRedrawFrequency($this->batchSize);
     $progress->start();
     foreach ($iterations as $iteration) {
         $entities = $this->getEntities($iteration);
         foreach ($entities as $entity) {
             $document = $this->type->createDocument($entity, $locale);
             $this->manager->addDocument($document);
             $progress->advance();
         }
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln('<info>Optimizing index</info>');
     $this->manager->optimizeIndex($locale);
 }
コード例 #9
0
 /**
  * Executes a raw import.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param OutputInterface $output
  * @param int             $bulkSize
  */
 protected function executeRawImport(Manager $manager, $filename, OutputInterface $output, $bulkSize)
 {
     $reader = $this->getReader($manager, $filename, false);
     if (class_exists('\\Symfony\\Component\\Console\\Helper\\ProgressBar')) {
         $progress = new ProgressBar($output, $reader->count());
         $progress->setRedrawFrequency(100);
         $progress->start();
     } else {
         $progress = new ProgressHelper();
         $progress->setRedrawFrequency(100);
         $progress->start($output, $reader->count());
     }
     foreach ($reader as $key => $document) {
         $data = $document['_source'];
         $data['_id'] = $document['_id'];
         $manager->getConnection()->bulk('index', $document['_type'], $data);
         if (($key + 1) % $bulkSize == 0) {
             $manager->commit();
         }
         $progress->advance();
     }
     if (($key + 1) % $bulkSize != 0) {
         $manager->commit();
     }
     $progress->finish();
     $output->writeln('');
 }
コード例 #10
0
 public function __construct(InputInterface $input, OutputInterface $output)
 {
     if (!$input->getOption('no-progress-bar')) {
         $progressBar = new ProgressBar($output);
         $progressBar->setFormat('verbose');
         $progressBar->setBarWidth(58);
         if (!$output->isDecorated()) {
             $progressBar->setRedrawFrequency(60);
         }
         $this->progressBar = $progressBar;
     } else {
         $this->isDisabled = true;
     }
 }
コード例 #11
0
ファイル: ProgressIterator.php プロジェクト: juniwalk/darwin
 public function execute()
 {
     $bar = new ProgressBar($this->output, iterator_count($this->values));
     $bar->setFormat(" %current%/%max% [%bar%] %percent:3s%%\n %message%");
     $bar->setRedrawFrequency(100);
     $this->onBeforeStart($bar);
     $bar->start();
     foreach ($this->values as $value) {
         $this->onSingleStep($bar, $value);
         usleep(250);
     }
     $this->onBeforeFinish($bar);
     $bar->finish();
     $this->output->writeln(PHP_EOL);
 }
コード例 #12
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $pathToIlios2 = $input->getArgument('pathToIlios2');
     if (!$this->symfonyFileSystem->exists($pathToIlios2)) {
         throw new \Exception("'{$pathToIlios2}' does not exist.");
     }
     $totalLearningMaterialsCount = $this->learningMaterialManager->getTotalFileLearningMaterialCount();
     $helper = $this->getHelper('question');
     $output->writeln('');
     $question = new ConfirmationQuestion('<question>Ready to copy ' . $totalLearningMaterialsCount . ' learning materials. Shall we continue? </question>' . "\n", true);
     if ($helper->ask($input, $output, $question)) {
         $progress = new ProgressBar($output, $totalLearningMaterialsCount);
         $progress->setRedrawFrequency(208);
         $output->writeln("<info>Starting migration of learning materials...</info>");
         $progress->start();
         $migrated = 0;
         $skipped = 0;
         $offset = 0;
         $limit = 50;
         while ($migrated + $skipped < $totalLearningMaterialsCount) {
             $learningMaterials = $this->learningMaterialManager->findFileLearningMaterials($limit, $offset);
             foreach ($learningMaterials as $lm) {
                 $fullPath = $pathToIlios2 . $lm->getRelativePath();
                 if (!$this->symfonyFileSystem->exists($fullPath)) {
                     $skipped++;
                 } else {
                     $file = $this->iliosFileSystem->getSymfonyFileForPath($fullPath);
                     $newPath = $this->iliosFileSystem->storeLearningMaterialFile($file);
                     $lm->setRelativePath($newPath);
                     $this->learningMaterialManager->update($lm, false);
                     $migrated++;
                 }
                 $progress->advance();
             }
             $this->learningMaterialManager->flushAndClear();
             $offset += $limit;
         }
         $progress->finish();
         $output->writeln('');
         $output->writeln("<info>Migrated {$migrated} learning materials successfully!</info>");
         if ($skipped) {
             $msg = "<comment>Skipped {$skipped} learning materials because they could not be located " . "or were already migrated.</comment>";
             $output->writeln($msg);
         }
     } else {
         $output->writeln('<comment>Migration canceled.</comment>');
     }
 }
コード例 #13
0
ファイル: DownloadHelper.php プロジェクト: gushphp/gush
 /**
  * Download a file from the URL to the destination.
  *
  * @param string $url      Fully qualified URL to the file.
  * @param bool   $progress Show the progressbar when downloading.
  */
 public function downloadFile($url, $progress = true)
 {
     /** @var ProgressBar|null $progressBar */
     $progressBar = null;
     $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar) {
         // Don't initialize the progress bar for redirects as the size is much smaller.
         if ($response->getStatusCode() >= 300) {
             return;
         }
         if (null === $progressBar) {
             ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) {
                 return $this->formatSize($bar->getMaxSteps());
             });
             ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) {
                 return str_pad($this->formatSize($bar->getProgress()), 11, ' ', STR_PAD_LEFT);
             });
             $progressBar = new ProgressBar($this->output, $size);
             $progressBar->setFormat('%current%/%max% %bar%  %percent:3s%%');
             $progressBar->setRedrawFrequency(max(1, floor($size / 1000)));
             $progressBar->setBarWidth(60);
             if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
                 $progressBar->setEmptyBarCharacter('░');
                 // light shade character \u2591
                 $progressBar->setProgressCharacter('');
                 $progressBar->setBarCharacter('▓');
                 // dark shade character \u2593
             }
             $progressBar->start();
         }
         $progressBar->setProgress($downloaded);
     };
     $client = $this->getGuzzleClient();
     if ($progress) {
         $this->output->writeln(sprintf("\n Downloading %s...\n", $url));
         $client->getEmitter()->attach(new Progress(null, $downloadCallback));
     }
     $response = $client->get($url);
     $tmpFile = $this->filesystemHelper->newTempFilename();
     $this->fs->dumpFile($tmpFile, $response->getBody());
     if (null !== $progressBar) {
         $progressBar->finish();
         $this->output->writeln("\n");
     }
     return $tmpFile;
 }
コード例 #14
0
 /**
  * Exports es index to provided file.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param array           $types
  * @param int             $chunkSize
  * @param OutputInterface $output
  */
 public function exportIndex(Manager $manager, $filename, $types, $chunkSize, OutputInterface $output)
 {
     $params = ['search_type' => 'scan', 'scroll' => '5m', 'size' => $chunkSize, 'source' => true, 'body' => ['query' => ['match_all' => []]], 'index' => $manager->getIndexName(), 'type' => $types];
     $results = new SearchHitIterator(new SearchResponseIterator($manager->getClient(), $params));
     $progress = new ProgressBar($output, $results->count());
     $progress->setRedrawFrequency(100);
     $progress->start();
     $metadata = ['count' => $results->count(), 'date' => date(\DateTime::ISO8601)];
     $writer = $this->getWriter($this->getFilePath($filename), $metadata);
     foreach ($results as $data) {
         $doc = array_intersect_key($data, array_flip(['_id', '_type', '_source', 'fields']));
         $writer->push($doc);
         $progress->advance();
     }
     $writer->finalize();
     $progress->finish();
     $output->writeln('');
 }
コード例 #15
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $scanner = new Scanner($this->getHost($input, 'scan-host'), $input->getArgument('scan'));
     $indexer = new Indexer($this->getHost($input, 'index-host'), $input->getArgument('index'));
     $indexer->setBulkSize(intval($input->getOption('bulk')));
     $output->writeln('<info>Scanning & indexing...</info>');
     $progress = new ProgressBar($output);
     $progress->setFormat('debug_nomax');
     $progress->setRedrawFrequency(100);
     foreach ($scanner->scan() as $document) {
         $indexer->index($document);
         $progress->advance();
     }
     $progress->finish();
     $output->write("\n<info>Flushing...</info>");
     $indexer->ensure();
     $output->writeln('<comment>done.</comment>');
     return 0;
 }
コード例 #16
0
 /**
  * Imports Elasticsearch index data.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param OutputInterface $output
  * @param int             $bulkSize
  */
 public function importIndex(Manager $manager, $filename, OutputInterface $output, $bulkSize)
 {
     $reader = $this->getReader($manager, $this->getFilePath($filename), false);
     $progress = new ProgressBar($output, $reader->count());
     $progress->setRedrawFrequency(100);
     $progress->start();
     foreach ($reader as $key => $document) {
         $data = $document['_source'];
         $data['_id'] = $document['_id'];
         if (array_key_exists('fields', $document)) {
             $data = array_merge($document['fields'], $data);
         }
         $manager->bulk('index', $document['_type'], $data);
         if (($key + 1) % $bulkSize == 0) {
             $manager->commit();
         }
         $progress->advance();
     }
     $manager->commit();
     $progress->finish();
     $output->writeln('');
 }
コード例 #17
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $client = new Client();
     $repository = $this->getContainer()->get('botrelli.package.doctrine.repository');
     $packages = $client->all();
     $packagesAdded = 0;
     $output->writeln("Number 5 is Analyzing Packagist packages... be patient or die, human.");
     $progress = new ProgressBar($output, count($packages));
     $progress->setRedrawFrequency(100);
     $progress->start();
     foreach ($packages as $package) {
         $localPackage = $repository->get($package);
         if (null === $localPackage) {
             $localPackage = Package::create($package);
             $repository->add($localPackage);
             $packagesAdded++;
         }
         $progress->advance();
     }
     $progress->finish();
     $output->writeln("Number 5 added {$packagesAdded} missing packages to the local Database.");
 }
コード例 #18
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var QuestionHelper $question_helper */
     $question_helper = $this->getHelper('question');
     $field_name = $question_helper->ask($input, $output, new Question("Quel champ voulez-vous extraire ?\n(Les leads ne contenant pas ce champ ne seront pas modifiées) : "));
     $attribute_name = $question_helper->ask($input, $output, new Question("Vers quel attribut de l'entité ? (Celui-ci doit déjà exister) : "));
     $setter = 'set' . ucfirst($attribute_name);
     if (!method_exists('Tellaw\\LeadsFactoryBundle\\Entity\\Leads', $setter)) {
         $output->writeln('La méthode Tellaw\\LeadsFactoryBundle\\Entity\\Leads::' . $setter . ' n\'existe pas!');
         return;
     }
     $leads_repository = $this->em->getRepository('TellawLeadsFactoryBundle:Leads');
     $leads = $leads_repository->findAll();
     $progress = new ProgressBar($output, count($leads));
     $progress->setRedrawFrequency(50);
     $count = 0;
     /** @var Leads $lead */
     foreach ($leads as $lead) {
         $progress->advance();
         $data = $lead->getData();
         if (empty($data)) {
             continue;
         }
         try {
             $data = json_decode($data, true);
         } catch (Exception $e) {
             $output->writeln($e->getMessage());
             continue;
         }
         if (array_key_exists($field_name, $data)) {
             ++$count;
             $lead->{$setter}($data[$field_name]);
         }
     }
     $progress->finish();
     $this->em->flush();
     $output->writeln('');
     $output->writeln("{$count} {$attribute_name} extraits");
 }
コード例 #19
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $totalLearningMaterialsCount = $this->learningMaterialManager->getTotalFileLearningMaterialCount();
     $progress = new ProgressBar($output, $totalLearningMaterialsCount);
     $progress->setRedrawFrequency(208);
     $output->writeln("<info>Starting validate of learning materials...</info>");
     $progress->start();
     $valid = 0;
     $broken = [];
     $offset = 0;
     $limit = 500;
     while ($valid + count($broken) < $totalLearningMaterialsCount) {
         $learningMaterials = $this->learningMaterialManager->findFileLearningMaterials($limit, $offset);
         foreach ($learningMaterials as $lm) {
             if ($this->iliosFileSystem->checkLearningMaterialFilePath($lm)) {
                 $valid++;
             } else {
                 $broken[] = ['id' => $lm->getId(), 'path' => $lm->getRelativePath()];
             }
             $progress->advance();
         }
         $offset += $limit;
     }
     $progress->finish();
     $output->writeln('');
     $output->writeln("<info>Validated {$valid} learning materials file path.</info>");
     if (count($broken)) {
         $msg = "<error>Unable to find the files for " . count($broken) . ' learning material.</error>';
         $output->writeln($msg);
         $rows = array_map(function ($arr) {
             return [$arr['id'], $arr['path']];
         }, $broken);
         $table = new Table($output);
         $table->setHeaders(array('Learning Material ID', 'Relative Path'))->setRows($rows);
         $table->render();
     }
 }
コード例 #20
0
 /**
  * Exports es index to provided file.
  *
  * @param Manager         $manager
  * @param string          $filename
  * @param int             $chunkSize
  * @param OutputInterface $output
  */
 public function exportIndex(Manager $manager, $filename, $chunkSize, OutputInterface $output)
 {
     $types = $manager->getTypesMapping();
     $repo = $manager->getRepository($types);
     $results = $this->getResults($repo, $chunkSize);
     if (class_exists('\\Symfony\\Component\\Console\\Helper\\ProgressBar')) {
         $progress = new ProgressBar($output, $results->getTotalCount());
         $progress->setRedrawFrequency(100);
         $progress->start();
     } else {
         $progress = new ProgressHelper();
         $progress->setRedrawFrequency(100);
         $progress->start($output, $results->getTotalCount());
     }
     $metadata = ['count' => $results->getTotalCount(), 'date' => date(\DateTime::ISO8601)];
     $writer = $this->getWriter($this->getFilePath($filename), $metadata);
     foreach ($results as $data) {
         $writer->push(array_intersect_key($data, array_flip(['_id', '_type', '_source'])));
         $progress->advance();
     }
     $writer->finalize();
     $progress->finish();
     $output->writeln('');
 }
コード例 #21
0
 /**
  * Fetch all media information on filesystem
  *
  * @param string $mediaBaseDir
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return array
  */
 protected function _getMediaDuplicates($mediaBaseDir, InputInterface $input, OutputInterface $output)
 {
     $quiet = $input->getOption('quiet');
     $limit = (int) $input->getOption('limit');
     $totalSteps = $this->_getTotalSteps();
     $currentStep = $this->_getCurrentStep();
     $this->_advanceNextStep();
     !$quiet && $output->writeln("<comment>Building files and hash table</comment> ({$currentStep}/{$totalSteps})");
     // Get all files without cache
     $mediaFiles = $this->_getMediaFiles($mediaBaseDir);
     // Slice and dice for fast testing
     $limit && ($mediaFiles = array_slice($mediaFiles, 0, $limit));
     $mediaFilesCount = count($mediaFiles);
     $progressBar = new ProgressBar($output, $mediaFilesCount);
     $progressBar->setRedrawFrequency(50);
     $mediaFilesHashes = $this->_getMediaFileHashes($mediaFiles, function () use($progressBar, $quiet) {
         !$quiet && $progressBar->advance();
     });
     !$quiet && $progressBar->finish();
     $currentStep = $this->_getCurrentStep();
     $this->_advanceNextStep();
     !$quiet && $output->writeln("\n<comment>Creating duplicates index</comment> ({$currentStep}/{$totalSteps})");
     $progressBar = new ProgressBar($output, $mediaFilesCount);
     $progressBar->setRedrawFrequency(50);
     $mediaFilesReduced = [];
     $mediaFilesSize = 0;
     $mediaFilesReducedSize = 0;
     array_walk($mediaFilesHashes, function ($hashInfo) use(&$mediaFilesReduced, &$mediaFilesSize, &$mediaFilesReducedSize, $quiet, $progressBar) {
         $hash = $hashInfo['hash'];
         $file = $hashInfo['file'];
         $mediaFilesSize += $hashInfo['size'];
         if (!isset($mediaFilesReduced[$hash])) {
             // Keep first file, remove all others
             $mediaFilesReducedSize += $hashInfo['size'];
             $mediaFilesReduced[$hash] = ['size' => $hashInfo['size'], 'md5sum' => $hashInfo['md5sum'], 'file' => $file, 'others' => []];
         } else {
             $mediaFilesReduced[$hash]['others'][] = $file;
         }
         !$quiet && $progressBar->advance();
     });
     !$quiet && $progressBar->finish();
     $mediaFilesReducedCount = count($mediaFilesReduced);
     $mediaFilesToReduce = array_filter($mediaFilesReduced, function ($record) {
         return !!count($record['others']);
     });
     // Display some nice stats before proceeding
     if (!$quiet) {
         $output->writeln("\n");
     }
     return ['stats' => ['count' => ['before' => $mediaFilesCount, 'after' => $mediaFilesReducedCount, 'percent' => 1 - $mediaFilesReducedCount / $mediaFilesCount], 'size' => ['before' => $mediaFilesSize, 'after' => $mediaFilesReducedSize, 'percent' => 1 - $mediaFilesReducedSize / $mediaFilesSize]], 'files' => $mediaFilesToReduce];
 }
コード例 #22
0
ファイル: UpgradeCommand.php プロジェクト: samuell/wireshell
 /**
  * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the
  * available operating system uncompressing commands and the enabled PHP extensions
  * and it downloads the file.
  *
  * @throws \RuntimeException if the ProcessWire archive could not be downloaded
  */
 private function download()
 {
     $this->output->writeln("\n  Downloading ProcessWire Version " . $this->branch['version'] . "...");
     $distill = new Distill();
     $pwArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFile($this->branch['zipURL'])->getPreferredFile();
     /** @var ProgressBar|null $progressBar */
     $progressBar = null;
     $downloadCallback = function ($size, $downloaded, $client, $request, Response $response) use(&$progressBar) {
         // Don't initialize the progress bar for redirects as the size is much smaller
         if ($response->getStatusCode() >= 300) {
             return;
         }
         if (null === $progressBar) {
             ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) {
                 return $this->formatSize($bar->getMaxSteps());
             });
             ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) {
                 return str_pad($this->formatSize($bar->getStep()), 11, ' ', STR_PAD_LEFT);
             });
             $progressBar = new ProgressBar($this->output, $size);
             $progressBar->setFormat('%current%/%max% %bar%  %percent:3s%%');
             $progressBar->setRedrawFrequency(max(1, floor($size / 1000)));
             $progressBar->setBarWidth(60);
             if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
                 $progressBar->setEmptyBarCharacter('░');
                 // light shade character \u2591
                 $progressBar->setProgressCharacter('');
                 $progressBar->setBarCharacter('▓');
                 // dark shade character \u2593
             }
             $progressBar->start();
         }
         $progressBar->setProgress($downloaded);
     };
     $client = new Client();
     $client->getEmitter()->attach(new Progress(null, $downloadCallback));
     // store the file in a temporary hidden directory with a random name
     $this->compressedFilePath = getcwd() . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'pw.' . pathinfo($pwArchiveFile, PATHINFO_EXTENSION);
     try {
         $response = $client->get($pwArchiveFile);
     } catch (ClientException $e) {
         if ($e->getCode() === 403 || $e->getCode() === 404) {
             throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Try the special \"latest\" version to install the latest stable ProcessWire release:\n" . '%s %s %s latest', $this->version, $_SERVER['PHP_SELF'], $this->getName(), $this->projectDir));
         } else {
             throw new \RuntimeException(sprintf("The selected version (%s) couldn't be downloaded because of the following error:\n%s", $this->version, $e->getMessage()));
         }
     }
     $this->fs->dumpFile($this->compressedFilePath, $response->getBody());
     if (null !== $progressBar) {
         $progressBar->finish();
         $this->output->writeln("\n");
     }
     return $this;
 }
コード例 #23
0
 /**
  * Execute the command
  *
  * @param InputInterface  $input
  * @param OutputInterface $output
  *
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $db = $input->getOption('db');
     // Throw an exception immediately if we dont have the required DB parameter
     if (empty($db)) {
         throw new \InvalidArgumentException('Database name is required (--db)');
     }
     $password = $input->getOption('password');
     if (is_null($password)) {
         $helper = $this->getHelper('question');
         $question = new Question('Password: '******'host'), $input->getOption('user'), $password);
         $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     } catch (\Exception $e) {
         throw new \RuntimeException("Can't connect to the database. Check your credentials");
     }
     // Anon READER
     $reader = new \Inet\Neuralyzer\Configuration\Reader($input->getOption('config'));
     // Now work on the DB
     $anon = new \Inet\Neuralyzer\Anonymizer\DB($pdo);
     $anon->setConfiguration($reader);
     $stopwatch = new Stopwatch();
     $stopwatch->start('Neuralyzer');
     // Get tables
     $tables = $reader->getEntities();
     foreach ($tables as $table) {
         try {
             $result = $pdo->query("SELECT COUNT(1) FROM {$table}");
         } catch (\Exception $e) {
             throw new \InvalidArgumentException("Could not count records in table '{$table}' defined in your config");
         }
         $data = $result->fetchAll(\PDO::FETCH_COLUMN);
         $total = (int) $data[0];
         if ($total === 0) {
             $output->writeln("<info>{$table} is empty</info>");
             continue;
         }
         $bar = new ProgressBar($output, $total);
         $bar->setRedrawFrequency($total > 100 ? 100 : 0);
         $output->writeln("<info>Anonymizing {$table}</info>");
         $queries = $anon->processEntity($table, function () use($bar) {
             $bar->advance();
         }, $input->getOption('pretend'), $input->getOption('sql'));
         $output->writeln(PHP_EOL);
         if ($input->getOption('sql')) {
             $output->writeln('<comment>Queries:</comment>');
             $output->writeln(implode(PHP_EOL, $queries));
             $output->writeln(PHP_EOL);
         }
     }
     // Get memory and execution time information
     $event = $stopwatch->stop('Neuralyzer');
     $memory = round($event->getMemory() / 1024 / 1024, 2);
     $time = round($event->getDuration() / 1000, 2);
     $time = $time > 180 ? round($time / 60, 2) . 'mins' : "{$time} sec";
     $output->writeln("<info>Done in {$time} using {$memory} Mb of memory</info>");
 }
コード例 #24
0
 /**
  * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the
  * available operating system uncompressing commands and the enabled PHP extensions
  * and it downloads the file.
  *
  * @return $this
  *
  * @throws \RuntimeException If the Symfony archive could not be downloaded
  */
 protected function download()
 {
     $this->output->writeln(sprintf("\n Downloading %s...\n", $this->getDownloadedApplicationType()));
     // decide which is the best compressed version to download
     $distill = new Distill();
     $symfonyArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFilesWithDifferentExtensions($this->getRemoteFileUrl(), ['tgz', 'zip'])->getPreferredFile();
     /** @var ProgressBar|null $progressBar */
     $progressBar = null;
     $downloadCallback = function (ProgressEvent $event) use(&$progressBar) {
         $downloadSize = $event->downloadSize;
         $downloaded = $event->downloaded;
         // progress bar is only displayed for files larger than 1MB
         if ($downloadSize < 1 * 1024 * 1024) {
             return;
         }
         if (null === $progressBar) {
             ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) {
                 return $this->formatSize($bar->getMaxSteps());
             });
             ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) {
                 return str_pad($this->formatSize($bar->getProgress()), 11, ' ', STR_PAD_LEFT);
             });
             $progressBar = new ProgressBar($this->output, $downloadSize);
             $progressBar->setFormat('%current%/%max% %bar%  %percent:3s%%');
             $progressBar->setRedrawFrequency(max(1, floor($downloadSize / 1000)));
             $progressBar->setBarWidth(60);
             if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
                 $progressBar->setEmptyBarCharacter('░');
                 // light shade character \u2591
                 $progressBar->setProgressCharacter('');
                 $progressBar->setBarCharacter('▓');
                 // dark shade character \u2593
             }
             $progressBar->start();
         }
         $progressBar->setProgress($downloaded);
     };
     $client = $this->getGuzzleClient();
     // store the file in a temporary hidden directory with a random name
     $this->downloadedFilePath = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'symfony.' . pathinfo($symfonyArchiveFile, PATHINFO_EXTENSION);
     try {
         $request = $client->createRequest('GET', $symfonyArchiveFile);
         $request->getEmitter()->on('progress', $downloadCallback);
         $response = $client->send($request);
     } catch (ClientException $e) {
         if ('new' === $this->getName() && ($e->getCode() === 403 || $e->getCode() === 404)) {
             throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Execute the following command to install the latest stable Symfony release:\n" . '%s new %s', $this->version, $_SERVER['PHP_SELF'], str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $this->projectDir)));
         } else {
             throw new \RuntimeException(sprintf("There was an error downloading %s from symfony.com server:\n%s", $this->getDownloadedApplicationType(), $e->getMessage()), null, $e);
         }
     }
     $this->fs->dumpFile($this->downloadedFilePath, $response->getBody());
     if (null !== $progressBar) {
         $progressBar->finish();
         $this->output->writeln("\n");
     }
     return $this;
 }
コード例 #25
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     global $amp_conf;
     /* This makes pandas sad. :( */
     if (version_compare(PHP_VERSION, '5.3.3', '<')) {
         //charset=utf8 requires php 5.3.6 (http://php.net/manual/en/mysqlinfo.concepts.charset.php)
         $output->writeln("<error>FreePBX Requires PHP Version 5.3.3 or Higher, you have: " . PHP_VERSION . "</error>");
         return false;
     }
     //still needed for module_admin and retrieve_conf
     $filePath = stream_resolve_include_path('Console/Getopt.php');
     if ($filePath === false) {
         $output->writeln("<error>PEAR must be installed (requires Console/Getopt.php)</error>");
         return false;
     }
     $this->rootPath = dirname(__DIR__);
     date_default_timezone_set('America/Los_Angeles');
     $style = new OutputFormatterStyle('white', 'black', array('bold'));
     $output->getFormatter()->setStyle('bold', $style);
     //STATIC???
     define("AMP_CONF", "/etc/amportal.conf");
     define("ODBC_INI", "/etc/odbc.ini");
     define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf");
     define("FREEPBX_CONF", "/etc/freepbx.conf");
     define("FILES_DIR", $this->rootPath . "/installlib/files");
     define("SQL_DIR", $this->rootPath . "/installlib/SQL");
     define("MODULE_DIR", $this->rootPath . "/amp_conf/htdocs/admin/modules");
     define("UPGRADE_DIR", $this->rootPath . "/upgrades");
     // Fail if !root
     $euid = posix_getpwuid(posix_geteuid());
     if ($euid['name'] != 'root') {
         $output->writeln("<error>" . $this->getName() . " must be run as root</error>");
         exit(1);
     }
     foreach ($this->settings as $key => $setting) {
         $answers[$key] = $input->getOption($key);
     }
     if ($input->isInteractive()) {
         $helper = $this->getHelper('question');
         foreach ($this->settings as $key => $setting) {
             if (isset($setting['default'])) {
                 $question = new Question($setting['description'] . ($setting['default'] ? ' [' . $setting['default'] . ']' : '') . ': ', $answers[$key]);
                 $answers[$key] = $helper->ask($input, $output, $question);
             }
         }
     }
     $dbroot = $input->getOption('rootdb');
     $force = $input->getOption('force');
     if ($force) {
         $output->writeln("<info>Force Install. This will reset everything!</info>");
     }
     if ($dbroot || $answers['dbuser'] == 'root') {
         $output->writeln("<info>Assuming you are Database Root</info>");
         $dbroot = true;
     }
     // Make sure SELinux is disabled
     $output->write("Checking if SELinux is enabled...");
     exec("getenforce 2>/dev/null", $tmpout, $ret);
     if (isset($tmpout[0]) && ($tmpout[0] === "Enabled" || $tmpout[0] === "Enforcing")) {
         $output->writeln("<error>Error!</error>");
         $output->writeln("<error>SELinux is enabled.  Please disable SELinux before installing FreePBX.</error>");
         exit(1);
     }
     $output->writeln("Its not (good)!");
     unset($tmpout);
     require_once 'installlib/installer.class.php';
     $installer = new Installer($input, $output);
     // Copy asterisk.conf
     if (!file_exists(ASTERISK_CONF)) {
         $output->write("No " . ASTERISK_CONF . " file detected. Installing...");
         $aconf = $installer->asterisk_conf_read(FILES_DIR . "/asterisk.conf");
         if (empty($aconf['directories'])) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Unable to read " . FILES_DIR . "/asterisk.conf or it was missing a directories section</error>");
             exit(1);
         }
         $aconf['directories']['astmoddir'] = file_exists('/usr/lib64/asterisk/modules') ? '/usr/lib64/asterisk/modules' : '/usr/lib/asterisk/modules';
         $installer->asterisk_conf_write(ASTERISK_CONF, $aconf);
         $asterisk_conf = $aconf['directories'];
         $output->writeln("Done!");
     } else {
         $output->write("Reading " . ASTERISK_CONF . "...");
         $aconf = $installer->asterisk_conf_read(ASTERISK_CONF);
         if (empty($aconf['directories'])) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Unable to read " . ASTERISK_CONF . " or it was missing a directories section</error>");
             exit(1);
         }
         $output->writeln("Done");
         $asterisk_conf = $aconf['directories'];
         $asterisk_defaults_conf = array('astetcdir' => '/etc/asterisk', 'astmoddir' => file_exists('/usr/lib64/asterisk/modules') ? '/usr/lib64/asterisk/modules' : '/usr/lib/asterisk/modules', 'astvarlibdir' => '/var/lib/asterisk', 'astagidir' => '/var/lib/asterisk/agi-bin', 'astspooldir' => '/var/spool/asterisk', 'astrundir' => '/var/run/asterisk', 'astlogdir' => '/var/log/asterisk');
         foreach ($asterisk_defaults_conf as $key => $value) {
             if (!isset($asterisk_conf[$key])) {
                 $asterisk_conf[$key] = $value;
             }
         }
     }
     //Check Asterisk (before file writes)
     $output->write("Checking if Asterisk is running and we can talk to it as the '" . $answers['user'] . "' user...");
     $c = 0;
     $determined = false;
     while ($c < 5) {
         exec("sudo -u " . $answers['user'] . " asterisk -rx 'core show version' 2>&1", $tmpout, $ret);
         if ($ret != 0) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Error communicating with Asterisk.  Ensure that Asterisk is properly installed and running as the " . $answers['user'] . " user</error>");
             if (file_exists($asterisk_conf['astrundir'] . "/asterisk.ctl")) {
                 $info = posix_getpwuid(fileowner($asterisk_conf['astrundir'] . "/asterisk.ctl"));
                 $output->writeln("<error>Asterisk appears to be running as " . $info['name'] . "</error>");
             } else {
                 $output->writeln("<error>Asterisk does not appear to be running</error>");
             }
             $output->writeln("<error>Try starting Asterisk with the './start_asterisk start' command in this directory</error>");
             exit(1);
         } else {
             $astver = $tmpout[0];
             unset($tmpout);
             // Parse Asterisk version.
             if (preg_match('/^Asterisk (?:SVN-|GIT-)?(?:branch-)?(\\d+(\\.\\d+)*)(-?(.*)) built/', $astver, $matches)) {
                 $determined = true;
                 if (version_compare($matches[1], "11") < 0 || version_compare($matches[1], "15", "ge")) {
                     $output->writeln("<error>Error!</error>");
                     $output->writeln("<error>Unsupported Version of " . $matches[1] . "</error>");
                     $output->writeln("<error>Supported Asterisk versions: 11, 12, 13, 14</error>");
                     exit(1);
                 }
                 break;
             }
         }
         sleep(1);
         $c++;
     }
     if (!$determined) {
         $output->writeln("<error>Error!</error>");
         $output->writeln("<error>Could not determine Asterisk version (got: " . $astver . "). Please report this.</error>");
     }
     $output->writeln("Done!");
     if (file_exists(FREEPBX_CONF) && !file_exists(AMP_CONF) || !file_exists(FREEPBX_CONF) && file_exists(AMP_CONF)) {
         if (file_exists(FREEPBX_CONF)) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Half-baked install previously detected. " . FREEPBX_CONF . " should not exist if " . AMP_CONF . " does not exist</error>");
         } else {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Half-baked install previously detected. " . AMP_CONF . " should not exist if " . FREEPBX_CONF . " does not exist</error>");
         }
         exit(1);
     }
     $output->writeln("Preliminary checks done. Starting FreePBX Installation");
     // Copy default amportal.conf
     $output->write("Checking if this is a new install...");
     if (!file_exists(AMP_CONF) || $force) {
         $output->writeln("Yes (No " . AMP_CONF . " file detected)");
         $newinstall = true;
         require_once 'amp_conf/htdocs/admin/functions.inc.php';
     } else {
         $output->writeln("No (" . AMP_CONF . " file detected)");
         $bootstrap_settings['freepbx_auth'] = false;
         $restrict_mods = true;
         if (!@(include_once getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
             include_once '/etc/asterisk/freepbx.conf';
         }
     }
     if (isset($answers['dbengine'])) {
         $amp_conf['AMPDBENGINE'] = $answers['dbengine'];
     }
     if (isset($answers['dbname'])) {
         $amp_conf['AMPDBNAME'] = $answers['dbname'];
     }
     if (isset($answers['cdrdbname'])) {
         $amp_conf['CDRDBNAME'] = $answers['cdrdbname'];
     }
     if (isset($answers['webroot'])) {
         $amp_conf['AMPWEBROOT'] = $answers['webroot'];
     }
     if (isset($answers['user'])) {
         $amp_conf['AMPASTERISKUSER'] = $answers['user'];
         $amp_conf['AMPASTERISKWEBUSER'] = $answers['user'];
         $amp_conf['AMPDEVUSER'] = $answers['user'];
     }
     if (isset($answers['group'])) {
         $amp_conf['AMPASTERISKGROUP'] = $answers['group'];
         $amp_conf['AMPASTERISKWEBGROUP'] = $answers['group'];
         $amp_conf['AMPDEVGROUP'] = $answers['group'];
     }
     if (!isset($amp_conf['AMPMANAGERHOST'])) {
         $amp_conf['AMPMANAGERHOST'] = 'localhost';
     }
     if ($newinstall || $force) {
         $amp_conf['AMPMGRUSER'] = '******';
         $amp_conf['AMPMGRPASS'] = md5(uniqid());
         $amp_conf['AMPDBUSER'] = $answers['dbuser'];
         $amp_conf['AMPDBPASS'] = $answers['dbpass'];
         $amp_conf['AMPDBHOST'] = 'localhost';
         if ($dbroot) {
             $output->write("Database Root installation checking credentials and permissions..");
         } else {
             $output->write("Database installation checking credentials and permissions..");
         }
         $dsn = $amp_conf['AMPDBENGINE'] . ":host=" . $amp_conf['AMPDBHOST'];
         try {
             $pdodb = new \PDO($dsn, $amp_conf['AMPDBUSER'], $amp_conf['AMPDBPASS']);
         } catch (\Exception $e) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Invalid Database Permissions. The error was: " . $e->getMessage() . "</error>");
             exit(1);
         }
         $output->writeln("Connected!");
     }
     if (!file_exists(ODBC_INI)) {
         $output->write("No " . ODBC_INI . " file detected. Installing...");
         if (!copy(FILES_DIR . "/odbc.ini", ODBC_INI)) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Unable to copy " . FILES_DIR . "/odbc.ini to " . ODBC_INI . "</error>");
             exit(1);
         }
         $output->writeln("Done");
     } elseif (file_exists(ODBC_INI)) {
         $conf = file_get_contents(ODBC_INI);
         $conf = trim($conf);
         if (empty($conf)) {
             $output->write("Blank " . ODBC_INI . " file detected. Installing...");
             if (!copy(FILES_DIR . "/odbc.ini", ODBC_INI)) {
                 $output->writeln("<error>Error!</error>");
                 $output->writeln("<error>Unable to copy " . FILES_DIR . "/odbc.ini to " . ODBC_INI . "</error>");
                 exit(1);
             }
             $output->writeln("Done");
         }
     }
     if (isset($asterisk_conf['astetcdir'])) {
         $amp_conf['ASTETCDIR'] = $asterisk_conf['astetcdir'];
     }
     if (isset($asterisk_conf['astmoddir'])) {
         $amp_conf['ASTMODDIR'] = $asterisk_conf['astmoddir'];
     }
     if (isset($asterisk_conf['astvarlibdir'])) {
         $amp_conf['ASTVARLIBDIR'] = $asterisk_conf['astvarlibdir'];
     }
     if (isset($asterisk_conf['astagidir'])) {
         $amp_conf['ASTAGIDIR'] = $asterisk_conf['astagidir'];
     }
     if (isset($asterisk_conf['astspooldir'])) {
         $amp_conf['ASTSPOOLDIR'] = $asterisk_conf['astspooldir'];
     }
     if (isset($asterisk_conf['astrundir'])) {
         $amp_conf['ASTRUNDIR'] = $asterisk_conf['astrundir'];
     }
     if (isset($asterisk_conf['astlogdir'])) {
         $amp_conf['ASTLOGDIR'] = $asterisk_conf['astlogdir'];
     }
     // Create database(s).
     if ($newinstall) {
         global $db;
         require_once 'amp_conf/htdocs/admin/libraries/BMO/FreePBX.class.php';
         require_once 'amp_conf/htdocs/admin/libraries/DB.class.php';
         if ($dbroot) {
             $amp_conf['AMPDBUSER'] = '******';
             $amp_conf['AMPDBPASS'] = md5(uniqid());
         } else {
             $amp_conf['AMPDBUSER'] = $answers['dbuser'];
             $amp_conf['AMPDBPASS'] = $answers['dbpass'];
         }
         if ($dbroot) {
             if ($force) {
                 $pdodb->query("DROP DATABASE IF EXISTS " . $amp_conf['AMPDBNAME']);
             }
             $pdodb->query("CREATE DATABASE IF NOT EXISTS " . $amp_conf['AMPDBNAME'] . " DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci");
             $sql = "GRANT ALL PRIVILEGES ON " . $amp_conf['AMPDBNAME'] . ".* TO '" . $amp_conf['AMPDBUSER'] . "'@'localhost' IDENTIFIED BY '" . $amp_conf['AMPDBPASS'] . "'";
             $pdodb->query($sql);
         } else {
             //check collate
         }
         $bmo = new \FreePBX($amp_conf);
         $dsn = $amp_conf['AMPDBENGINE'] . ":host=" . $amp_conf['AMPDBHOST'];
         $db = new \DB(new \FreePBX\Database($dsn, $answers['dbuser'], $answers['dbpass']));
         $db->query("USE " . $amp_conf['AMPDBNAME']);
         $sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" . $amp_conf['AMPDBNAME'] . "';";
         if (!$db->getOne($sql)) {
             $output->writeln("Empty " . $amp_conf['AMPDBNAME'] . " Database going to populate it");
             $installer->install_sql_file(SQL_DIR . '/asterisk.sql');
         }
         if ($dbroot) {
             if ($force) {
                 $db->query("DROP DATABASE IF EXISTS " . $amp_conf['CDRDBNAME']);
             }
             $db->query("CREATE DATABASE IF NOT EXISTS " . $amp_conf['CDRDBNAME'] . " DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci");
             $sql = "GRANT ALL PRIVILEGES ON " . $amp_conf['CDRDBNAME'] . ".* TO '" . $amp_conf['AMPDBUSER'] . "'@'localhost' IDENTIFIED BY '" . $amp_conf['AMPDBPASS'] . "'";
             $db->query($sql);
         } else {
             //check collate
         }
         $db->query("USE " . $amp_conf['CDRDBNAME']);
         $sql = "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '" . $amp_conf['CDRDBNAME'] . "';";
         if (!$db->getOne($sql)) {
             $output->writeln("Empty " . $amp_conf['CDRDBNAME'] . " Database going to populate it");
             $installer->install_sql_file(SQL_DIR . '/cdr.sql');
         }
         $db->query("USE " . $amp_conf['AMPDBNAME']);
     }
     // Get version of FreePBX.
     $version = $installer->get_version();
     $output->writeln("Initializing FreePBX Settings");
     $installer_amp_conf = $amp_conf;
     // freepbx_settings_init();
     $installer->freepbx_settings_init(true);
     // Use the installer defined amp_conf settings
     $freepbx_conf =& \freepbx_conf::create();
     foreach ($installer_amp_conf as $keyword => $value) {
         if ($freepbx_conf->conf_setting_exists($keyword) && $amp_conf[$keyword] != $value) {
             $output->writeln("\tChanging " . $keyword . " to match what was given at install time");
             $freepbx_conf->set_conf_values(array($keyword => $value), false, true);
         }
     }
     $freepbx_conf->commit_conf_settings();
     $output->writeln("Finished initalizing settings");
     if (!file_exists($amp_conf['AMPWEBROOT'])) {
         @mkdir($amp_conf['AMPWEBROOT'], 0777, true);
     }
     if (!is_writeable($amp_conf['AMPWEBROOT'])) {
         throw new \Exception($amp_conf['AMPWEBROOT'] . " is NOT writable!");
     }
     chown($amp_conf['AMPWEBROOT'], $amp_conf['AMPASTERISKWEBUSER']);
     // Copy amp_conf/
     $verb = $answers['dev-links'] ? "Linking" : "Copying";
     $output->writeln($verb . " files (this may take a bit)....");
     if (is_dir($this->rootPath . "/amp_conf")) {
         $total_files = $this->getFileCount($this->rootPath . "/amp_conf");
         $progress = new ProgressBar($output, $total_files);
         $progress->setRedrawFrequency(100);
         $progress->start();
         $this->recursive_copy($input, $output, $progress, $this->rootPath . "/amp_conf", "", $newinstall, $answers['dev-links']);
         $progress->finish();
     }
     $output->writeln("");
     $output->writeln("Done");
     //Last minute symlinks
     $sbin = \FreePBX::Config()->get("AMPSBIN");
     $bin = \FreePBX::Config()->get("AMPBIN");
     //Put new fwconsole into place
     if (!file_exists($sbin . "/fwconsole")) {
         $output->write("Symlinking " . $bin . "/fwconsole to " . $sbin . "/fwconsole ...");
         if (!symlink($bin . "/fwconsole", $sbin . "/fwconsole")) {
             $output->writeln("<error>Error</error>");
         }
         $output->writeln("Done");
     } elseif (file_exists($sbin . "/fwconsole") && (!is_link($sbin . "/fwconsole") || readlink($sbin . "/fwconsole") != $bin . "/fwconsole")) {
         unlink($sbin . "/fwconsole");
         $output->write("Symlinking " . $bin . "/fwconsole to " . $sbin . "/fwconsole ...");
         if (!symlink($bin . "/fwconsole", $sbin . "/fwconsole")) {
             $output->writeln("<error>Error</error>");
         }
         $output->writeln("Done");
     }
     //put old amportal into place
     if (!file_exists($sbin . "/amportal")) {
         if (is_link($sbin . "/amportal")) {
             unlink($sbin . "/amportal");
         }
         $output->write("Symlinking " . $bin . "/amportal to " . $sbin . "/amportal ...");
         if (!symlink($bin . "/amportal", $sbin . "/amportal")) {
             $output->writeln("<error>Error</error>");
         }
         $output->writeln("Done");
     } elseif (file_exists($sbin . "/amportal") && (!is_link($sbin . "/amportal") || readlink($sbin . "/amportal") != $bin . "/amportal")) {
         unlink($sbin . "/amportal");
         $output->write("Symlinking " . $bin . "/amportal to " . $sbin . "/amportal ...");
         if (!symlink($bin . "/amportal", $sbin . "/amportal")) {
             $output->writeln("<error>Error</error>");
         }
         $output->writeln("Done");
     }
     $output->write("Finishing up directory processes...");
     $binFiles = array($bin . "/freepbx_engine" => 0755, $bin . "/freepbx_setting" => 0755, $bin . "/fwconsole" => 0755, $bin . "/gen_amp_conf" => 0755, $bin . "/retrieve_conf" => 0755, $sbin . "/amportal" => 0755, $sbin . "/fwconsole" => 0755);
     foreach ($binFiles as $file => $perms) {
         if (file_exists($file)) {
             chmod($file, $perms);
         }
     }
     // Create dirs
     // 	/var/www/html/admin/modules/framework/
     // 	/var/www/html/admin/modules/_cache/
     //	./amp_conf/htdocs/admin/modules/_cache/
     $extraDirs = array($amp_conf['AMPWEBROOT'] . "/admin/modules/_cache" => 0777, $amp_conf['AMPWEBROOT'] . "/admin/modules/framework" => 0777, $amp_conf['ASTSPOOLDIR'] . "/voicemail/device" => 0755, $amp_conf['ASTSPOOLDIR'] . "/fax" => 0766, $amp_conf['ASTSPOOLDIR'] . "/monitor" => 0766);
     foreach ($extraDirs as $dir => $perms) {
         if (!file_exists($dir)) {
             mkdir($dir, $perms, true);
         }
     }
     $copyFrameworkFiles = array("module.xml", "module.sig", "install.php", "LICENSE", "README.md");
     foreach ($copyFrameworkFiles as $file) {
         if (file_exists($this->rootPath . "/" . $file)) {
             copy($this->rootPath . "/" . $file, $amp_conf['AMPWEBROOT'] . "/admin/modules/framework/" . $file);
         }
     }
     // Copy /etc/asterisk/voicemail.conf.template
     // ... to /etc/asterisk/voicemail.conf
     if (!file_exists($amp_conf['ASTETCDIR'] . "/voicemail.conf")) {
         copy($amp_conf['ASTETCDIR'] . "/voicemail.conf.template", $amp_conf['ASTETCDIR'] . "/voicemail.conf");
     }
     $output->writeln("Done!");
     // Create missing #include files.
     $output->write("Creating missing #include files...");
     foreach (glob($amp_conf['ASTETCDIR'] . "/*.conf") as $file) {
         $data = file_get_contents($file);
         if (preg_match_all("/#include\\s(.*)/", $data, $matches)) {
             if (!empty($matches[1])) {
                 foreach ($matches[1] as $include) {
                     if (!file_exists($amp_conf['ASTETCDIR'] . "/" . $include)) {
                         touch($amp_conf['ASTETCDIR'] . "/" . $include);
                     }
                 }
             }
         }
     }
     $output->writeln("Done");
     //File variable replacement
     $rfiles = array($amp_conf['ASTETCDIR'] . "/manager.conf", $amp_conf['ASTETCDIR'] . "/cdr_adaptive_odbc.conf", ODBC_INI);
     $output->write("Running variable replacement...");
     foreach ($rfiles as $file) {
         if (!file_exists($file) || !is_writable($file)) {
             continue;
         }
         $conf = file_get_contents($file);
         $replace = array('AMPMGRUSER' => $amp_conf['AMPMGRUSER'], 'AMPMGRPASS' => $amp_conf['AMPMGRPASS'], 'CDRDBNAME' => $amp_conf['CDRDBNAME'], 'AMPDBUSER' => $amp_conf['AMPDBUSER'], 'AMPDBPASS' => $amp_conf['AMPDBPASS']);
         $conf = str_replace(array_keys($replace), array_values($replace), $conf);
         file_put_contents($file, $conf);
     }
     $output->writeln("Done");
     //setup and get manager working
     $output->write("Setting up Asterisk Manager Connection...");
     exec("sudo -u " . $answers['user'] . " asterisk -rx 'module reload manager'", $o, $r);
     if ($r !== 0) {
         $output->writeln("<error>Unable to reload Asterisk Manager</error>");
         exit(127);
     }
     //TODO: we should check to make sure manager worked at this stage..
     $output->writeln("Done");
     $output->writeln("Running through upgrades...");
     // Upgrade framework (upgrades/ dir)
     $installer->install_upgrades($version);
     $output->writeln("Finished upgrades");
     $fwxml = simplexml_load_file($this->rootPath . '/module.xml');
     //setversion to whatever is in framework.xml forever for here on out.
     $fwver = (string) $fwxml->version;
     $output->write("Setting FreePBX version to " . $fwver . "...");
     $installer->set_version($fwver);
     $output->writeln("Done");
     $output->write("Writing out " . AMP_CONF . "...");
     if (!file_put_contents(AMP_CONF, $freepbx_conf->amportal_generate(true))) {
         $output->writeln("<error>Error!</error>");
         $output->writeln("<error>Unable to write to file</error>");
         exit(1);
     }
     $output->writeln("Done");
     if ($newinstall) {
         /* Write freepbx.conf */
         $conf = "<?php\n\$amp_conf['AMPDBUSER'] = '******'AMPDBUSER']}';\n\$amp_conf['AMPDBPASS'] = '******'AMPDBPASS']}';\n\$amp_conf['AMPDBHOST'] = '{$amp_conf['AMPDBHOST']}';\n\$amp_conf['AMPDBNAME'] = '{$amp_conf['AMPDBNAME']}';\n\$amp_conf['AMPDBENGINE'] = '{$amp_conf['AMPDBENGINE']}';\n\$amp_conf['datasource'] = ''; //for sqlite3\n\nrequire_once('{$amp_conf['AMPWEBROOT']}/admin/bootstrap.php');\n";
         $output->write("Writing out " . FREEPBX_CONF . "...");
         if (!file_put_contents(FREEPBX_CONF, $conf)) {
             $output->writeln("<error>Error!</error>");
             $output->writeln("<error>Unable to write to file</error>");
             exit(1);
         }
         $output->writeln("Done");
     }
     //run this here so that we make sure everything is square for asterisk
     passthru($amp_conf['AMPSBIN'] . "/fwconsole chown");
     if (!$answers['dev-links']) {
         // install_modules()
         $included_modules = array();
         /* read modules list from MODULE_DIR */
         if (file_exists(MODULE_DIR)) {
             $dir = opendir(MODULE_DIR);
             while ($file = readdir($dir)) {
                 if ($file[0] != "." && $file[0] != "_" && is_dir(MODULE_DIR . "/" . $file)) {
                     $included_modules[] = $file;
                 }
             }
             closedir($dir);
             $output->write("Installing all modules...");
             $this->install_modules($included_modules);
             $output->writeln("Done installing modules");
         }
     }
     // module_admin install framework
     $output->writeln("Installing framework...");
     $this->install_modules(array('framework'));
     $output->writeln("Done");
     // generate_configs();
     $output->writeln("Generating default configurations...");
     passthru("sudo -u " . $amp_conf['AMPASTERISKUSER'] . " " . $amp_conf["AMPBIN"] . "/retrieve_conf --skip-registry-checks");
     $output->writeln("Finished generating default configurations");
     // GPG setup - trustFreePBX();
     $output->write("Trusting FreePBX...");
     try {
         \FreePBX::GPG()->trustFreePBX();
     } catch (\Exception $e) {
         $output->writeln("<error>Error!</error>");
         $output->writeln("<error>Error while trusting FreePBX: " . $e->getMessage() . "</error>");
         exit(1);
     }
     $output->writeln("Trusted");
     //run this here so that we make sure everything is square for asterisk
     passthru($amp_conf['AMPSBIN'] . "/fwconsole chown");
     $output->writeln("<info>You have successfully installed FreePBX</info>");
 }
コード例 #26
0
 /**
  * Get media files to remove
  *
  * @param string $mediaBaseDir
  * @param InputInterface $input
  * @param OutputInterface $output
  * @return array
  */
 protected function _getMediaToRemove($mediaBaseDir, InputInterface $input, OutputInterface $output)
 {
     $quiet = $input->getOption('quiet');
     $limit = (int) $input->getOption('limit');
     $totalSteps = $this->_getTotalSteps();
     $currentStep = $this->_getCurrentStep();
     $this->_advanceNextStep();
     !$quiet && $output->writeln("<comment>Looking up files</comment> ({$currentStep}/{$totalSteps})");
     $mediaFiles = $this->_getMediaFiles($mediaBaseDir);
     $limit && ($mediaFiles = array_slice($mediaFiles, 0, $limit));
     $mediaFilesCount = count($mediaFiles);
     $progressBar = new ProgressBar($output, $mediaFilesCount);
     $progressBar->setRedrawFrequency(50);
     $mediaFilesHashes = $this->_getMediaFileHashes($mediaFiles, function () use($progressBar, $quiet) {
         !$quiet && $progressBar->advance();
     });
     !$quiet && $progressBar->finish();
     $currentStep = $this->_getCurrentStep();
     $this->_advanceNextStep();
     !$quiet && $output->writeln("\n<comment>Reading database data</comment> ({$currentStep}/{$totalSteps})");
     $values = $this->_getProductImageValues();
     $gallery = $this->_getProductImageGallery();
     $mediaFilesToRemove = [];
     $sizeBefore = 0;
     $sizeAfter = 0;
     array_walk($mediaFilesHashes, function ($hashInfo) use($mediaBaseDir, &$mediaFilesToRemove, &$sizeBefore, &$sizeAfter, &$values, &$gallery) {
         $sizeBefore += $hashInfo['size'];
         $file = str_replace($mediaBaseDir, '', $hashInfo['file']);
         if (isset($values[$file]) || isset($gallery[$file])) {
             // Exists in gallery or values
             $sizeAfter += $hashInfo['size'];
             return;
         }
         // Add to list of files to remove
         $mediaFilesToRemove[] = $hashInfo['file'];
     });
     $mediaFilesToRemoveCount = $mediaFilesCount - count($mediaFilesToRemove);
     return ['stats' => ['count' => ['before' => $mediaFilesCount, 'after' => $mediaFilesToRemoveCount, 'percent' => 1 - $mediaFilesToRemoveCount / $mediaFilesCount], 'size' => ['before' => $sizeBefore, 'after' => $sizeAfter, 'percent' => 1 - $sizeAfter / $sizeBefore]], 'files' => $mediaFilesToRemove];
 }
コード例 #27
0
ファイル: CommandJBZoo.php プロジェクト: jbzoo/cck-cli
 /**
  * Show progress bar and run the loop
  *
  * @param string   $name
  * @param int      $total
  * @param int      $stepSize
  * @param \Closure $callback
  */
 protected function _progressBar($name, $total, $stepSize, $callback)
 {
     $this->_('Current progress of ' . $name . ' (Wait! or `Ctrl+C` to cancel):');
     $progressBar = new ProgressBar($this->_out, $total);
     $progressBar->display();
     $progressBar->setRedrawFrequency(1);
     for ($currentStep = 0; $currentStep <= $total; $currentStep += $stepSize) {
         $callbackResult = $callback($currentStep, $stepSize);
         if ($callbackResult === false) {
             break;
         }
         $progressBar->setProgress($currentStep);
     }
     $progressBar->finish();
     $this->_('');
     // Progress bar hack for rendering
 }
コード例 #28
0
ファイル: DownloadCommand.php プロジェクト: cweagans/podcastr
 /**
  * Downloads $url to $save_path and displays progress in the terminal.
  *
  * @param $url
  * @param $save_path
  * @param $filename
  */
 protected function downloadFileWithProgress($url, $save_path, $filename)
 {
     $headers = get_headers($url, 1);
     $dl_progress = isset($headers['Content-Length']);
     $remote_file = fopen($url, 'rb');
     $local_file = fopen($save_path, 'wb');
     if ($remote_file && $local_file) {
         if ($dl_progress) {
             $progress = new ProgressBar($this->output, (int) $headers['Content-Length'][1]);
             $progress->setFormat("    [%bar%] %percent:3s%%");
         } else {
             $progress = new ProgressBar($this->output);
             $progress->setFormat("    [%bar%] %current%");
         }
         $progress->setRedrawFrequency(1000);
         $this->output->writeln("  - Downloading <info>{$filename}</info>");
         $progress->start();
         while (!feof($remote_file)) {
             fwrite($local_file, fread($remote_file, 1024), 1024);
             $progress->advance(1024);
         }
         $progress->finish();
         $progress->clear();
         $this->output->write("\r");
         if ($this->output->isVerbose()) {
             $file_size = (int) $headers['Content-Length'] / 1024 / 1024;
             $this->output->writeln("    <comment>Success - downloaded {$file_size} MB</comment>");
         } else {
             $this->output->writeln("    <comment>Success!</comment>");
         }
     } else {
         $this->output->writeln("<error>Couldn't open either the remote file or the local file.</error>");
     }
     fclose($remote_file);
     fclose($local_file);
 }
コード例 #29
0
ファイル: ProgressBarTest.php プロジェクト: symfony/symfony
    public function testRedrawFrequencyIsAtLeastOneIfSmallerOneGiven()
    {
        $bar = new ProgressBar($output = $this->getOutputStream());
        $bar->setRedrawFrequency(0.9);
        $bar->start();
        $bar->advance();

        rewind($output->getStream());
        $this->assertEquals(
            '    0 [>---------------------------]'.
            $this->generateOutput('    1 [->--------------------------]'),
            stream_get_contents($output->getStream())
        );
    }
コード例 #30
0
 /**
  * @return ProgressBar
  */
 private function createProgressHelper()
 {
     $progress = new ProgressBar($this->getOutput(), $this->fileCnt);
     $progress->setFormat(Message::PROGRESS_DISPLAY);
     if ($this->fileCnt > 5000) {
         $progress->setRedrawFrequency(100);
     }
     return $progress;
 }