PageRenderTime 40ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/develop/fill.php

http://github.com/phpbb/phpbb3
PHP | 189 lines | 95 code | 31 blank | 63 comment | 13 complexity | 61ac730df54b4c200a6f4c2226c39710 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. //
  14. // Security message:
  15. //
  16. // This script is potentially dangerous.
  17. // Remove or comment the next line (die(".... ) to enable this script.
  18. // Do NOT FORGET to either remove this script or disable it after you have used it.
  19. //
  20. die("Please read the first lines of this script for instructions on how to enable it");
  21. define('IN_PHPBB', true);
  22. $phpbb_root_path = './../';
  23. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  24. include($phpbb_root_path . 'common.'.$phpEx);
  25. include($phpbb_root_path . 'includes/functions_admin.'.$phpEx);
  26. set_time_limit(0);
  27. header('Expires: 0');
  28. ignore_user_abort(true);
  29. // number of topics to create
  30. $num_topics = 10000;
  31. // number of topics to be generated per call
  32. $batch_size = 2000;
  33. // max number of posts per topic
  34. $posts_per_topic = 500;
  35. // general vars
  36. $mode = $request->variable('mode', 'generate');
  37. $start = $request->variable('start', 0);
  38. switch ($mode)
  39. {
  40. case 'generate':
  41. $user_ids = $forum_ids = $topic_rows = array();
  42. $sql = 'SELECT user_id FROM ' . USERS_TABLE . ' WHERE user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ') OR user_id = ' . ANONYMOUS;
  43. $result = $db->sql_query($sql);
  44. while ($row = $db->sql_fetchrow($result))
  45. {
  46. $user_ids[] = $row['user_id'];
  47. }
  48. $db->sql_freeresult($result);
  49. $sql = 'SELECT forum_id FROM ' . FORUMS_TABLE . ' WHERE forum_type = ' . FORUM_POST;
  50. $result = $db->sql_query($sql);
  51. while ($row = $db->sql_fetchrow($result))
  52. {
  53. $forum_ids[$row['forum_id']] = $row['forum_id'];
  54. }
  55. $db->sql_freeresult($result);
  56. if (!$start)
  57. {
  58. $db->sql_query('TRUNCATE TABLE ' . POSTS_TABLE);
  59. $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE);
  60. // $db->sql_query('TRUNCATE TABLE ' . TOPICS_TABLE . '_prefetch');
  61. }
  62. $db->sql_query('LOCK TABLES ' . POSTS_TABLE . ' WRITE, ' . TOPICS_TABLE . ' WRITE');
  63. for ($topic_id = $start + 1; $topic_id < min($start + $batch_size, $num_topics + 1); ++$topic_id)
  64. {
  65. $forum_id = array_rand($forum_ids);
  66. if (count($topic_rows) == 10)
  67. {
  68. $sql = 'INSERT IGNORE INTO ' . TOPICS_TABLE . " (topic_id, forum_id, topic_title, topic_reported)
  69. VALUES " . implode(', ', $topic_rows);
  70. $db->sql_query($sql);
  71. $topic_rows = array();
  72. }
  73. $topic_rows[] = "($topic_id, $forum_id, '$forum_id-$topic_id', " . (($topic_id % 34) ? '0' : '1') . ')';
  74. $sql = 'INSERT IGNORE INTO ' . POSTS_TABLE . ' (topic_id, forum_id, poster_id, post_subject, post_text, post_username, post_visibility, post_time, post_reported)
  75. VALUES ';
  76. $rows = array();
  77. $post_time = mt_rand(0, time());
  78. $num_posts = $posts_per_topic; //mt_rand(1, $posts_per_topic);
  79. for ($i = 0; $i < $num_posts; ++$i)
  80. {
  81. $poster_id = $user_ids[array_rand($user_ids)];
  82. $poster_name = ($poster_id == ANONYMOUS) ? rndm_username() : '';
  83. $rows[] = "($topic_id, $forum_id, $poster_id, '$forum_id-$topic_id-$i', '$forum_id-$topic_id-$i', '$poster_name', " . (mt_rand(0, 12) ? '1' : '0') . ', ' . ($post_time + $i * 60) . ', ' . (mt_rand(0, 32) ? '0' : '1') . ')';
  84. }
  85. $db->sql_query($sql . implode(', ', $rows));
  86. }
  87. if (count($topic_rows))
  88. {
  89. $sql = 'INSERT IGNORE INTO ' . TOPICS_TABLE . " (topic_id, forum_id, topic_title, topic_reported)
  90. VALUES " . implode(', ', $topic_rows);
  91. $db->sql_query($sql);
  92. }
  93. $db->sql_query('UNLOCK TABLES');
  94. if ($topic_id >= $num_topics)
  95. {
  96. echo '<meta http-equiv="refresh" content="10; url=fill.' . $phpEx . '?mode=sync&amp;' . time() . '">And now for something completely different...';
  97. $db->sql_query('ANALYZE TABLES ' . TOPICS_TABLE . ', ' . POSTS_TABLE);
  98. flush();
  99. }
  100. else
  101. {
  102. echo '<meta http-equiv="refresh" content="10; url=fill.' . $phpEx . '?start=' . $topic_id . '&amp;' . time() . '">To the next page... (' . $topic_id . '/' . $num_topics . ')';
  103. flush();
  104. }
  105. break;
  106. case 'sync':
  107. /* error_reporting(E_ALL);
  108. $sync_all = TRUE;
  109. if ($sync_all)
  110. {
  111. $s = explode(' ', microtime());
  112. sync('topic', '', '', TRUE, FALSE);
  113. // sync('forum');
  114. $e = explode(' ', microtime());
  115. echo '<pre><b>' . ($e[0] + $e[1] - $s[0] - $s[1]) . '</b></pre>';
  116. echo '<a href="fill.' . $phpEx . '">Here we go again</a>';
  117. }
  118. else
  119. {
  120. $batch_size = $batch_size * 10;
  121. $end = $start + $batch_size;
  122. $s = explode(' ', microtime());
  123. sync('topic', 'range', "topic_id BETWEEN $start AND $end", TRUE, FALSE);
  124. $e = explode(' ', microtime());
  125. echo '<pre>Time taken: <b>' . ($e[0] + $e[1] - $s[0] - $s[1]) . '</b></pre>';
  126. if ($end < $num_topics)
  127. {
  128. $start += $batch_size;
  129. echo '<meta http-equiv="refresh" content="0; url=fill.' . $phpEx . "?mode=sync&amp;start=$start&amp;" . time() . "\">And now for something completely different... ($start/$num_topics)";
  130. }
  131. else
  132. {
  133. echo '<a href="fill.' . $phpEx . '">Here we go again</a>';
  134. }
  135. }
  136. if (isset($_GET['explain']))
  137. {
  138. trigger_error('Done');
  139. }
  140. */
  141. }
  142. $db->sql_close();
  143. function rndm_username()
  144. {
  145. static $usernames;
  146. if (!isset($usernames))
  147. {
  148. $usernames = get_defined_functions();
  149. $usernames = $usernames['internal'];
  150. }
  151. return $usernames[array_rand($usernames)];
  152. }