header(); switch ($step) { case 0 : $this->greet(); break; case 1 : $this->get_targets(); break; case 2 : $this->get_terms(); break; case 3 : $this->get_searchwords(); break; case 4 : $this->delete_taxonomies(); break; case 5 : $this->delete_terms(); break; case 6 : $this->optimize_wptables(); break; case 7 : $this->delete_oldtables(); break; case 8 : $this->show_queries(); break; } // load the footer $this->footer(); } function header() { echo '
'; echo '

'.__('bSuite bStat Upgrader').'

'; } function footer() { echo '
'; } function greet() { global $wpdb, $bsuite; set_time_limit( 0 ); echo '
'; echo '

'.__('If you used bSuite3 for stats collection, you’ll need this (or some manual MySQL queries) to move the data into the new bSuite4 tables.').'

'; if( !count( $wpdb->get_col( $this->query_checktables ))){ echo '

'.__('Huh? You’ve got no tables to import.').'

'; }else if( ! 0 == ini_get( 'max_execution_time' ) ){ echo '

'.__('Ack! Failed to reset PHP’s maximum execution time.').'

'; echo '

'. __('Your server’s default time of ') . ini_get( 'max_execution_time' ) . __(' seconds may be too low to complete this upgrade. Some queries can take as long as 30 minutes to complete on a large data set.') .'

'; echo '

'. __('You can try executing the MySQL commands manually if you’d like.') .'

'; }else if( !$bsuite ){ echo '

'.__('You must activate bSuite 4 before proceeding.').'

'; }else{ $bsuite->createtables(); // just to make sure echo '

'.__('It’s worth mentioning that most people recommend backing up your database before doing things like this.').'

'; echo '
'; echo '

'; echo '

No thanks, just show me the MySQL queries.

'; echo '
'; } echo '
'; // options we don't need anymore delete_option( 'bstat_import_refs' ); delete_option( 'bstat_import_hits' ); } function get_targets() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 1 of 7.

'; echo '

Importing post hits stats.

'; echo '

Please be patient, this could take a long time.

'; flush(); foreach( explode( ';', $this->query_delete_oldtables ) as $query ) $wpdb->get_results( $query ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function get_terms() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 2 of 7.

'; echo '

Importing old search terms.

'; echo '

Please be patient, this could take a long time.

'; flush(); $wpdb->get_results( $this->query_get_terms ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function get_searchwords() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 6000 ); echo '
'; echo '

Step 3 of 7.

'; echo '

Importing old search targets.

'; echo '

Please be patient, this could take a long time.

'; flush(); $wpdb->get_results( $this->query_get_searchwords ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function delete_taxonomies() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 4 of 7.

'; echo '

Cleaning up WordPress’ term_taxonomy table.

'; echo '

Please be patient, this could take a long time.

'; $wpdb->get_results( $this->query_delete_taxonomies ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function delete_terms() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 5 of 7.

'; echo '

Cleaning up WordPress’ terms table.

'; echo '

Please be patient, this could take a long time.

'; flush(); $wpdb->get_results( $this->query_delete_terms ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function optimize_wptables() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 6 of 7.

'; echo '

Optimizing WordPress’ terms and term_taxonomies tables.

'; flush(); foreach( explode( ';', $this->query_optimize_wptables ) as $query ) $wpdb->get_results( $query ); echo '

Done!

'; echo '
'; echo '

'; echo '
'; } function delete_oldtables() { set_time_limit( 0 ); global $wpdb, $bsuite; update_option('bsuite_doing_migration', time() + 3000 ); echo '
'; echo '

Step 7 of 7.

'; echo '

Deleting old bSuite3 tables.

'; flush(); foreach( explode( ';', $this->query_delete_oldtables ) as $query ) $wpdb->get_results( $query ); echo '

Done!

'; echo '

Fini!

'; echo '

All set!

'; echo '

Don’t forget to disable this plugin.

'; } function show_queries() { echo '
    '; echo '
  1. Import page load stats:

    '. $this->query_get_targets .'
    '; echo '
  2. Import search terms:

    '. $this->query_get_terms .'
    '; echo '
  3. Import search targets:

    '. $this->query_get_searchwords .'
    '; echo '
  4. Clean up taxonomies:

    '. $this->query_delete_taxonomies .'
    '; echo '
  5. Clean up terms:

    '. $this->query_delete_terms .'
    '; echo '
  6. Optimize tables:

    '. $this->query_optimize_wptables .'
    '; echo '
  7. Delete old tables:

    '. $this->query_delete_oldtables .'
    '; echo '
'; } // Default constructor function bStat_Import() { global $wpdb, $bsuite; $home = $bsuite->bstat_insert_term( get_settings( 'siteurl' )); // the queries we use $this->query_checktables = 'SHOW TABLES LIKE "'. $wpdb->prefix .'bsuite3%"'; $this->query_get_targets = 'INSERT IGNORE INTO '. $bsuite->hits_targets .' SELECT post_id AS object_id, 0 AS object_type, hit_count, hit_date FROM '. $wpdb->prefix .'bsuite3_hits WHERE post_id != 0 ; INSERT IGNORE INTO '. $bsuite->hits_targets .' SELECT '. $home .' AS object_id, 1 AS object_type, hit_count, hit_date FROM '. $wpdb->prefix .'bsuite3_hits WHERE post_id = 0 ;'; $this->query_get_terms = 'INSERT IGNORE INTO '. $bsuite->hits_terms .' (name) SELECT t.name AS name FROM '. $wpdb->terms .' t LEFT JOIN '. $wpdb->term_taxonomy .' tt ON t.term_id = tt.term_id WHERE tt.taxonomy = "bsuite_search";'; $this->query_get_searchwords = 'INSERT IGNORE INTO '. $bsuite->hits_searchphrases .' SELECT a.post_id AS object_id, 0 AS object_type, c.term_id AS term_id, SUM(a.hit_count) AS hit_count FROM '. $wpdb->prefix .'bsuite3_refs_terms a LEFT JOIN '. $wpdb->terms .' b ON a.term_id = b.term_id LEFT JOIN '. $bsuite->hits_terms .' c ON b.name = c.name WHERE a.post_id != 0 GROUP BY object_id, object_type, term_id;'; $this->query_delete_taxonomies = 'DELETE QUICK FROM '. $wpdb->term_taxonomy .' WHERE taxonomy = "bsuite_search";'; $this->query_delete_terms = 'DELETE QUICK FROM '. $wpdb->terms .' USING '. $wpdb->terms .' LEFT JOIN '. $wpdb->term_taxonomy .' ON '. $wpdb->terms .'.term_id = '. $wpdb->term_taxonomy .'.term_id WHERE '. $wpdb->term_taxonomy .'.term_id IS NULL;'; $this->query_optimize_wptables = 'OPTIMIZE TABLE '. $wpdb->terms .'; OPTIMIZE TABLE '. $wpdb->term_taxonomy .';'; $this->query_delete_oldtables = 'DROP TABLE '. $wpdb->prefix .'bsuite3_hits; DROP TABLE '. $wpdb->prefix .'bsuite3_refs_terms; DROP TABLE '. $wpdb->prefix .'bsuite3_search;'; } } // Instantiate and register the importer include_once(ABSPATH . 'wp-admin/includes/import.php'); if(function_exists('register_importer')) { $bstat_import = new bStat_Import(); register_importer($bstat_import->importer_code, $bstat_import->importer_name, $bstat_import->importer_desc, array (&$bstat_import, 'dispatch')); } add_action('activate_'.plugin_basename(__FILE__), 'bstat_importer_activate'); function bstat_importer_activate() { global $wp_db_version; // Deactivate on pre 2.3 blogs if($wp_db_version<6075) { $current = get_settings('active_plugins'); array_splice($current, array_search( plugin_basename(__FILE__), $current), 1 ); update_option('active_plugins', $current); do_action('deactivate_'.plugin_basename(__FILE__)); } } ?>