|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Saito - The Threaded Web Forum |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
8
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Command\Command; |
|
15
|
|
|
use Cake\Console\Arguments; |
|
16
|
|
|
use Cake\Console\ConsoleIo; |
|
17
|
|
|
use Saito\App\Registry; |
|
18
|
|
|
use Saito\RememberTrait; |
|
19
|
|
|
use Saito\User\CurrentUser\CurrentUserFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Creates dummy data for development |
|
23
|
|
|
* |
|
24
|
|
|
* @property \App\Model\Table\EntriesTable $Entries |
|
25
|
|
|
* @property \App\Model\Table\UsersTable $Users |
|
26
|
|
|
*/ |
|
27
|
|
|
class SaitoDummyDataCommand extends Command |
|
28
|
|
|
{ |
|
29
|
|
|
use RememberTrait; |
|
30
|
|
|
|
|
31
|
|
|
protected $_text = null; |
|
32
|
|
|
|
|
33
|
|
|
protected $_Threads = []; |
|
34
|
|
|
|
|
35
|
|
|
protected $_users = [ |
|
36
|
|
|
'Aaron', |
|
37
|
|
|
'Alex', |
|
38
|
|
|
'Amy', |
|
39
|
|
|
'Ana-Lucia', |
|
40
|
|
|
'Anthony', |
|
41
|
|
|
'Ben', |
|
42
|
|
|
'Bernard', |
|
43
|
|
|
'Boone', |
|
44
|
|
|
'Carmen', |
|
45
|
|
|
'Carole', |
|
46
|
|
|
'Charles', |
|
47
|
|
|
'Charlie', |
|
48
|
|
|
'Charlotte', |
|
49
|
|
|
'Christian', |
|
50
|
|
|
'Claire', |
|
51
|
|
|
'Daniel', |
|
52
|
|
|
'Danielle', |
|
53
|
|
|
'Desmond', |
|
54
|
|
|
'Dogen', |
|
55
|
|
|
'Eko', |
|
56
|
|
|
'Eloise', |
|
57
|
|
|
'Ethan', |
|
58
|
|
|
'Frank', |
|
59
|
|
|
'Frogurt', |
|
60
|
|
|
'George', |
|
61
|
|
|
'Gina', |
|
62
|
|
|
'Horace', |
|
63
|
|
|
'Hugo', |
|
64
|
|
|
'Ilana', |
|
65
|
|
|
'Jack', |
|
66
|
|
|
'Jacob', |
|
67
|
|
|
'James', |
|
68
|
|
|
'Jin', |
|
69
|
|
|
'John', |
|
70
|
|
|
'Juliet', |
|
71
|
|
|
'Kate', |
|
72
|
|
|
'Kelvin', |
|
73
|
|
|
'Liam', |
|
74
|
|
|
'Libby', |
|
75
|
|
|
'Martin', |
|
76
|
|
|
'Maninbla', |
|
77
|
|
|
'Michael', |
|
78
|
|
|
'Michelle', |
|
79
|
|
|
'Miles', |
|
80
|
|
|
'Nadia', |
|
81
|
|
|
'Naomi', |
|
82
|
|
|
'Nikki', |
|
83
|
|
|
'Omar', |
|
84
|
|
|
'Paulo', |
|
85
|
|
|
'Penny', |
|
86
|
|
|
'Pierre', |
|
87
|
|
|
'Richard', |
|
88
|
|
|
'Sarah', |
|
89
|
|
|
'Sayid', |
|
90
|
|
|
'Shannon', |
|
91
|
|
|
'Stuart', |
|
92
|
|
|
'Sun', |
|
93
|
|
|
'Teresa', |
|
94
|
|
|
'Tom', |
|
95
|
|
|
'Walt', |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function initialize(): void |
|
102
|
|
|
{ |
|
103
|
|
|
parent::initialize(); |
|
104
|
|
|
Registry::initialize(); |
|
105
|
|
|
$this->loadModel('Entries'); |
|
106
|
|
|
$this->loadModel('Users'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritDoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function execute(Arguments $args, ConsoleIo $io) |
|
113
|
|
|
{ |
|
114
|
|
|
$io->out('Hello world.'); |
|
115
|
|
|
$this->generateUsers($io); |
|
116
|
|
|
$this->generatePostings($io); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Generate postings |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Cake\Console\ConsoleIo $io I/O |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function generatePostings(ConsoleIo $io) |
|
126
|
|
|
{ |
|
127
|
|
|
$nPostings = (int)$io->ask('Number of postings to generate?', '100'); |
|
128
|
|
|
if ($nPostings === 0) { |
|
129
|
|
|
return; |
|
130
|
|
|
} |
|
131
|
|
|
$ratio = (int)$io->ask('Average answers per thread?', '10'); |
|
132
|
|
|
$seed = $nPostings / $ratio; |
|
133
|
|
|
|
|
134
|
|
|
for ($i = 0; $i < $nPostings; $i++) { |
|
135
|
|
|
$newThread = $i < $seed; |
|
136
|
|
|
|
|
137
|
|
|
$user = $this->_randomUser(); |
|
138
|
|
|
$posting = [ |
|
139
|
|
|
'name' => $user->get('username'), |
|
140
|
|
|
'subject' => "$i", |
|
141
|
|
|
'text' => rand(0, 1) ? $this->_randomText() : '', |
|
142
|
|
|
'user_id' => $user->getId(), |
|
143
|
|
|
]; |
|
144
|
|
|
if ($newThread) { |
|
145
|
|
|
$posting['pid'] = 0; |
|
146
|
|
|
$posting['category_id'] = $this->_randomCategory(); |
|
147
|
|
|
} else { |
|
148
|
|
|
$id = array_rand($this->_Threads, 1); |
|
149
|
|
|
$posting['category_id'] = $this->_Threads[$id]['category_id']; |
|
150
|
|
|
$posting['tid'] = $this->_Threads[$id]['tid']; |
|
151
|
|
|
$posting['pid'] = $this->_Threads[$id]['id']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$posting = $this->Entries->createEntry($posting); |
|
155
|
|
|
if ($posting->hasErrors()) { |
|
156
|
|
|
var_dump($posting->getErrors()); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if (empty($posting)) { |
|
160
|
|
|
throw new \RuntimeException( |
|
161
|
|
|
'Could not create posting.' |
|
162
|
|
|
); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->_progress($io, $i, $nPostings); |
|
166
|
|
|
|
|
167
|
|
|
$id = $posting->get('id'); |
|
168
|
|
|
$this->_Threads[] = [ |
|
169
|
|
|
'category_id' => $posting->get('category_id'), |
|
170
|
|
|
'id' => $id, |
|
171
|
|
|
'tid' => $posting->get('tid'), |
|
172
|
|
|
]; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$io->out(''); |
|
176
|
|
|
$io->out("Generated $i postings."); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* generate users |
|
181
|
|
|
* |
|
182
|
|
|
* @param \Cake\Console\ConsoleIo $io I/O |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
|
|
public function generateUsers(ConsoleIo $io) |
|
186
|
|
|
{ |
|
187
|
|
|
$max = count($this->_users); |
|
188
|
|
|
$n = (int)$io->ask("Number of users to generate (max: $max)?", '0'); |
|
189
|
|
|
if ($n === 0) { |
|
190
|
|
|
return; |
|
191
|
|
|
} |
|
192
|
|
|
if ($n > $max) { |
|
193
|
|
|
$n = $max; |
|
194
|
|
|
} |
|
195
|
|
|
$users = array_rand($this->_users, $n); |
|
196
|
|
|
$i = 0; |
|
197
|
|
|
foreach ($users as $user) { |
|
198
|
|
|
$name = $this->_users[$user]; |
|
199
|
|
|
$data = [ |
|
200
|
|
|
'username' => $name, |
|
201
|
|
|
'password' => 'test', |
|
202
|
|
|
'password_confirm' => 'test', |
|
203
|
|
|
'user_email' => "[email protected]", |
|
204
|
|
|
]; |
|
205
|
|
|
$this->Users->register($data, true); |
|
206
|
|
|
$this->_progress($io, $i++, $n); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$io->out(''); |
|
210
|
|
|
$io->out("Generated $i users."); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Update progress |
|
215
|
|
|
* |
|
216
|
|
|
* @param \Cake\Console\ConsoleIo $io I/O |
|
217
|
|
|
* @param int $i current |
|
218
|
|
|
* @param int $off 100% |
|
219
|
|
|
* @return void |
|
220
|
|
|
*/ |
|
221
|
|
|
protected function _progress($io, $i, $off) |
|
222
|
|
|
{ |
|
223
|
|
|
if ($i < 1) { |
|
224
|
|
|
return; |
|
225
|
|
|
} |
|
226
|
|
|
$io->out('.', 0); |
|
227
|
|
|
$line = $i % 50; |
|
228
|
|
|
if ($i > 1 && !$line) { |
|
229
|
|
|
$percent = (int)floor($i / $off * 100); |
|
230
|
|
|
$io->out(sprintf(' %3s%%', $percent), 1); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Return random category |
|
236
|
|
|
* |
|
237
|
|
|
* @return int category_id |
|
238
|
|
|
*/ |
|
239
|
|
|
protected function _randomCategory() |
|
240
|
|
|
{ |
|
241
|
|
|
$categories = $this->remember('existingCategories', function (): array { |
|
242
|
|
|
return $this->Entries->Categories->find( |
|
243
|
|
|
'all', |
|
244
|
|
|
['fields' => ['id']] |
|
245
|
|
|
)->toArray(); |
|
246
|
|
|
}); |
|
247
|
|
|
$id = array_rand($categories, 1); |
|
248
|
|
|
|
|
249
|
|
|
return $categories[$id]->get('id'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Return random user |
|
254
|
|
|
* |
|
255
|
|
|
* @return \Saito\User\CurrentUser\CurrentUserInterface a user |
|
256
|
|
|
*/ |
|
257
|
|
|
protected function _randomUser() |
|
258
|
|
|
{ |
|
259
|
|
|
$users = $this->remember('existingUsers', function (): array { |
|
260
|
|
|
return $this->Users->find( |
|
261
|
|
|
'all', |
|
262
|
|
|
['conditions' => ['activate_code' => 0]] |
|
263
|
|
|
)->toArray(); |
|
264
|
|
|
}); |
|
265
|
|
|
$id = array_rand($users, 1); |
|
266
|
|
|
|
|
267
|
|
|
$user = CurrentUserFactory::createDummy($users[$id]->toArray()); |
|
268
|
|
|
$user->set('user_type', 'admin'); |
|
269
|
|
|
|
|
270
|
|
|
return $user; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Return random text |
|
275
|
|
|
* |
|
276
|
|
|
* @return string text |
|
277
|
|
|
*/ |
|
278
|
|
|
protected function _randomText() |
|
279
|
|
|
{ |
|
280
|
|
|
if (empty($this->_text)) { |
|
281
|
|
|
$this->_text = file_get_contents( |
|
282
|
|
|
'http://loripsum.net/api/short/plaintext' |
|
283
|
|
|
); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
return $this->_text; |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|