]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / createsim.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
23
24 $shortoptions = 'b:g:j:n:t:u:w:x:z:';
25 $longoptions = array(
26     'subscriptions=',
27     'groups=',
28     'joins=',
29     'notices=',
30     'tags=',
31     'users=',
32     'words=',
33     'prefix=',
34     'groupprefix=',
35 //    'faves=',
36 );
37
38 $helptext = <<<END_OF_CREATESIM_HELP
39 Creates a lot of test users and notices to (loosely) simulate a real server.
40
41     -b --subscriptions Average subscriptions per user (default no. users/20)
42     -g --groups        Number of groups (default 20)
43     -j --joins         Number of groups per user (default 5)
44     -n --notices       Average notices per user (default 100)
45     -t --tags          Number of distinct hash tags (default 10000)
46     -u --users         Number of users (default 100)
47     -w --words         Words file (default '/usr/share/dict/words')
48     -x --prefix        User name prefix (default 'testuser')
49     -z --groupprefix   Group name prefix (default 'testgroup')
50
51 END_OF_CREATESIM_HELP;
52
53 require_once INSTALLDIR.'/scripts/commandline.inc';
54
55 // XXX: make these command-line options
56
57 function newUser($i)
58 {
59     global $userprefix;
60     $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
61                                  'password' => sprintf('password%d', $i),
62                                  'fullname' => sprintf('Test User %d', $i)));
63     if (!empty($user)) {
64         $user->free();
65     }
66 }
67
68 function newGroup($i, $j)
69 {
70     global $groupprefix;
71     global $userprefix;
72
73     // Pick a random user to be the admin
74
75     $n = rand(0, max($j - 1, 0));
76     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
77
78     $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
79                                         'local'    => true,
80                                         'userid'   => $user->id,
81                                         'fullname' => sprintf('Test Group %d', $i)));
82 }
83
84 function newNotice($i, $tagmax)
85 {
86     global $userprefix;
87
88     $options = array('scope' => Notice::defaultScope());
89
90     $n = rand(0, $i - 1);
91     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
92
93     $is_reply = rand(0, 1);
94
95     $content = testNoticeContent();
96
97     if ($is_reply == 0) {
98         $stream = new InboxNoticeStream($user->getProfile(), $user->getProfile());
99         $notices = $stream->getNotices(0, 20);
100         if ($notices->N > 0) {
101             $nval = rand(0, $notices->N - 1);
102             $notices->fetch(); // go to 0th
103             for ($i = 0; $i < $nval; $i++) {
104                 $notices->fetch();
105             }
106             $options['reply_to'] = $notices->id;
107             $dont_use_nickname = rand(0, 2);
108             if ($dont_use_nickname != 0) {
109                 $rprofile = $notices->getProfile();
110                 $content = "@".$rprofile->nickname." ".$content;
111             }
112             $private_to_addressees = rand(0, 4);
113             if ($private_to_addressees == 0) {
114                 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
115             }
116         }
117     } else {
118         $is_directed = rand(0, 4);
119
120         if ($is_directed == 0) {
121             $subs = $user->getSubscribed(0, 100)->fetchAll();
122             if (count($subs) > 0) {
123                 $seen = array();
124                 $f = rand(0, 9);
125                 if ($f <= 6) {
126                     $addrs = 1;
127                 } else if ($f <= 8) {
128                     $addrs = 2;
129                 } else {
130                     $addrs = 3;
131                 }
132                 for ($m = 0; $m < $addrs; $m++) {
133                     $x = rand(0, count($subs) - 1);
134                     if ($seen[$x]) {
135                         continue;
136                     }
137                     if ($subs[$x]->id == $user->id) {
138                         continue;
139                     }
140                     $seen[$x] = true;
141                     $rprofile = $subs[$x];
142                     $content = "@".$rprofile->nickname." ".$content;
143                 }
144                 $private_to_addressees = rand(0, 4);
145                 if ($private_to_addressees == 0) {
146                     $options['scope'] |= Notice::ADDRESSEE_SCOPE;
147                 }
148             }
149         }
150     }
151
152     $has_hash = rand(0, 2);
153
154     if ($has_hash == 0) {
155         $hashcount = rand(0, 2);
156         for ($j = 0; $j < $hashcount; $j++) {
157             $h = rand(0, $tagmax);
158             $content .= " #tag{$h}";
159         }
160     }
161
162     $in_group = rand(0, 5);
163
164     if ($in_group == 0) {
165         $groups = $user->getGroups();
166         if ($groups instanceof User_group) {
167             $gval = rand(0, $groups->N - 1);
168             $groups->fetch(); // go to 0th
169             for ($i = 0; $i < $gval; $i++) {
170                 $groups->fetch();
171             }
172             $options['groups'] = array($groups->id);
173             $content = "!".$groups->nickname." ".$content;
174             $private_to_group = rand(0, 2);
175             if ($private_to_group == 0) {
176                 $options['scope'] |= Notice::GROUP_SCOPE;
177             }
178         }
179     }
180
181     $private_to_site = rand(0, 4);
182
183     if ($private_to_site == 0) {
184         $options['scope'] |= Notice::SITE_SCOPE;
185     }
186
187     $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
188 }
189
190 /* Plugins should be part of the simulation too!
191 function newMessage($i)
192 {
193     global $userprefix;
194
195     $n = rand(0, $i - 1);
196     $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
197
198     $content = testNoticeContent();
199
200     $friends = $user->mutuallySubscribedUsers()->fetchAll();
201
202     if (count($friends) == 0) {
203         return;
204     }
205
206     $j = rand(0, count($friends) - 1);
207     
208     $other = $friends[$j];
209
210     $message = Message::saveNew($user->id, $other->id, $content, 'createsim');
211 }*/
212
213 function newSub($i)
214 {
215     global $userprefix;
216     $f = rand(0, $i - 1);
217
218     $fromnick = sprintf('%s%d', $userprefix, $f);
219
220     $from = User::getKV('nickname', $fromnick);
221
222     if (empty($from)) {
223         throw new Exception("Can't find user '$fromnick'.");
224     }
225
226     $t = rand(0, $i - 1);
227
228     if ($t == $f) {
229         $t++;
230         if ($t > $i - 1) {
231             $t = 0;
232         }
233     }
234
235     $tunic = sprintf('%s%d', $userprefix, $t);
236
237     $to = User::getKV('nickname', $tunic);
238
239     if (!($to instanceof User)) {
240         throw new Exception("Can't find user '$tunic'.");
241     }
242
243     Subscription::start($from->getProfile(), $to->getProfile());
244
245     $from->free();
246     $to->free();
247 }
248
249 function newJoin($u, $g)
250 {
251     global $userprefix;
252     global $groupprefix;
253
254     $userNumber = rand(0, $u - 1);
255
256     $userNick = sprintf('%s%d', $userprefix, $userNumber);
257
258     $user = User::getKV('nickname', $userNick);
259
260     if (empty($user)) {
261         throw new Exception("Can't find user '$fromnick'.");
262     }
263
264     $groupNumber = rand(0, $g - 1);
265
266     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
267
268     $group = User_group::getKV('nickname', $groupNick);
269
270     if (empty($group)) {
271         throw new Exception("Can't find group '$groupNick'.");
272     }
273
274     if (!$user->isMember($group)) {
275         $user->joinGroup($group);
276     }
277 }
278
279 /* Plugins should be part of the simulation too!
280 function newFave($u)
281 {
282     global $userprefix;
283     global $groupprefix;
284
285     $userNumber = rand(0, $u - 1);
286
287     $userNick = sprintf('%s%d', $userprefix, $userNumber);
288
289     $user = User::getKV('nickname', $userNick);
290
291     if (empty($user)) {
292         throw new Exception("Can't find user '$userNick'.");
293     }
294
295     // NB: it's OK to like your own stuff!
296
297     $otherNumber = rand(0, $u - 1);
298
299     $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
300
301     $other = User::getKV('nickname', $otherNick);
302
303     if (empty($other)) {
304         throw new Exception("Can't find user '$otherNick'.");
305     }
306
307     $notices = $other->getNotices()->fetchAll();
308
309     if (count($notices) == 0) {
310         return;
311     }
312
313     $idx = rand(0, count($notices) - 1);
314
315     $notice = $notices[$idx];
316
317     if ($user->hasFave($notice)) {
318         return;
319     }
320
321     Fave::addNew($user->getProfile(), $notice);
322 }*/
323
324 function testNoticeContent()
325 {
326     global $words;
327
328     if (is_null($words)) {
329         return "test notice content";
330     }
331
332     $cnt = rand(3, 8);
333
334     $ids = array_rand($words, $cnt);
335
336     foreach ($ids as $id) {
337         $parts[] = $words[$id];
338     }
339
340     $text = implode(' ', $parts);
341
342     if (mb_strlen($text) > 80) {
343         $text = substr($text, 0, 77) . "...";
344     }
345
346     return $text;
347 }
348
349 //function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax)
350 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
351 {
352     global $config;
353     $config['site']['dupelimit'] = -1;
354
355     $n = 0;
356     $g = 0;
357
358     // Make users first
359
360     $preuser = min($usercount, 5);
361
362     for ($j = 0; $j < $preuser; $j++) {
363         printfv("$i Creating user $n\n");
364         newUser($n);
365         $n++;
366     }
367
368     $pregroup = min($groupcount, 3);
369
370     for ($k = 0; $k < $pregroup; $k++) {
371         printfv("$i Creating group $g\n");
372         newGroup($g, $n);
373         $g++;
374     }
375
376     // # registrations + # notices + # subs
377
378     //$events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg + $messageavg));
379     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
380
381     $events -= $preuser;
382     $events -= $pregroup;
383
384     $ut = $usercount;
385     $gt = $ut + $groupcount;
386     $nt = $gt + ($usercount * $noticeavg);
387     $st = $nt + ($usercount * $subsavg);
388     $jt = $st + ($usercount * $joinsavg);
389 //    $ft = $jt + ($usercount * $favesavg);
390 //    $mt = $ft + ($usercount * $messageavg);
391
392 //    printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft, $mt)\n");
393     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
394
395     for ($i = 0; $i < $events; $i++)
396     {
397         $e = rand(0, $events);
398
399         if ($e >= 0 && $e <= $ut) {
400             printfv("$i Creating user $n\n");
401             newUser($n);
402             $n++;
403         } else if ($e > $ut && $e <= $gt) {
404             printfv("$i Creating group $g\n");
405             newGroup($g, $n);
406             $g++;
407         } else if ($e > $gt && $e <= $nt) {
408             printfv("$i Making a new notice\n");
409             newNotice($n, $tagmax);
410         } else if ($e > $nt && $e <= $st) {
411             printfv("$i Making a new subscription\n");
412             newSub($n);
413         } else if ($e > $st && $e <= $jt) {
414             printfv("$i Making a new group join\n");
415             newJoin($n, $g);
416 /*        } else if ($e > $jt && $e <= $ft) {
417             printfv("$i Making a new fave\n");
418             newFave($n);*/
419 /*        } else if ($e > $ft && $e <= $mt) {
420             printfv("$i Making a new message\n");
421             newMessage($n);*/
422         } else {
423             printfv("No event for $i!");
424         }
425     }
426 }
427
428 $defaultWordsfile = '/usr/share/dict/words';
429
430 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
431 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
432 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
433 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
434 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
435 //$favesavg    = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
436 //$messageavg  = (have_option('m', 'messages')) ? get_option_value('m', 'messages') : max($noticeavg/10, 5);
437 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
438 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
439 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
440 $wordsfile   = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
441
442 if (is_readable($wordsfile)) {
443     $words = file($wordsfile);
444 } else {
445    if ($wordsfile != $defaultWordsfile) {
446       // user specified words file couldn't be read
447       throw new Exception("Couldn't read words file: {$wordsfile}.");
448     }
449     $words = null;
450 }
451
452 try {
453     //main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax);
454     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
455 } catch (Exception $e) {
456     printfv("Got an exception: ".$e->getMessage());
457 }