b2evolution PHP Cross Reference Blogging Systems

Source: /inc/tools/model/_dbmaintenance.funcs.php - 747 lines - 23689 bytes - Summary - Text - Print

Description: This file implements functions to work with DB maintenance. 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 DB maintenance.

   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: _dbmaintenance.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   * Clear pre-renderered item cache (DB)

  24   */
  25  function dbm_delete_itemprecache()
  26  {
  27      global $DB, $Messages;
  28  
  29      $DB->query('DELETE FROM T_items__prerendering WHERE 1=1');
  30  
  31      $Messages->add( sprintf( T_('Removed %d cached entries.'), $DB->rows_affected ), 'success' );
  32  }
  33  
  34  
  35  /**

  36   * Clear full page cache (/cache directory)

  37   */
  38  function dbm_delete_pagecache()
  39  {
  40      global $DB, $Messages, $cache_path;
  41  
  42      // Clear general cache directory

  43      if( cleardir_r( $cache_path.'general' ) )
  44      {
  45          $Messages->add( sprintf( T_('General cache deleted: %s'), $cache_path.'general' ), 'note' );
  46      }
  47      else
  48      {
  49          $Messages->add( sprintf( T_('Could not delete general cache: %s'), $cache_path.'general' ), 'error' );
  50      }
  51  
  52      $SQL = 'SELECT blog_ID FROM T_blogs
  53              INNER JOIN T_coll_settings ON ( blog_ID = cset_coll_ID
  54                          AND cset_name = "cache_enabled"
  55                          AND cset_value = "1" )
  56              WHERE 1=1';
  57  
  58      if( $blog_array = $DB->get_col( $SQL ) )
  59      {
  60          foreach( $blog_array as $l_blog )
  61          {    // Clear blog cache
  62              if( cleardir_r( $cache_path.'c'.$l_blog ) )
  63              {
  64                  $Messages->add( sprintf( T_('Blog %d cache deleted: %s'), $l_blog, $cache_path.'c'.$l_blog ), 'note' );
  65              }
  66              else
  67              {
  68                  $Messages->add( sprintf( T_('Could not delete blog %d cache: %s'), $l_blog, $cache_path.'c'.$l_blog ), 'error' );
  69              }
  70              // Create .htaccess file with deny rules

  71              create_htaccess_deny( $cache_path );
  72          }
  73      }
  74  
  75      $Messages->add( T_('Page cache deleted.'), 'success' );
  76  }
  77  
  78  
  79  /**

  80   * Clear thumbnail caches (?evocache directories)

  81   */
  82  function dbm_delete_filecache()
  83  {
  84      global $Messages;
  85  
  86      // TODO> handle custom media directories dh> ??

  87      // Delete any ?evocache folders:

  88      $deleted_dirs = delete_cachefolders( $Messages );
  89  
  90      $Messages->add( sprintf( T_('Deleted %d directories.'), $deleted_dirs ), 'success' );
  91  }
  92  
  93  
  94  /**

  95   * Repair cache

  96   */
  97  function dbm_repair_cache()
  98  {
  99      global $Messages;
 100  
 101      load_funcs( 'tools/model/_system.funcs.php' );
 102      $result = system_check_caches();
 103      if( empty( $result ) )
 104      {
 105          $Messages->add( T_( 'All cache folders are working properly.' ), 'success' );
 106      }
 107      else
 108      {
 109          $error_message = T_( 'Unable to repair all cache folders becaue of file permissions' ).':<br />';
 110          $Messages->add( $error_message.implode( '<br />', $result ) );
 111      }
 112  }
 113  
 114  
 115  /**

 116   * Optimize DB tables (MyISAM & InnoDB)

 117   *

 118   * @param boolean Display messages

 119   * @param boolean TRUE - to make optimize query for each table separately

 120   * @return array Results of the mysql command 'OPTIMIZE'

 121   */
 122  function dbm_optimize_tables( $display_messages = true, $separate_tables = true )
 123  {
 124      global $tableprefix, $DB, $Timer, $dbm_tables_count;
 125  
 126      $tables = $DB->get_results( 'SHOW TABLE STATUS FROM `'.$DB->dbname.'` LIKE \''.$tableprefix.'%\'');
 127  
 128      $myisam_tables = array();
 129      $innodb_tables = array();
 130      foreach( $tables as $table )
 131      {
 132          // Before MySQL 4.1.2, the "Engine" field was labeled as "Type".

 133          if( ( ( isset( $table->Engine ) && $table->Engine == 'MyISAM' )
 134                  || ( isset( $table->Type ) && $table->Type == 'MyISAM' ) )
 135              && $table->Data_free )
 136          {    // Optimization needed for MyISAM table
 137              $myisam_tables[] = $table->Name;
 138          }
 139          else if( ( ( isset( $table->Engine ) && $table->Engine == 'InnoDB' )
 140                  || ( isset( $table->Type ) && $table->Type == 'InnoDB' ) )
 141              && $table->Data_free )
 142          {    // Optimization needed for InnoDB table
 143              $innodb_tables[] = $table->Name;
 144          }
 145      }
 146      $dbm_tables_count = count( $myisam_tables ) + count( $innodb_tables );
 147  
 148      // Optimize MyISAM tables

 149      $myisam_results = dbm_optimize_tables_process( $display_messages, $separate_tables, $myisam_tables, 'MyISAM' );
 150      $results = $myisam_results;
 151  
 152      // Optimize InnoDB tables

 153      $innodb_results = dbm_optimize_tables_process( $display_messages, $separate_tables, $innodb_tables, 'InnoDB' );
 154      $results = array_merge( $results, $innodb_results );
 155  
 156      return $results;
 157  }
 158  
 159  
 160  /**

 161   * Optimize process DB tables (MyISAM & InnoDB)

 162   *

 163   * @param boolean Display messages

 164   * @param boolean TRUE - to make optimize query for each table separately

 165   * @param array Tables

 166   * @param string Table type: 'MyISAM' or 'InnoDB'

 167   * @return array Results of the mysql command 'OPTIMIZE'

 168   */
 169  function dbm_optimize_tables_process( $display_messages = true, $separate_tables = true, $tables = array(), $table_type = '' )
 170  {
 171      global $DB;
 172  
 173      load_class( '_core/model/_timer.class.php', 'Timer' );
 174      $Timer = new Timer('dbm_optimize');
 175  
 176      $results = array();
 177  
 178      if( $display_messages )
 179      { // Display messages
 180          echo '<b>'.sprintf( T_('Optimize %s tables...'), $table_type ).'</b><br />';
 181          evo_flush();
 182      }
 183      $timer_name = 'optimize_'.strtolower( $table_type );
 184  
 185      $Timer->start( $timer_name );
 186  
 187      if( !empty( $tables ) )
 188      { // Optimize tables
 189          if( $separate_tables )
 190          { // Optimize each table in separate query
 191              foreach( $tables as $table )
 192              {
 193                  $Timer->start( $timer_name.'_table' );
 194                  $table_results = $DB->get_results( 'OPTIMIZE NO_WRITE_TO_BINLOG TABLE '.$table );
 195                  $Timer->stop( $timer_name.'_table' );
 196                  if( $display_messages )
 197                  { // Display messages
 198                      dbm_display_result_messages( $table_results, 'optimize' );
 199                      echo '<b>'.sprintf( T_('Time: %s seconds'), $Timer->get_duration( $timer_name.'_table' ) ).'</b><br /><br />';
 200                  }
 201                  evo_flush();
 202                  $results = array_merge( $results, $table_results );
 203              }
 204          }
 205          else
 206          { // Optimize all table by one query, Used for cron job
 207              $results = $DB->get_results( 'OPTIMIZE NO_WRITE_TO_BINLOG TABLE '.implode( ', ', $tables ) );
 208          }
 209      }
 210  
 211      $Timer->stop( $timer_name );
 212  
 213      if( $display_messages )
 214      { // Display messages
 215          if( !$separate_tables || empty( $tables ) )
 216          { // Display full report log for case when the tables were optimized by one query
 217              dbm_display_result_messages( $results, 'optimize' );
 218          }
 219          echo '<b>'.sprintf( T_('Full execution time: %s seconds'), $Timer->get_duration( $timer_name ) ).'</b><br /><br />';
 220          evo_flush();
 221      }
 222  
 223      return $results;
 224  }
 225  
 226  
 227  /**

 228   * Print on screen the messages of 'OPTIMIZE', 'CHECK' OR 'ANALYZE' commands

 229   *

 230   * @param array Results of the mysql commands 'OPTIMIZE', 'CHECK' OR 'ANALYZE'

 231   * @param string mysql command type: optimize, check, analyze

 232   */
 233  function dbm_display_result_messages( $results, $command_type )
 234  {
 235      switch( $command_type )
 236      {
 237          case 'optimize':
 238              $params = array(
 239                      'message_ok'    => T_('Database table %s is optimized.'),
 240                      'message_error' => T_('Database table %s could not be optimized. Message: %s'),
 241                      'message_done'  => T_('All database tables are already optimized.'),
 242                  );
 243              break;
 244  
 245          case 'check':
 246              $params = array(
 247                      'message_ok'    => T_('Database table %s is checked.'),
 248                      'message_error' => T_('Database table %s could not be checked. Message: %s'),
 249                      'message_done'  => T_('All database tables are already checked.'),
 250                  );
 251              break;
 252  
 253          case 'analyze':
 254              $params = array(
 255                      'message_ok'    => T_('Database table %s is analyzed.'),
 256                      'message_error' => T_('Database table %s could not be analyzed. Message: %s'),
 257                      'message_done'  => T_('All database tables are already analyzed.'),
 258                  );
 259              break;
 260  
 261          default:
 262              // Don't support unknown commants, Exit here

 263              return;
 264      }
 265  
 266      if( count( $results ) > 0 )
 267      {
 268          foreach( $results as $result )
 269          {
 270              if( $result->Msg_type == 'status' && $result->Msg_text == 'OK' )
 271              { // OK
 272                  echo sprintf( $params['message_ok'], '<b>'.$result->Table.'</b>' ).'<br />';
 273              }
 274              elseif( $result->Msg_type == 'note' && $result->Msg_text == 'Table does not support optimize, doing recreate + analyze instead' )
 275              { // This warning is comming for every innodb table, but that is normal, Display for info
 276                  echo sprintf( T_('Database table %s does not support optimize, doing recreate + analyze instead'), '<b>'.$result->Table.'</b>' ).'<br />';
 277              }
 278              else
 279              { // Some errors
 280                  $message_class = $result->Msg_type == 'status' ? 'orange' : 'red';
 281                  echo '<span class="'.$message_class.'">'.sprintf( $params['message_error'], '<b>'.$result->Table.'</b>', '"'.$result->Msg_text.'"' ).'</span><br />';
 282              }
 283          }
 284      }
 285      else
 286      { // No tables found to optimize, probably all tables already were optimized
 287          echo $params['message_done'].'<br />';
 288      }
 289  }
 290  
 291  
 292  /**

 293   * Find all broken posts that have no matching category

 294   */
 295  function dbm_find_broken_posts()
 296  {
 297      global $DB, $Messages;
 298  
 299      // select broken items

 300      $sql = 'SELECT * FROM T_items__item
 301                  WHERE post_canonical_slug_ID NOT IN (
 302                      SELECT slug_ID FROM T_slug )';
 303      $broken_items = $DB->get_results( $sql, OBJECT, 'Find broken posts' );
 304  
 305      $num_deleted = 0;
 306      foreach( $broken_items as $row )
 307      { // delete broken items
 308          $broken_Item = new Item( $row );
 309          if( $broken_Item->dbdelete() )
 310          {
 311              $num_deleted++;
 312          }
 313      }
 314  
 315      $Messages->add( sprintf( T_('Deleted %d posts.'), $num_deleted ), 'success' );
 316  }
 317  
 318  
 319  /**

 320   * Find all broken slugs that have no matching target post

 321   */
 322  function dbm_find_broken_slugs()
 323  {
 324      global $DB, $Messages;
 325  
 326      // delete broken slugs

 327      $r = $DB->query( 'DELETE FROM T_slug
 328                          WHERE slug_type = "item" and slug_itm_ID NOT IN (
 329                              SELECT post_ID FROM T_items__item )' );
 330  
 331      if( $r !== false )
 332      {
 333          $Messages->add( sprintf( T_('Deleted %d slugs.'), $r ), 'success' );
 334      }
 335  }
 336  
 337  
 338  /**

 339   * Find and delete orphan comment uploads

 340   */
 341  function dbm_delete_orphan_comment_uploads()
 342  {
 343      global $Messages;
 344  
 345      $count = remove_orphan_files( NULL, 24 );
 346  
 347      $Messages->add( sprintf( T_('%d files have been deleted'), $count ), 'success' );
 348  }
 349  
 350  
 351  /**

 352   * Find and delete orphan File objects with no matching file on disk

 353   */
 354  function dbm_delete_orphan_files()
 355  {
 356      global $DB, $admin_url;
 357  
 358      $FileCache = & get_FileCache();
 359      $FileCache->clear();
 360  
 361      echo T_('Deleting of the orphan File objects from the database...');
 362      evo_flush();
 363  
 364      $files_SQL = new SQL();
 365      $files_SQL->SELECT( '*' );
 366      $files_SQL->FROM( 'T_files' );
 367      $files_SQL->ORDER_BY( 'file_ID' );
 368  
 369      $count_files_valid = 0;
 370      $count_files_invalid = 0;
 371      $count_files_deleted = 0;
 372  
 373      $page_size = 100;
 374      $current_page = 0;
 375      // Search the files by page to save memory

 376      $files_SQL->LIMIT( '0, '.$page_size );
 377      while( $loaded_Files = $FileCache->load_by_sql( $files_SQL ) )
 378      { // Check all loaded files
 379          foreach( $loaded_Files as $File )
 380          {
 381              if( is_null( $File ) )
 382              { // The File object couldn't be created because the db entry is invalid
 383                  $count_files_invalid++;
 384                  continue;
 385              }
 386              if( $File->exists() )
 387              { // File exists on the disk
 388                  $count_files_valid++;
 389              }
 390              else
 391              { // File doesn't exist on the disk, Remove it from DB
 392                  $File->dbdelete();
 393                  $count_files_deleted++;
 394              }
 395          }
 396  
 397          echo ' .';
 398          evo_flush();
 399  
 400          // Clear cache after each page to save memory

 401          $FileCache->clear();
 402  
 403          $current_page++;
 404          $files_SQL->LIMIT( ( $current_page * $page_size ).', '.$page_size );
 405      }
 406  
 407      echo 'OK<p>';
 408      echo sprintf( T_('The number of deleted orphan File objects: %d.'), $count_files_deleted ).'<br />';
 409      echo sprintf( T_('The number of valid File objects in the database: %d.'), $count_files_valid ).'</p>';
 410  
 411      if( $count_files_invalid )
 412      { // There are invalid files in the database
 413          // Display warning to show that the 'Remove orphan file roots' tool should be also called

 414          $remove_orphan_file_roots = 'href="'.$admin_url.'ctrl=tools&amp;action=delete_orphan_file_roots&amp;'.url_crumb('tools').'"';
 415          $invalid_files_note = ( $count_files_invalid == 1 ) ? T_('An invalid File object was found in the database.') : sprintf( T_('%d invalid File objects were found in the database.'), $count_files_invalid );
 416          echo '<p class="warning">'.$invalid_files_note."<br/>"
 417              .sprintf( T_('It is strongly recommended to also execute the &lt;<a %s>Remove orphan file roots</a>&gt; tool to remove invalid files from the database and from the disk as well!'), $remove_orphan_file_roots )
 418              .'</p>';
 419      }
 420  }
 421  
 422  
 423  /**

 424   * Remove orphan file roots ( with no matching Blog or User entry in the database ) recursively with all of the content

 425   */
 426  function dbm_delete_orphan_file_roots()
 427  {
 428      global $DB, $media_path;
 429  
 430      echo T_('Removing of the orphan file roots recursively with all of the content... ');
 431      evo_flush();
 432  
 433      // Store all directories that must be deleted

 434      $delete_dirs = array();
 435  
 436      /* BLOGS */

 437  
 438      // Get the media diretories of all existing blogs

 439      $BlogCache = & get_BlogCache();
 440      $BlogCache->load_all();
 441      $blog_dirs = array();
 442      foreach( $BlogCache->cache as $Blog )
 443      {
 444          $blog_dirs[] = $Blog->get_media_dir();
 445      }
 446      $BlogCache->clear();
 447  
 448      $media_path_blogs = $media_path.'blogs/';
 449  
 450      if( ( $media_dir_blogs = @opendir( $media_path_blogs ) ) === false )
 451      { // Could not open blogs media dir
 452          echo '<p class="red">'.sprintf( T_('Cannot open blogs media directory %s'), '<b>'.$media_path_blogs.'</b>' ).'</p>';
 453      }
 454      else
 455      {
 456          // Find the blog dirs that must be deleted

 457          while( ( $folder = readdir( $media_dir_blogs ) ) !== false )
 458          {
 459              if( $folder == '.' || $folder == '..' || ! is_dir( $media_path_blogs.$folder ) )
 460              { // Skip files
 461                  continue;
 462              }
 463              if( ! in_array( $media_path_blogs.$folder.'/', $blog_dirs ) )
 464              { // This dir must be deleted because it is not media dir of the existing blogs
 465                  $delete_dirs[] = $media_path_blogs.$folder.'/';
 466              }
 467          }
 468  
 469          closedir( $media_dir_blogs );
 470      }
 471  
 472      /* USERS */

 473      echo '. ';
 474      evo_flush();
 475  
 476      // Get logins of all existing users

 477      $SQL = new SQL();
 478      $SQL->SELECT( 'user_login' );
 479      $SQL->FROM( 'T_users' );
 480      $user_logins = $DB->get_col( $SQL->get() );
 481  
 482      $media_path_users = $media_path.'users/';
 483  
 484      if( ( $media_dir_users = @opendir( $media_path_users ) ) === false )
 485      { // Could not open users media dir
 486          echo '<p class="red">'.sprintf( T_('Cannot open users media directory %s'), '<b>'.$media_path_users.'</b>' ).'</p>';
 487      }
 488      else
 489      {
 490          // Find the user dirs that must be deleted

 491          while( ( $folder = readdir( $media_dir_users ) ) !== false )
 492          {
 493              if( $folder == '.' || $folder == '..' || ! is_dir( $media_path_users.$folder ) )
 494              { // Skip files
 495                  continue;
 496              }
 497              if( ! in_array( $folder, $user_logins ) )
 498              { // This dir must be deleted because it is not media dir of the existing users
 499                  $delete_dirs[] = $media_path_users.$folder.'/';
 500              }
 501          }
 502  
 503          closedir( $media_dir_users );
 504      }
 505  
 506      /* DELETE broken  file roots */

 507      echo '. ';
 508      evo_flush();
 509  
 510      foreach( $delete_dirs as $delete_dir )
 511      {
 512          if( rmdir_r( $delete_dir ) )
 513          { // Success deleting
 514              echo '<p class="green">'.sprintf( T_('Invalid file root %s was found and removed with all of its content.'), '<b>'.$delete_dir.'</b>' ).'</p>';
 515          }
 516          else
 517          { // Failed deleting
 518              echo '<p class="red">'.sprintf( T_('Cannot delete directory %s. Please check the permissions or delete it manually.'), '<b>'.$delete_dir.'</b>' ).'</p>';
 519          }
 520      }
 521  
 522      /* DELETE orphan DB file records of the blogs and the users */

 523      echo '. ';
 524      evo_flush();
 525  
 526      $count_files_deleted = $DB->query( 'DELETE f, l, fv FROM T_files AS f
 527               LEFT JOIN T_links AS l ON l.link_file_ID = f.file_ID
 528               LEFT JOIN T_files__vote AS fv ON fv.fvot_file_ID = f.file_ID
 529          WHERE ( file_root_type = "collection"
 530                  AND file_root_ID NOT IN ( SELECT blog_ID FROM T_blogs ) )
 531             OR ( file_root_type = "user"
 532                  AND file_root_ID NOT IN ( SELECT user_ID FROM T_users ) )' );
 533  
 534      echo 'OK.<p>';
 535      echo sprintf( T_('%d File roots have been removed from the disk.'), count( $delete_dirs ) ).'<br />';
 536      echo sprintf( T_('%d File objects have been deleted from DB.'), intval( $count_files_deleted ) ).'</p>';
 537  }
 538  
 539  
 540  /**

 541   * Recreate all item slugs (change title-[0-9] canonical slugs to a slug generated from current title). Old slugs will still work, but redirect to the new one.

 542   */
 543  function dbm_recreate_itemslugs()
 544  {
 545      global $Messages;
 546  
 547      $ItemCache = get_ItemCache();
 548      $ItemCache->load_where( '( post_title != "" ) AND ( post_urltitle = "title" OR post_urltitle LIKE "title-%" )');
 549      $items = $ItemCache->get_ID_array();
 550      $count_slugs = 0;
 551  
 552      foreach( $items as $item_ID )
 553      {
 554          $Item = $ItemCache->get_by_ID($item_ID);
 555  
 556          $prev_urltitle = $Item->get( 'urltitle' );
 557          $item_title = $Item->get( 'title' );
 558  
 559          // check if post title is not empty and urltitle was auto generated ( equals title or title-[0-9]+ )

 560          // Note: urltitle will be auto generated on this form (title-[0-9]+), if post title wass empty and, urltitle was not set

 561          // Note: Even if a post title was set to 'title' on purpose it's possible, that this tool will change the post urltitle

 562          if( ( ! empty( $item_title ) ) && ( ( $prev_urltitle == 'title' ) || ( preg_match( '#^title-[0-9]+$#', $prev_urltitle ) ) ) )
 563          {
 564              // set urltitle empty, so the item update function will regenerate the item slug

 565              $Item->set( 'urltitle', '' );
 566              $result = $Item->dbupdate(/* do not autotrack modification */ false, /* update slug */ true, /* do not update excerpt */ false);
 567              if( ( $result ) && ( $prev_urltitle != $Item->get( 'urltitle' ) ) )
 568              { // update was successful, and item urltitle was changed
 569                  $count_slugs++;
 570              }
 571          }
 572      }
 573      $Messages->add( sprintf( 'Created %d new URL slugs.', $count_slugs ), 'success' );
 574  }
 575  
 576  
 577  /**

 578   * Recreate all autogenerated posts excerpts.

 579   */
 580  function dbm_recreate_autogenerated_excerpts()
 581  {
 582      global $DB;
 583  
 584      $continue_url = regenerate_url('action,crumb,remove_all_excerpts', 'action=recreate_autogenerated_excerpts&amp;remove_all_excerpts=0&amp;'.url_crumb('tools') );
 585      $remove_all_excerpts = param( 'remove_all_excerpts', 'boolean', 1 );
 586  
 587      // Display process status

 588      echo $remove_all_excerpts ? T_('Re-creating of autogenerated excerpts...') : T_('Continue re-creating of autogenerated excerpts...');
 589      evo_flush();
 590  
 591      recreate_autogenerated_excerpts( $continue_url, $remove_all_excerpts, true );
 592      $custom_excerpts = $DB->get_var( 'SELECT count(*) FROM T_items__item WHERE post_excerpt_autogenerated = 0' );
 593      echo '<br />'.sprintf( T_('All autogenerated excerpts were re-created ( %d custom excerpts were left untouched ).'), $custom_excerpts ).'<br />';
 594  }
 595  
 596  
 597  /**

 598   * Check DB tables

 599   *

 600   * @param boolean Display messages

 601   * @param boolean TRUE - to make optimize query for each table separately

 602   * @return array Results of the mysql command 'CHECK'

 603   */
 604  function dbm_check_tables( $display_messages = true, $separate_tables = true )
 605  {
 606      global $DB, $tableprefix, $dbm_tables_count;
 607  
 608      load_class( '_core/model/_timer.class.php', 'Timer' );
 609      $Timer = new Timer('dbm_check');
 610  
 611      // Get all table names from DB

 612      $tables = $DB->get_results( 'SHOW TABLE STATUS FROM `'.$DB->dbname.'` LIKE \''.$tableprefix.'%\'');
 613      $tables_names = array();
 614      foreach( $tables as $table )
 615      {
 616          $tables_names[] = $table->Name;
 617      }
 618      $dbm_tables_count = count( $tables_names );
 619  
 620      if( $display_messages )
 621      { // Display messages
 622          echo '<b>'.T_('Check tables...').'</b><br />';
 623          evo_flush();
 624      }
 625  
 626      $Timer->start( 'check_tables' );
 627      if( $separate_tables )
 628      { // Check each table in separate query
 629          $check_results = array();
 630          foreach( $tables_names as $table )
 631          {
 632              $Timer->start( 'check_one_table' );
 633              $table_results = $DB->get_results( 'CHECK TABLE '.$table.' FAST' );
 634              $Timer->stop( 'check_one_table' );
 635              if( $display_messages )
 636              { // Display messages
 637                  dbm_display_result_messages( $table_results, 'check' );
 638                  echo '<b>'.sprintf( T_('Time: %s seconds'), $Timer->get_duration( 'check_one_table' ) ).'</b><br /><br />';
 639              }
 640              evo_flush();
 641              $check_results = array_merge( $check_results, $table_results );
 642          }
 643      }
 644      else
 645      { // Check all table by one query, Used for cron job
 646          $check_results = $DB->get_results( 'CHECK TABLE '.implode( ', ', $tables_names ).' FAST' );
 647      }
 648      $Timer->stop( 'check_tables' );
 649  
 650      if( $display_messages )
 651      { // Display messages
 652          if( !$separate_tables )
 653          { // Display full report log for case when the tables were checked by one query
 654              dbm_display_result_messages( $check_results, 'check' );
 655          }
 656          echo '<b>'.sprintf( T_('Full execution time: %s seconds'), $Timer->get_duration( 'check_tables' ) ).'</b><br />';
 657      }
 658  
 659      return $check_results;
 660  }
 661  
 662  
 663  /**

 664   * Analyze DB tables

 665   *

 666   * @param boolean Display messages

 667   * @param boolean TRUE - to make optimize query for each table separately

 668   * @return array Results of the mysql command 'ANALYZE'

 669   */
 670  function dbm_analyze_tables( $display_messages = true, $separate_tables = true )
 671  {
 672      global $DB, $tableprefix, $dbm_tables_count;
 673  
 674      load_class( '_core/model/_timer.class.php', 'Timer' );
 675      $Timer = new Timer('dbm_analyze');
 676  
 677      // Get all table names from DB

 678      $tables = $DB->get_results( 'SHOW TABLE STATUS FROM `'.$DB->dbname.'` LIKE \''.$tableprefix.'%\'');
 679      $tables_names = array();
 680      foreach( $tables as $table )
 681      {
 682          $tables_names[] = $table->Name;
 683      }
 684      $dbm_tables_count = count( $tables_names );
 685  
 686      if( $display_messages )
 687      { // Display messages
 688          echo '<b>'.T_('Analyze tables...').'</b><br />';
 689          evo_flush();
 690      }
 691  
 692      $Timer->start( 'analyze_tables' );
 693      if( $separate_tables )
 694      { // Analyze each table in separate query
 695          $analyze_results = array();
 696          foreach( $tables_names as $table )
 697          {
 698              $Timer->start( 'analyze_one_table' );
 699              $table_results = $DB->get_results( 'ANALYZE NO_WRITE_TO_BINLOG TABLE '.$table );
 700              $Timer->stop( 'analyze_one_table' );
 701              if( $display_messages )
 702              { // Display messages
 703                  dbm_display_result_messages( $table_results, 'analyze' );
 704                  echo '<b>'.sprintf( T_('Time: %s seconds'), $Timer->get_duration( 'analyze_one_table' ) ).'</b><br /><br />';
 705              }
 706              evo_flush();
 707              $analyze_results = array_merge( $analyze_results, $table_results );
 708          }
 709      }
 710      else
 711      { // Analyze all table by one query, Used for cron job
 712          $analyze_results = $DB->get_results( 'ANALYZE NO_WRITE_TO_BINLOG TABLE '.implode( ', ', $tables_names ) );
 713      }
 714      $Timer->stop( 'analyze_tables' );
 715  
 716      if( $display_messages )
 717      { // Display messages
 718          if( !$separate_tables )
 719          { // Display full report log for case when the tables were analyzed by one query
 720              dbm_display_result_messages( $analyze_results, 'analyze' );
 721          }
 722          echo '<b>'.sprintf( T_('Full execution time: %s seconds'), $Timer->get_duration( 'analyze_tables' ) ).'</b>';
 723      }
 724  
 725      return $analyze_results;
 726  }
 727  
 728  
 729  /**

 730   * Update a progress information, display how many is done from all

 731   *

 732   * @param string the id of the html element which content must be replaced with the current values

 733   * @param integer done

 734   * @param integer all

 735   */
 736  function echo_progress_log_update( $progress_log_id, $done, $all )
 737  {
 738      echo '<span class="function_echo_progress_log_update">';
 739      ?>
 740      <script type="text/javascript">
 741          jQuery('.function_echo_progress_log_update').remove();
 742          jQuery( '#' + '<?php echo $progress_log_id; ?>' ).html("<?php echo ' '.$done.' / '.$all ?>");
 743      </script>
 744      <?php
 745      echo '</span>';
 746  }
 747  ?>

title

Description

title

Description

title

Description

title

title

Body