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 | $user = $this->_randomUser(); |
||
144 | $posting = [ |
||
145 | 'name' => $user->get('username'), |
||
146 | 'subject' => "$i", |
||
147 | 'text' => rand(0, 1) ? $this->_randomText() : '', |
||
148 | 'user_id' => $user->getId(), |
||
149 | ]; |
||
150 | if ($newThread) { |
||
151 | $posting['pid'] = 0; |
||
152 | $posting['category_id'] = $this->_randomCategory(); |
||
153 | } else { |
||
154 | $id = array_rand($this->_Threads, 1); |
||
155 | $posting['category_id'] = $this->_Threads[$id]['category_id']; |
||
156 | $posting['tid'] = $this->_Threads[$id]['tid']; |
||
157 | $posting['pid'] = $this->_Threads[$id]['id']; |
||
158 | } |
||
159 | |||
160 | $posting = $this->Entries->createEntry($posting); |
||
161 | if ($posting->hasErrors()) { |
||
162 | var_dump($posting->errors()); |
||
0 ignored issues
–
show
|
|||
163 | } |
||
164 | |||
165 | if (empty($posting)) { |
||
166 | throw new \RuntimeException( |
||
167 | 'Could not create posting.' |
||
168 | ); |
||
169 | } |
||
170 | |||
171 | $this->_progress($i, $nPostings); |
||
172 | |||
173 | $id = $posting->get('id'); |
||
174 | $this->_Threads[] = [ |
||
175 | 'category_id' => $posting->get('category_id'), |
||
176 | 'id' => $id, |
||
177 | 'tid' => $posting->get('tid'), |
||
178 | ]; |
||
179 | } |
||
180 | |||
181 | $this->out(); |
||
182 | $this->out("Generated $i postings."); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * generate users |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | public function generateUsers() |
||
191 | { |
||
192 | $max = count($this->_users); |
||
193 | $n = (int)$this->in( |
||
194 | "Number of users to generate (max: $max)?", |
||
195 | null, |
||
196 | '0' |
||
197 | ); |
||
198 | if ($n === 0) { |
||
199 | return; |
||
200 | } |
||
201 | if ($n > $max) { |
||
202 | $n = $max; |
||
203 | } |
||
204 | $users = array_rand($this->_users, $n); |
||
205 | $i = 0; |
||
206 | foreach ($users as $user) { |
||
207 | $name = $this->_users[$user]; |
||
208 | $data = [ |
||
209 | 'username' => $name, |
||
210 | 'password' => 'test', |
||
211 | 'password_confirm' => 'test', |
||
212 | 'user_email' => "[email protected]", |
||
213 | ]; |
||
214 | $this->Users->register($data, true); |
||
215 | $this->_progress($i++, $n); |
||
216 | } |
||
217 | |||
218 | $this->out(); |
||
219 | $this->out("Generated $i users."); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Update progress |
||
224 | * |
||
225 | * @param int $i current |
||
226 | * @param int $off 100% |
||
227 | * @return void |
||
228 | */ |
||
229 | protected function _progress($i, $off) |
||
230 | { |
||
231 | if ($i < 1) { |
||
232 | return; |
||
233 | } |
||
234 | $this->out('.', 0); |
||
235 | if ($i > 1 && !($i % 50)) { |
||
236 | $percent = (int)floor($i / $off * 100); |
||
237 | $this->out(sprintf(' %3s%%', $percent), 1); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Return random category |
||
243 | * |
||
244 | * @return int category_id |
||
245 | */ |
||
246 | protected function _randomCategory() |
||
247 | { |
||
248 | if ($this->_Categories === null) { |
||
249 | $this->_Categories = $this->Entries->Categories->find( |
||
250 | 'all', |
||
251 | ['fields' => ['id']] |
||
252 | )->toArray(); |
||
253 | } |
||
254 | $id = array_rand($this->_Categories, 1); |
||
255 | |||
256 | return $this->_Categories[$id]->get('id'); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Return random user |
||
261 | * |
||
262 | * @return CurrentUserInterface a user |
||
263 | */ |
||
264 | protected function _randomUser() |
||
265 | { |
||
266 | if ($this->_Users === null) { |
||
267 | $this->_Users = $this->Users->find( |
||
268 | 'all', |
||
269 | ['conditions' => ['activate_code' => 0]] |
||
270 | )->toArray(); |
||
271 | } |
||
272 | $id = array_rand($this->_Users, 1); |
||
273 | |||
274 | $user = CurrentUserFactory::createDummy($this->_Users[$id]->toArray()); |
||
275 | $user->set('user_type', 'admin'); |
||
276 | |||
277 | return $user; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Return random text |
||
282 | * |
||
283 | * @return string text |
||
284 | */ |
||
285 | protected function _randomText() |
||
286 | { |
||
287 | if (empty($this->_text)) { |
||
288 | $this->_text = file_get_contents( |
||
289 | 'http://loripsum.net/api/short/plaintext' |
||
290 | ); |
||
291 | } |
||
292 | |||
293 | return $this->_text; |
||
294 | } |
||
295 | } |
||
296 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.