Path: blob/master/src/applications/files/management/PhabricatorFilesManagementEncodeWorkflow.php
13419 views
<?php12final class PhabricatorFilesManagementEncodeWorkflow3extends PhabricatorFilesManagementWorkflow {45protected function didConstruct() {6$arguments = $this->newIteratorArguments();78$arguments[] = array(9'name' => 'as',10'param' => 'format',11'help' => pht('Select the storage format to use.'),12);1314$arguments[] = array(15'name' => 'key',16'param' => 'keyname',17'help' => pht('Select a specific storage key.'),18);1920$arguments[] = array(21'name' => 'force',22'help' => pht(23'Re-encode files which are already stored in the target '.24'encoding.'),25);2627$this28->setName('encode')29->setSynopsis(30pht('Change the storage encoding of files.'))31->setArguments($arguments);32}3334public function execute(PhutilArgumentParser $args) {35$iterator = $this->buildIterator($args);3637$force = (bool)$args->getArg('force');3839$format_list = PhabricatorFileStorageFormat::getAllFormats();40$format_list = array_keys($format_list);41$format_list = implode(', ', $format_list);4243$format_key = $args->getArg('as');44if (!strlen($format_key)) {45throw new PhutilArgumentUsageException(46pht(47'Use --as <format> to select a target encoding format. Available '.48'formats are: %s.',49$format_list));50}5152$format = PhabricatorFileStorageFormat::getFormat($format_key);53if (!$format) {54throw new PhutilArgumentUsageException(55pht(56'Storage format "%s" is not valid. Available formats are: %s.',57$format_key,58$format_list));59}6061$key_name = $args->getArg('key');62if (strlen($key_name)) {63$format->selectMasterKey($key_name);64}6566$engines = PhabricatorFileStorageEngine::loadAllEngines();6768$failed = array();69foreach ($iterator as $file) {70$monogram = $file->getMonogram();7172$engine_key = $file->getStorageEngine();73$engine = idx($engines, $engine_key);7475if (!$engine) {76echo tsprintf(77"%s\n",78pht(79'%s: Uses unknown storage engine "%s".',80$monogram,81$engine_key));82$failed[] = $file;83continue;84}8586if ($engine->isChunkEngine()) {87echo tsprintf(88"%s\n",89pht(90'%s: Stored as chunks, no data to encode directly.',91$monogram));92continue;93}9495if (($file->getStorageFormat() == $format_key) && !$force) {96echo tsprintf(97"%s\n",98pht(99'%s: Already encoded in target format.',100$monogram));101continue;102}103104echo tsprintf(105"%s\n",106pht(107'%s: Changing encoding from "%s" to "%s".',108$monogram,109$file->getStorageFormat(),110$format_key));111112try {113$file->migrateToStorageFormat($format);114115echo tsprintf(116"%s\n",117pht('Done.'));118} catch (Exception $ex) {119echo tsprintf(120"%B\n",121pht('Failed! %s', (string)$ex));122$failed[] = $file;123}124}125126if ($failed) {127$monograms = mpull($failed, 'getMonogram');128129echo tsprintf(130"%s\n",131pht('Failures: %s.', implode(', ', $monograms)));132133return 1;134}135136return 0;137}138139}140141142