Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / files / management / PhabricatorFilesManagementEncodeWorkflow.php
blobf7d299bc5f64dd2936129c7f6e1953992a022e0c
1 <?php
3 final class PhabricatorFilesManagementEncodeWorkflow
4 extends PhabricatorFilesManagementWorkflow {
6 protected function didConstruct() {
7 $arguments = $this->newIteratorArguments();
9 $arguments[] = array(
10 'name' => 'as',
11 'param' => 'format',
12 'help' => pht('Select the storage format to use.'),
15 $arguments[] = array(
16 'name' => 'key',
17 'param' => 'keyname',
18 'help' => pht('Select a specific storage key.'),
21 $arguments[] = array(
22 'name' => 'force',
23 'help' => pht(
24 'Re-encode files which are already stored in the target '.
25 'encoding.'),
28 $this
29 ->setName('encode')
30 ->setSynopsis(
31 pht('Change the storage encoding of files.'))
32 ->setArguments($arguments);
35 public function execute(PhutilArgumentParser $args) {
36 $iterator = $this->buildIterator($args);
38 $force = (bool)$args->getArg('force');
40 $format_list = PhabricatorFileStorageFormat::getAllFormats();
41 $format_list = array_keys($format_list);
42 $format_list = implode(', ', $format_list);
44 $format_key = $args->getArg('as');
45 if (!strlen($format_key)) {
46 throw new PhutilArgumentUsageException(
47 pht(
48 'Use --as <format> to select a target encoding format. Available '.
49 'formats are: %s.',
50 $format_list));
53 $format = PhabricatorFileStorageFormat::getFormat($format_key);
54 if (!$format) {
55 throw new PhutilArgumentUsageException(
56 pht(
57 'Storage format "%s" is not valid. Available formats are: %s.',
58 $format_key,
59 $format_list));
62 $key_name = $args->getArg('key');
63 if (strlen($key_name)) {
64 $format->selectMasterKey($key_name);
67 $engines = PhabricatorFileStorageEngine::loadAllEngines();
69 $failed = array();
70 foreach ($iterator as $file) {
71 $monogram = $file->getMonogram();
73 $engine_key = $file->getStorageEngine();
74 $engine = idx($engines, $engine_key);
76 if (!$engine) {
77 echo tsprintf(
78 "%s\n",
79 pht(
80 '%s: Uses unknown storage engine "%s".',
81 $monogram,
82 $engine_key));
83 $failed[] = $file;
84 continue;
87 if ($engine->isChunkEngine()) {
88 echo tsprintf(
89 "%s\n",
90 pht(
91 '%s: Stored as chunks, no data to encode directly.',
92 $monogram));
93 continue;
96 if (($file->getStorageFormat() == $format_key) && !$force) {
97 echo tsprintf(
98 "%s\n",
99 pht(
100 '%s: Already encoded in target format.',
101 $monogram));
102 continue;
105 echo tsprintf(
106 "%s\n",
107 pht(
108 '%s: Changing encoding from "%s" to "%s".',
109 $monogram,
110 $file->getStorageFormat(),
111 $format_key));
113 try {
114 $file->migrateToStorageFormat($format);
116 echo tsprintf(
117 "%s\n",
118 pht('Done.'));
119 } catch (Exception $ex) {
120 echo tsprintf(
121 "%B\n",
122 pht('Failed! %s', (string)$ex));
123 $failed[] = $file;
127 if ($failed) {
128 $monograms = mpull($failed, 'getMonogram');
130 echo tsprintf(
131 "%s\n",
132 pht('Failures: %s.', implode(', ', $monograms)));
134 return 1;
137 return 0;