3 require_once(ABSPATH . '/wp-admin/admin-functions.php');
4 require_once(ABSPATH . '/wp-admin/upgrade-schema.php');
5 // Functions to be called in install and upgrade scripts
6 function upgrade_all() {
7 global $wp_current_db_version, $wp_db_version, $wp_rewrite;
8 $wp_current_db_version = __get_option('db_version');
10 // We are up-to-date. Nothing to do.
11 if ( $wp_db_version == $wp_current_db_version )
14 // If the version is not set in the DB, try to guess the version.
15 if ( empty($wp_current_db_version) ) {
16 $wp_current_db_version = 0;
18 // If the template option exists, we have 1.5.
19 $template = __get_option('template');
20 if ( !empty($template) )
21 $wp_current_db_version = 2541;
26 if ( $wp_current_db_version < 2541 ) {
33 if ( $wp_current_db_version < 3308 )
36 $wp_rewrite->flush_rules();
38 update_option('db_version', $wp_db_version);
41 function upgrade_100() {
44 // Get the title and ID of every post, post_name to check if it already has a value
45 $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
47 foreach($posts as $post) {
48 if ('' == $post->post_name) {
49 $newtitle = sanitize_title($post->post_title);
50 $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'");
55 $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
56 foreach ($categories as $category) {
57 if ('' == $category->category_nicename) {
58 $newtitle = sanitize_title($category->cat_name);
59 $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'");
64 $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
65 WHERE option_name LIKE 'links_rating_image%'
66 AND option_value LIKE 'wp-links/links-images/%'");
68 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
70 foreach ($done_ids as $done_id) :
71 $done_posts[] = $done_id->post_id;
73 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
78 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
80 foreach ($allposts as $post) {
81 // Check to see if it's already been imported
82 $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category");
83 if (!$cat && 0 != $post->post_category) { // If there's no result
85 INSERT INTO $wpdb->post2cat
86 (post_id, category_id)
88 ('$post->ID', '$post->post_category')
95 function upgrade_101() {
98 // Clean up indices, add a few
99 add_clean_index($wpdb->posts, 'post_name');
100 add_clean_index($wpdb->posts, 'post_status');
101 add_clean_index($wpdb->categories, 'category_nicename');
102 add_clean_index($wpdb->comments, 'comment_approved');
103 add_clean_index($wpdb->comments, 'comment_post_ID');
104 add_clean_index($wpdb->links , 'link_category');
105 add_clean_index($wpdb->links , 'link_visible');
109 function upgrade_110() {
112 // Set user_nicename.
113 $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
114 foreach ($users as $user) {
115 if ('' == $user->user_nicename) {
116 $newname = sanitize_title($user->user_nickname);
117 $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'");
121 $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
122 foreach ($users as $row) {
123 if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
124 $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
129 // Get the GMT offset, we'll use that later on
130 $all_options = get_alloptions_110();
132 $time_difference = $all_options->time_difference;
134 $server_time = time()+date('Z');
135 $weblogger_time = $server_time + $time_difference*3600;
138 $diff_gmt_server = ($gmt_time - $server_time) / 3600;
139 $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
140 $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
141 $gmt_offset = -$diff_gmt_weblogger;
143 // Add a gmt_offset option, with value $gmt_offset
144 add_option('gmt_offset', $gmt_offset);
146 // Check if we already set the GMT fields (if we did, then
147 // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
148 // <michel_v> I just slapped myself silly for not thinking about it earlier
149 $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
151 if (!$got_gmt_fields) {
153 // Add or substract time to all dates, to get GMT dates
154 $add_hours = intval($diff_gmt_weblogger);
155 $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
156 $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
157 $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
158 $wpdb->query("UPDATE $wpdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'");
159 $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
160 $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
165 function upgrade_130() {
166 global $wpdb, $table_prefix;
168 // Remove extraneous backslashes.
169 $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
171 foreach($posts as $post) {
172 $post_content = addslashes(deslash($post->post_content));
173 $post_title = addslashes(deslash($post->post_title));
174 $post_excerpt = addslashes(deslash($post->post_excerpt));
175 if ( empty($post->guid) )
176 $guid = get_permalink($post->ID);
180 $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
184 // Remove extraneous backslashes.
185 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
187 foreach($comments as $comment) {
188 $comment_content = addslashes(deslash($comment->comment_content));
189 $comment_author = addslashes(deslash($comment->comment_author));
190 $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
194 // Remove extraneous backslashes.
195 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
197 foreach($links as $link) {
198 $link_name = addslashes(deslash($link->link_name));
199 $link_description = addslashes(deslash($link->link_description));
200 $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
204 // The "paged" option for what_to_show is no more.
205 if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
206 $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
209 $active_plugins = __get_option('active_plugins');
211 // If plugins are not stored in an array, they're stored in the old
212 // newline separated format. Convert to new format.
213 if ( !is_array( $active_plugins ) ) {
214 $active_plugins = explode("\n", trim($active_plugins));
215 update_option('active_plugins', $active_plugins);
219 $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optionvalues');
220 $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiontypes');
221 $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiongroups');
222 $wpdb->query('DROP TABLE IF EXISTS ' . $table_prefix . 'optiongroup_options');
224 // Update comments table to use comment_type
225 $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
226 $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
228 // Some versions have multiple duplicate option_name rows with the same values
229 $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
230 foreach ( $options as $option ) {
231 if ( 1 != $option->dupes ) { // Could this be done in the query?
232 $limit = $option->dupes - 1;
233 $dupe_ids = $wpdb->get_col("SELECT option_id FROM $wpdb->options WHERE option_name = '$option->option_name' LIMIT $limit");
234 $dupe_ids = join($dupe_ids, ',');
235 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
242 function upgrade_160() {
243 global $wpdb, $table_prefix, $wp_current_db_version;
245 populate_roles_160();
247 $users = $wpdb->get_results("SELECT * FROM $wpdb->users");
248 foreach ( $users as $user ) :
249 if ( !empty( $user->user_firstname ) )
250 update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
251 if ( !empty( $user->user_lastname ) )
252 update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
253 if ( !empty( $user->user_nickname ) )
254 update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
255 if ( !empty( $user->user_level ) )
256 update_usermeta( $user->ID, $table_prefix . 'user_level', $user->user_level );
257 if ( !empty( $user->user_icq ) )
258 update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
259 if ( !empty( $user->user_aim ) )
260 update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
261 if ( !empty( $user->user_msn ) )
262 update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
263 if ( !empty( $user->user_yim ) )
264 update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
265 if ( !empty( $user->user_description ) )
266 update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) );
268 if ( isset( $user->user_idmode ) ):
269 $idmode = $user->user_idmode;
270 if ($idmode == 'nickname') $id = $user->user_nickname;
271 if ($idmode == 'login') $id = $user->user_login;
272 if ($idmode == 'firstname') $id = $user->user_firstname;
273 if ($idmode == 'lastname') $id = $user->user_lastname;
274 if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
275 if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
276 if (!$idmode) $id = $user->user_nickname;
277 $id = $wpdb->escape( $id );
278 $wpdb->query("UPDATE $wpdb->users SET display_name = '$id' WHERE ID = '$user->ID'");
281 // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
282 $caps = get_usermeta( $user->ID, $table_prefix . 'capabilities');
283 if ( empty($caps) || defined('RESET_CAPS') ) {
284 $level = get_usermeta($user->ID, $table_prefix . 'user_level');
285 $role = translate_level_to_role($level);
286 update_usermeta( $user->ID, $table_prefix . 'capabilities', array($role => true) );
290 $old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
291 $wpdb->hide_errors();
292 foreach ( $old_user_fields as $old )
293 $wpdb->query("ALTER TABLE $wpdb->users DROP $old");
294 $wpdb->show_errors();
296 if ( 0 == $wpdb->get_var("SELECT SUM(category_count) FROM $wpdb->categories") ) { // Create counts
297 $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
298 foreach ( $categories as $cat_id ) {
299 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'");
300 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'");
304 // populate comment_count field of posts table
305 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
306 if( is_array( $comments ) ) {
307 foreach ($comments as $comment) {
308 $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID'" );
312 // Some alpha versions used a post status of object instead of attachment and put
313 // the mime type in post_type instead of post_mime_type.
314 if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
315 $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'");
316 foreach ($objects as $object) {
317 $wpdb->query("UPDATE $wpdb->posts SET post_status = 'attachment',
318 post_mime_type = '$object->post_type',
320 WHERE ID = $object->ID");
322 $meta = get_post_meta($object->ID, 'imagedata', true);
323 if ( ! empty($meta['file']) )
324 add_post_meta($object->ID, '_wp_attached_file', $meta['file']);
329 // The functions we use to actually do stuff
332 function maybe_create_table($table_name, $create_ddl) {
334 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
335 if ($table == $table_name) {
339 //didn't find it try to create it.
340 $q = $wpdb->query($create_ddl);
341 // we cannot directly tell that whether this succeeded!
342 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) {
343 if ($table == $table_name) {
350 function drop_index($table, $index) {
352 $wpdb->hide_errors();
353 $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`");
354 // Now we need to take out all the extra ones we may have created
355 for ($i = 0; $i < 25; $i++) {
356 $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`");
358 $wpdb->show_errors();
362 function add_clean_index($table, $index) {
364 drop_index($table, $index);
365 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )");
370 ** maybe_add_column()
371 ** Add column to db table if it doesn't exist.
372 ** Returns: true if already exists or on successful completion
375 function maybe_add_column($table_name, $column_name, $create_ddl) {
376 global $wpdb, $debug;
377 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
378 if ($debug) echo("checking $column == $column_name<br />");
379 if ($column == $column_name) {
383 //didn't find it try to create it.
384 $q = $wpdb->query($create_ddl);
385 // we cannot directly tell that whether this succeeded!
386 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) {
387 if ($column == $column_name) {
395 // get_alloptions as it was for 1.2.
396 function get_alloptions_110() {
398 if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) {
399 foreach ($options as $option) {
400 // "When trying to design a foolproof system,
401 // never underestimate the ingenuity of the fools :)" -- Dougal
402 if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
403 if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
404 if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value);
405 $all_options->{$option->option_name} = stripslashes($option->option_value);
411 // Version of get_option that is private to install/upgrade.
412 function __get_option($setting) {
415 $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'");
417 if ( 'home' == $setting && '' == $option )
418 return __get_option('siteurl');
420 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
421 $option = preg_replace('|/+$|', '', $option);
423 @ $kellogs = unserialize($option);
424 if ($kellogs !== FALSE)
430 function deslash($content) {
431 // Note: \\\ inside a regex denotes a single backslash.
433 // Replace one or more backslashes followed by a single quote with
435 $content = preg_replace("/\\\+'/", "'", $content);
437 // Replace one or more backslashes followed by a double quote with
439 $content = preg_replace('/\\\+"/', '"', $content);
441 // Replace one or more backslashes with one backslash.
442 $content = preg_replace("/\\\+/", "\\", $content);
447 function dbDelta($queries, $execute = true) {
450 // Seperate individual queries into an array
451 if( !is_array($queries) ) {
452 $queries = explode( ';', $queries );
453 if('' == $queries[count($queries) - 1]) array_pop($queries);
456 $cqueries = array(); // Creation Queries
457 $iqueries = array(); // Insertion Queries
458 $for_update = array();
460 // Create a tablename index for an array ($cqueries) of queries
461 foreach($queries as $qry) {
462 if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {
463 $cqueries[strtolower($matches[1])] = $qry;
464 $for_update[$matches[1]] = 'Created table '.$matches[1];
466 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) {
467 array_unshift($cqueries, $qry);
469 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) {
472 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) {
476 // Unrecognized query type
480 // Check to see which tables and fields exist
481 if($tables = $wpdb->get_col('SHOW TABLES;')) {
482 // For every table in the database
483 foreach($tables as $table) {
484 // If a table query exists for the database table...
485 if( array_key_exists(strtolower($table), $cqueries) ) {
486 // Clear the field and index arrays
489 // Get all of the field names in the query from between the parens
490 preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2);
491 $qryline = trim($match2[1]);
493 // Separate field lines into an array
494 $flds = explode("\n", $qryline);
496 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>";
498 // For every field line specified in the query
499 foreach($flds as $fld) {
500 // Extract the field name
501 preg_match("|^([^ ]*)|", trim($fld), $fvals);
502 $fieldname = $fvals[1];
504 // Verify the found field name
506 switch(strtolower($fieldname))
515 $indices[] = trim(trim($fld), ", \n");
520 // If it's a valid field, add it to the field array
522 $cfields[strtolower($fieldname)] = trim($fld, ", \n");
526 // Fetch the table column structure from the database
527 $tablefields = $wpdb->get_results("DESCRIBE {$table};");
529 // For every field in the table
530 foreach($tablefields as $tablefield) {
531 // If the table field exists in the field array...
532 if(array_key_exists(strtolower($tablefield->Field), $cfields)) {
533 // Get the field type from the query
534 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches);
535 $fieldtype = $matches[1];
537 // Is actual field type different from the field type in query?
538 if($tablefield->Type != $fieldtype) {
539 // Add a query to change the column type
540 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)];
541 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}";
544 // Get the default value from the array
545 //echo "{$cfields[strtolower($tablefield->Field)]}<br>";
546 if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {
547 $default_value = $matches[1];
548 if($tablefield->Default != $default_value)
550 // Add a query to change the column's default value
551 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'";
552 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}";
556 // Remove the field from the array (so it's not added)
557 unset($cfields[strtolower($tablefield->Field)]);
560 // This field exists in the table, but not in the creation queries?
564 // For every remaining field specified for the table
565 foreach($cfields as $fieldname => $fielddef) {
566 // Push a query line into $cqueries that adds the field to that table
567 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef";
568 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname;
571 // Index stuff goes here
572 // Fetch the table index structure from the database
573 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
576 // Clear the index array
579 // For every index in the table
580 foreach($tableindices as $tableindex) {
581 // Add the index to the index data array
582 $keyname = $tableindex->Key_name;
583 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part);
584 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false;
587 // For each actual index in the index array
588 foreach($index_ary as $index_name => $index_data) {
589 // Build a create string to compare to the query
591 if($index_name == 'PRIMARY') {
592 $index_string .= 'PRIMARY ';
594 else if($index_data['unique']) {
595 $index_string .= 'UNIQUE ';
597 $index_string .= 'KEY ';
598 if($index_name != 'PRIMARY') {
599 $index_string .= $index_name;
602 // For each column in the index
603 foreach($index_data['columns'] as $column_data) {
604 if($index_columns != '') $index_columns .= ',';
605 // Add the field to the column list string
606 $index_columns .= $column_data['fieldname'];
607 if($column_data['subpart'] != '') {
608 $index_columns .= '('.$column_data['subpart'].')';
611 // Add the column list to the index create string
612 $index_string .= ' ('.$index_columns.')';
614 if(!(($aindex = array_search($index_string, $indices)) === false)) {
615 unset($indices[$aindex]);
616 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br/>Found index:".$index_string."</pre>\n";
618 //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";
622 // For every remaining index specified for the table
623 foreach($indices as $index) {
624 // Push a query line into $cqueries that adds the index to that table
625 $cqueries[] = "ALTER TABLE {$table} ADD $index";
626 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index;
629 // Remove the original table creation query from processing
630 unset($cqueries[strtolower($table)]);
631 unset($for_update[strtolower($table)]);
633 // This table exists in the database, but not in the creation queries?
638 $allqueries = array_merge($cqueries, $iqueries);
640 foreach($allqueries as $query) {
641 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n";
642 $wpdb->query($query);
649 function make_db_current() {
652 $alterations = dbDelta($wp_queries);
654 foreach($alterations as $alteration) echo "<li>$alteration</li>\n";
658 function make_db_current_silent() {
661 $alterations = dbDelta($wp_queries);
664 function make_site_theme_from_oldschool($theme_name, $template) {
665 $home_path = get_home_path();
666 $site_dir = ABSPATH . "wp-content/themes/$template";
668 if (! file_exists("$home_path/index.php"))
671 // Copy files from the old locations to the site theme.
672 // TODO: This does not copy arbitarary include dependencies. Only the
673 // standard WP files are copied.
674 $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php');
676 foreach ($files as $oldfile => $newfile) {
677 if ($oldfile == 'index.php')
678 $oldpath = $home_path;
682 if ($oldfile == 'index.php') { // Check to make sure it's not a new index
683 $index = implode('', file("$oldpath/$oldfile"));
684 if ( strstr( $index, 'WP_USE_THEMES' ) ) {
685 if (! @copy(ABSPATH . 'wp-content/themes/default/index.php', "$site_dir/$newfile"))
687 continue; // Don't copy anything
691 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile"))
694 chmod("$site_dir/$newfile", 0777);
696 // Update the blog header include in each file.
697 $lines = explode("\n", implode('', file("$site_dir/$newfile")));
699 $f = fopen("$site_dir/$newfile", 'w');
701 foreach ($lines as $line) {
702 if (preg_match('/require.*wp-blog-header/', $line))
703 $line = '//' . $line;
705 // Update stylesheet references.
706 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line);
708 // Update comments template inclusion.
709 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line);
711 fwrite($f, "{$line}\n");
717 // Add a theme header.
718 $header = "/*\nTheme Name: $theme_name\nTheme URI: " . __get_option('siteurl') . "\nDescription: A theme automatically created by the upgrade.\nVersion: 1.0\nAuthor: Moi\n*/\n";
720 $stylelines = file_get_contents("$site_dir/style.css");
722 $f = fopen("$site_dir/style.css", 'w');
725 fwrite($f, $stylelines);
732 function make_site_theme_from_default($theme_name, $template) {
733 $site_dir = ABSPATH . "wp-content/themes/$template";
734 $default_dir = ABSPATH . 'wp-content/themes/default';
736 // Copy files from the default theme to the site theme.
737 //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css');
739 $theme_dir = @ dir("$default_dir");
741 while(($theme_file = $theme_dir->read()) !== false) {
742 if (is_dir("$default_dir/$theme_file"))
744 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file"))
746 chmod("$site_dir/$theme_file", 0777);
750 // Rewrite the theme header.
751 $stylelines = explode("\n", implode('', file("$site_dir/style.css")));
753 $f = fopen("$site_dir/style.css", 'w');
755 foreach ($stylelines as $line) {
756 if (strstr($line, "Theme Name:")) $line = "Theme Name: $theme_name";
757 elseif (strstr($line, "Theme URI:")) $line = "Theme URI: " . __get_option('siteurl');
758 elseif (strstr($line, "Description:")) $line = "Description: Your theme";
759 elseif (strstr($line, "Version:")) $line = "Version: 1";
760 elseif (strstr($line, "Author:")) $line = "Author: You";
761 fwrite($f, "{$line}\n");
768 if (! mkdir("$site_dir/images", 0777)) {
772 $images_dir = @ dir("$default_dir/images");
774 while(($image = $images_dir->read()) !== false) {
775 if (is_dir("$default_dir/images/$image"))
777 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image"))
779 chmod("$site_dir/images/$image", 0777);
784 // Create a site theme from the default theme.
785 function make_site_theme() {
786 // Name the theme after the blog.
787 $theme_name = __get_option('blogname');
788 $template = sanitize_title($theme_name);
789 $site_dir = ABSPATH . "wp-content/themes/$template";
791 // If the theme already exists, nothing to do.
792 if ( is_dir($site_dir)) {
796 // We must be able to write to the themes dir.
797 if (! is_writable(ABSPATH . "wp-content/themes")) {
802 if (! mkdir($site_dir, 0777)) {
806 if (file_exists(ABSPATH . 'wp-layout.css')) {
807 if (! make_site_theme_from_oldschool($theme_name, $template)) {
808 // TODO: rm -rf the site theme directory.
812 if (! make_site_theme_from_default($theme_name, $template))
813 // TODO: rm -rf the site theme directory.
817 // Make the new site theme active.
818 $current_template = __get_option('template');
819 if ($current_template == 'default') {
820 update_option('template', $template);
821 update_option('stylesheet', $template);
826 function translate_level_to_role($level) {
831 return 'administrator';
841 return 'contributor';