Skip to content

Instantly share code, notes, and snippets.

@r-a-y
Created August 8, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-a-y/da5311f64b6befd0e44eea84f76d80a2 to your computer and use it in GitHub Desktop.
Save r-a-y/da5311f64b6befd0e44eea84f76d80a2 to your computer and use it in GitHub Desktop.
Upgrade StatPress DB tables via `wp eval-file`. Only use this via wp-cli!
<?php
// Site IDs with StatPress activated.
$site_ids = array(184,605,673,694,773,820,834,854,913,984,1053,1105,1120,1121,1145,1154,1198,1215,1244,1266,1332,1333,1405,1466,1509,1538,1564,1612,1621,1736,1768,1771,1773,1808,1871,1961,1963,2011,2025,2219,2220,2249,2355,2368,2375,2520,2552,2601,2691);
if ( ! function_exists( 'StatPressV_activate' ) ) {
define("STATPRESS_V_DB_VERSION", "1.5");
//require ABSPATH . '/wp-content/plugins/statpress-visitors/statpress.php';
}
set_time_limit( 0 );
wp_suspend_cache_addition( true );
wp_suspend_cache_invalidation( true );
// duplicate of luc_StatPressV_CreateTable(), but without the STATPRESS_V_TABLE_NAME define
// since defines are not redefineable.
function cac_statpress_upgrade_db_schema()
{
global $wpdb, $wp_db_version, $StatPressV_Option;
// Ray mod - Changed this to use current WPDB prefix.
$table_name = $wpdb->prefix . "statpress";
$sql_createtable = "CREATE TABLE " . $table_name . " (
id MEDIUMINT(9)UNSIGNED NOT NULL AUTO_INCREMENT,
date INT(8) UNSIGNED NOT NULL,
time CHAR(8),
ip VARCHAR(39),
urlrequested TEXT,
agent TEXT,
referrer TEXT,
search TEXT,
os TINYTEXT,
browser TINYTEXT,
searchengine TINYTEXT,
spider TINYTEXT,
feed TINYTEXT,
user TINYTEXT,
timestamp INT(10) UNSIGNED NOT NULL,
language VARCHAR(3),
country VARCHAR(3),
realpost BOOLEAN,
post_title TINYTEXT,
UNIQUE KEY id (id),
KEY `date` (`date`)
);";
if ($wp_db_version >= 5540)
$page = 'wp-admin/includes/upgrade.php';
else
$page = 'wp-admin/upgrade-functions.php';
require_once (ABSPATH . $page);
dbDelta($sql_createtable);
// update the database version
$StatPressV_Option['StatPressV_DB_Version'] = STATPRESS_V_DB_VERSION;
update_option('StatPressV_Option', $StatPressV_Option);
// Remove useless column from some statpress
$wpdb->query("ALTER TABLE $table_name DROP COLUMN threat_score");
$wpdb->query("ALTER TABLE $table_name DROP COLUMN threat_type");
$wpdb->query("ALTER TABLE $table_name DROP COLUMN nation");
// Remove useless column from StatpressCN
$wpdb->query("ALTER TABLE $table_name DROP COLUMN ptype");
$wpdb->query("ALTER TABLE $table_name DROP COLUMN pvalue");
$wpdb->query("ALTER TABLE $table_name DROP COLUMN statuscode");
// Remove useless index from NewStatPress
$wpdb->query("ALTER TABLE $table_name DROP INDEX spider_nation");
$wpdb->query("ALTER TABLE $table_name DROP INDEX ip_date");
$wpdb->query("ALTER TABLE $table_name DROP INDEX agent");
$wpdb->query("ALTER TABLE $table_name DROP INDEX search");
$wpdb->query("ALTER TABLE $table_name DROP INDEX referrer");
$wpdb->query("ALTER TABLE $table_name DROP INDEX feed_spider_os");
$wpdb->query("ALTER TABLE $table_name DROP INDEX os");
$wpdb->query("ALTER TABLE $table_name DROP INDEX date_feed_spider");
$wpdb->query("ALTER TABLE $table_name DROP INDEX feed_spider_browser");
$wpdb->query("ALTER TABLE $table_name DROP INDEX browser");
// Remove useless index from Statpress-Seolution
$wpdb->query("ALTER TABLE $table_name DROP INDEX time_hour");
$wpdb->query("ALTER TABLE $table_name DROP INDEX date_time");
$wpdb->query("ALTER TABLE $table_name DROP INDEX spiders");
// Remove useless Row from Statpress-visitors
$wpdb->query("DELETE FROM $table_name WHERE ip IS NULL");
$wpdb->query('OPTIMIZE TABLE ' . $table_name);
}
// Loop through all sites and upgrade StatPress tables.
foreach( $site_ids as $sid ) {
switch_to_blog( $sid );
cac_statpress_upgrade_db_schema();
restore_current_blog();
}
echo 'StatPress DB tables all upgraded!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment