Esempio n. 1
0
 private static function install_signups()
 {
     global $wpdb;
     // Signups is not there and we need it so let's create it
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     // Use WP's core CREATE TABLE query
     $create_queries = wp_get_db_schema('ms_global');
     if (!is_array($create_queries)) {
         $create_queries = explode(';', $create_queries);
         $create_queries = array_filter($create_queries);
     }
     // Filter out all the queries except wp_signups
     foreach ($create_queries as $key => $query) {
         if (preg_match("|CREATE TABLE ([^ ]*)|", $query, $matches)) {
             if (trim($matches[1], '`') !== $wpdb->signups) {
                 unset($create_queries[$key]);
             }
         }
     }
     // Run WordPress's database upgrader
     if (!empty($create_queries)) {
         dbDelta($create_queries);
     }
 }
Esempio n. 2
0
 function install_network()
 {
     if (!defined('WP_INSTALLING_NETWORK')) {
         define('WP_INSTALLING_NETWORK', true);
     }
     dbDelta(wp_get_db_schema('global'));
 }
/**
 * {@internal Missing Short Description}}
 *
 * {@internal Missing Long Description}}
 *
 * @since 1.5.0
 *
 * @param unknown_type $queries
 * @param unknown_type $execute
 * @return unknown
 */
function dbDelta($queries = '', $execute = true)
{
    global $wpdb;
    if (in_array($queries, array('', 'all', 'blog', 'global', 'ms_global'), true)) {
        $queries = wp_get_db_schema($queries);
    }
    // Separate individual queries into an array
    if (!is_array($queries)) {
        $queries = explode(';', $queries);
        $queries = array_filter($queries);
    }
    /** 
     * Filter the dbDelta SQL queries.
     *
     * @since 3.3.0
     *
     * @param array $queries An array of dbDelta SQL queries.
     */
    $queries = apply_filters('dbdelta_queries', $queries);
    $cqueries = array();
    // Creation Queries
    $iqueries = array();
    // Insertion Queries
    $for_update = array();
    // Create a tablename index for an array ($cqueries) of queries
    foreach ($queries as $qry) {
        if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
            $cqueries[trim($matches[1], '`')] = $qry;
            $for_update[$matches[1]] = 'Created table ' . $matches[1];
        } else {
            if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
                array_unshift($cqueries, $qry);
            } else {
                if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
                    $iqueries[] = $qry;
                } else {
                    if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
                        $iqueries[] = $qry;
                    } else {
                        // Unrecognized query type
                    }
                }
            }
        }
    }
    /** 
     * Filter the dbDelta SQL queries for creating tables and/or databases.
     *
     * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
     * 
     * @since 3.3.0
     *
     * @param array $cqueries An array of dbDelta create SQL queries.
     */
    $cqueries = apply_filters('dbdelta_create_queries', $cqueries);
    /** 
     * Filter the dbDelta SQL queries for inserting or updating.
     *
     * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
     * 
     * @since 3.3.0
     *
     * @param array $iqueries An array of dbDelta insert or update SQL queries.
     */
    $iqueries = apply_filters('dbdelta_insert_queries', $iqueries);
    $global_tables = $wpdb->tables('global');
    foreach ($cqueries as $table => $qry) {
        // Upgrade global tables only for the main site. Don't upgrade at all if DO_NOT_UPGRADE_GLOBAL_TABLES is defined.
        if (in_array($table, $global_tables) && (!is_main_site() || defined('DO_NOT_UPGRADE_GLOBAL_TABLES'))) {
            unset($cqueries[$table], $for_update[$table]);
            continue;
        }
        // Fetch the table column structure from the database
        $suppress = $wpdb->suppress_errors();
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $wpdb->suppress_errors($suppress);
        if (!$tablefields) {
            continue;
        }
        // Clear the field and index arrays
        $cfields = $indices = array();
        // Get all of the field names in the query from between the parens
        preg_match("|\\((.*)\\)|ms", $qry, $match2);
        $qryline = trim($match2[1]);
        // Separate field lines into an array
        $flds = explode("\n", $qryline);
        //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
        // For every field line specified in the query
        foreach ($flds as $fld) {
            // Extract the field name
            preg_match("|^([^ ]*)|", trim($fld), $fvals);
            $fieldname = trim($fvals[1], '`');
            // Verify the found field name
            $validfield = true;
            switch (strtolower($fieldname)) {
                case '':
                case 'primary':
                case 'index':
                case 'fulltext':
                case 'unique':
                case 'key':
                    $validfield = false;
                    $indices[] = trim(trim($fld), ", \n");
                    break;
            }
            $fld = trim($fld);
            // If it's a valid field, add it to the field array
            if ($validfield) {
                $cfields[strtolower($fieldname)] = trim($fld, ", \n");
            }
        }
        // For every field in the table
        foreach ($tablefields as $tablefield) {
            // If the table field exists in the field array...
            if (array_key_exists(strtolower($tablefield->Field), $cfields)) {
                // Get the field type from the query
                preg_match("|" . $tablefield->Field . " ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
                $fieldtype = $matches[1];
                // Is actual field type different from the field type in query?
                if ($tablefield->Type != $fieldtype) {
                    // Add a query to change the column type
                    $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
                    $for_update[$table . '.' . $tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
                }
                // Get the default value from the array
                //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
                if (preg_match("| DEFAULT '(.*?)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
                    $default_value = $matches[1];
                    if ($tablefield->Default != $default_value) {
                        // Add a query to change the column's default value
                        $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
                        $for_update[$table . '.' . $tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
                    }
                }
                // Remove the field from the array (so it's not added)
                unset($cfields[strtolower($tablefield->Field)]);
            } else {
                // This field exists in the table, but not in the creation queries?
            }
        }
        // For every remaining field specified for the table
        foreach ($cfields as $fieldname => $fielddef) {
            // Push a query line into $cqueries that adds the field to that table
            $cqueries[] = "ALTER TABLE {$table} ADD COLUMN {$fielddef}";
            $for_update[$table . '.' . $fieldname] = 'Added column ' . $table . '.' . $fieldname;
        }
        // Index stuff goes here
        // Fetch the table index structure from the database
        $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
        if ($tableindices) {
            // Clear the index array
            unset($index_ary);
            // For every index in the table
            foreach ($tableindices as $tableindex) {
                // Add the index to the index data array
                $keyname = $tableindex->Key_name;
                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
                $index_ary[$keyname]['unique'] = $tableindex->Non_unique == 0 ? true : false;
            }
            // For each actual index in the index array
            foreach ($index_ary as $index_name => $index_data) {
                // Build a create string to compare to the query
                $index_string = '';
                if ($index_name == 'PRIMARY') {
                    $index_string .= 'PRIMARY ';
                } else {
                    if ($index_data['unique']) {
                        $index_string .= 'UNIQUE ';
                    }
                }
                $index_string .= 'KEY ';
                if ($index_name != 'PRIMARY') {
                    $index_string .= $index_name;
                }
                $index_columns = '';
                // For each column in the index
                foreach ($index_data['columns'] as $column_data) {
                    if ($index_columns != '') {
                        $index_columns .= ',';
                    }
                    // Add the field to the column list string
                    $index_columns .= $column_data['fieldname'];
                    if ($column_data['subpart'] != '') {
                        $index_columns .= '(' . $column_data['subpart'] . ')';
                    }
                }
                // Add the column list to the index create string
                $index_string .= ' (' . $index_columns . ')';
                if (!(($aindex = array_search($index_string, $indices)) === false)) {
                    unset($indices[$aindex]);
                    //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n";
                }
                //else echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br /><b>Did not find index:</b>".$index_string."<br />".print_r($indices, true)."</pre>\n";
            }
        }
        // For every remaining index specified for the table
        foreach ((array) $indices as $index) {
            // Push a query line into $cqueries that adds the index to that table
            $cqueries[] = "ALTER TABLE {$table} ADD {$index}";
            $for_update[] = 'Added index ' . $table . ' ' . $index;
        }
        // Remove the original table creation query from processing
        unset($cqueries[$table], $for_update[$table]);
    }
    $allqueries = array_merge($cqueries, $iqueries);
    if ($execute) {
        foreach ($allqueries as $query) {
            //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
            $wpdb->query($query);
        }
    }
    return $for_update;
}
 /**
  * Get standard network tables
  *
  * @return array
  */
 private function get_network_core_table_names()
 {
     $schema = wp_get_db_schema('global');
     return $this->extract_names_from_schema($schema, $this->wpdb->base_prefix);
 }
/**
 * Install the signups table.
 *
 * @since BuddyPress (2.0.0)
 *
 * @global $wpdb
 * @uses wp_get_db_schema() to get WordPress ms_global schema
 */
function bp_core_install_signups()
{
    global $wpdb;
    // Signups is not there and we need it so let's create it
    require_once buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php';
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    // Never use bp_core_get_table_prefix() for any global users tables
    $wpdb->signups = $wpdb->base_prefix . 'signups';
    // Use WP's core CREATE TABLE query
    $create_queries = wp_get_db_schema('ms_global');
    if (!is_array($create_queries)) {
        $create_queries = explode(';', $create_queries);
        $create_queries = array_filter($create_queries);
    }
    // Filter out all the queries except wp_signups
    foreach ($create_queries as $key => $query) {
        if (preg_match("|CREATE TABLE ([^ ]*)|", $query, $matches)) {
            if (trim($matches[1], '`') !== $wpdb->signups) {
                unset($create_queries[$key]);
            }
        }
    }
    // Run WordPress's database upgrader
    if (!empty($create_queries)) {
        dbDelta($create_queries);
    }
}
/**
 * Modifies the database based on specified SQL statements.
 *
 * Useful for creating new tables and updating existing tables to a new structure.
 *
 * @since 1.5.0
 *
 * @global wpdb  $wpdb
 *
 * @param string|array $queries Optional. The query to run. Can be multiple queries
 *                              in an array, or a string of queries separated by
 *                              semicolons. Default empty.
 * @param bool         $execute Optional. Whether or not to execute the query right away.
 *                              Default true.
 * @return array Strings containing the results of the various update queries.
 */
function dbDelta($queries = '', $execute = true)
{
    global $wpdb;
    if (in_array($queries, array('', 'all', 'blog', 'global', 'ms_global'), true)) {
        $queries = wp_get_db_schema($queries);
    }
    // Separate individual queries into an array
    if (!is_array($queries)) {
        $queries = explode(';', $queries);
        $queries = array_filter($queries);
    }
    /**
     * Filters the dbDelta SQL queries.
     *
     * @since 3.3.0
     *
     * @param array $queries An array of dbDelta SQL queries.
     */
    $queries = apply_filters('dbdelta_queries', $queries);
    $cqueries = array();
    // Creation Queries
    $iqueries = array();
    // Insertion Queries
    $for_update = array();
    // Create a tablename index for an array ($cqueries) of queries
    foreach ($queries as $qry) {
        if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
            $cqueries[trim($matches[1], '`')] = $qry;
            $for_update[$matches[1]] = 'Created table ' . $matches[1];
        } elseif (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
            array_unshift($cqueries, $qry);
        } elseif (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
            $iqueries[] = $qry;
        } elseif (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
            $iqueries[] = $qry;
        } else {
            // Unrecognized query type
        }
    }
    /**
     * Filters the dbDelta SQL queries for creating tables and/or databases.
     *
     * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
     *
     * @since 3.3.0
     *
     * @param array $cqueries An array of dbDelta create SQL queries.
     */
    $cqueries = apply_filters('dbdelta_create_queries', $cqueries);
    /**
     * Filters the dbDelta SQL queries for inserting or updating.
     *
     * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
     *
     * @since 3.3.0
     *
     * @param array $iqueries An array of dbDelta insert or update SQL queries.
     */
    $iqueries = apply_filters('dbdelta_insert_queries', $iqueries);
    $text_fields = array('tinytext', 'text', 'mediumtext', 'longtext');
    $blob_fields = array('tinyblob', 'blob', 'mediumblob', 'longblob');
    $global_tables = $wpdb->tables('global');
    foreach ($cqueries as $table => $qry) {
        // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
        if (in_array($table, $global_tables) && !wp_should_upgrade_global_tables()) {
            unset($cqueries[$table], $for_update[$table]);
            continue;
        }
        // Fetch the table column structure from the database
        $suppress = $wpdb->suppress_errors();
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $wpdb->suppress_errors($suppress);
        if (!$tablefields) {
            continue;
        }
        // Clear the field and index arrays.
        $cfields = $indices = array();
        // Get all of the field names in the query from between the parentheses.
        preg_match("|\\((.*)\\)|ms", $qry, $match2);
        $qryline = trim($match2[1]);
        // Separate field lines into an array.
        $flds = explode("\n", $qryline);
        // For every field line specified in the query.
        foreach ($flds as $fld) {
            $fld = trim($fld, " \t\n\r\v,");
            // Default trim characters, plus ','.
            // Extract the field name.
            preg_match('|^([^ ]*)|', $fld, $fvals);
            $fieldname = trim($fvals[1], '`');
            $fieldname_lowercased = strtolower($fieldname);
            // Verify the found field name.
            $validfield = true;
            switch ($fieldname_lowercased) {
                case '':
                case 'primary':
                case 'index':
                case 'fulltext':
                case 'unique':
                case 'key':
                case 'spatial':
                    $validfield = false;
                    /*
                     * Normalize the index definition.
                     *
                     * This is done so the definition can be compared against the result of a
                     * `SHOW INDEX FROM $table_name` query which returns the current table
                     * index information.
                     */
                    // Extract type, name and columns from the definition.
                    preg_match('/^' . '(?P<index_type>' . 'PRIMARY\\s+KEY|(?:UNIQUE|FULLTEXT|SPATIAL)\\s+(?:KEY|INDEX)|KEY|INDEX' . ')' . '\\s+' . '(?:' . '`?' . '(?P<index_name>' . '(?:[0-9a-zA-Z$_-]|[\\xC2-\\xDF][\\x80-\\xBF])+' . ')' . '`?' . '\\s+' . ')*' . '\\(' . '(?P<index_columns>' . '.+?' . ')' . '\\)' . '$/im', $fld, $index_matches);
                    // Uppercase the index type and normalize space characters.
                    $index_type = strtoupper(preg_replace('/\\s+/', ' ', trim($index_matches['index_type'])));
                    // 'INDEX' is a synonym for 'KEY', standardize on 'KEY'.
                    $index_type = str_replace('INDEX', 'KEY', $index_type);
                    // Escape the index name with backticks. An index for a primary key has no name.
                    $index_name = 'PRIMARY KEY' === $index_type ? '' : '`' . strtolower($index_matches['index_name']) . '`';
                    // Parse the columns. Multiple columns are separated by a comma.
                    $index_columns = array_map('trim', explode(',', $index_matches['index_columns']));
                    // Normalize columns.
                    foreach ($index_columns as &$index_column) {
                        // Extract column name and number of indexed characters (sub_part).
                        preg_match('/' . '`?' . '(?P<column_name>' . '(?:[0-9a-zA-Z$_-]|[\\xC2-\\xDF][\\x80-\\xBF])+' . ')' . '`?' . '(?:' . '\\s*' . '\\(' . '\\s*' . '(?P<sub_part>' . '\\d+' . ')' . '\\s*' . '\\)' . ')?' . '/', $index_column, $index_column_matches);
                        // Escape the column name with backticks.
                        $index_column = '`' . $index_column_matches['column_name'] . '`';
                        // Append the optional sup part with the number of indexed characters.
                        if (isset($index_column_matches['sub_part'])) {
                            $index_column .= '(' . $index_column_matches['sub_part'] . ')';
                        }
                    }
                    // Build the normalized index definition and add it to the list of indices.
                    $indices[] = "{$index_type} {$index_name} (" . implode(',', $index_columns) . ")";
                    // Destroy no longer needed variables.
                    unset($index_column, $index_column_matches, $index_matches, $index_type, $index_name, $index_columns);
                    break;
            }
            // If it's a valid field, add it to the field array.
            if ($validfield) {
                $cfields[$fieldname_lowercased] = $fld;
            }
        }
        // For every field in the table.
        foreach ($tablefields as $tablefield) {
            $tablefield_field_lowercased = strtolower($tablefield->Field);
            $tablefield_type_lowercased = strtolower($tablefield->Type);
            // If the table field exists in the field array ...
            if (array_key_exists($tablefield_field_lowercased, $cfields)) {
                // Get the field type from the query.
                preg_match('|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[$tablefield_field_lowercased], $matches);
                $fieldtype = $matches[1];
                $fieldtype_lowercased = strtolower($fieldtype);
                // Is actual field type different from the field type in query?
                if ($tablefield->Type != $fieldtype) {
                    $do_change = true;
                    if (in_array($fieldtype_lowercased, $text_fields) && in_array($tablefield_type_lowercased, $text_fields)) {
                        if (array_search($fieldtype_lowercased, $text_fields) < array_search($tablefield_type_lowercased, $text_fields)) {
                            $do_change = false;
                        }
                    }
                    if (in_array($fieldtype_lowercased, $blob_fields) && in_array($tablefield_type_lowercased, $blob_fields)) {
                        if (array_search($fieldtype_lowercased, $blob_fields) < array_search($tablefield_type_lowercased, $blob_fields)) {
                            $do_change = false;
                        }
                    }
                    if ($do_change) {
                        // Add a query to change the column type.
                        $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN `{$tablefield->Field}` " . $cfields[$tablefield_field_lowercased];
                        $for_update[$table . '.' . $tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
                    }
                }
                // Get the default value from the array.
                if (preg_match("| DEFAULT '(.*?)'|i", $cfields[$tablefield_field_lowercased], $matches)) {
                    $default_value = $matches[1];
                    if ($tablefield->Default != $default_value) {
                        // Add a query to change the column's default value
                        $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN `{$tablefield->Field}` SET DEFAULT '{$default_value}'";
                        $for_update[$table . '.' . $tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
                    }
                }
                // Remove the field from the array (so it's not added).
                unset($cfields[$tablefield_field_lowercased]);
            } else {
                // This field exists in the table, but not in the creation queries?
            }
        }
        // For every remaining field specified for the table.
        foreach ($cfields as $fieldname => $fielddef) {
            // Push a query line into $cqueries that adds the field to that table.
            $cqueries[] = "ALTER TABLE {$table} ADD COLUMN {$fielddef}";
            $for_update[$table . '.' . $fieldname] = 'Added column ' . $table . '.' . $fieldname;
        }
        // Index stuff goes here. Fetch the table index structure from the database.
        $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
        if ($tableindices) {
            // Clear the index array.
            $index_ary = array();
            // For every index in the table.
            foreach ($tableindices as $tableindex) {
                // Add the index to the index data array.
                $keyname = strtolower($tableindex->Key_name);
                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
                $index_ary[$keyname]['unique'] = $tableindex->Non_unique == 0 ? true : false;
                $index_ary[$keyname]['index_type'] = $tableindex->Index_type;
            }
            // For each actual index in the index array.
            foreach ($index_ary as $index_name => $index_data) {
                // Build a create string to compare to the query.
                $index_string = '';
                if ($index_name == 'primary') {
                    $index_string .= 'PRIMARY ';
                } elseif ($index_data['unique']) {
                    $index_string .= 'UNIQUE ';
                }
                if ('FULLTEXT' === strtoupper($index_data['index_type'])) {
                    $index_string .= 'FULLTEXT ';
                }
                if ('SPATIAL' === strtoupper($index_data['index_type'])) {
                    $index_string .= 'SPATIAL ';
                }
                $index_string .= 'KEY ';
                if ('primary' !== $index_name) {
                    $index_string .= '`' . $index_name . '`';
                }
                $index_columns = '';
                // For each column in the index.
                foreach ($index_data['columns'] as $column_data) {
                    if ($index_columns != '') {
                        $index_columns .= ',';
                    }
                    // Add the field to the column list string.
                    $index_columns .= '`' . $column_data['fieldname'] . '`';
                    if ($column_data['subpart'] != '') {
                        $index_columns .= '(' . $column_data['subpart'] . ')';
                    }
                }
                // The alternative index string doesn't care about subparts
                $alt_index_columns = preg_replace('/\\([^)]*\\)/', '', $index_columns);
                // Add the column list to the index create string.
                $index_strings = array("{$index_string} ({$index_columns})", "{$index_string} ({$alt_index_columns})");
                foreach ($index_strings as $index_string) {
                    if (!(($aindex = array_search($index_string, $indices)) === false)) {
                        unset($indices[$aindex]);
                        break;
                    }
                }
            }
        }
        // For every remaining index specified for the table.
        foreach ((array) $indices as $index) {
            // Push a query line into $cqueries that adds the index to that table.
            $cqueries[] = "ALTER TABLE {$table} ADD {$index}";
            $for_update[] = 'Added index ' . $table . ' ' . $index;
        }
        // Remove the original table creation query from processing.
        unset($cqueries[$table], $for_update[$table]);
    }
    $allqueries = array_merge($cqueries, $iqueries);
    if ($execute) {
        foreach ($allqueries as $query) {
            $wpdb->query($query);
        }
    }
    return $for_update;
}
Esempio n. 7
0
/**
 * Modifies the database based on specified SQL statements.
 *
 * Useful for creating new tables and updating existing tables to a new structure.
 *
 * @since 1.5.0
 *
 * @global wpdb  $wpdb
 *
 * @param string|array $queries Optional. The query to run. Can be multiple queries
 *                              in an array, or a string of queries separated by
 *                              semicolons. Default empty.
 * @param bool         $execute Optional. Whether or not to execute the query right away.
 *                              Default true.
 * @return array Strings containing the results of the various update queries.
 */
function dbDelta($queries = '', $execute = true)
{
    global $wpdb;
    if (in_array($queries, array('', 'all', 'blog', 'global', 'ms_global'), true)) {
        $queries = wp_get_db_schema($queries);
    }
    // Separate individual queries into an array
    if (!is_array($queries)) {
        $queries = explode(';', $queries);
        $queries = array_filter($queries);
    }
    /**
     * Filters the dbDelta SQL queries.
     *
     * @since 3.3.0
     *
     * @param array $queries An array of dbDelta SQL queries.
     */
    $queries = apply_filters('dbdelta_queries', $queries);
    $cqueries = array();
    // Creation Queries
    $iqueries = array();
    // Insertion Queries
    $for_update = array();
    // Create a tablename index for an array ($cqueries) of queries
    foreach ($queries as $qry) {
        if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
            $cqueries[trim($matches[1], '`')] = $qry;
            $for_update[$matches[1]] = 'Created table ' . $matches[1];
        } elseif (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
            array_unshift($cqueries, $qry);
        } elseif (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
            $iqueries[] = $qry;
        } elseif (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
            $iqueries[] = $qry;
        } else {
            // Unrecognized query type
        }
    }
    /**
     * Filters the dbDelta SQL queries for creating tables and/or databases.
     *
     * Queries filterable via this hook contain "CREATE TABLE" or "CREATE DATABASE".
     *
     * @since 3.3.0
     *
     * @param array $cqueries An array of dbDelta create SQL queries.
     */
    $cqueries = apply_filters('dbdelta_create_queries', $cqueries);
    /**
     * Filters the dbDelta SQL queries for inserting or updating.
     *
     * Queries filterable via this hook contain "INSERT INTO" or "UPDATE".
     *
     * @since 3.3.0
     *
     * @param array $iqueries An array of dbDelta insert or update SQL queries.
     */
    $iqueries = apply_filters('dbdelta_insert_queries', $iqueries);
    $text_fields = array('tinytext', 'text', 'mediumtext', 'longtext');
    $blob_fields = array('tinyblob', 'blob', 'mediumblob', 'longblob');
    $global_tables = $wpdb->tables('global');
    foreach ($cqueries as $table => $qry) {
        // Upgrade global tables only for the main site. Don't upgrade at all if conditions are not optimal.
        if (in_array($table, $global_tables) && !wp_should_upgrade_global_tables()) {
            unset($cqueries[$table], $for_update[$table]);
            continue;
        }
        // Fetch the table column structure from the database
        $suppress = $wpdb->suppress_errors();
        $tablefields = $wpdb->get_results("DESCRIBE {$table};");
        $wpdb->suppress_errors($suppress);
        if (!$tablefields) {
            continue;
        }
        // Clear the field and index arrays.
        $cfields = $indices = array();
        // Get all of the field names in the query from between the parentheses.
        preg_match("|\\((.*)\\)|ms", $qry, $match2);
        $qryline = trim($match2[1]);
        // Separate field lines into an array.
        $flds = explode("\n", $qryline);
        // For every field line specified in the query.
        foreach ($flds as $fld) {
            // Extract the field name.
            preg_match("|^([^ ]*)|", trim($fld), $fvals);
            $fieldname = trim($fvals[1], '`');
            $fieldname_lowercased = strtolower($fieldname);
            // Verify the found field name.
            $validfield = true;
            switch ($fieldname_lowercased) {
                case '':
                case 'primary':
                case 'index':
                case 'fulltext':
                case 'unique':
                case 'key':
                case 'spatial':
                    $validfield = false;
                    $indices[] = trim(trim($fld), ", \n");
                    break;
            }
            $fld = trim($fld);
            // If it's a valid field, add it to the field array.
            if ($validfield) {
                $cfields[$fieldname_lowercased] = trim($fld, ", \n");
            }
        }
        // For every field in the table.
        foreach ($tablefields as $tablefield) {
            $tablefield_field_lowercased = strtolower($tablefield->Field);
            $tablefield_type_lowercased = strtolower($tablefield->Type);
            // If the table field exists in the field array ...
            if (array_key_exists($tablefield_field_lowercased, $cfields)) {
                // Get the field type from the query.
                preg_match('|`?' . $tablefield->Field . '`? ([^ ]*( unsigned)?)|i', $cfields[$tablefield_field_lowercased], $matches);
                $fieldtype = $matches[1];
                $fieldtype_lowercased = strtolower($fieldtype);
                // Is actual field type different from the field type in query?
                if ($tablefield->Type != $fieldtype) {
                    $do_change = true;
                    if (in_array($fieldtype_lowercased, $text_fields) && in_array($tablefield_type_lowercased, $text_fields)) {
                        if (array_search($fieldtype_lowercased, $text_fields) < array_search($tablefield_type_lowercased, $text_fields)) {
                            $do_change = false;
                        }
                    }
                    if (in_array($fieldtype_lowercased, $blob_fields) && in_array($tablefield_type_lowercased, $blob_fields)) {
                        if (array_search($fieldtype_lowercased, $blob_fields) < array_search($tablefield_type_lowercased, $blob_fields)) {
                            $do_change = false;
                        }
                    }
                    if ($do_change) {
                        // Add a query to change the column type.
                        $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[$tablefield_field_lowercased];
                        $for_update[$table . '.' . $tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
                    }
                }
                // Get the default value from the array.
                if (preg_match("| DEFAULT '(.*?)'|i", $cfields[$tablefield_field_lowercased], $matches)) {
                    $default_value = $matches[1];
                    if ($tablefield->Default != $default_value) {
                        // Add a query to change the column's default value
                        $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
                        $for_update[$table . '.' . $tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
                    }
                }
                // Remove the field from the array (so it's not added).
                unset($cfields[$tablefield_field_lowercased]);
            } else {
                // This field exists in the table, but not in the creation queries?
            }
        }
        // For every remaining field specified for the table.
        foreach ($cfields as $fieldname => $fielddef) {
            // Push a query line into $cqueries that adds the field to that table.
            $cqueries[] = "ALTER TABLE {$table} ADD COLUMN {$fielddef}";
            $for_update[$table . '.' . $fieldname] = 'Added column ' . $table . '.' . $fieldname;
        }
        // Index stuff goes here. Fetch the table index structure from the database.
        $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
        if ($tableindices) {
            // Clear the index array.
            $index_ary = array();
            // For every index in the table.
            foreach ($tableindices as $tableindex) {
                // Add the index to the index data array.
                $keyname = $tableindex->Key_name;
                $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
                $index_ary[$keyname]['unique'] = $tableindex->Non_unique == 0 ? true : false;
                $index_ary[$keyname]['index_type'] = $tableindex->Index_type;
            }
            // For each actual index in the index array.
            foreach ($index_ary as $index_name => $index_data) {
                // Build a create string to compare to the query.
                $index_string = '';
                if ($index_name == 'PRIMARY') {
                    $index_string .= 'PRIMARY ';
                } elseif ($index_data['unique']) {
                    $index_string .= 'UNIQUE ';
                }
                if ('FULLTEXT' === strtoupper($index_data['index_type'])) {
                    $index_string .= 'FULLTEXT ';
                }
                if ('SPATIAL' === strtoupper($index_data['index_type'])) {
                    $index_string .= 'SPATIAL ';
                }
                $index_string .= 'KEY ';
                if ($index_name != 'PRIMARY') {
                    $index_string .= $index_name;
                }
                $index_columns = '';
                // For each column in the index.
                foreach ($index_data['columns'] as $column_data) {
                    if ($index_columns != '') {
                        $index_columns .= ',';
                    }
                    // Add the field to the column list string.
                    $index_columns .= $column_data['fieldname'];
                    if ($column_data['subpart'] != '') {
                        $index_columns .= '(' . $column_data['subpart'] . ')';
                    }
                }
                // The alternative index string doesn't care about subparts
                $alt_index_columns = preg_replace('/\\([^)]*\\)/', '', $index_columns);
                // Add the column list to the index create string.
                $index_strings = array("{$index_string} ({$index_columns})", "{$index_string} ({$alt_index_columns})");
                foreach ($index_strings as $index_string) {
                    if (!(($aindex = array_search($index_string, $indices)) === false)) {
                        unset($indices[$aindex]);
                        break;
                    }
                }
            }
        }
        // For every remaining index specified for the table.
        foreach ((array) $indices as $index) {
            // Push a query line into $cqueries that adds the index to that table.
            $cqueries[] = "ALTER TABLE {$table} ADD {$index}";
            $for_update[] = 'Added index ' . $table . ' ' . $index;
        }
        // Remove the original table creation query from processing.
        unset($cqueries[$table], $for_update[$table]);
    }
    $allqueries = array_merge($cqueries, $iqueries);
    if ($execute) {
        foreach ($allqueries as $query) {
            $wpdb->query($query);
        }
    }
    return $for_update;
}
Esempio n. 8
0
/**
 * Function to create tables according to the schemas of WordPress.
 *
 * This is executed only once while installation.
 *
 * @return boolean
 */
function make_db_sqlite()
{
    include_once PDODIR . 'query_create.class.php';
    include_once ABSPATH . 'wp-admin/includes/schema.php';
    $index_array = array();
    //ob_end_clean();
    $table_schemas = wp_get_db_schema();
    $queries = explode(";", $table_schemas);
    $query_parser = new CreateQuery();
    try {
        $pdo = new PDO('sqlite:' . FQDB, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
    } catch (PDOException $err) {
        $err_data = $err->errorInfo;
        $message = 'Database connection error!<br />';
        $message .= sprintf("Error message is: %s", $err_data[2]);
        wp_die($message, 'Database Error!');
    }
    try {
        $pdo->beginTransaction();
        foreach ($queries as $query) {
            $query = trim($query);
            if (empty($query)) {
                continue;
            }
            $rewritten_query = $query_parser->rewrite_query($query);
            if (is_array($rewritten_query)) {
                $table_query = array_shift($rewritten_query);
                $index_queries = $rewritten_query;
                $table_query = trim($table_query);
                $pdo->exec($table_query);
                //foreach($rewritten_query as $single_query) {
                //  $single_query = trim($single_query);
                //  $pdo->exec($single_query);
                //}
            } else {
                $rewritten_query = trim($rewritten_query);
                $pdo->exec($rewritten_query);
            }
        }
        $pdo->commit();
        if ($index_queries) {
            // $query_parser rewrites KEY to INDEX, so we don't need KEY pattern
            $pattern = '/CREATE\\s*(UNIQUE\\s*INDEX|INDEX)\\s*IF\\s*NOT\\s*EXISTS\\s*(\\w+)?\\s*.*/im';
            $pdo->beginTransaction();
            foreach ($index_queries as $index_query) {
                preg_match($pattern, $index_query, $match);
                $index_name = trim($match[2]);
                if (in_array($index_name, $index_array)) {
                    $r = rand(0, 50);
                    $replacement = $index_name . "_{$r}";
                    $index_query = str_ireplace('EXISTS ' . $index_name, 'EXISTS ' . $replacement, $index_query);
                } else {
                    $index_array[] = $index_name;
                }
                $pdo->exec($index_query);
            }
            $pdo->commit();
        }
    } catch (PDOException $err) {
        $err_data = $err->errorInfo;
        $err_code = $err_data[1];
        if (5 == $err_code || 6 == $err_code) {
            // if the database is locked, commit again
            $pdo->commit();
        } else {
            $pdo->rollBack();
            $message = sprintf("Error occured while creating tables or indexes...<br />Query was: %s<br />", var_export($rewritten_query, true));
            $message .= sprintf("Error message is: %s", $err_data[2]);
            wp_die($message, 'Database Error!');
        }
    }
    $query_parser = null;
    $pdo = null;
    return true;
}
 /**
  * @ticket 20263
  */
 function test_wp_get_db_schema_does_no_alter_queries_on_existing_install()
 {
     $updates = dbDelta(wp_get_db_schema());
     $this->assertEmpty($updates);
 }
 /**
  * Get core tables only.
  *
  * @param  bool $do_prefix Should the table names be prefixed?
  * @return array
  */
 public function get_core_site_tables($do_prefix = TRUE)
 {
     $cache_key = "site-core-{$this->site_id}-" . ($do_prefix ? 1 : 0);
     $cache = wp_cache_get($cache_key, $this->cache_group);
     if ($cache) {
         return $cache;
     }
     if ($this->site_id !== get_current_blog_id()) {
         switch_to_blog($this->site_id);
     }
     $schema = wp_get_db_schema('blog', $this->site_id);
     $tables = $this->extract_names_from_schema($schema, $this->wpdb->prefix);
     if ($do_prefix) {
         $tables = $this->prefix_table_names($tables, $this->wpdb->prefix);
     }
     restore_current_blog();
     wp_cache_set($cache_key, $tables, $this->cache_group);
     return $tables;
 }
Esempio n. 11
0
/**
 * Modifies the database based on specified SQL statements.
 *
 * Useful for creating new tables and updating existing tables to a new structure.
 *
 * @since 1.5.0
 *
 * @global wpdb  $wpdb
 *
 * @param string|array $queries Optional. The query to run. Can be multiple queries
 *                              in an array, or a string of queries separated by
 *                              semicolons. Default empty.
 * @param bool         $execute Optional. Whether or not to execute the query right away.
 *                              Default true.
 * @return array Strings containing the results of the various update queries.
 */
function dbDelta($queries = '', $execute = true)
{
    global $wpdb;
    if (in_array($queries, array('', 'all', 'blog', 'global', 'ms_global'), true)) {
        $queries = wp_get_db_schema($queries);
    }
    // Separate individual queries into an array
    if (!is_array($queries)) {
        if (stristr($queries, "GO") !== FALSE) {
            $queries = explode('GO', $queries);
        } else {
            $queries = explode(';', $queries);
        }
        $queries = array_filter($queries);
    }
    foreach ($queries as $query) {
        $wpdb->query($query);
    }
    return array();
}
Esempio n. 12
0
            break;
        case 'ms_global':
            $queries = $ms_global_tables;
            break;
        case 'all':
        default:
            $queries = $global_tables . $blog_tables;
            break;
    }
    if (isset($old_blog_id)) {
        $wpdb->set_blog_id($old_blog_id);
    }
    return $queries;
}
// Populate for back compat.
$wp_queries = wp_get_db_schema('all');
/**
 * Create WordPress options and set the default values.
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @global int  $wp_db_version
 * @global int  $wp_current_db_version
 */
function populate_options()
{
    global $wpdb, $wp_db_version, $wp_current_db_version;
    $guessurl = wp_guess_url();
    /**
     * Fires before creating WordPress options and populating their default values.
Esempio n. 13
0
 /**
  * Returns an array with the names of all tables for the site with the given ID.
  *
  * @since 3.0.0
  *
  * @param int $site_id Site ID.
  *
  * @return string[] The names of all tables for the site with the given ID.
  */
 public function site_tables($site_id)
 {
     $prefix = $this->db->get_blog_prefix($site_id);
     return $this->extract_tables_from_schema(wp_get_db_schema('blog', $site_id), $prefix);
 }