Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/lipsum/management/PhabricatorLipsumGenerateWorkflow.php
12256 views
1
<?php
2
3
final class PhabricatorLipsumGenerateWorkflow
4
extends PhabricatorLipsumManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('generate')
9
->setExamples('**generate**')
10
->setSynopsis(pht('Generate synthetic test objects.'))
11
->setArguments(
12
array(
13
array(
14
'name' => 'force',
15
'short' => 'f',
16
'help' => pht(
17
'Generate objects without prompting for confirmation.'),
18
),
19
array(
20
'name' => 'quickly',
21
'help' => pht(
22
'Generate objects as quickly as possible.'),
23
),
24
array(
25
'name' => 'args',
26
'wildcard' => true,
27
),
28
));
29
}
30
31
public function execute(PhutilArgumentParser $args) {
32
$config_key = 'phabricator.developer-mode';
33
if (!PhabricatorEnv::getEnvConfig($config_key)) {
34
throw new PhutilArgumentUsageException(
35
pht(
36
'lipsum is a development and testing tool and may only be run '.
37
'on installs in developer mode. Enable "%s" in your configuration '.
38
'to enable lipsum.',
39
$config_key));
40
}
41
42
$all_generators = id(new PhutilClassMapQuery())
43
->setAncestorClass('PhabricatorTestDataGenerator')
44
->setUniqueMethod('getGeneratorKey')
45
->execute();
46
47
$argv = $args->getArg('args');
48
$is_force = $args->getArg('force');
49
$is_quickly = $args->getArg('quickly');
50
51
$all = 'all';
52
53
if (isset($all_generators[$all])) {
54
throw new Exception(
55
pht(
56
'A lipsum generator is registered with key "%s". This key is '.
57
'reserved.',
58
$all));
59
}
60
61
if (!$argv) {
62
ksort($all_generators);
63
64
$names = array();
65
foreach ($all_generators as $generator) {
66
$names[] = tsprintf(
67
'%s (%s)',
68
$generator->getGeneratorKey(),
69
$generator->getGeneratorName());
70
}
71
72
$list = id(new PhutilConsoleList())
73
->setWrap(false)
74
->addItems($names);
75
76
id(new PhutilConsoleBlock())
77
->addParagraph(
78
pht(
79
'Choose which type or types of test data you want to generate, '.
80
'or select "%s".',
81
$all))
82
->addList($list)
83
->draw();
84
85
return 0;
86
}
87
88
$generators = array();
89
foreach ($argv as $arg_original) {
90
$arg = phutil_utf8_strtolower($arg_original);
91
92
if ($arg == 'all') {
93
$matches = $all_generators;
94
} else {
95
$matches = array();
96
foreach ($all_generators as $generator) {
97
$name = phutil_utf8_strtolower($generator->getGeneratorKey());
98
99
// If there's an exact match, select just that generator.
100
if ($arg == $name) {
101
$matches = array($generator);
102
break;
103
}
104
105
// If there's a partial match, match that generator but continue.
106
if (strpos($name, $arg) !== false) {
107
$matches[] = $generator;
108
}
109
}
110
111
if (!$matches) {
112
throw new PhutilArgumentUsageException(
113
pht(
114
'Argument "%s" does not match the name of any generators.',
115
$arg_original));
116
}
117
118
if (count($matches) > 1) {
119
throw new PhutilArgumentUsageException(
120
pht(
121
'Argument "%s" is ambiguous, and matches multiple '.
122
'generators: %s.',
123
$arg_original,
124
implode(', ', mpull($matches, 'getGeneratorName'))));
125
}
126
}
127
128
foreach ($matches as $match) {
129
$generators[] = $match;
130
}
131
}
132
133
$generators = mpull($generators, null, 'getGeneratorKey');
134
135
echo tsprintf(
136
"**<bg:blue> %s </bg>** %s\n",
137
pht('GENERATORS'),
138
pht(
139
'Selected generators: %s.',
140
implode(', ', mpull($generators, 'getGeneratorName'))));
141
142
if (!$is_force) {
143
echo tsprintf(
144
"**<bg:yellow> %s </bg>** %s\n",
145
pht('WARNING'),
146
pht(
147
'This command generates synthetic test data, including user '.
148
'accounts. It is intended for use in development environments so '.
149
'you can test features more easily. There is no easy way to delete '.
150
'this data or undo the effects of this command. If you run it in a '.
151
'production environment, it will pollute your data with large '.
152
'amounts of meaningless garbage that you can not get rid of.'));
153
154
$prompt = pht('Are you sure you want to generate piles of garbage?');
155
if (!phutil_console_confirm($prompt, true)) {
156
return;
157
}
158
}
159
160
echo tsprintf(
161
"**<bg:green> %s </bg>** %s\n",
162
pht('LIPSUM'),
163
pht(
164
'Generating synthetic test objects forever. '.
165
'Use ^C to stop when satisfied.'));
166
167
$this->generate($generators, $is_quickly);
168
}
169
170
protected function generate(array $generators, $is_quickly) {
171
$viewer = $this->getViewer();
172
173
foreach ($generators as $generator) {
174
$generator->setViewer($this->getViewer());
175
}
176
177
while (true) {
178
$generator = $generators[array_rand($generators)];
179
180
try {
181
$object = $generator->generateObject();
182
} catch (Exception $ex) {
183
echo tsprintf(
184
"**<bg:yellow> %s </bg>** %s\n",
185
pht('OOPS'),
186
pht(
187
'Generator ("%s") was unable to generate an object.',
188
$generator->getGeneratorName()));
189
190
echo tsprintf(
191
"%B\n",
192
$ex->getMessage());
193
194
continue;
195
}
196
197
if (is_string($object)) {
198
$object_phid = $object;
199
} else {
200
$object_phid = $object->getPHID();
201
}
202
203
$handles = $viewer->loadHandles(array($object_phid));
204
205
echo tsprintf(
206
"%s\n",
207
pht(
208
'Generated "%s": %s',
209
$handles[$object_phid]->getTypeName(),
210
$handles[$object_phid]->getFullName()));
211
212
if (!$is_quickly) {
213
sleep(1);
214
}
215
}
216
}
217
218
}
219
220