[pLog-svn] r3239 - plog/trunk/tools
oscar at devel.lifetype.net
oscar at devel.lifetype.net
Tue Apr 11 22:02:59 GMT 2006
Author: oscar
Date: 2006-04-11 22:02:58 +0000 (Tue, 11 Apr 2006)
New Revision: 3239
Added:
plog/trunk/tools/generateData.php
Log:
useful script, updated version. Thanks Christoph!
Added: plog/trunk/tools/generateData.php
===================================================================
--- plog/trunk/tools/generateData.php 2006-04-11 21:46:58 UTC (rev 3238)
+++ plog/trunk/tools/generateData.php 2006-04-11 22:02:58 UTC (rev 3239)
@@ -0,0 +1,207 @@
+<?php
+
+if (!defined( "PLOG_CLASS_PATH" )) {
+ define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
+}
+
+include_once( PLOG_CLASS_PATH."class/config/configfilestorage.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/bloginfo.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/blogsettings.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/article.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/usercomment.class.php" );
+
+
+// please tweak these parameters to suit your needs
+$params = Array ( "numUsers" => 50,
+ "numBlogs" => 50,
+ "numPostsPerBlog" => 15,
+ "numCategoriesPerBlog" => 10,
+ "numCategoriesPerPost" => 3,
+ "numUsersPerBlog" => 3,
+ "numCommentsPerArticle" => 5 );
+
+// to keep track of users
+$userPool = Array();
+// to keep track of blogs
+$blogPool = Array();
+
+function generateRandomWord($lenght, $uppercase = false, $html = true) {
+ $newcode_length = 1;
+ while($newcode_length < $lenght) {
+ $a=97;
+ $b=122;
+ if ($newcode_length == 1) {
+ if (rand(1,4) == 1 || $uppercase) {
+ $a=65;
+ $b=90;
+ }
+ }
+ $code_part=chr(rand($a,$b));
+ $newcode_length++;
+ $newcode = $newcode.$code_part;
+ }
+ if ($html && rand(1, 50) == 1) {
+ return "<a href=\"http://www.lifetype.net\">$newcode</a>";
+ }
+ return $newcode;
+}
+
+function generateRandomWords( $count, $uppercase = false, $html = true) {
+ $words = generateRandomWord( rand( 3, 12), $uppercase );
+ for ($i = 1; $i < $count; $i++) {
+ $rand = rand(3, 12);
+ $words .= ' ' . generateRandomWord( $rand );
+ }
+ return $words;
+}
+
+function generateRandomParagraph() {
+ $length = rand( 3, 20 );
+ $counter = 0;
+ $text = '<p>' . generateRandomWord( rand( 3, 12 ), true );
+ while ($counter < $length) {
+ $word = generateRandomWords( rand(3, 12) );
+ $text = $text . ' ' . $word;
+ $counter++;
+ }
+ $text .= "</p>";
+ return $text;
+}
+
+function generateRandomParagraphs( $count ) {
+ $text = '';
+ for ($i = 1; $i < $count; $i++) {
+ $text .= generateRandomParagraph();
+ }
+ return $text;
+}
+
+function generateRandomDate() {
+ // generate random date between 2000-01-01 and now
+ $time = rand(strtotime('01 January 2005'), time());
+ return date("Y-m-d H:i:s", $time);
+}
+
+// generate as many users as required and put them in the pool
+$i = 0;
+$users = new Users();
+print("Adding ".$params["numUsers"]." users...<br/>");
+while( $i < $params["numUsers"] ) {
+ $text = generateRandomWord( rand( 3, 12), false, false );
+ $user = new UserInfo( $text, // username
+ $text, // password
+ "{$text}@whatever.com", // email address
+ generateRandomWords(rand(3, 12), false, false ), // about meself
+ $text." ".generateRandomWord(rand(3, 12), false, false )); // fullname
+
+ // add the user to the db
+ if( $users->addUser( $user )) {
+ $userPool[$i+1] = $user;
+ }
+ else {
+ print("There was an error adding user $i - $text<br/>");
+ }
+
+ $i++;
+}
+
+// generate the blogs and assign users from the pool to them
+$i = 0;
+$blogs = new Blogs();
+$numBlogs = $params["numBlogs"];
+$usersPerBlog = $params["numUsersPerBlog"];
+print("Adding ".$numBlogs." blogs with $usersPerBlog users<br/>");
+while( $i < $numBlogs ) {
+ $randUserId = rand(1,$params["numUsers"]);
+ $blog = new BlogInfo( generateRandomWords( rand(1, 10 )), // blog title
+ $userPool[$randUserId]->getId(), // random user owner
+ generateRandomWords( rand(1,20)), // about blog
+ new BlogSettings()); // empty settings
+ // add the blog
+ if( $blogs->addBlog( $blog )) {
+ $blogPool[$blog->getId()] = $blog;
+ // add the extra users
+ $j = 0;
+ $numUsers = rand(1,$usersPerBlog);
+ $permissions = new UserPermissions();
+
+ while( $j < $numUsers ) {
+ $perm = new UserPermission( rand(1,$params["numUsers"]), $blog->getId(),PERMISSION_BLOG_USER );
+ $permissions->grantPermission( $perm );
+ $j++;
+ }
+
+ // add the categories
+ $p = 0;
+ $categories = new ArticleCategories();
+ $blogCatPool = Array();
+ $catsPerBlog = rand(1,$params["numCategoriesPerBlog"]);
+ while( $p < $catsPerBlog ) {
+
+ // create the category
+ $category = new ArticleCategory( generateRandomWord( rand(5,12)), // cat name
+ '', // url
+ $blog->getId(), // blog id
+ generateRandomWords( rand(4,15))); // description
+ $catId = $categories->addArticleCategory( $category );
+ $category->setId( $catId );
+
+ // add it to to the blog category pool
+ $blogCatPool[$p+1] = $category;
+
+ $p++;
+ }
+
+ // add the posts
+ $k = 0;
+ $articles = new Articles();
+ $articlesPerBlog = rand(1,$params["numPostsPerBlog"]);
+ while( $k < $articlesPerBlog ) {
+ // create the post
+ $article = new Article( generateRandomWords( rand(1,5)), // topic
+ generateRandomParagraphs(rand(1,4)), // text
+ Array( $blogCatPool[rand(1,$catsPerBlog)]->getId()), // category id
+ $blog->getOwner(), // poster
+ $blog->getId(), // blog
+ POST_STATUS_PUBLISHED, // post status
+ 0 ); // number of times that the post has been read
+ $article->setDate( generateRandomDate() );
+ $article->setCommentsEnabled( true );
+
+ // add the post to the blog
+ $articles->addArticle( $article );
+
+ // add the comments per article
+ $l = 0;
+ $commentsPerArticle = rand(1,$params["numCommentsPerArticle"] );
+ $userComments = new ArticleComments();
+ while( $l < $commentsPerArticle ) {
+ $userComment = new UserComment( $article->getId(), // art id
+ $blog->getId(), // blog id
+ 0, // parent id
+ generateRandomWords(rand(1,3)), // topic
+ generateRandomParagraph()); // text
+ $userComments->addComment( $userComment );
+ $l++;
+ }
+
+ $k++;
+ }
+ }
+ else {
+ print("There was an error adding blog $i<br/>");
+ }
+
+ $i++;
+}
+
+// generate the categories and put them in the category pool
+
+print("Complete!<br/>");
+?>
More information about the pLog-svn
mailing list