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