[pLog-svn] r1813 - plog/branches/plog-1.1-ben

oscar at devel.plogworld.net oscar at devel.plogworld.net
Mon Apr 11 20:23:28 GMT 2005


Author: oscar
Date: 2005-04-11 20:23:27 +0000 (Mon, 11 Apr 2005)
New Revision: 1813

Added:
   plog/branches/plog-1.1-ben/generateData.php
Log:
another random data generator, built upon Ben's but adding features such as customization of the number of items, multiple blogs, multiple users per blog, etc. Feel free to work on this!


Added: plog/branches/plog-1.1-ben/generateData.php
===================================================================
--- plog/branches/plog-1.1-ben/generateData.php	2005-04-11 19:48:57 UTC (rev 1812)
+++ plog/branches/plog-1.1-ben/generateData.php	2005-04-11 20:23:27 UTC (rev 1813)
@@ -0,0 +1,197 @@
+<?php
+
+if (!defined( "PLOG_CLASS_PATH" )) {
+    define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
+}
+
+include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
+include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
+
+// please tweak these parameters to suit your needs
+$params = Array ( "numUsers" => 5,
+                  "numBlogs" => 5,
+                  "numPostsPerBlog" => 10,
+                  "numCategoriesPerBlog" => 5,
+                  "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.plogworld.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 ) {
+            $permissions->grantPermission( rand(1,$param["numUsers"]),   // user id
+                                           $blog->getId(),   // blog id
+                                           PERMISSION_BLOG_USER );  // permission type
+                                           
+            $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
+                                                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/>");
+?>
\ No newline at end of file




More information about the pLog-svn mailing list