]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
Make favorites in createsim
[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', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'b:g:j:n:t:u:w:x:z:';
24 $longoptions = array(
25     'subscriptions=',
26     'groups=',
27     'joins=',
28     'notices=',
29     'tags=',
30     'users=',
31     'words=',
32     'prefix=',
33     'groupprefix=',
34     'faves='
35 );
36
37 $helptext = <<<END_OF_CREATESIM_HELP
38 Creates a lot of test users and notices to (loosely) simulate a real server.
39
40     -b --subscriptions Average subscriptions per user (default no. users/20)
41     -g --groups        Number of groups (default 20)
42     -j --joins         Number of groups per user (default 5)
43     -f --faves         Number of faves per user (default notices/10)
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::staticGet('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::staticGet('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, $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     }
118
119     $has_hash = rand(0, 2);
120
121     if ($has_hash == 0) {
122         $hashcount = rand(0, 2);
123         for ($j = 0; $j < $hashcount; $j++) {
124             $h = rand(0, $tagmax);
125             $content .= " #tag{$h}";
126         }
127     }
128
129     $in_group = rand(0, 5);
130
131     if ($in_group == 0) {
132         $groups = $user->getGroups();
133         if ($groups->N > 0) {
134             $gval = rand(0, $groups->N - 1);
135             $groups->fetch(); // go to 0th
136             for ($i = 0; $i < $gval; $i++) {
137                 $groups->fetch();
138             }
139             $options['groups'] = array($groups->id);
140             $content = "!".$groups->nickname." ".$content;
141             $private_to_group = rand(0, 2);
142             if ($private_to_group == 0) {
143                 $options['scope'] |= Notice::GROUP_SCOPE;
144             }
145         }
146     }
147
148     $private_to_site = rand(0, 4);
149
150     if ($private_to_site == 0) {
151         $options['scope'] |= Notice::SITE_SCOPE;
152     }
153
154     $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
155 }
156
157 function newSub($i)
158 {
159     global $userprefix;
160     $f = rand(0, $i - 1);
161
162     $fromnick = sprintf('%s%d', $userprefix, $f);
163
164     $from = User::staticGet('nickname', $fromnick);
165
166     if (empty($from)) {
167         throw new Exception("Can't find user '$fromnick'.");
168     }
169
170     $t = rand(0, $i - 1);
171
172     if ($t == $f) {
173         $t++;
174         if ($t > $i - 1) {
175             $t = 0;
176         }
177     }
178
179     $tunic = sprintf('%s%d', $userprefix, $t);
180
181     $to = User::staticGet('nickname', $tunic);
182
183     if (empty($to)) {
184         throw new Exception("Can't find user '$tunic'.");
185     }
186
187     subs_subscribe_to($from, $to);
188
189     $from->free();
190     $to->free();
191 }
192
193 function newJoin($u, $g)
194 {
195     global $userprefix;
196     global $groupprefix;
197
198     $userNumber = rand(0, $u - 1);
199
200     $userNick = sprintf('%s%d', $userprefix, $userNumber);
201
202     $user = User::staticGet('nickname', $userNick);
203
204     if (empty($user)) {
205         throw new Exception("Can't find user '$fromnick'.");
206     }
207
208     $groupNumber = rand(0, $g - 1);
209
210     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
211
212     $group = User_group::staticGet('nickname', $groupNick);
213
214     if (empty($group)) {
215         throw new Exception("Can't find group '$groupNick'.");
216     }
217
218     if (!$user->isMember($group)) {
219         $user->joinGroup($group);
220     }
221 }
222
223 function newFave($u)
224 {
225     global $userprefix;
226     global $groupprefix;
227
228     $userNumber = rand(0, $u - 1);
229
230     $userNick = sprintf('%s%d', $userprefix, $userNumber);
231
232     $user = User::staticGet('nickname', $userNick);
233
234     if (empty($user)) {
235         throw new Exception("Can't find user '$userNick'.");
236     }
237
238     // NB: it's OK to like your own stuff!
239
240     $otherNumber = rand(0, $u - 1);
241
242     $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
243
244     $other = User::staticGet('nickname', $otherNick);
245
246     if (empty($other)) {
247         throw new Exception("Can't find user '$otherNick'.");
248     }
249
250     $notices = $other->getNotices()->fetchAll();
251
252     if (count($notices) == 0) {
253         return;
254     }
255
256     $idx = rand(0, count($notices) - 1);
257
258     $notice = $notices[$idx];
259
260     if ($user->hasFave($notice)) {
261         return;
262     }
263
264     Fave::addNew($user->getProfile(), $notice);
265 }
266
267 function testNoticeContent()
268 {
269     global $words;
270
271     if (is_null($words)) {
272         return "test notice content";
273     }
274
275     $cnt = rand(3, 8);
276
277     $ids = array_rand($words, $cnt);
278
279     foreach ($ids as $id) {
280         $parts[] = $words[$id];
281     }
282
283     $text = implode(' ', $parts);
284
285     if (mb_strlen($text) > 80) {
286         $text = substr($text, 0, 77) . "...";
287     }
288
289     return $text;
290 }
291
292 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $tagmax)
293 {
294     global $config;
295     $config['site']['dupelimit'] = -1;
296
297     $n = 0;
298     $g = 0;
299
300     // Make users first
301
302     $preuser = min($usercount, 5);
303
304     for ($j = 0; $j < $preuser; $j++) {
305         printfv("$i Creating user $n\n");
306         newUser($n);
307         $n++;
308     }
309
310     $pregroup = min($groupcount, 3);
311
312     for ($k = 0; $k < $pregroup; $k++) {
313         printfv("$i Creating group $g\n");
314         newGroup($g, $n);
315         $g++;
316     }
317
318     // # registrations + # notices + # subs
319
320     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg));
321
322     $events -= $preuser;
323     $events -= $pregroup;
324
325     $ut = $usercount;
326     $gt = $ut + $groupcount;
327     $nt = $gt + ($usercount * $noticeavg);
328     $st = $nt + ($usercount * $subsavg);
329     $jt = $st + ($usercount * $joinsavg);
330     $ft = $jt + ($usercount * $favesavg);
331
332     printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft)\n");
333
334     for ($i = 0; $i < $events; $i++)
335     {
336         $e = rand(0, $events);
337
338         if ($e >= 0 && $e <= $ut) {
339             printfv("$i Creating user $n\n");
340             newUser($n);
341             $n++;
342         } else if ($e > $ut && $e <= $gt) {
343             printfv("$i Creating group $g\n");
344             newGroup($g, $n);
345             $g++;
346         } else if ($e > $gt && $e <= $nt) {
347             printfv("$i Making a new notice\n");
348             newNotice($n, $tagmax);
349         } else if ($e > $nt && $e <= $st) {
350             printfv("$i Making a new subscription\n");
351             newSub($n);
352         } else if ($e > $st && $e <= $jt) {
353             printfv("$i Making a new group join\n");
354             newJoin($n, $g);
355         } else if ($e > $jt && $e <= $ft) {
356             printfv("$i Making a new fave\n");
357             newFave($n);
358         } else {
359             printfv("No event for $i!");
360         }
361     }
362 }
363
364 $defaultWordsfile = '/usr/share/dict/words';
365
366 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
367 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
368 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
369 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
370 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
371 $favesavg    = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
372 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
373 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
374 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
375 $wordsfile   = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
376
377 if (is_readable($wordsfile)) {
378     $words = file($wordsfile);
379 } else {
380    if ($wordsfile != $defaultWordsfile) {
381       // user specified words file couldn't be read
382       throw new Exception("Couldn't read words file: {$wordsfile}.");
383     }
384     $words = null;
385 }
386
387 try {
388     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $tagmax);
389 } catch (Exception $e) {
390     printfv("Got an exception: ".$e->getMessage());
391 }