b2evolution PHP Cross Reference Blogging Systems

Source: /inc/tools/model/_tool.funcs.php - 427 lines - 13494 bytes - Summary - Text - Print

Description: This file implements functions to work with tools. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements functions to work with tools.

   4   *

   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.

   9   *

  10   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)

  11   *

  12   * @package evocore

  13   *

  14   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  15   * @author fplanque: Francois PLANQUE.

  16   *

  17   * @version $Id: _tool.funcs.php 1500 2012-07-10 11:38:31Z yura $

  18   */
  19  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  20  
  21  
  22  /**

  23   * Create sample comments and display a process of creating

  24   *

  25   * @param integer Blog ID

  26   * @param integer Number of comments

  27   * @param integer Number of posts

  28   */
  29  function tool_create_sample_comments( $blog_ID, $num_comments, $num_posts )
  30  {
  31      global $DB, $posttypes_specialtypes, $localtimenow, $Hit, $Messages, $Debuglog;
  32  
  33      $BlogCache = & get_BlogCache();
  34      $selected_Blog = $BlogCache->get_by_ID( $blog_ID, false, false );
  35      if( $selected_Blog == NULL )
  36      { // Incorrect blog ID, Exit here
  37          return;
  38      }
  39  
  40      echo T_('Creating of the sample comments...');
  41      evo_flush();
  42  
  43      /**

  44       * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."

  45       */
  46      $DB->log_queries = false;
  47  
  48      $curr_orderby = $selected_Blog->get_setting('orderby');
  49      if( $curr_orderby == 'RAND' )
  50      {
  51          $curr_orderby .= '()';
  52      }
  53      else
  54      {
  55          $curr_orderby = 'post_'.$curr_orderby;
  56      }
  57      $curr_orderdir = $selected_Blog->get_setting('orderdir');
  58  
  59      // find the $num_posts latest posts in blog

  60      $SQL = new SQL();
  61      $SQL->SELECT( 'post_ID' );
  62      $SQL->FROM( 'T_items__item' );
  63      $SQL->FROM_add( 'INNER JOIN T_categories ON post_main_cat_ID = cat_ID' );
  64      $SQL->WHERE( 'cat_blog_ID = '.$DB->quote( $blog_ID ) );
  65      $SQL->WHERE_and( 'post_status = '.$DB->quote( 'published' ) );
  66      // Set condition to not create sample comments for special posts

  67      $SQL->WHERE_and( 'post_ptyp_ID NOT IN ( '.$DB->quote( $posttypes_specialtypes ).' )' );
  68      $SQL->ORDER_BY( $curr_orderby.' '.$curr_orderdir.', post_ID '.$curr_orderdir );
  69      $SQL->LIMIT( $num_posts );
  70      $items_result = $DB->get_results( $SQL->get(), ARRAY_A, 'Find the x latest posts in blog' );
  71  
  72      $count = 1;
  73      $fix_content = 'This is an auto generated comment for testing the moderation features.
  74                      http://www.test.com/test_comment_';
  75      // go through on selected items

  76      foreach( $items_result as $row )
  77      {
  78          $item_ID = $row['post_ID'];
  79  
  80          $ItemCache = & get_ItemCache();
  81          $commented_Item = & $ItemCache->get_by_ID( $item_ID );
  82  
  83          // create $num_comments comments for each item

  84          for( $i = 0; $i < $num_comments; $i++ )
  85          {
  86              $author = 'Test '.$count;
  87              $email = 'test_'.$count.'@test.com';
  88              $url = 'http://www.test-'.rand(1,3).'.com/test_comment_'.$count;
  89  
  90              $content = $fix_content.$count;
  91              for( $j = 0; $j < 50; $j++ )
  92              { // create 50 random word
  93                  $length = rand(1, 15);
  94                  $word = generate_random_key( $length, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' );
  95                  $content = $content.' '.$word;
  96              }
  97  
  98              // create and save a new comment

  99              $Comment = new Comment();
 100              $Comment->set_Item( $commented_Item );
 101              $Comment->set( 'status', 'draft' );
 102              $Comment->set( 'author', $author );
 103              $Comment->set( 'author_email', $email );
 104              $Comment->set( 'author_url', $url );
 105              $Comment->set( 'content', $content );
 106              $Comment->set( 'date', date( 'Y-m-d H:i:s', $localtimenow ) );
 107              $Comment->set( 'author_IP', $Hit->IP );
 108              $Comment->dbsave();
 109              $count++;
 110  
 111              if( $count % 100 == 0 )
 112              { // Display a process of creating by one dot for 100 comments
 113                  echo ' .';
 114                  evo_flush();
 115              }
 116  
 117              // Clear all debug messages, To avoid an error about full memory

 118              $Debuglog->clear( 'all' );
 119          }
 120      }
 121  
 122      echo ' OK.';
 123  
 124      $Messages->add( sprintf( T_('Created %d comments.'), $count - 1 ), 'success' );
 125  }
 126  
 127  
 128  /**

 129   * Create sample posts and display a process of creating

 130   *

 131   * @param integer Blog ID

 132   * @param integer Number of posts

 133   */
 134  function tool_create_sample_posts( $blog_ID, $num_posts )
 135  {
 136      global $Messages, $DB, $Debuglog;
 137  
 138      $BlogCache = & get_BlogCache();
 139      $selected_Blog = $BlogCache->get_by_ID( $blog_ID, false, false );
 140      if( $selected_Blog == NULL )
 141      { // Incorrect blog ID, Exit here
 142          return;
 143      }
 144  
 145      echo T_('Creating of the sample posts...');
 146      evo_flush();
 147  
 148      /**

 149       * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."

 150       */
 151      $DB->log_queries = false;
 152  
 153      $count = 1;
 154      $num_posts_created = 0;
 155      $content = T_( 'This is an auto generated post for testing moderation.' );
 156      for( $i = 1; $i <= $num_posts; $i++ )
 157      {
 158          // Spaces and line breaks make generated string look like real text

 159          $length = rand(300, 500);
 160          $word = generate_random_key( $length, "\n     abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" );
 161          $post_content = $content.' '.$word;
 162          $urltitle = strtolower( str_replace( array("\n", ' ', '-'), '', substr($word, 50, 20) ) );
 163          $urltitle = trim( $urltitle, '-' );
 164  
 165          $Item = new Item();
 166          $Item->set( 'title', 'Generated post '.$i );
 167          $Item->set( 'content', $post_content );
 168          $Item->set( 'status', 'published' );
 169          $Item->set( 'dateset', 1 );
 170          // Set post main cat ID, from selected blog

 171          $Item->set( 'main_cat_ID', $selected_Blog->get_default_cat_ID() );
 172          // Random post url slug

 173          $Item->set( 'urltitle', $urltitle );
 174          if( $Item->dbinsert_test() )
 175          {
 176              $num_posts_created++;
 177          }
 178          $count++;
 179  
 180          if( $count % 100 == 0 )
 181          { // Display a process of creating by one dot for 100 posts
 182              echo ' .';
 183              //pre_dump( memory_get_usage() );

 184              evo_flush();
 185          }
 186  
 187          // Clear all debug messages, To avoid an error about full memory

 188          $Debuglog->clear( 'all' );
 189      }
 190  
 191      echo ' OK.';
 192  
 193      $Messages->add( sprintf( T_('Created %d posts.'), $num_posts_created ), 'success' );
 194      if( $num_posts > $num_posts_created )
 195      { // Some post creation failed because of concurtent modification error
 196          // Note: This message should not appear offten, so it doesn't need translation

 197          $Messages->add( sprintf( 'Creation of %d post(s) failed becuase of concurrent modification error.', $num_posts - $num_posts_created ), 'note' );
 198      }
 199  }
 200  
 201  
 202  /**

 203   * Create sample users and display a process of creating

 204   *

 205   * @param integer Group ID

 206   * @param integer Number of users

 207   */
 208  function tool_create_sample_users( $group_ID, $num_users )
 209  {
 210      global $Messages, $DB, $Debuglog;
 211  
 212      echo T_('Creating of the sample users...');
 213      evo_flush();
 214  
 215      /**

 216       * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."

 217       */
 218      $DB->log_queries = false;
 219  
 220      $count = 1;
 221      for( $i = 1; $i <= $num_users; $i++ )
 222      {
 223          $login = generate_random_key( rand(3, 20), 'abcdefghijklmnopqrstuvwxyz1234567890' );
 224          while( user_exists( $login ) )
 225          { // Generate new unique login
 226              $login = generate_random_key( rand(3, 20), 'abcdefghijklmnopqrstuvwxyz1234567890' );
 227          }
 228  
 229          $User = new User();
 230          // Create out of range hashes for better security

 231          $User->set( 'pass', generate_random_key( 32, 'abcdefghijklmnopqrstuvwxyz1234567890' ) );
 232          $User->set( 'login', $login );
 233          $User->set( 'email', 'test_'.$i.'@test.com' );
 234          $User->set( 'firstname', 'Test user '.$i );
 235          $User->set( 'url', 'http://www.test-'.rand(1,3).'.com/test_user_'.$i );
 236          $User->set( 'grp_ID', $group_ID );
 237          $User->dbinsert();
 238          $count++;
 239  
 240          if( $count % 100 == 0 )
 241          { // Display a process of creating by one dot for 100 users
 242              echo ' .';
 243              evo_flush();
 244          }
 245  
 246          // Clear all debug messages, To avoid an error about full memory

 247          $Debuglog->clear( 'all' );
 248      }
 249  
 250      echo ' OK.';
 251  
 252      $Messages->add( sprintf( T_('Created %d users.'), $num_users ), 'success' );
 253  }
 254  
 255  
 256  /**

 257   * Create sample hits and display a process of creating

 258   *

 259   * @param integer Days of stats to generate

 260   * @param integer Minimal interval between 2 consecutive hits (sec)

 261   * @param integer Maximal interval between 2 consecutive hits (sec)

 262   */
 263  function tool_create_sample_hits( $days, $min_interval, $max_interval )
 264  {
 265      global $Messages, $DB;
 266  
 267      load_funcs('sessions/model/_hitlog.funcs.php');
 268  
 269      echo T_('Creating of the sample hits...');
 270      evo_flush();
 271  
 272      /**

 273       * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."

 274       */
 275      $DB->log_queries = false;
 276  
 277      $insert_data_count = generate_hit_stat( $days, $min_interval, $max_interval, true );
 278  
 279      echo ' OK.';
 280  
 281      $Messages->add( sprintf( '%d test data hits are added.', $insert_data_count ), 'success' );
 282  }
 283  
 284  
 285  /**

 286   * Create sample messages and display a process of creating

 287   *

 288   * @param integer Number of loops

 289   * @param integer Number of messages in each conversation

 290   * @param integer Number of words in each message

 291   * @param integer Max # of participants in a conversation

 292   */
 293  function tool_create_sample_messages( $num_loops, $num_messages, $num_words, $max_users )
 294  {
 295      global $Messages, $DB;
 296  
 297      echo T_('Creating of the sample messages...');
 298      evo_flush();
 299  
 300      /**

 301       * Disable log queries because it increases the memory and stops the process with error "Allowed memory size of X bytes exhausted..."

 302       */
 303      $DB->log_queries = false;
 304  
 305      // Get all users

 306      $SQL = new SQL();
 307      $SQL->SELECT( 'user_ID' );
 308      $SQL->FROM( 'T_users' );
 309      $users = $DB->get_col( $SQL->get() );
 310  
 311      if( count( $users ) < 2 )
 312      {    // No users
 313          $Messages->add( T_('At least two users must exist in DB to create the messages'), 'error' );
 314          $action = 'show_create_messages';
 315          break;
 316      }
 317  
 318      $count_threads = 0;
 319      $count_messages = 0;
 320      for( $l = 0; $l < $num_loops; $l++ )
 321      {
 322          $user_links = array();
 323          foreach( $users as $from_user_ID )
 324          {
 325              foreach( $users as $to_user_ID )
 326              {
 327                  if( $from_user_ID == $to_user_ID || isset( $user_links[ (string) $from_user_ID.'-'.$to_user_ID ] ) )
 328                  {
 329                      continue;
 330                  }
 331  
 332                  $user_links[ $from_user_ID.'-'.$to_user_ID ] = 1;
 333                  // Insert thread

 334                  $DB->query( 'INSERT INTO T_messaging__thread ( thrd_title, thrd_datemodified )
 335                      VALUES ( '.$DB->quote( generate_random_key( 16 ) ).', '.$DB->quote( date( 'Y-m-d H:i:s' ) ).' )' );
 336  
 337                  $thread_ID = $DB->insert_id;
 338                  $count_threads++;
 339  
 340                  for( $m = 0; $m < $num_messages; $m++ )
 341                  {
 342                      $msg_text = '';
 343                      for( $w = 0; $w < $num_words; $w++ )
 344                      {
 345                          $msg_text .= generate_random_key( 8 ).' ';
 346                      }
 347                      $message_user_ID = $m % 2 == 0 ? $from_user_ID : $to_user_ID;
 348                      // Insert message

 349                      $DB->query( 'INSERT INTO T_messaging__message ( msg_author_user_ID , msg_datetime, msg_thread_ID, msg_text )
 350                          VALUES ( '.$DB->quote( $message_user_ID ).', '.$DB->quote( date( 'Y-m-d H:i:s' ) ).', '.$DB->quote( $thread_ID ).', '.$DB->quote( $msg_text ).' )' );
 351                      $count_messages++;
 352  
 353                      if( $count_messages % 100 == 0 )
 354                      { // Display a process of creating by one dot for 100 users
 355                          echo ' .';
 356                          evo_flush();
 357                      }
 358                  }
 359  
 360                  // Insert link for thread & user

 361                  $DB->query( 'INSERT INTO T_messaging__threadstatus ( tsta_thread_ID , tsta_user_ID, tsta_first_unread_msg_ID )
 362                      VALUES ( '.$DB->quote( $thread_ID ).', '.$DB->quote( $from_user_ID ).', NULL ),
 363                                   ( '.$DB->quote( $thread_ID ).', '.$DB->quote( $to_user_ID ).', NULL )' );
 364              }
 365          }
 366  
 367          /** Create one conversation between all users ( limit by $max_users ) **/

 368  
 369          // Insert thread

 370          $DB->query( 'INSERT INTO T_messaging__thread ( thrd_title, thrd_datemodified )
 371              VALUES ( '.$DB->quote( generate_random_key( 16 ) ).', '.$DB->quote( date( 'Y-m-d H:i:s' ) ).' )' );
 372  
 373          $thread_ID = $DB->insert_id;
 374          $count_threads++;
 375  
 376          $user_number = 0;
 377          for( $m = 0; $m < $num_messages; $m++ )
 378          {
 379              $msg_text = '';
 380              for( $w = 0; $w < $num_words; $w++ )
 381              {
 382                  $msg_text .= generate_random_key( 8 ).' ';
 383              }
 384              // Insert message

 385              $DB->query( 'INSERT INTO T_messaging__message ( msg_author_user_ID , msg_datetime, msg_thread_ID, msg_text )
 386                  VALUES ( '.$DB->quote( $users[ $user_number ] ).', '.$DB->quote( date( 'Y-m-d H:i:s' ) ).', '.$DB->quote( $thread_ID ).', '.$DB->quote( $msg_text ).' )' );
 387              $count_messages++;
 388              $user_number++;
 389              if( $user_number == count( $users ) || $user_number == $max_users - 1 )
 390              {    // Reset user number to start of the list
 391                  $user_number = 0;
 392              }
 393          }
 394  
 395          // Insert the links between thread & users

 396          $threadstatuses = array();
 397          foreach( $users as $u => $user_ID )
 398          {
 399              $threadstatuses[] = '( '.$DB->quote( $thread_ID ).', '.$DB->quote( $user_ID ).', NULL )';
 400              if( $u == $max_users - 1 )
 401              {    // limit by max users in one thread
 402                  break;
 403              }
 404          }
 405          $DB->query( 'INSERT INTO T_messaging__threadstatus ( tsta_thread_ID , tsta_user_ID, tsta_first_unread_msg_ID )
 406              VALUES '.implode( ', ', $threadstatuses ) );
 407      }
 408  
 409      echo ' OK.';
 410  
 411      $Messages->add( sprintf( T_('%d threads and %d messages have been created.'), $count_threads, $count_messages ), 'success' );
 412  }
 413  
 414  
 415  /**

 416   * Test a flush function

 417   */
 418  function tool_test_flush()
 419  {
 420      for( $i = 1; $i <= 30; $i++ )
 421      {
 422          echo T_('Sleeping for 1 second...').'<br />';
 423          evo_flush();
 424          sleep( 1 );
 425      }
 426  }
 427  ?>

title

Description

title

Description

title

Description

title

title

Body