Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/management/PhabricatorFilesManagementEncodeWorkflow.php
13419 views
1
<?php
2
3
final class PhabricatorFilesManagementEncodeWorkflow
4
extends PhabricatorFilesManagementWorkflow {
5
6
protected function didConstruct() {
7
$arguments = $this->newIteratorArguments();
8
9
$arguments[] = array(
10
'name' => 'as',
11
'param' => 'format',
12
'help' => pht('Select the storage format to use.'),
13
);
14
15
$arguments[] = array(
16
'name' => 'key',
17
'param' => 'keyname',
18
'help' => pht('Select a specific storage key.'),
19
);
20
21
$arguments[] = array(
22
'name' => 'force',
23
'help' => pht(
24
'Re-encode files which are already stored in the target '.
25
'encoding.'),
26
);
27
28
$this
29
->setName('encode')
30
->setSynopsis(
31
pht('Change the storage encoding of files.'))
32
->setArguments($arguments);
33
}
34
35
public function execute(PhutilArgumentParser $args) {
36
$iterator = $this->buildIterator($args);
37
38
$force = (bool)$args->getArg('force');
39
40
$format_list = PhabricatorFileStorageFormat::getAllFormats();
41
$format_list = array_keys($format_list);
42
$format_list = implode(', ', $format_list);
43
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));
51
}
52
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));
60
}
61
62
$key_name = $args->getArg('key');
63
if (strlen($key_name)) {
64
$format->selectMasterKey($key_name);
65
}
66
67
$engines = PhabricatorFileStorageEngine::loadAllEngines();
68
69
$failed = array();
70
foreach ($iterator as $file) {
71
$monogram = $file->getMonogram();
72
73
$engine_key = $file->getStorageEngine();
74
$engine = idx($engines, $engine_key);
75
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;
85
}
86
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;
94
}
95
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;
103
}
104
105
echo tsprintf(
106
"%s\n",
107
pht(
108
'%s: Changing encoding from "%s" to "%s".',
109
$monogram,
110
$file->getStorageFormat(),
111
$format_key));
112
113
try {
114
$file->migrateToStorageFormat($format);
115
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;
124
}
125
}
126
127
if ($failed) {
128
$monograms = mpull($failed, 'getMonogram');
129
130
echo tsprintf(
131
"%s\n",
132
pht('Failures: %s.', implode(', ', $monograms)));
133
134
return 1;
135
}
136
137
return 0;
138
}
139
140
}
141
142