root/tags/1.3/wp-admin/includes/upgrade.php

Revision 1139, 46.6 kB (checked in by donncha, 10 months ago)

Merge with WordPress? 2.3.1

Line 
1 <?php
2
3 if ( file_exists(ABSPATH . 'wp-content/install.php') )
4     require (ABSPATH . 'wp-content/install.php');
5 require_once(ABSPATH . 'wp-admin/includes/admin.php');
6 require_once(ABSPATH . 'wp-admin/includes/schema.php');
7
8 if ( !function_exists('wp_install') ) :
9 function wp_install($blog_title, $user_name, $user_email, $public, $meta='') {
10     global $wp_rewrite;
11
12     wp_check_mysql_version();
13     wp_cache_flush();
14     make_db_current_silent();
15     populate_options();
16     populate_roles();
17
18     update_option('blogname', $blog_title);
19     update_option('admin_email', $user_email);
20     update_option('blog_public', $public);
21     $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
22
23     if ( defined('WP_SITEURL') && '' != WP_SITEURL )
24         $guessurl = WP_SITEURL;
25     else
26         $guessurl = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
27
28     update_option('siteurl', $guessurl);
29
30     // If not a public blog, don't ping.
31     if ( ! $public )
32         update_option('default_pingback_flag', 0);
33
34     // Create default user.  If the user already exists, the user tables are
35     // being shared among blogs.  Just set the role in that case.
36     $user_id = username_exists($user_name);
37     if ( !$user_id ) {
38         $random_password = substr(md5(uniqid(microtime())), 0, 6);
39         $user_id = wp_create_user($user_name, $random_password, $user_email);
40     } else {
41         $random_password = __('User already exists.  Password inherited.');
42     }
43
44     $user = new WP_User($user_id);
45     $user->set_role('administrator');
46
47     wp_install_defaults($user_id);
48
49     $wp_rewrite->flush_rules();
50
51     wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password);
52
53     wp_cache_flush();
54
55     return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password);
56 }
57 endif;
58
59 if ( !function_exists('wp_install_defaults') ) :
60 function wp_install_defaults($user_id) {
61     global $wpdb;
62
63     // Default category
64     $cat_name = $wpdb->escape(__('Uncategorized'));
65     $cat_slug = sanitize_title(__('Uncategorized'));
66     $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
67     $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('1', 'category', '', '0', '1')");
68
69     // Default link category
70     $cat_name = $wpdb->escape(__('Blogroll'));
71     $cat_slug = sanitize_title(__('Blogroll'));
72     $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$cat_name', '$cat_slug', '0')");
73     $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('2', 'link_category', '', '0', '7')");
74
75     // Now drop in some default links
76     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://codex.wordpress.org/', 'Documentation', 0, '', '');");
77     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 2)" );
78
79     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/development/', 'Development Blog', 0, 'http://wordpress.org/development/feed/', '');");
80     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (2, 2)" );
81
82     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/ideas/', 'Suggest Ideas', 0, '', '');");
83     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (3, 2)" );
84
85     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/support/', 'Support Forum', 0, '', '');");
86     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (4, 2)" );
87
88     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/plugins/', 'Plugins', 0, '', '');");
89     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (5, 2)" );
90
91     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://wordpress.org/extend/themes/', 'Themes', 0, '', '');");
92     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (6, 2)" );
93
94     $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://planet.wordpress.org/', 'WordPress Planet', 0, '', '');");
95     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (7, 2)" );
96
97     // First post
98     $now = date('Y-m-d H:i:s');
99     $now_gmt = gmdate('Y-m-d H:i:s');
100     $first_post_guid = get_option('home') . '/?p=1';
101     $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, guid, comment_count, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!'))."', '', '".$wpdb->escape(__('Hello world!'))."', '0', '".$wpdb->escape(__('hello-world'))."', '$now', '$now_gmt', '$first_post_guid', '1', '', '', '')");
102     $wpdb->query( "INSERT INTO $wpdb->term_relationships (`object_id`, `term_taxonomy_id`) VALUES (1, 1)" );
103
104     // Default comment
105     $wpdb->query("INSERT INTO $wpdb->comments (comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_date, comment_date_gmt, comment_content) VALUES ('1', '".$wpdb->escape(__('Mr WordPress'))."', '', 'http://wordpress.org/', '$now', '$now_gmt', '".$wpdb->escape(__('Hi, this is a comment.<br />To delete a comment, just log in and view the post&#039;s comments. There you will have the option to edit or delete them.'))."')");
106
107     // First Page
108     $wpdb->query("INSERT INTO $wpdb->posts (post_author, post_date, post_date_gmt, post_content, post_excerpt, post_title, post_category, post_name, post_modified, post_modified_gmt, post_status, post_type, to_ping, pinged, post_content_filtered) VALUES ($user_id, '$now', '$now_gmt', '".$wpdb->escape(__('This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress.'))."', '', '".$wpdb->escape(__('About'))."', '0', '".$wpdb->escape(__('about'))."', '$now', '$now_gmt', 'publish', 'page', '', '', '')");
109 }
110 endif;
111
112 if ( !function_exists('wp_new_blog_notification') ) :
113 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
114     $user = new WP_User($user_id);
115     $email = $user->user_email;
116     $name = $user->user_login;
117     $message_headers = 'From: "' . $blog_title . '" <wordpress@' . $_SERVER['SERVER_NAME'] . '>';
118     $message = sprintf(__("Your new WordPress blog has been successfully set up at:
119
120 %1\$s
121
122 You can log in to the administrator account with the following information:
123
124 Username: %2\$s
125 Password: %3\$s
126
127 We hope you enjoy your new blog. Thanks!
128
129 --The WordPress Team
130 http://wordpress.org/
131 "), $blog_url, $name, $password);
132
133     @wp_mail($email, __('New WordPress Blog'), $message, $message_headers);
134 }
135 endif;
136
137 if ( !function_exists('wp_upgrade') ) :
138 function wp_upgrade() {
139     global $wp_current_db_version, $wp_db_version;
140
141     $wp_current_db_version = __get_option('db_version');
142
143     // We are up-to-date.  Nothing to do.
144     if ( $wp_db_version == $wp_current_db_version )
145         return;
146
147     wp_check_mysql_version();
148     wp_cache_flush();
149     make_db_current_silent();
150     upgrade_all();
151     wp_cache_flush();
152 }
153 endif;
154
155 // Functions to be called in install and upgrade scripts
156 function upgrade_all() {
157     global $wp_current_db_version, $wp_db_version, $wp_rewrite;
158     $wp_current_db_version = __get_option('db_version');
159
160     // We are up-to-date.  Nothing to do.
161     if ( $wp_db_version == $wp_current_db_version )
162         return;
163
164     // If the version is not set in the DB, try to guess the version.
165     if ( empty($wp_current_db_version) ) {
166         $wp_current_db_version = 0;
167
168         // If the template option exists, we have 1.5.
169         $template = __get_option('template');
170         if ( !empty($template) )
171             $wp_current_db_version = 2541;
172     }
173
174     if ( $wp_current_db_version < 6039 )
175         upgrade_230_options_table();
176
177     populate_options();
178
179     if ( $wp_current_db_version < 2541 ) {
180         upgrade_100();
181         upgrade_101();
182         upgrade_110();
183         upgrade_130();
184     }
185
186     if ( $wp_current_db_version < 3308 )
187         upgrade_160();
188
189     if ( $wp_current_db_version < 4772 )
190         upgrade_210();
191
192     if ( $wp_current_db_version < 4351 )
193         upgrade_old_slugs();
194
195     if ( $wp_current_db_version < 5539 )
196         upgrade_230();
197
198     if ( $wp_current_db_version < 6124 )
199         upgrade_230_old_tables();
200
201     maybe_disable_automattic_widgets();
202
203     $wp_rewrite->flush_rules();
204
205     update_option('db_version', $wp_db_version);
206 }
207
208 function upgrade_100() {
209     global $wpdb;
210
211     // Get the title and ID of every post, post_name to check if it already has a value
212     $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''");
213     if ($posts) {
214         foreach($posts as $post) {
215             if ('' == $post->post_name) {
216                 $newtitle = sanitize_title($post->post_title);
217                 $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'");
218             }
219         }
220     }
221
222     $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories");
223     foreach ($categories as $category) {
224         if ('' == $category->category_nicename) {
225             $newtitle = sanitize_title($category->cat_name);
226             $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'");
227         }
228     }
229
230
231     $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
232     WHERE option_name LIKE 'links_rating_image%'
233     AND option_value LIKE 'wp-links/links-images/%'");
234
235     $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat");
236     if ($done_ids) :
237         foreach ($done_ids as $done_id) :
238             $done_posts[] = $done_id->post_id;
239         endforeach;
240         $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
241     else:
242         $catwhere = '';
243     endif;
244
245     $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere");
246     if ($allposts) :
247         foreach ($allposts as $post) {
248             // Check to see if it's already been imported
249             $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category");
250             if (!$cat && 0 != $post->post_category) { // If there's no result
251                 $wpdb->query("
252                     INSERT INTO $wpdb->post2cat
253                     (post_id, category_id)
254                     VALUES
255                     ('$post->ID', '$post->post_category')
256                     ");
257             }
258         }
259     endif;
260 }
261
262 function upgrade_101() {
263     global $wpdb;
264
265     // Clean up indices, add a few
266     add_clean_index($wpdb->posts, 'post_name');
267     add_clean_index($wpdb->posts, 'post_status');
268     add_clean_index($wpdb->categories, 'category_nicename');
269     add_clean_index($wpdb->comments, 'comment_approved');
270     add_clean_index($wpdb->comments, 'comment_post_ID');
271     add_clean_index($wpdb->links , 'link_category');
272     add_clean_index($wpdb->links , 'link_visible');
273 }
274
275
276 function upgrade_110() {
277     global $wpdb;
278
279     // Set user_nicename.
280     $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users");
281     foreach ($users as $user) {
282         if ('' == $user->user_nicename) {
283             $newname = sanitize_title($user->user_nickname);
284             $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'");
285         }
286     }
287
288     $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users");
289     foreach ($users as $row) {
290         if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
291             $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\'');
292         }
293     }
294
295
296     // Get the GMT offset, we'll use that later on
297     $all_options = get_alloptions_110();
298
299     $time_difference = $all_options->time_difference;
300
301     $server_time = time()+date('Z');
302     $weblogger_time = $server_time + $time_difference*3600;
303     $gmt_time = time();
304
305     $diff_gmt_server = ($gmt_time - $server_time) / 3600;
306     $diff_weblogger_server = ($weblogger_time - $server_time) / 3600;
307     $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
308     $gmt_offset = -$diff_gmt_weblogger;
309
310     // Add a gmt_offset option, with value $gmt_offset
311     add_option('gmt_offset', $gmt_offset);
312
313     // Check if we already set the GMT fields (if we did, then
314     // MAX(post_date_gmt) can't be '0000-00-00 00:00:00'
315     // <michel_v> I just slapped myself silly for not thinking about it earlier
316     $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true;
317
318     if (!$got_gmt_fields) {
319
320         // Add or substract time to all dates, to get GMT dates
321         $add_hours = intval($diff_gmt_weblogger);
322         $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
323         $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
324         $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date");
325         $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'");
326         $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
327         $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
328     }
329
330 }
331
332 function upgrade_130() {
333     global $wpdb;
334
335     // Remove extraneous backslashes.
336     $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts");
337     if ($posts) {
338         foreach($posts as $post) {
339             $post_content = addslashes(deslash($post->post_content));
340             $post_title = addslashes(deslash($post->post_title));
341             $post_excerpt = addslashes(deslash($post->post_excerpt));
342             if ( empty($post->guid) )
343                 $guid = get_permalink($post->ID);
344             else
345                 $guid = $post->guid;
346
347             $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'");
348         }
349     }
350
351     // Remove extraneous backslashes.
352     $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments");
353     if ($comments) {
354         foreach($comments as $comment) {
355             $comment_content = addslashes(deslash($comment->comment_content));
356             $comment_author = addslashes(deslash($comment->comment_author));
357             $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'");
358         }
359     }
360
361     // Remove extraneous backslashes.
362     $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links");
363     if ($links) {
364         foreach($links as $link) {
365             $link_name = addslashes(deslash($link->link_name));
366             $link_description = addslashes(deslash($link->link_description));
367             $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'");
368         }
369     }
370
371     // The "paged" option for what_to_show is no more.
372     if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') {
373         $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'");
374     }
375
376     $active_plugins = __get_option('active_plugins');
377
378     // If plugins are not stored in an array, they're stored in the old
379     // newline separated format.  Convert to new format.
380     if ( !is_array( $active_plugins ) ) {
381         $active_plugins = explode("\n", trim($active_plugins));
382         update_option('active_plugins', $active_plugins);
383     }
384
385     // Obsolete tables
386     $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues');
387     $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes');
388     $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups');
389     $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options');
390
391     // Update comments table to use comment_type
392     $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
393     $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
394
395     // Some versions have multiple duplicate option_name rows with the same values
396     $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name");
397     foreach ( $options as $option ) {
398         if ( 1 != $option->dupes ) { // Could this be done in the query?
399             $limit = $option->dupes - 1;
400             $dupe_ids = $wpdb->get_col("SELECT option_id FROM $wpdb->options WHERE option_name = '$option->option_name' LIMIT $limit");
401             $dupe_ids = join($dupe_ids, ',');
402             $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)");
403         }
404     }
405
406     make_site_theme();
407 }
408
409 function upgrade_160_helper( $users ) {
410     global $wpdb;
411
412     populate_roles_160();
413
414     foreach ( $users as $user_details ) :
415         $user = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE ID = '" . $user_details[ 'user_id' ] . "'");
416         if ( !empty( $user->user_firstname ) )
417             update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) );
418         if ( !empty( $user->user_lastname ) )
419             update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) );
420         if ( !empty( $user->user_nickname ) )
421             update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) );
422         if ( !empty( $user->user_level ) )
423             update_usermeta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level );
424         if ( !empty( $user->user_icq ) )
425             update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) );
426         if ( !empty( $user->user_aim ) )
427             update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) );
428         if ( !empty( $user->user_msn ) )
429             update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) );
430         if ( !empty( $user->user_yim ) )
431             update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) );
432         if ( !empty( $user->user_description ) )
433             update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) );
434
435         if ( isset( $user->user_idmode ) ):
436             $idmode = $user->user_idmode;
437             if ($idmode == 'nickname') $id = $user->user_nickname;
438             if ($idmode == 'login') $id = $user->user_login;
439             if ($idmode == 'firstname') $id = $user->user_firstname;
440             if ($idmode == 'lastname') $id = $user->user_lastname;
441             if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
442             if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
443             if (!$idmode) $id = $user->user_nickname;
444             $id