Example #1
0
/**
 * Create sample posts and display a process of creating
 *
 * @param integer Blog ID
 * @param integer Number of posts
 */
function tool_create_sample_posts($blog_ID, $num_posts)
{
    global $Messages, $DB, $Debuglog;
    $BlogCache =& get_BlogCache();
    $selected_Blog = $BlogCache->get_by_ID($blog_ID, false, false);
    if ($selected_Blog == NULL) {
        // Incorrect blog ID, Exit here
        return;
    }
    echo T_('Creating of the sample posts...');
    evo_flush();
    /**
     * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."
     */
    $DB->log_queries = false;
    $count = 1;
    $num_posts_created = 0;
    $content = T_('This is an auto generated post for testing moderation.');
    for ($i = 1; $i <= $num_posts; $i++) {
        // Spaces and line breaks make generated string look like real text
        $length = rand(300, 500);
        $word = generate_random_key($length, "\n     abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        $post_content = $content . ' ' . $word;
        $urltitle = strtolower(str_replace(array("\n", ' ', '-'), '', substr($word, 50, 20)));
        $urltitle = trim($urltitle, '-');
        $Item = new Item();
        $Item->set('title', 'Generated post ' . $i);
        $Item->set('content', $post_content);
        $Item->set('status', 'published');
        $Item->set('dateset', 1);
        // Set post main cat ID, from selected blog
        $Item->set('main_cat_ID', $selected_Blog->get_default_cat_ID());
        // Random post url slug
        $Item->set('urltitle', $urltitle);
        if ($Item->dbinsert_test()) {
            $num_posts_created++;
        }
        $count++;
        if ($count % 100 == 0) {
            // Display a process of creating by one dot for 100 posts
            echo ' .';
            //pre_dump( memory_get_usage() );
            evo_flush();
        }
        // Clear all debug messages, To avoid an error about full memory
        $Debuglog->clear('all');
    }
    echo ' OK.';
    $Messages->add(sprintf(T_('Created %d posts.'), $num_posts_created), 'success');
    if ($num_posts > $num_posts_created) {
        // Some post creation failed because of concurtent modification error
        // Note: This message should not appear offten, so it doesn't need translation
        $Messages->add(sprintf('Creation of %d post(s) failed becuase of concurrent modification error.', $num_posts - $num_posts_created), 'note');
    }
}