/** * Upgrade routines * * @since 1.0 */ private function upgrade() { global $wpdb, $searchwp; if (version_compare($this->last_version, '1.3.1', '<')) { // clean up misuse of cron schedule wp_clear_scheduled_hook('swp_cron_indexer'); } if (version_compare($this->last_version, '1.6.7', '<')) { // truncate logs table $prefix = $wpdb->prefix . SEARCHWP_DBPREFIX; $tableName = $prefix . 'log'; $wpdb->query("TRUNCATE TABLE {$tableName}"); } if (version_compare($this->last_version, '1.8', '<')) { // fix a possible issue with settings storage resulting in MySQL errors after update $settings = get_option(SEARCHWP_PREFIX . 'settings'); if (is_array($settings)) { // make sure additional array keys are present and defined foreach ($settings['engines'] as $engine_key => $engine_setting) { foreach ($settings['engines'][$engine_key] as $post_type => $post_type_settings) { if (is_array($settings['engines'][$engine_key][$post_type]) && isset($settings['engines'][$engine_key][$post_type]['options']) && !is_array($settings['engines'][$engine_key][$post_type]['options'])) { $settings['engines'][$engine_key][$post_type]['options'] = array('exclude' => false, 'attribute_to' => false, 'stem' => false); } } } } searchwp_update_option('settings', $settings); } // index cleanup and optimization if (version_compare($this->last_version, '1.9', '<')) { $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'termindex'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX termindex;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'stemindex'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX stemindex;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'id'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX id;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_index` WHERE Key_name = 'id'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_index DROP INDEX id;"); } $wpdb->query("CREATE INDEX termindex ON {$wpdb->prefix}swp_terms(term(2));"); $wpdb->query("CREATE INDEX stemindex ON {$wpdb->prefix}swp_terms(stem(2));"); } // consolidate settings into one database record if (version_compare($this->last_version, '1.9.1', '<')) { $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'termindex'", ARRAY_N); if (empty($index_exists)) { $wpdb->query("CREATE INDEX termindex ON {$wpdb->prefix}swp_terms(term(2));"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'stemindex'", ARRAY_N); if (empty($index_exists)) { $wpdb->query("CREATE INDEX stemindex ON {$wpdb->prefix}swp_terms(term(2));"); } $old_settings = searchwp_get_option('settings'); $engines = isset($old_settings['engines']) ? $old_settings['engines'] : array(); // clear out the old settings because we're using the same key searchwp_delete_option('settings'); // in with the new searchwp_generate_settings($engines); // delete the old options searchwp_delete_option('activated'); searchwp_delete_option('license_nag'); searchwp_delete_option('dismissed'); searchwp_delete_option('ignored_queries'); searchwp_delete_option('indexer_nag'); searchwp_delete_option('valid_db_environment'); searchwp_delete_option('running'); searchwp_delete_option('total'); searchwp_delete_option('remaining'); searchwp_delete_option('done'); searchwp_delete_option('in_process'); searchwp_delete_option('initial'); searchwp_delete_option('initial_notified'); searchwp_delete_option('purgeQueue'); searchwp_delete_option('processingPurgeQueue'); searchwp_delete_option('mysql_version_nag'); searchwp_delete_option('remote'); searchwp_delete_option('remote_meta'); searchwp_delete_option('paused'); searchwp_delete_option('nuke_on_delete'); searchwp_delete_option('indexnonce'); } if (version_compare($this->last_version, '1.9.2.2', '<')) { searchwp_add_option('progress', -1); } if (version_compare($this->last_version, '1.9.4', '<')) { // clean up a potential useless settings save $live_settings = searchwp_get_option('settings'); $update_settings_record = false; if (is_array($live_settings)) { foreach ($live_settings as $live_setting_key => $live_setting_value) { // none of our keys should be numeric (specifically going after a rogue 'running' setting that // may have been inadvertently set in 1.9.2, we just don't want it in there at all if (is_numeric($live_setting_key)) { unset($live_settings[$live_setting_key]); $update_settings_record = true; } // also update 'nuke_on_delete' to be a boolean if necessary if ('nuke_on_delete' === $live_setting_key) { $live_settings['nuke_on_delete'] = empty($live_setting_value) ? false : true; $update_settings_record = true; } } } if ($update_settings_record) { // save the cleaned up settings array searchwp_update_option('settings', $live_settings); $searchwp->settings = $live_settings; } } if (version_compare($this->last_version, '1.9.5', '<')) { // move indexer-specific settings to their own record as they're being constantly updated $live_settings = searchwp_get_option('settings'); $indexer_settings = array(); // whether the initial index has been built if (isset($live_settings['initial_index_built'])) { $indexer_settings['initial_index_built'] = (bool) $live_settings['initial_index_built']; unset($live_settings['initial_index_built']); } else { $indexer_settings['initial_index_built'] = false; } // all of the stats if (isset($live_settings['stats'])) { $indexer_settings['stats'] = $live_settings['stats']; unset($live_settings['stats']); } else { $indexer_settings['stats'] = array(); } // whether the indexer is running if (isset($live_settings['running'])) { $indexer_settings['running'] = (bool) $live_settings['running']; unset($live_settings['running']); } else { $indexer_settings['running'] = false; } // whether the indexer is paused (disabled) if (isset($live_settings['paused'])) { $indexer_settings['paused'] = (bool) $live_settings['paused']; unset($live_settings['paused']); } else { $indexer_settings['paused'] = false; } // whether the indexer is processing the purge queue if (isset($live_settings['processing_purge_queue'])) { $indexer_settings['processing_purge_queue'] = (bool) $live_settings['processing_purge_queue']; unset($live_settings['processing_purge_queue']); } else { $indexer_settings['processing_purge_queue'] = false; } // the purge queue will be moved to it's own option to avoid conflict if (isset($live_settings['purge_queue'])) { searchwp_add_option('purge_queue', $live_settings['purge_queue']); unset($live_settings['purge_queue']); } searchwp_update_option('settings', $live_settings); searchwp_add_option('indexer', $indexer_settings); } if (version_compare($this->last_version, '1.9.6', '<')) { // wake up the indexer if necessary $running = searchwp_get_setting('running'); if (empty($running)) { searchwp_set_setting('running', false); } } // make ignored queries for search stats per-user if (version_compare($this->last_version, '2.0.2', '<')) { $user_id = get_current_user_id(); if ($user_id) { $ignored_queries = searchwp_get_setting('ignored_queries'); update_user_meta($user_id, SEARCHWP_PREFIX . 'ignored_queries', $ignored_queries); } } // add 'busy' option if (version_compare($this->last_version, '2.1.5', '<')) { searchwp_add_option('busy', false); searchwp_add_option('doing_delta', false); } // force a wakeup if (version_compare($this->last_version, '2.2.1', '<')) { if (function_exists('searchwp_wake_up_indexer')) { searchwp_wake_up_indexer(); } } // add new 'waiting' flag, prep for possible new custom endpoint, clear out redundant post meta if (version_compare($this->last_version, '2.3', '<')) { searchwp_add_option('waiting', false); searchwp_set_setting('endpoint', ''); // now using last_index instead of indexed, we don't need separate records $wpdb->delete($wpdb->prefix . 'postmeta', array('meta_key' => '_' . SEARCHWP_PREFIX . 'indexed')); } if (version_compare($this->last_version, '2.4.5', '<')) { // implement our settings backup $live_settings = searchwp_get_option('settings'); $settings_backups = array(); $settings_backups[current_time('timestamp')] = $live_settings; searchwp_add_option('settings_backup', $settings_backups); // there was a bug triggered by a custom post type name of 'label' that caused issues // so we need to update all of the supplemental engine label keys to searchwp_engine_label // which will not trigger the issue because it is 21 characters in length and WordPress // requires post type names to be 20 characters or less if (isset($live_settings['engines'])) { foreach ($live_settings['engines'] as $live_settings_engine_key => $live_settings_engine_values) { if (isset($live_settings_engine_values['label'])) { $engine_label = $live_settings_engine_values['label']; unset($live_settings['engines'][$live_settings_engine_key]['label']); $live_settings['engines'][$live_settings_engine_key]['searchwp_engine_label'] = $engine_label; } } } searchwp_update_option('settings', $live_settings); } /** * The upgrade routine for 2.5.7 was designed to implement support for utf8mb4 as per WordPress 4.2, it even * used the same code to do so. Unfortunately the index key changes and charset changes can take a very (very) * long time depending on the power of the server and the size of the database tables. Unfortunately SearchWP's * tables are quite large, and the update routine took *way* too long on some test machines. While the update * was running, performance on the front end was erratic at best, primarily because the table updates caused * MySQL to utilize ~100% CPU, thus preventing other traffic from reaching the server. As a result, existing * installations of SearchWP will not be converted to utf8mb4, only fresh installations. The indexer and search * algorithm will actively strip out problematic characters (e.g. emoji) if the tables are not prepared for them. * if ( version_compare( $this->last_version, '2.5.7', '<' ) ) { require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); SWP()->indexer_pause(); // utf8mb4 index length limit is 191 @link https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ $wpdb->query( "ALTER TABLE {$wpdb->prefix}swp_cf DROP INDEX metakey, ADD INDEX metakey(metakey(191));" ); $wpdb->query( "ALTER TABLE {$wpdb->prefix}swp_log DROP INDEX queryindex, ADD INDEX queryindex(query(191));" ); $wpdb->query( "ALTER TABLE {$wpdb->prefix}swp_log DROP INDEX engineindex, ADD INDEX engineindex(engine(191));" ); // loop through tables and upgrade them to utf8mb4 $tables = array( $wpdb->prefix . 'swp_cf', $wpdb->prefix . 'swp_index', $wpdb->prefix . 'swp_log', $wpdb->prefix . 'swp_tax', $wpdb->prefix . 'swp_terms', ); $successful = true; foreach ( $tables as $table ) { // WordPress 4.2 added maybe_convert_table_to_utf8mb4() but // we don't necessarily have access to it (e.g. user is running <4.2) // but we also don't want to have to keep track of what WP version // is in play and have to continually compare that to whether this // upgrade routine has run so the function has been copied verbatim // for use here because utf8mb4 is fully backwards compatible so we're // going for the full upgrade by using a copy of that function $result = searchwp_maybe_convert_table_to_utf8mb4( $table ); if ( ( is_wp_error( $result ) || false === $result ) ) { // there was a problem $successful = false; } } if ( ! $successful ) { // there was a problem with the utf8mb4 upgrade but that doesn't necessarily // mean there is a show-stopping issue, just that the table is still utf8 // so log that the upgrade failed and indicate it in System Info searchwp_add_option( 'utf8mb4_upgrade_failed', true ); } SWP()->indexer_unpause(); } */ }
/** * Upgrade routines * * @since 1.0 */ private function upgrade() { global $wpdb, $searchwp; if (version_compare($this->last_version, '1.3.1', '<')) { // clean up misuse of cron schedule wp_clear_scheduled_hook('swp_cron_indexer'); } if (version_compare($this->last_version, '1.6.7', '<')) { // truncate logs table $prefix = $wpdb->prefix . SEARCHWP_DBPREFIX; $tableName = $prefix . 'log'; $wpdb->query("TRUNCATE TABLE {$tableName}"); } if (version_compare($this->last_version, '1.8', '<')) { // fix a possible issue with settings storage resulting in MySQL errors after update $settings = get_option(SEARCHWP_PREFIX . 'settings'); if (is_array($settings)) { // make sure additional array keys are present and defined foreach ($settings['engines'] as $engine_key => $engine_setting) { foreach ($settings['engines'][$engine_key] as $post_type => $post_type_settings) { if (is_array($settings['engines'][$engine_key][$post_type]) && !is_array($settings['engines'][$engine_key][$post_type]['options'])) { $settings['engines'][$engine_key][$post_type]['options'] = array('exclude' => false, 'attribute_to' => false, 'stem' => false); } } } } searchwp_update_option('settings', $settings); } // index cleanup and optimization if (version_compare($this->last_version, '1.9', '<')) { $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'termindex'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX termindex;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'stemindex'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX stemindex;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'id'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_terms DROP INDEX id;"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_index` WHERE Key_name = 'id'", ARRAY_N); if (!empty($index_exists)) { $wpdb->query("ALTER TABLE {$wpdb->prefix}swp_index DROP INDEX id;"); } $wpdb->query("CREATE INDEX termindex ON {$wpdb->prefix}swp_terms(term(2));"); $wpdb->query("CREATE INDEX stemindex ON {$wpdb->prefix}swp_terms(stem(2));"); } // consolidate settings into one database record if (version_compare($this->last_version, '1.9.1', '<')) { $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'termindex'", ARRAY_N); if (empty($index_exists)) { $wpdb->query("CREATE INDEX termindex ON {$wpdb->prefix}swp_terms(term(2));"); } $index_exists = $wpdb->get_results("SHOW INDEX FROM `{$wpdb->prefix}swp_terms` WHERE Key_name = 'stemindex'", ARRAY_N); if (empty($index_exists)) { $wpdb->query("CREATE INDEX stemindex ON {$wpdb->prefix}swp_terms(term(2));"); } $old_settings = searchwp_get_option('settings'); $engines = isset($old_settings['engines']) ? $old_settings['engines'] : array(); // clear out the old settings because we're using the same key searchwp_delete_option('settings'); // in with the new searchwp_generate_settings($engines); // delete the old options searchwp_delete_option('activated'); searchwp_delete_option('license_nag'); searchwp_delete_option('dismissed'); searchwp_delete_option('ignored_queries'); searchwp_delete_option('indexer_nag'); searchwp_delete_option('valid_db_environment'); searchwp_delete_option('running'); searchwp_delete_option('total'); searchwp_delete_option('remaining'); searchwp_delete_option('done'); searchwp_delete_option('in_process'); searchwp_delete_option('initial'); searchwp_delete_option('initial_notified'); searchwp_delete_option('purgeQueue'); searchwp_delete_option('processingPurgeQueue'); searchwp_delete_option('mysql_version_nag'); searchwp_delete_option('remote'); searchwp_delete_option('remote_meta'); searchwp_delete_option('paused'); searchwp_delete_option('nuke_on_delete'); searchwp_delete_option('indexnonce'); } if (version_compare($this->last_version, '1.9.2.2', '<')) { searchwp_add_option('progress', -1); } if (version_compare($this->last_version, '1.9.4', '<')) { // clean up a potential useless settings save $live_settings = searchwp_get_option('settings'); $update_settings_record = false; if (is_array($live_settings)) { foreach ($live_settings as $live_setting_key => $live_setting_value) { // none of our keys should be numeric (specifically going after a rogue 'running' setting that // may have been inadvertently set in 1.9.2, we just don't want it in there at all if (is_numeric($live_setting_key)) { unset($live_settings[$live_setting_key]); $update_settings_record = true; } // also update 'nuke_on_delete' to be a boolean if necessary if ('nuke_on_delete' === $live_setting_key) { $live_settings['nuke_on_delete'] = empty($live_setting_value) ? false : true; $update_settings_record = true; } } } if ($update_settings_record) { // save the cleaned up settings array searchwp_update_option('settings', $live_settings); $searchwp->settings = $live_settings; } } if (version_compare($this->last_version, '1.9.5', '<')) { // move indexer-specific settings to their own record as they're being constantly updated $live_settings = searchwp_get_option('settings'); $indexer_settings = array(); // whether the initial index has been built if (isset($live_settings['initial_index_built'])) { $indexer_settings['initial_index_built'] = (bool) $live_settings['initial_index_built']; unset($live_settings['initial_index_built']); } else { $indexer_settings['initial_index_built'] = false; } // all of the stats if (isset($live_settings['stats'])) { $indexer_settings['stats'] = $live_settings['stats']; unset($live_settings['stats']); } else { $indexer_settings['stats'] = array(); } // whether the indexer is running if (isset($live_settings['running'])) { $indexer_settings['running'] = (bool) $live_settings['running']; unset($live_settings['running']); } else { $indexer_settings['running'] = false; } // whether the indexer is paused (disabled) if (isset($live_settings['paused'])) { $indexer_settings['paused'] = (bool) $live_settings['paused']; unset($live_settings['paused']); } else { $indexer_settings['paused'] = false; } // whether the indexer is processing the purge queue if (isset($live_settings['processing_purge_queue'])) { $indexer_settings['processing_purge_queue'] = (bool) $live_settings['processing_purge_queue']; unset($live_settings['processing_purge_queue']); } else { $indexer_settings['processing_purge_queue'] = false; } // the purge queue will be moved to it's own option to avoid conflict if (isset($live_settings['purge_queue'])) { searchwp_add_option('purge_queue', $live_settings['purge_queue']); unset($live_settings['purge_queue']); } searchwp_update_option('settings', $live_settings); searchwp_add_option('indexer', $indexer_settings); } if (version_compare($this->last_version, '1.9.6', '<')) { // wake up the indexer if necessary $running = searchwp_get_setting('running'); if (empty($running)) { searchwp_set_setting('running', false); } } // make ignored queries for search stats per-user if (version_compare($this->last_version, '2.0.2', '<')) { $user_id = get_current_user_id(); if ($user_id) { $ignored_queries = searchwp_get_setting('ignored_queries'); update_user_meta($user_id, SEARCHWP_PREFIX . 'ignored_queries', $ignored_queries); } } // add 'busy' option if (version_compare($this->last_version, '2.1.5', '<')) { searchwp_add_option('busy', false); searchwp_add_option('doing_delta', false); } // force a wakeup if (version_compare($this->last_version, '2.2.1', '<')) { if (function_exists('searchwp_wake_up_indexer')) { searchwp_wake_up_indexer(); } } }