public function downloadAction() { $request = $this->getRequest(); if (!$request instanceof ConsoleRequest) { throw new \RuntimeException('You can only use this action from a console!'); } $destination = $request->getParam('destination', ''); $languages = explode(',', $request->getParam('language', 'pt_BR')); $formats = explode(',', $request->getParam('format', 'php')); if (!file_exists($destination)) { throw new \InvalidArgumentException('Destination directory does not exists.'); } if (!is_writable($destination)) { throw new \InvalidArgumentException('Destination directory is not writeable.'); } $baseUrl = 'https://raw.githubusercontent.com/Lansoweb/losi18n-data/master/data/'; echo "Downloading files ...\n"; $adapter = new Console(); $progressBar = new ProgressBar($adapter, 0, count($languages) * count($formats)); $counter = 0; foreach ($languages as $language) { if (!file_exists("{$destination}/{$language}")) { mkdir("{$destination}/{$language}"); } foreach ($formats as $format) { $this->downloadFile("{$baseUrl}/{$language}/languages.{$format}", "{$destination}/{$language}/languages.{$format}"); $progressBar->update($counter++, 0); $this->downloadFile("{$baseUrl}/{$language}/countries.{$format}", "{$destination}/{$language}/countries.{$format}"); $progressBar->update($counter++, 0); } } $progressBar->finish(); }
/** * @param ProgressAdapter|null $progressAdapter */ public function updateAddressFormats(ProgressAdapter $progressAdapter = null) { $localeDataUri = $this->options->getLocaleDataUri(); $dataPath = $this->options->getDataPath(); $locales = json_decode($this->httpClient->setUri($localeDataUri)->send()->getContent()); foreach (scandir($dataPath) as $file) { if (fnmatch('*.json', $file)) { unlink($dataPath . '/' . $file); } } $countryCodes = isset($locales->countries) ? explode('~', $locales->countries) : []; $countryCodes[] = 'ZZ'; if ($progressAdapter !== null) { $progressBar = new ProgressBar($progressAdapter, 0, count($countryCodes)); } foreach ($countryCodes as $countryCode) { file_put_contents($dataPath . '/' . $countryCode . '.json', $this->httpClient->setUri($localeDataUri . '/' . $countryCode)->send()->getContent()); if (isset($progressBar)) { $progressBar->next(); } } if (isset($progressBar)) { $progressBar->finish(); } // We clearly don't want the "ZZ" in the array! array_pop($countryCodes); $writer = new PhpArrayWriter(); $writer->setUseBracketArraySyntax(true); $writer->toFile($this->options->getCountryCodesPath(), $countryCodes, false); }
public function expireJobsAction() { $services = $this->getServiceLocator(); $repositories = $services->get('repositories'); /* @var \Jobs\Repository\Job $jobsRepo */ $jobsRepo = $repositories->get('Jobs/Job'); $filter = \Zend\Json\Json::decode($this->params('filter', '{}')); $query = array(); $limit = 10; foreach ($filter as $key => $value) { switch ($key) { case "limit": $limit = $value; break; case "days": $date = new \DateTime(); $date->modify('-' . (int) $value . ' day'); $q = array('$lt' => $date); if (isset($query['datePublishStart.date'])) { $query['datePublishStart.date'] = array_merge($query['datePublishStart.date'], $q); } else { $query['datePublishStart.date'] = $q; } break; default: $query[$key] = $value; break; } } $query['status.name'] = 'active'; $jobs = $jobsRepo->findBy($query, null, $limit); $count = count($jobs); if (0 === $count) { return 'No jobs found.'; } foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) { $repositories->getEventManager()->removeEventListener('preUpdate', $listener); } echo "{$count} jobs found, which have to expire ...\n"; $progress = new ProgressBar(new ConsoleAdapter(array('elements' => array(ConsoleAdapter::ELEMENT_TEXT, ConsoleAdapter::ELEMENT_BAR, ConsoleAdapter::ELEMENT_PERCENT, ConsoleAdapter::ELEMENT_ETA), 'textWidth' => 20, 'barLeftChar' => '-', 'barRightChar' => ' ', 'barIndicatorChar' => '>')), 0, count($jobs)); $i = 0; /* @var \Jobs\Entity\Job $job */ foreach ($jobs as $job) { $progress->update($i++, 'Job ' . $i . ' / ' . $count); $job->changeStatus('expired'); if (0 == $i % 500) { $progress->update($i, 'Write to database...'); $repositories->flush(); } } $progress->update($i, 'Write to database...'); $repositories->flush(); $progress->update($i, 'Done'); $progress->finish(); return PHP_EOL; }
public function expireJobsAction() { $services = $this->serviceLocator; $repositories = $services->get('repositories'); /* @var \Jobs\Repository\Job $jobsRepo */ $jobsRepo = $repositories->get('Jobs/Job'); $days = (int) $this->params('days'); $limit = (string) $this->params('limit'); $info = $this->params('info'); if (!$days) { return 'Invalid value for --days. Must be integer.'; } $date = new \DateTime('today'); $date->sub(new \DateInterval('P' . $days . 'D')); $query = ['status.name' => StatusInterface::ACTIVE, 'datePublishStart.date' => ['$lt' => $date]]; $offset = 0; if ($limit && false !== strpos($limit, ',')) { list($limit, $offset) = explode(',', $limit); } $jobs = $jobsRepo->findBy($query, null, (int) $limit, (int) $offset); $count = count($jobs); if (0 === $count) { return 'No jobs found.'; } if ($info) { echo count($jobs), ' Jobs'; if ($offset) { echo ' starting from ' . $offset; } echo PHP_EOL . PHP_EOL; $this->listExpiredJobs($jobs); return; } foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) { $repositories->getEventManager()->removeEventListener('preUpdate', $listener); } echo "{$count} jobs found, which have to expire ...\n"; $progress = new ProgressBar(new ConsoleAdapter(array('elements' => array(ConsoleAdapter::ELEMENT_TEXT, ConsoleAdapter::ELEMENT_BAR, ConsoleAdapter::ELEMENT_PERCENT, ConsoleAdapter::ELEMENT_ETA), 'textWidth' => 20, 'barLeftChar' => '-', 'barRightChar' => ' ', 'barIndicatorChar' => '>')), 0, count($jobs)); $i = 0; /* @var \Jobs\Entity\Job $job */ foreach ($jobs as $job) { $progress->update($i++, 'Job ' . $i . ' / ' . $count); $job->changeStatus('expired'); if (0 == $i % 500) { $progress->update($i, 'Write to database...'); $repositories->flush(); } } $progress->update($i, 'Write to database...'); $repositories->flush(); $progress->update($i, 'Done'); $progress->finish(); return PHP_EOL; }
/** * Will create all images. * * <code> * $ php path/to/index.php image-generate --ignore * </code> * @throws \RuntimeException */ public function imageGenerateAction() { if (!$this->getRequest() instanceof ConsoleRequest) { throw new RuntimeException('You can only use this action from a console!'); } $rawFilePath = implode(DIRECTORY_SEPARATOR, [self::PATH_IMAGES, FileProperties::DIR_RAW]); //COUNT // count how many file there are. $counter = 0; foreach (new DirectoryIterator($rawFilePath) as $fileInfo) { if ($this->isImage($fileInfo)) { continue; } $counter++; } $adapter = new Console(); $progressBar = new ProgressBar($adapter, 0, $counter); $imageDirectory = new DirectoryIterator(self::PATH_IMAGES); //FOR EVERY // for every file in directory... foreach (new DirectoryIterator($rawFilePath) as $fileInfo) { if ($this->isImage($fileInfo)) { continue; } if ($this->getRequest()->getParam('ignore', false)) { $smallFilePath = implode(DIRECTORY_SEPARATOR, [self::PATH_IMAGES, FileProperties::DIR_SMALL, $fileInfo->getFilename()]); if (is_file($smallFilePath)) { $progressBar->next(); continue; } } try { (new ImageGenerator($fileInfo, $imageDirectory))->execute(); } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; } $progressBar->next(); } $progressBar->finish(); }
/** * Returns the actual progress of file up-/downloads * * @param string $id The upload to get the progress for * @return array|null */ public static function getProgress($id = null) { if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) { throw new Exception\PhpEnvironmentException('Neither APC nor uploadprogress extension installed'); } $session = 'Zend\\File\\Transfer\\Adapter\\Http\\ProgressBar'; $status = array('total' => 0, 'current' => 0, 'rate' => 0, 'message' => '', 'done' => false); if (is_array($id)) { if (isset($id['progress'])) { $adapter = $id['progress']; } if (isset($id['session'])) { $session = $id['session']; } if (isset($id['id'])) { $id = $id['id']; } else { unset($id); } } if (!empty($id) && ($id instanceof Adapter\Adapter || $id instanceof ProgressBar\ProgressBar)) { $adapter = $id; unset($id); } if (empty($id)) { if (!isset($_GET['progress_key'])) { $status['message'] = 'No upload in progress'; $status['done'] = true; } else { $id = $_GET['progress_key']; } } if (!empty($id)) { if (self::isApcAvailable()) { $call = call_user_func(self::$_callbackApc, ini_get('apc.rfc1867_prefix') . $id); if (is_array($call)) { $status = $call + $status; } } else { if (self::isUploadProgressAvailable()) { $call = call_user_func(self::$_callbackUploadProgress, $id); if (is_array($call)) { $status = $call + $status; $status['total'] = $status['bytes_total']; $status['current'] = $status['bytes_uploaded']; $status['rate'] = $status['speed_average']; if ($status['total'] == $status['current']) { $status['done'] = true; } } } } if (!is_array($call)) { $status['done'] = true; $status['message'] = 'Failure while retrieving the upload progress'; } else { if (!empty($status['cancel_upload'])) { $status['done'] = true; $status['message'] = 'The upload has been canceled'; } else { $status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']); } } $status['id'] = $id; } if (isset($adapter) && isset($status['id'])) { if ($adapter instanceof Adapter\Adapter) { $adapter = new ProgressBar\ProgressBar($adapter, 0, $status['total'], $session); } if (!$adapter instanceof ProgressBar\ProgressBar) { throw new Exception\RuntimeException('Unknown Adapter given'); } if ($status['done']) { $adapter->finish(); } else { $adapter->update($status['current'], $status['message']); } $status['progress'] = $adapter; } return $status; }
public function generateKeywordsAction() { $services = $this->getServiceLocator(); $repositories = $services->get('repositories'); $jobsRepo = $repositories->get('Jobs/Job'); $filter = \Zend\Json\Json::decode($this->params('filter', '{}')); $query = array(); $limit = 0; foreach ($filter as $key => $value) { switch ($key) { case "limit": $limit = $value; break; case "before": $date = new \DateTime($value); $q = array('$lt' => $date); if (isset($query['datePublishStart.date'])) { $query['datePublishStart.date'] = array_merge($query['datePublishStart.date'], $q); } else { $query['datePublishStart.date'] = $q; } break; case "after": $date = new \DateTime($value); $q = array('$gt' => $date); if (isset($query['datePublishStart.date'])) { $query['datePublishStart.date'] = array_merge($query['datePublishStart.date'], $q); } else { $query['datePublishStart.date'] = $q; } break; case "title": $query['title'] = '/' == $value[0] ? new \MongoRegex($value) : $value; break; default: $query[$key] = $value; break; } } $jobs = $jobsRepo->findBy($query); $jobs->limit($limit); $count = $jobs->count(true); if (0 === $count) { return 'No jobs found.'; } foreach ($repositories->getEventManager()->getListeners('preUpdate') as $listener) { $repositories->getEventManager()->removeEventListener('preUpdate', $listener); } echo "Generate keywords for {$count} jobs...\n"; $progress = new ProgressBar(new ConsoleAdapter(array('elements' => array(ConsoleAdapter::ELEMENT_TEXT, ConsoleAdapter::ELEMENT_BAR, ConsoleAdapter::ELEMENT_PERCENT, ConsoleAdapter::ELEMENT_ETA), 'textWidth' => 20, 'barLeftChar' => '-', 'barRightChar' => ' ', 'barIndicatorChar' => '>')), 0, count($jobs)); $filter = $services->get('filtermanager')->get('Core/Repository/PropertyToKeywords'); $i = 0; foreach ($jobs as $job) { $progress->update($i++, 'Job ' . $i . ' / ' . $count); $keywords = $filter->filter($job); $job->setKeywords($keywords); if (0 == $i % 500) { $progress->update($i, 'Write to database...'); $repositories->flush(); } } $progress->update($i, 'Write to database...'); $repositories->flush(); $progress->update($i, 'Done'); $progress->finish(); return PHP_EOL; }
use Zend\ProgressBar\Adapter\JsPull; use Zend\ProgressBar\ProgressBar; /** * This sample file demonstrates a simple use case of a jspull-driven progressbar */ if (isset($_GET['uploadId'])) { require_once dirname(dirname(dirname(__DIR__))) . '/library/Zend/Loader/StandardAutoloader.php'; $loader = new StandardAutoloader(array('autoregister_zf' => true)); $loader->register(); $data = uploadprogress_get_info($_GET['uploadId']); $bytesTotal = $data === null ? 0 : $data['bytes_total']; $bytesUploaded = $data === null ? 0 : $data['bytes_uploaded']; $adapter = new JsPull(); $progressBar = new ProgressBar($adapter, 0, $bytesTotal, 'uploadProgress'); if ($bytesTotal === $bytesUploaded) { $progressBar->finish(); } else { $progressBar->update($bytesUploaded); } } ?> <html> <head> <title>Zend_ProgressBar Upload Demo</title> <style type="text/css"> iframe { position: absolute; left: -100px; top: -100px; width: 10px;
/** * Handles Event FormatEvents::onSchemaEnd * * @param GenerateEvent $event */ public function onSchemaEnd(GenerateEvent $event) { $this->bar->finish(); }
public function finish() { $this->update($this->max, 'Done'); return parent::finish(); }
public function build() { if (empty($this->source)) { throw new \RuntimeException('Source directory not defined.'); } if (empty($this->destination)) { throw new \RuntimeException('Destination directory not defined.'); } if (!empty($this->language)) { $this->buildForLang($this->language); return; } $fileList = glob("{$this->source}/*.xml"); $adapter = new Console(); $progressBar = new ProgressBar($adapter, 0, count($fileList) + 1); $counter = 0; foreach ($fileList as $fileName) { $posDot = strrpos($fileName, '.'); $posSlash = strrpos($fileName, '/'); $language = substr($fileName, $posSlash + 1, $posDot - $posSlash - 1); // Only 'pt' or 'pt_BR' like (not "dead" languages like arc=Aramaic) if (strlen($language) == 2 || strlen($language) == 5) { $this->buildForLang($language); } $progressBar->update($counter++, 0); } if (count($this->natives) > 0) { $this->saveNativeLanguages($this->natives); } $progressBar->finish(); }
/** * Rebuild search index. * * <code> * $ php index.php search index * </code> * * @throws \RuntimeException * @deprecated */ public function searchIndexAction() { $request = $this->getRequest(); // Make sure that we are running in a console and the user has not tricked our // application into running this action from a public web server. if (!$request instanceof ConsoleRequest) { throw new \RuntimeException('You can only use this action from a console!'); } $sm = $this->getServiceLocator(); $index = $sm->get('Search\\Index\\Search'); //Event // index event entries. // $counter = 0; $eventService = $sm->get('Stjornvisi\\Service\\Event'); $events = $eventService->fetchAll(); $adapter = new Console(); echo "\nIndexing Event entries\n"; $progressBar = new ProgressBar($adapter, $counter, count($events)); $i = new \Stjornvisi\Search\Index\Event(); foreach ($events as $item) { $i->index($item, $index); $progressBar->update(++$counter); } $index->commit(); $progressBar->finish(); //NEWS // index news entries. // $counter = 0; $newsService = $sm->get('Stjornvisi\\Service\\News'); $news = $newsService->fetchAll(); $adapter = new Console(); echo "\nIndexing News entries\n"; $progressBar = new ProgressBar($adapter, $counter, count($news)); $i = new \Stjornvisi\Search\Index\News(); foreach ($news as $item) { $i->index($item, $index); $progressBar->update(++$counter); } $index->commit(); $progressBar->finish(); //Articles // index article entries. // $counter = 0; $articleService = $sm->get('Stjornvisi\\Service\\Article'); $articles = $articleService->fetchAll(); $adapter = new Console(); echo "\nIndexing News entries\n"; $progressBar = new ProgressBar($adapter, $counter, count($articles)); $i = new \Stjornvisi\Search\Index\Article(); foreach ($articles as $item) { $i->index($item, $index); $progressBar->update(++$counter); } $index->commit(); $progressBar->finish(); //Group // index group entries. // $counter = 0; $groupService = $sm->get('Stjornvisi\\Service\\Group'); $groups = $groupService->fetchAll(); $adapter = new Console(); echo "\nIndexing Group entries\n"; $progressBar = new ProgressBar($adapter, $counter, count($groups)); $i = new \Stjornvisi\Search\Index\Group(); foreach ($groups as $item) { $i->index($item, $index); $progressBar->update(++$counter); } $index->commit(); $progressBar->finish(); }