Ejemplo n.º 1
0
function cnRunDBUpgrade()
{
    global $wpdb, $connections;
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    require_once CN_PATH . 'includes/class.schema.php';
    $urlPath = admin_url() . 'admin.php?page=' . $_GET['page'];
    if (!empty($wpdb->charset)) {
        $charsetCollate = "DEFAULT CHARACTER SET {$wpdb->charset}";
    }
    if (!empty($wpdb->collate)) {
        $charsetCollate .= " COLLATE {$wpdb->collate}";
    }
    // Check to ensure that the table exists
    if ($wpdb->get_var("SHOW TABLES LIKE 'CN_ENTRY_TABLE'") != CN_ENTRY_TABLE) {
        echo '<h3>', __('Upgrade the database structure...', 'connections'), '</h3>' . "\n";
        $wpdb->show_errors();
        $dbVersion = $connections->options->getDBVersion();
        if (version_compare($dbVersion, '0.1.0', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.0.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            echo '<li>', __('Changing "id" type-length/values to BIGINT(20)', 'connections'), "</li>\n";
            if (!$wpdb->query("ALTER TABLE " . CN_ENTRY_TABLE . " CHANGE id id BIGINT(20) NOT NULL AUTO_INCREMENT")) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "date_added"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'date_added', 'tinytext NOT NULL AFTER ts')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "entry_type"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'entry_type', 'tinytext NOT NULL AFTER date_added')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "honorable_prefix"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'honorable_prefix', 'tinytext NOT NULL AFTER entry_type')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "middle_name"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'middle_name', 'tinytext NOT NULL AFTER first_name')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "honorable_suffix"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'honorable_suffix', 'tinytext NOT NULL AFTER last_name')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "social"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'social', 'longtext NOT NULL AFTER im')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "added_by"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'added_by', 'bigint(20) NOT NULL AFTER options')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "edited_by"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'edited_by', 'bigint(20) NOT NULL AFTER added_by')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "owner"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'owner', 'bigint(20) NOT NULL AFTER edited_by')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "status"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'status', 'varchar(20) NOT NULL AFTER owner')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "contact_first_name"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'contact_first_name', 'tinytext NOT NULL AFTER department')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding Column... "contact_last_name"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'contact_last_name', 'tinytext NOT NULL AFTER contact_first_name')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            echo '<h4>', __('Adding default term relationship.', 'connections'), '</h4>';
            // Add the Uncategorized category to all previous entries.
            $term = $connections->term->getTermBy('slug', 'uncategorized', 'category');
            $entryIDs = $wpdb->get_col("SELECT id FROM " . CN_ENTRY_TABLE);
            $termID[] = $term->term_taxonomy_id;
            foreach ($entryIDs as $entryID) {
                $connections->term->setTermRelationships($entryID, $termID, 'category');
            }
            $connections->options->setDBVersion('0.1.0');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.1', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.1.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<h4>', __('Setting all current entries to the "approved" status.', 'connections'), "</h4>\n";
            $wpdb->query('UPDATE ' . CN_ENTRY_TABLE . ' SET status = \'approved\'');
            $connections->options->setDBVersion('0.1.1');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.2', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.2.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<h4>', __('Setting all current entries `entry_type` column.', 'connections'), "</h4>\n";
            $sql = 'SELECT DISTINCT `id`, `options` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                $options = array();
                $options = unserialize($result->options);
                if (isset($options['entry']['type'])) {
                    $entryType = $options['entry']['type'];
                } else {
                    $entryType = 'individual';
                }
                $wpdb->query('UPDATE ' . CN_ENTRY_TABLE . ' SET entry_type = \'' . $entryType . '\' WHERE id = ' . $result->id);
            }
            $connections->options->setDBVersion('0.1.2');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.3', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.3.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            echo '<li>', __('Changing column name from group_name to family_name...', 'connections'), "</li>\n";
            if (cnAlterTable(CN_ENTRY_TABLE, 'family_name', 'CHANGE COLUMN group_name family_name tinytext NOT NULL')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.3');
        }
        if (version_compare($dbVersion, '0.1.4', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.4.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            echo '<li>', __('Changing column name from honorable_prefix to honorific_prefix...', 'connections'), "</li>\n";
            if (cnAlterTable(CN_ENTRY_TABLE, 'honorific_prefix', 'CHANGE COLUMN honorable_prefix honorific_prefix tinytext NOT NULL')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Changing column name from honorable_suffix to honorific_suffix...', 'connections'), "</li>\n";
            if (cnAlterTable(CN_ENTRY_TABLE, 'honorific_suffix', 'CHANGE COLUMN honorable_suffix honorific_suffix tinytext NOT NULL')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.4');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.5', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.5.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_TABLE_META . "'") != CN_ENTRY_TABLE_META) {
                echo '<li>', __('Add the entry meta table.', 'connections'), "</li>\n";
                $entryTableMeta = "CREATE TABLE " . CN_ENTRY_TABLE_META . " (\n\t\t\t        meta_id bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\tentry_id bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\tmeta_key varchar(255) default NULL,\n\t\t\t\t\tmeta_value longtext,\n\t\t\t\t\tPRIMARY KEY  (meta_id),\n\t\t\t\t\tKEY entry_id (entry_id),\n\t\t\t\t\tKEY meta_key (meta_key)\n\t\t\t    ) {$charsetCollate};";
                dbDelta($entryTableMeta);
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.5');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.6', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.6.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_ADDRESS_TABLE . "'") != CN_ENTRY_ADDRESS_TABLE) {
                echo '<li>', __('Add the entry address table.', 'connections'), "</li>\n";
                $entryTableAddress = "CREATE TABLE " . CN_ENTRY_ADDRESS_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`line_1` tinytext NOT NULL,\n\t\t\t\t\t`line_2` tinytext NOT NULL,\n\t\t\t\t\t`line_3` tinytext NOT NULL,\n\t\t\t\t\t`city` tinytext NOT NULL,\n\t\t\t\t\t`state` tinytext NOT NULL,\n\t\t\t\t\t`zipcode` tinytext NOT NULL,\n\t\t\t\t\t`country` tinytext NOT NULL,\n\t\t\t\t\t`latitude` decimal(15,12) default NULL,\n\t\t\t\t\t`longitude` decimal(15,12) default NULL,\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTableAddress);
            }
            echo '<li>', __('Porting addresses...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `addresses` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->addresses)) {
                    $addresses = unserialize($result->addresses);
                    $order = 0;
                    foreach ((array) $addresses as $key => $address) {
                        if (empty($address['type'])) {
                            $address['type'] = 'other';
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_ADDRESS_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`line_1`			= "%s",
												`line_2`			= "%s",
												`line_3`			= "%s",
												`city`				= "%s",
												`state`				= "%s",
												`zipcode`			= "%s",
												`country`			= "%s",
												`latitude`			= "%f",
												`longitude`			= "%f",
												`visibility`		= "%s"', $result->id, $order, 0, $address['type'], $address['address_line1'], $address['address_line2'], '', $address['city'], $address['state'], $address['zipcode'], $address['country'], $address['latitude'], $address['longitude'], 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_PHONE_TABLE . "'") != CN_ENTRY_PHONE_TABLE) {
                echo '<li>Add the entry phone table.', "</li>\n";
                $entryTablePhone = "CREATE TABLE " . CN_ENTRY_PHONE_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`number` tinytext NOT NULL,\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTablePhone);
            }
            echo '<li>', __('Porting phone numbers...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `phone_numbers` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->phone_numbers)) {
                    $phoneNumbers = unserialize($result->phone_numbers);
                    $order = 0;
                    foreach ((array) $phoneNumbers as $key => $phone) {
                        if (empty($phone['number'])) {
                            continue;
                        }
                        if (empty($phone['type'])) {
                            $phone['type'] = 'homephone';
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_PHONE_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`number`			= "%s",
												`visibility`		= "%s"', $result->id, $order, 0, $phone['type'], $phone['number'], 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_EMAIL_TABLE . "'") != CN_ENTRY_EMAIL_TABLE) {
                echo '<li>', __('Add the entry email table.', 'connections'), "</li>\n";
                $entryTableEmail = "CREATE TABLE " . CN_ENTRY_EMAIL_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`address` tinytext NOT NULL,\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTableEmail);
            }
            echo '<li>', __('Porting email addresses...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `email` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->email)) {
                    $emailAddresses = unserialize($result->email);
                    $order = 0;
                    foreach ((array) $emailAddresses as $key => $email) {
                        if (empty($email['address'])) {
                            continue;
                        }
                        if (empty($email['type'])) {
                            $email['type'] = 'personal';
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_EMAIL_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`address`			= "%s",
												`visibility`		= "%s"', $result->id, $order, 0, $email['type'], $email['address'], 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_MESSENGER_TABLE . "'") != CN_ENTRY_MESSENGER_TABLE) {
                echo '<li>', __('Add the entry messenger table.', 'connections'), "</li>\n";
                $entryTableMessenger = "CREATE TABLE " . CN_ENTRY_MESSENGER_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`uid` tinytext NOT NULL,\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTableMessenger);
            }
            echo '<li>', __('Porting IM IDs...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `im` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->im)) {
                    $imIDs = unserialize($result->im);
                    $order = 0;
                    foreach ((array) $imIDs as $key => $network) {
                        if (empty($network['id'])) {
                            continue;
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_MESSENGER_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`uid`				= "%s",
												`visibility`		= "%s"', $result->id, $order, 0, $network['type'], $network['id'], 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_SOCIAL_TABLE . "'") != CN_ENTRY_SOCIAL_TABLE) {
                echo '<li>', __('Add the entry social media table.', 'connections'), "</li>\n";
                $entryTableSocialMedia = "CREATE TABLE " . CN_ENTRY_SOCIAL_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`url` tinytext NOT NULL,\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTableSocialMedia);
            }
            echo '<li>', __('Porting Social Media IDs...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `social` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->social)) {
                    $socialMediaIDs = unserialize($result->social);
                    $order = 0;
                    foreach ((array) $socialMediaIDs as $key => $network) {
                        if (empty($network['url'])) {
                            continue;
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_SOCIAL_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`url`				= "%s",
												`visibility`		= "%s"', $result->id, $order, 0, $network['type'], $network['url'], 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_LINK_TABLE . "'") != CN_ENTRY_LINK_TABLE) {
                echo '<li>', __('Add the entry link table.', 'connections'), "</li>\n";
                $entryTableLink = "CREATE TABLE " . CN_ENTRY_LINK_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`title` tinytext NOT NULL,\n\t\t\t\t\t`url` tinytext NOT NULL,\n\t\t\t\t\t`target` tinytext NOT NULL,\n\t\t\t\t\t`follow` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                dbDelta($entryTableLink);
            }
            echo '<li>', __('Porting websites...', 'connections'), "</li>\n";
            $sql = 'SELECT DISTINCT `id`, `websites` FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                if (!empty($result->websites)) {
                    $websites = unserialize($result->websites);
                    $order = 0;
                    foreach ((array) $websites as $key => $website) {
                        if (empty($website['address'])) {
                            continue;
                        }
                        $sql = $wpdb->prepare('INSERT INTO ' . CN_ENTRY_LINK_TABLE . ' SET
												`entry_id`			= "%d",
												`order`				= "%d",
												`preferred`			= "%d",
												`type`				= "%s",
												`title`				= "%s",
												`url`				= "%s",
												`target`			= "%s",
												`follow`			= "%d",
												`visibility`		= "%s"', $result->id, $order, 0, 'website', 'Visit Website', $website['address'], 'new', 0, 'public');
                        $wpdb->query($sql);
                        $order++;
                    }
                }
            }
            echo '<li>', __('Changing column name from websites to links...', 'connections'), "</li>\n";
            if (cnAlterTable(CN_ENTRY_TABLE, 'links', 'CHANGE COLUMN websites links longtext NOT NULL')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding column... "slug"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'slug', 'tinytext NOT NULL AFTER visibility')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding entry slugs...', 'connections'), "</li>\n";
            $sql = 'SELECT id,
						CASE entry_type
						  WHEN \'individual\' THEN CONCAT_WS( \'-\' , LOWER(first_name) , LOWER(last_name) )
						  WHEN \'organization\' THEN LOWER(organization)
						  WHEN \'connection_group\' THEN LOWER(family_name)
						  WHEN \'family\' THEN LOWER(family_name)
						END AS slug_temp
					FROM ' . CN_ENTRY_TABLE;
            $results = $wpdb->get_results($sql);
            foreach ($results as $result) {
                $slug = sanitize_title($result->slug_temp);
                $query = $wpdb->prepare('SELECT slug FROM ' . CN_ENTRY_TABLE . ' WHERE slug = %s', $slug);
                if ($wpdb->get_var($query)) {
                    $num = 2;
                    do {
                        $alt_slug = $slug . "-{$num}";
                        $num++;
                        $slug_check = $wpdb->get_var($wpdb->prepare('SELECT slug FROM ' . CN_ENTRY_TABLE . ' WHERE slug = %s', $alt_slug));
                    } while ($slug_check);
                    $slug = $alt_slug;
                }
                $wpdb->query('UPDATE ' . CN_ENTRY_TABLE . ' SET slug = \'' . $slug . '\' WHERE id = ' . $result->id);
            }
            echo '</ul>';
            /*
             * Not a db upgrade but we'll drop this in here to make it simple.
             * This file is no longer needed so we'll try to remove it.
             */
            if (file_exists(ABSPATH . 'download.vCard.php')) {
                @unlink(ABSPATH . 'download.vCard.php');
            }
            // Create the cache folder.
            wp_mkdir_p(CN_CACHE_PATH);
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0746);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0747);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0756);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0757);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0764);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0765);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0766);
            }
            if (file_exists(CN_CACHE_PATH) && !is_writeable(CN_CACHE_PATH)) {
                @chmod(CN_CACHE_PATH, 0767);
            }
            // Create the images folder.
            // wp_mkdir_p( CN_IMAGE_PATH );
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0746);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0747);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0756);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0757);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0764);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0765);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0766);
            }
            if (file_exists(CN_IMAGE_PATH) && !is_writeable(CN_IMAGE_PATH)) {
                @chmod(CN_IMAGE_PATH, 0767);
            }
            // Create the custom template folder.
            wp_mkdir_p(CN_CUSTOM_TEMPLATE_PATH);
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0746);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0747);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0756);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0757);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0764);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0765);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0766);
            }
            if (file_exists(CN_CUSTOM_TEMPLATE_PATH) && !is_writeable(CN_CUSTOM_TEMPLATE_PATH)) {
                @chmod(CN_CUSTOM_TEMPLATE_PATH, 0767);
            }
            $connections->options->setDBVersion('0.1.6');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.7', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.7.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            echo '<li>', __('Adding column "image" to the links table.', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_LINK_TABLE, 'image', "tinyint unsigned NOT NULL default '0' AFTER follow")) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding column "logo" to the links table.', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_LINK_TABLE, 'logo', "tinyint unsigned NOT NULL default '0' AFTER image")) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.7');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.8', '<')) {
            $fields['fields_entry'] = array('family_name', 'first_name', 'middle_name', 'last_name', 'title', 'organization', 'department', 'contact_first_name', 'contact_last_name', 'bio', 'notes');
            $fields['fields_address'] = array('line_1', 'line_2', 'line_3', 'city', 'state', 'zipcode', 'country');
            $fields['fields_phone'] = array('number');
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.8.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<p><strong>', __('NOTE', 'connections'), ':</strong> ', __('You might receive this error: "The used table type doesn\'t support FULLTEXT indexes".', 'connections'), '</p>';
            echo '<p>', __('This is not a critical error. What this means is that the database does not support FULLTEXT query statments. Connections will perform a secondary search query in order to return search results.', 'connections'), '</p>';
            echo '<ul>';
            echo '<li>Adding FULLTEXT to ' . CN_ENTRY_TABLE . ' ' . "</li>\n";
            $wpdb->query('ALTER TABLE ' . CN_ENTRY_TABLE . ' ADD FULLTEXT (' . implode(',', $fields['fields_entry']) . ')');
            echo '<li>Adding FULLTEXT to ' . CN_ENTRY_ADDRESS_TABLE . ' ' . "</li>\n";
            $wpdb->query('ALTER TABLE ' . CN_ENTRY_ADDRESS_TABLE . ' ADD FULLTEXT (' . implode(',', $fields['fields_address']) . ')');
            echo '<li>Adding FULLTEXT to ' . CN_ENTRY_PHONE_TABLE . ' ' . "</li>\n";
            $wpdb->query('ALTER TABLE ' . CN_ENTRY_PHONE_TABLE . ' ADD FULLTEXT (' . implode(',', $fields['fields_phone']) . ')');
            echo '</ul>';
            echo '<p>', __('The activate action for Connections 0.7.2.2 was not properly updated which created a fatal bug for new installations of Connections. Checking for the missing table columns in the Links table and add them, if missing.', 'connections'), '</p>';
            echo '<ul>';
            echo '<li>', __('Adding, if missing, column "image" to the links table.', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_LINK_TABLE, 'image', "tinyint unsigned NOT NULL default '0' AFTER follow")) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding, if missing, column "logo" to the links table.', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_LINK_TABLE, 'logo', "tinyint unsigned NOT NULL default '0' AFTER image")) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.8');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.1.9', '<')) {
            echo '<h4>', sprintf(__('Upgrade from database version %1$s to database version 0.1.9.', 'connections'), $connections->options->getDBVersion()), "</h4>\n";
            echo '<ul>';
            if ($wpdb->get_var("SHOW TABLES LIKE '" . CN_ENTRY_DATE_TABLE . "'") != CN_ENTRY_DATE_TABLE) {
                echo '<li>', __('Add the date table.', 'connections'), "</li>\n";
                $entryTableDate = "CREATE TABLE " . CN_ENTRY_DATE_TABLE . " (\n\t\t\t        `id` bigint(20) unsigned NOT NULL auto_increment,\n\t\t\t\t\t`entry_id` bigint(20) unsigned NOT NULL default '0',\n\t\t\t\t\t`order` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`preferred` tinyint unsigned NOT NULL default '0',\n\t\t\t\t\t`type` tinytext NOT NULL,\n\t\t\t\t\t`date` date NOT NULL default '0000-00-00',\n\t\t\t\t\t`visibility` tinytext NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`, `entry_id`)\n\t\t\t    ) {$charsetCollate}";
                // Create the table
                dbDelta($entryTableDate);
            }
            echo '<li>', __('Adding column... "user"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'user', 'tinytext NOT NULL AFTER owner')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '<li>', __('Adding column... "dates"', 'connections'), "</li>\n";
            if (cnAddTableColumn(CN_ENTRY_TABLE, 'dates', 'longtext NOT NULL AFTER links')) {
                echo '<ul><li>', __('SUCCESS', 'connections'), '</li></ul>';
            }
            echo '</ul>';
            $connections->options->setDBVersion('0.1.9');
            // Save the options
            $connections->options->saveOptions();
        }
        if (version_compare($dbVersion, '0.2', '<')) {
            cnSchema::create();
            $connections->options->setDBVersion('0.2');
            // Save the options
            $connections->options->saveOptions();
        }
        $connections->options->saveOptions();
        echo '<h4>', __('Upgrade completed.', 'connections'), "</h4>\n";
        echo '<h4><a class="button-primary" href="' . $urlPath . '">', __('Continue', 'connections'), '</a></h4>';
        $wpdb->hide_errors();
    }
}
Ejemplo n.º 2
0
function cnRunDBUpgrade()
{
	global $wpdb, $connections;
	
	$urlPath = admin_url() . 'admin.php?page=' . $_GET['page'];
	
	// Check to ensure that the table exists
	if ($wpdb->get_var("SHOW TABLES LIKE 'CN_ENTRY_TABLE'") != CN_ENTRY_TABLE)
	{
		echo '<h3>Upgrade the database structure...</h3>' . "\n";
		$wpdb->show_errors();
		
		$dbVersion = $connections->options->getDBVersion();
		
		if (version_compare($dbVersion, '0.1.0', '<'))
		{
			echo '<h4>Upgrade from database version ' . $connections->options->getDBVersion() . ' to database version 0.1.0 ' . ".</h4>\n";
			echo '<ul>';
			
			echo '<li>Changing "id" type-length/values to BIGINT(20)' . "</li>\n";
			if (!$wpdb->query("ALTER TABLE " . CN_ENTRY_TABLE . " CHANGE id id BIGINT(20) NOT NULL AUTO_INCREMENT")) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "date_added"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'date_added', 'tinytext NOT NULL AFTER ts')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "entry_type"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'entry_type', 'tinytext NOT NULL AFTER date_added')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "honorable_prefix"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'honorable_prefix', 'tinytext NOT NULL AFTER entry_type')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "middle_name"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'middle_name', 'tinytext NOT NULL AFTER first_name')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "honorable_suffix"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'honorable_suffix', 'tinytext NOT NULL AFTER last_name')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "social"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'social', 'longtext NOT NULL AFTER im')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "added_by"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'added_by', 'bigint(20) NOT NULL AFTER options')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "edited_by"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'edited_by', 'bigint(20) NOT NULL AFTER added_by')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "owner"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'owner', 'bigint(20) NOT NULL AFTER edited_by')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "status"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'status', 'varchar(20) NOT NULL AFTER owner')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "contact_first_name"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'contact_first_name', 'tinytext NOT NULL AFTER department')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Adding Column... "contact_last_name"' . "</li>\n";
			if (cnAddTableColumn(CN_ENTRY_TABLE, 'contact_last_name', 'tinytext NOT NULL AFTER contact_first_name')) echo '<ul><li>SUCCESS</li></ul>';
			echo '</ul>';
		
			echo '<h4>Adding default term relationship.</h4>';
			
			// Add the Uncategorized category to all previous entries.
			$term = $connections->term->getTermBy('slug', 'uncategorized', 'category');
			
			$entryIDs = $wpdb->get_col( "SELECT id FROM " . CN_ENTRY_TABLE );
			
			$termID[] = $term->term_taxonomy_id;
			
			foreach ($entryIDs as $entryID)
			{
				$connections->term->setTermRelationships($entryID, $termID, 'category');
			}
			
			$connections->options->setDBVersion('0.1.0');
		}
		
		if (version_compare($dbVersion, '0.1.1', '<'))
		{
			echo '<h4>Upgrade from database version ' . $connections->options->getDBVersion() . ' to database version 0.1.1 ' . ".</h4>\n";
			
			//echo '<h4>Setting all current entries to the "approved" status.' . "</h4>\n";
			/*$results = $connections->retrieve->entries();
			
			foreach ($results as $result)
			{
				$entry = new cnEntry($result);
				$entry->update();
			}*/
			
			$connections->options->setDBVersion('0.1.1');
		}
		
		if (version_compare($dbVersion, '0.1.2', '<'))
		{
			echo '<h4>Upgrade from database version ' . $connections->options->getDBVersion() . ' to database version 0.1.2 ' . ".</h4>\n";
			
			//echo '<h4>Setting all current entries `entry_type` column.' . "</h4>\n";
			/*$results = $connections->retrieve->entries();
			
			foreach ($results as $result)
			{
				$entry = new cnEntry($result);
				$entry->update();
			}*/
			
			$connections->options->setDBVersion('0.1.2');
		}
		
		if (version_compare($dbVersion, '0.1.3', '<'))
		{
			echo '<h4>Upgrade from database version ' . $connections->options->getDBVersion() . ' to database version ' . CN_DB_VERSION . ".</h4>\n";
			
			echo '<ul>';
			echo '<li>Changing column name from group_name to family_name...' . "</li>\n";
			if (cnAlterTable(CN_ENTRY_TABLE, 'CHANGE COLUMN group_name family_name tinytext NOT NULL')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '</ul>';
			
			$connections->options->setDBVersion('0.1.3');
		}
		
		if (version_compare($dbVersion, '0.1.4', '<'))
		{
			echo '<h4>Upgrade from database version ' . $connections->options->getDBVersion() . ' to database version ' . CN_DB_VERSION . ".</h4>\n";
			
			echo '<ul>';
			echo '<li>Changing column name from honorable_prefix to honorific_prefix...' . "</li>\n";
			if (cnAlterTable(CN_ENTRY_TABLE, 'CHANGE COLUMN honorable_prefix honorific_prefix tinytext NOT NULL')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '<li>Changing column name from honorable_suffix to honorific_suffix...' . "</li>\n";
			if (cnAlterTable(CN_ENTRY_TABLE, 'CHANGE COLUMN honorable_suffix honorific_suffix tinytext NOT NULL')) echo '<ul><li>SUCCESS</li></ul>';
			
			echo '</ul>';
			
			$connections->options->setDBVersion('0.1.4');
		}
		
		echo '<h4>Updating entries to the new database stucture.' . "</h4>\n";
		
		$results = $connections->retrieve->entries();
		
		foreach ($results as $result)
		{
			$entry = new cnEntry($result);
			$entry->update();
		}
		
		echo '<h4>Upgrade completed.' . "</h4>\n";
		echo '<h4><a href="' . $urlPath . '">Continue</a></h4>';
		
		$wpdb->hide_errors();
	}
}