| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
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/admin-functions.php'); 6 require_once (ABSPATH . '/wp-admin/admin-db.php'); 7 require_once (ABSPATH . '/wp-admin/upgrade-schema.php'); 8 require_once(ABSPATH . WPINC . '/registration.php'); 9 10 if ( !function_exists('wp_install') ) : 11 function wp_install($blog_title, $user_name, $user_email, $public, $meta='') { 12 global $wp_rewrite; 13 14 wp_cache_flush(); 15 make_db_current_silent(); 16 populate_options(); 17 populate_roles(); 18 19 update_option('blogname', $blog_title); 20 update_option('admin_email', $user_email); 21 update_option('blog_public', $public); 22 $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://'; 23 $guessurl = preg_replace('|/wp-admin/.*|i', '', $schema . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); 24 update_option('siteurl', $guessurl); 25 26 // If not a public blog, don't ping. 27 if ( ! $public ) 28 update_option('default_pingback_flag', 0); 29 30 // Create default user. If the user already exists, the user tables are 31 // being shared among blogs. Just set the role in that case. 32 $user_id = username_exists($user_name); 33 if ( !$user_id ) { 34 $random_password = substr(md5(uniqid(microtime())), 0, 6); 35 $user_id = wp_create_user($user_name, $random_password, $user_email); 36 } else { 37 $random_password = __('User already exists. Password inherited.'); 38 } 39 40 $user = new WP_User($user_id); 41 $user->set_role('administrator'); 42 43 wp_install_defaults($user_id); 44 45 $wp_rewrite->flush_rules(); 46 47 wp_new_blog_notification($blog_title, $guessurl, $user_id, $random_password); 48 49 wp_cache_flush(); 50 51 return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $random_password); 52 } 53 endif; 54 55 if ( !function_exists('wp_install_defaults') ) : 56 function wp_install_defaults($user_id) { 57 global $wpdb; 58 59 // Default category 60 $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, category_count, category_description) VALUES ('0', '".$wpdb->escape(__('Uncategorized'))."', '".sanitize_title(__('Uncategorized'))."', '1', '')"); 61 62 // Default link category 63 $wpdb->query("INSERT INTO $wpdb->categories (cat_ID, cat_name, category_nicename, link_count, category_description) VALUES ('0', '".$wpdb->escape(__('Blogroll'))."', '".sanitize_title(__('Blogroll'))."', '7', '')"); 64 65 // Now drop in some default links 66 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://inphotos.org/', 'Donncha', 0, 'http://inphotos.org/feed/', '');"); 67 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (1, 2)" ); 68 69 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zengun.org/weblog/', 'Michel', 0, 'http://zengun.org/weblog/feed/', '');"); 70 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (2, 2)" ); 71 72 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://boren.nu/', 'Ryan', 0, 'http://boren.nu/feed/', '');"); 73 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (3, 2)" ); 74 75 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://photomatt.net/', 'Matt', 0, 'http://xml.photomatt.net/feed/', '');"); 76 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (4, 2)" ); 77 78 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://zed1.com/journalized/', 'Mike', 0, 'http://zed1.com/journalized/feed/', '');"); 79 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (5, 2)" ); 80 81 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://www.alexking.org/', 'Alex', 0, 'http://www.alexking.org/blog/wp-rss2.php', '');"); 82 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (6, 2)" ); 83 84 $wpdb->query("INSERT INTO $wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('http://dougal.gunters.org/', 'Dougal', 0, 'http://dougal.gunters.org/feed/', '');"); 85 $wpdb->query( "INSERT INTO $wpdb->link2cat (`link_id`, `category_id`) VALUES (7, 2)" ); 86 87 // First post 88 $now = date('Y-m-d H:i:s'); 89 $now_gmt = gmdate('Y-m-d H:i:s'); 90 $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, 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', '1', '', '', '')"); 91 92 $wpdb->query( "INSERT INTO $wpdb->post2cat (`rel_id`, `post_id`, `category_id`) VALUES (1, 1, 1)" ); 93 94 // Default comment 95 $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 posts\' comments, there you will have the option to edit or delete them.'))."')"); 96 97 // First Page 98 99 $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', '', '', '')"); 100 } 101 endif; 102 103 if ( !function_exists('wp_new_blog_notification') ) : 104 function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) { 105 $user = new WP_User($user_id); 106 $email = $user->user_email; 107 $name = $user->user_login; 108 $message_headers = 'From: ' . $blog_title . ' <wordpress@' . $_SERVER['SERVER_NAME'] . '>'; 109 $message = sprintf(__("Your new WordPress blog has been successfully set up at: 110 111 %1\$s 112 113 You can log in to the administrator account with the following information: 114 115 Username: %2\$s 116 Password: %3\$s 117 118 We hope you enjoy your new weblog. Thanks! 119 120 --The WordPress Team 121 http://wordpress.org/ 122 "), $blog_url, $name, $password); 123 124 @wp_mail($email, __('New WordPress Blog'), $message, $message_headers); 125 } 126 endif; 127 128 if ( !function_exists('wp_upgrade') ) : 129 function wp_upgrade() { 130 global $wp_current_db_version, $wp_db_version; 131 132 $wp_current_db_version = __get_option('db_version'); 133 134 // We are up-to-date. Nothing to do. 135 if ( $wp_db_version == $wp_current_db_version ) 136 return; 137 138 wp_cache_flush(); 139 make_db_current_silent(); 140 upgrade_all(); 141 wp_cache_flush(); 142 } 143 endif; 144 145 // Functions to be called in install and upgrade scripts 146 function upgrade_all() { 147 global $wp_current_db_version, $wp_db_version, $wp_rewrite; 148 $wp_current_db_version = __get_option('db_version'); 149 150 // We are up-to-date. Nothing to do. 151 if ( $wp_db_version == $wp_current_db_version ) 152 return; 153 154 // If the version is not set in the DB, try to guess the version. 155 if ( empty($wp_current_db_version) ) { 156 $wp_current_db_version = 0; 157 158 // If the template option exists, we have 1.5. 159 $template = __get_option('template'); 160 if ( !empty($template) ) 161 $wp_current_db_version = 2541; 162 } 163 164 populate_options(); 165 166 if ( $wp_current_db_version < 2541 ) { 167 upgrade_100(); 168 upgrade_101(); 169 upgrade_110(); 170 upgrade_130(); 171 } 172 173 if ( $wp_current_db_version < 3308 ) 174 upgrade_160(); 175 176 if ( $wp_current_db_version < 3845 ) 177 upgrade_210(); 178 179 $wp_rewrite->flush_rules(); 180 181 update_option('db_version', $wp_db_version); 182 } 183 184 function upgrade_100() { 185 global $wpdb; 186 187 // Get the title and ID of every post, post_name to check if it already has a value 188 $posts = $wpdb->get_results("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE post_name = ''"); 189 if ($posts) { 190 foreach($posts as $post) { 191 if ('' == $post->post_name) { 192 $newtitle = sanitize_title($post->post_title); 193 $wpdb->query("UPDATE $wpdb->posts SET post_name = '$newtitle' WHERE ID = '$post->ID'"); 194 } 195 } 196 } 197 198 $categories = $wpdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $wpdb->categories"); 199 foreach ($categories as $category) { 200 if ('' == $category->category_nicename) { 201 $newtitle = sanitize_title($category->cat_name); 202 $wpdb->query("UPDATE $wpdb->categories SET category_nicename = '$newtitle' WHERE cat_ID = '$category->cat_ID'"); 203 } 204 } 205 206 207 $wpdb->query("UPDATE $wpdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/') 208 WHERE option_name LIKE 'links_rating_image%' 209 AND option_value LIKE 'wp-links/links-images/%'"); 210 211 $done_ids = $wpdb->get_results("SELECT DISTINCT post_id FROM $wpdb->post2cat"); 212 if ($done_ids) : 213 foreach ($done_ids as $done_id) : 214 $done_posts[] = $done_id->post_id; 215 endforeach; 216 $catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')'; 217 else: 218 $catwhere = ''; 219 endif; 220 221 $allposts = $wpdb->get_results("SELECT ID, post_category FROM $wpdb->posts WHERE post_category != '0' $catwhere"); 222 if ($allposts) : 223 foreach ($allposts as $post) { 224 // Check to see if it's already been imported 225 $cat = $wpdb->get_row("SELECT * FROM $wpdb->post2cat WHERE post_id = $post->ID AND category_id = $post->post_category"); 226 if (!$cat && 0 != $post->post_category) { // If there's no result 227 $wpdb->query(" 228 INSERT INTO $wpdb->post2cat 229 (post_id, category_id) 230 VALUES 231 ('$post->ID', '$post->post_category') 232 "); 233 } 234 } 235 endif; 236 } 237 238 function upgrade_101() { 239 global $wpdb; 240 241 // Clean up indices, add a few 242 add_clean_index($wpdb->posts, 'post_name'); 243 add_clean_index($wpdb->posts, 'post_status'); 244 add_clean_index($wpdb->categories, 'category_nicename'); 245 add_clean_index($wpdb->comments, 'comment_approved'); 246 add_clean_index($wpdb->comments, 'comment_post_ID'); 247 add_clean_index($wpdb->links , 'link_category'); 248 add_clean_index($wpdb->links , 'link_visible'); 249 } 250 251 252 function upgrade_110() { 253 global $wpdb; 254 255 // Set user_nicename. 256 $users = $wpdb->get_results("SELECT ID, user_nickname, user_nicename FROM $wpdb->users"); 257 foreach ($users as $user) { 258 if ('' == $user->user_nicename) { 259 $newname = sanitize_title($user->user_nickname); 260 $wpdb->query("UPDATE $wpdb->users SET user_nicename = '$newname' WHERE ID = '$user->ID'"); 261 } 262 } 263 264 $users = $wpdb->get_results("SELECT ID, user_pass from $wpdb->users"); 265 foreach ($users as $row) { 266 if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) { 267 $wpdb->query('UPDATE '.$wpdb->users.' SET user_pass = MD5(\''.$row->user_pass.'\') WHERE ID = \''.$row->ID.'\''); 268 } 269 } 270 271 272 // Get the GMT offset, we'll use that later on 273 $all_options = get_alloptions_110(); 274 275 $time_difference = $all_options->time_difference; 276 277 $server_time = time()+date('Z'); 278 $weblogger_time = $server_time + $time_difference*3600; 279 $gmt_time = time(); 280 281 $diff_gmt_server = ($gmt_time - $server_time) / 3600; 282 $diff_weblogger_server = ($weblogger_time - $server_time) / 3600; 283 $diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server; 284 $gmt_offset = -$diff_gmt_weblogger; 285 286 // Add a gmt_offset option, with value $gmt_offset 287 add_option('gmt_offset', $gmt_offset); 288 289 // Check if we already set the GMT fields (if we did, then 290 // MAX(post_date_gmt) can't be '0000-00-00 00:00:00' 291 // <michel_v> I just slapped myself silly for not thinking about it earlier 292 $got_gmt_fields = ($wpdb->get_var("SELECT MAX(post_date_gmt) FROM $wpdb->posts") == '0000-00-00 00:00:00') ? false : true; 293 294 if (!$got_gmt_fields) { 295 296 // Add or substract time to all dates, to get GMT dates 297 $add_hours = intval($diff_gmt_weblogger); 298 $add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours)); 299 $wpdb->query("UPDATE $wpdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 300 $wpdb->query("UPDATE $wpdb->posts SET post_modified = post_date"); 301 $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'"); 302 $wpdb->query("UPDATE $wpdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 303 $wpdb->query("UPDATE $wpdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)"); 304 } 305 306 } 307 308 function upgrade_130() { 309 global $wpdb; 310 311 // Remove extraneous backslashes. 312 $posts = $wpdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $wpdb->posts"); 313 if ($posts) { 314 foreach($posts as $post) { 315 $post_content = addslashes(deslash($post->post_content)); 316 $post_title = addslashes(deslash($post->post_title)); 317 $post_excerpt = addslashes(deslash($post->post_excerpt)); 318 if ( empty($post->guid) ) 319 $guid = get_permalink($post->ID); 320 else 321 $guid = $post->guid; 322 323 $wpdb->query("UPDATE $wpdb->posts SET post_title = '$post_title', post_content = '$post_content', post_excerpt = '$post_excerpt', guid = '$guid' WHERE ID = '$post->ID'"); 324 } 325 } 326 327 // Remove extraneous backslashes. 328 $comments = $wpdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $wpdb->comments"); 329 if ($comments) { 330 foreach($comments as $comment) { 331 $comment_content = addslashes(deslash($comment->comment_content)); 332 $comment_author = addslashes(deslash($comment->comment_author)); 333 $wpdb->query("UPDATE $wpdb->comments SET comment_content = '$comment_content', comment_author = '$comment_author' WHERE comment_ID = '$comment->comment_ID'"); 334 } 335 } 336 337 // Remove extraneous backslashes. 338 $links = $wpdb->get_results("SELECT link_id, link_name, link_description FROM $wpdb->links"); 339 if ($links) { 340 foreach($links as $link) { 341 $link_name = addslashes(deslash($link->link_name)); 342 $link_description = addslashes(deslash($link->link_description)); 343 $wpdb->query("UPDATE $wpdb->links SET link_name = '$link_name', link_description = '$link_description' WHERE link_id = '$link->link_id'"); 344 } 345 } 346 347 // The "paged" option for what_to_show is no more. 348 if ($wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = 'what_to_show'") == 'paged') { 349 $wpdb->query("UPDATE $wpdb->options SET option_value = 'posts' WHERE option_name = 'what_to_show'"); 350 } 351 352 $active_plugins = __get_option('active_plugins'); 353 354 // If plugins are not stored in an array, they're stored in the old 355 // newline separated format. Convert to new format. 356 if ( !is_array( $active_plugins ) ) { 357 $active_plugins = explode("\n", trim($active_plugins)); 358 update_option('active_plugins', $active_plugins); 359 } 360 361 // Obsolete tables 362 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optionvalues'); 363 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiontypes'); 364 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroups'); 365 $wpdb->query('DROP TABLE IF EXISTS ' . $wpdb->prefix . 'optiongroup_options'); 366 367 // Update comments table to use comment_type 368 $wpdb->query("UPDATE $wpdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'"); 369 $wpdb->query("UPDATE $wpdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'"); 370 371 // Some versions have multiple duplicate option_name rows with the same values 372 $options = $wpdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$wpdb->options` GROUP BY option_name"); 373 foreach ( $options as $option ) { 374 if ( 1 != $option->dupes ) { // Could this be done in the query? 375 $limit = $option->dupes - 1; 376 $dupe_ids = $wpdb->get_col("SELECT option_id FROM $wpdb->options WHERE option_name = '$option->option_name' LIMIT $limit"); 377 $dupe_ids = join($dupe_ids, ','); 378 $wpdb->query("DELETE FROM $wpdb->options WHERE option_id IN ($dupe_ids)"); 379 } 380 } 381 382 make_site_theme(); 383 } 384 385 function upgrade_160() { 386 global $wpdb, $wp_current_db_version; 387 388 populate_roles_160(); 389 390 $users = $wpdb->get_results("SELECT * FROM $wpdb->users"); 391 foreach ( $users as $user ) : 392 if ( !empty( $user->user_firstname ) ) 393 update_usermeta( $user->ID, 'first_name', $wpdb->escape($user->user_firstname) ); 394 if ( !empty( $user->user_lastname ) ) 395 update_usermeta( $user->ID, 'last_name', $wpdb->escape($user->user_lastname) ); 396 if ( !empty( $user->user_nickname ) ) 397 update_usermeta( $user->ID, 'nickname', $wpdb->escape($user->user_nickname) ); 398 if ( !empty( $user->user_level ) ) 399 update_usermeta( $user->ID, $wpdb->prefix . 'user_level', $user->user_level ); 400 if ( !empty( $user->user_icq ) ) 401 update_usermeta( $user->ID, 'icq', $wpdb->escape($user->user_icq) ); 402 if ( !empty( $user->user_aim ) ) 403 update_usermeta( $user->ID, 'aim', $wpdb->escape($user->user_aim) ); 404 if ( !empty( $user->user_msn ) ) 405 update_usermeta( $user->ID, 'msn', $wpdb->escape($user->user_msn) ); 406 if ( !empty( $user->user_yim ) ) 407 update_usermeta( $user->ID, 'yim', $wpdb->escape($user->user_icq) ); 408 if ( !empty( $user->user_description ) ) 409 update_usermeta( $user->ID, 'description', $wpdb->escape($user->user_description) ); 410 411 if ( isset( $user->user_idmode ) ): 412 $idmode = $user->user_idmode; 413 if ($idmode == 'nickname') $id = $user->user_nickname; 414 if ($idmode == 'login') $id = $user->user_login; 415 if ($idmode == 'firstname') $id = $user->user_firstname; 416 if ($idmode == 'lastname') $id = $user->user_lastname; 417 if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname; 418 if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname; 419 if (!$idmode) $id = $user->user_nickname; 420 $id = $wpdb->escape( $id ); 421 $wpdb->query("UPDATE $wpdb->users SET display_name = '$id' WHERE ID = '$user->ID'"); 422 endif; 423 424 // FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set. 425 $caps = get_usermeta( $user->ID, $wpdb->prefix . 'capabilities'); 426 if ( empty($caps) || defined('RESET_CAPS') ) { 427 $level = get_usermeta($user->ID, $wpdb->prefix . 'user_level'); 428 $role = translate_level_to_role($level); 429 update_usermeta( $user->ID, $wpdb->prefix . 'capabilities', array($role => true) ); 430 } 431 432 endforeach; 433 $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' ); 434 $wpdb->hide_errors(); 435 foreach ( $old_user_fields as $old ) 436 $wpdb->query("ALTER TABLE $wpdb->users DROP $old"); 437 $wpdb->show_errors(); 438 439 if ( 0 == $wpdb->get_var("SELECT SUM(category_count) FROM $wpdb->categories") ) { // Create counts 440 $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); 441 foreach ( $categories as $cat_id ) { 442 $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'"); 443 $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); 444 } 445 } 446 447 // populate comment_count field of posts table 448 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" ); 449 if( is_array( $comments ) ) { 450 foreach ($comments as $comment) { 451 $wpdb->query( "UPDATE $wpdb->posts SET comment_count = $comment->c WHERE ID = '$comment->comment_post_ID'" ); 452 } 453 } 454 455 // Some alpha versions used a post status of object instead of attachment and put 456 // the mime type in post_type instead of post_mime_type. 457 if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) { 458 $objects = $wpdb->get_results("SELECT ID, post_type FROM $wpdb->posts WHERE post_status = 'object'"); 459 foreach ($objects as $object) { 460 $wpdb->query("UPDATE $wpdb->posts SET post_status = 'attachment', 461 post_mime_type = '$object->post_type', 462 post_type = '' 463 WHERE ID = $object->ID"); 464 465 $meta = get_post_meta($object->ID, 'imagedata', true); 466 if ( ! empty($meta['file']) ) 467 add_post_meta($object->ID, '_wp_attached_file', $meta['file']); 468 } 469 } 470 } 471 472 function upgrade_210() { 473 global $wpdb, $wp_current_db_version; 474 475 if ( $wp_current_db_version < 3506 ) { 476 // Update status and type. 477 $posts = $wpdb->get_results("SELECT ID, post_status FROM $wpdb->posts"); 478 479 if ( ! empty($posts) ) foreach ($posts as $post) { 480 $status = $post->post_status; 481 $type = 'post'; 482 483 if ( 'static' == $status ) { 484 $status = 'publish'; 485 $type = 'page'; 486 } else if ( 'attachment' == $status ) { 487 $status = 'inherit'; 488 $type = 'attachment'; 489 } 490 491 $wpdb->query("UPDATE $wpdb->posts SET post_status = '$status', post_type = '$type' WHERE ID = '$post->ID'"); 492 } 493 } 494 495 if ( $wp_current_db_version < 3845 ) { 496 populate_roles_210(); 497 } 498 499 if ( $wp_current_db_version < 3531 ) { 500 // Give future posts a post_status of future. 501 $now = gmdate('Y-m-d H:i:59'); 502 $wpdb->query ("UPDATE $wpdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'"); 503 504 $posts = $wpdb->get_results("SELECT ID, post_date FROM $wpdb->posts WHERE post_status ='future'"); 505 if ( !empty($posts) ) 506 foreach ( $posts as $post ) 507 wp_schedule_single_event(mysql2date('U', $post->post_date), 'publish_future_post', $post->ID); 508 } 509 if ( $wp_current_db_version < 3570 ) { 510 // Create categories for link categories if a category with the same 511 // name doesn't exist. Create a map of link cat IDs to cat IDs. 512 $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories"); 513 foreach ( $link_cats as $link_cat) { 514 if ( $cat_id = category_exists($link_cat->cat_name) ) { 515 $link_cat_id_map[$link_cat->cat_id] = $cat_id; 516 $default_link_cat = $cat_id; 517 } else { 518 $link_cat_id_map[$link_cat->cat_id] = wp_create_category($link_cat->cat_name); 519 $default_link_cat = $link_cat_id_map[$link_cat->cat_id]; 520 } 521 } 522 523 // Associate links to cats. 524 $links = $wpdb->get_results("SELECT link_id, link_category FROM $wpdb->links"); 525 if ( !empty($links) ) foreach ( $links as $link ) { 526 $link_cat = $link_cat_id_map[$link->link_category]; 527 $cat = $wpdb->get_row("SELECT * FROM $wpdb->link2cat WHERE link_id = '$link->link_id' AND category_id = '$link_cat'"); 528 if (!$cat && 0 != $link->link_category) { 529 $wpdb->query("INSERT INTO $wpdb->link2cat (link_id, category_id) 530 VALUES ('$link->link_id', '$link_cat')"); 531 } 532 } 533 534 // Set default to the last category we grabbed during the upgrade loop. 535 update_option('default_link_category', $default_link_cat); 536 537 // Count links per category. 538 if ( 0 == $wpdb->get_var("SELECT SUM(link_count) FROM $wpdb->categories") ) { 539 $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); 540 foreach ( $categories as $cat_id ) { 541 $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->link2cat, $wpdb->links WHERE $wpdb->links.link_id = $wpdb->link2cat.link_id AND category_id = '$cat_id'"); 542 $wpdb->query("UPDATE $wpdb->categories SET link_count = '$count' WHERE cat_ID = '$cat_id'"); 543 } 544 } 545 } 546 } 547 548 // The functions we use to actually do stuff 549 550 // General 551 function maybe_create_table($table_name, $create_ddl) { 552 global $wpdb; 553 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { 554 if ($table == $table_name) { 555 return true; 556 } 557 } 558 //didn't find it try to create it. 559 $q = $wpdb->query($create_ddl); 560 // we cannot directly tell that whether this succeeded! 561 foreach ($wpdb->get_col("SHOW TABLES",0) as $table ) { 562 if ($table == $table_name) { 563 return true; 564 } 565 } 566 return false; 567 } 568 569 function drop_index($table, $index) { 570 global $wpdb; 571 $wpdb->hide_errors(); 572 $wpdb->query("ALTER TABLE `$table` DROP INDEX `$index`"); 573 // Now we need to take out all the extra ones we may have created 574 for ($i = 0; $i < 25; $i++) { 575 $wpdb->query("ALTER TABLE `$table` DROP INDEX `{$index}_$i`"); 576 } 577 $wpdb->show_errors(); 578 return true; 579 } 580 581 function add_clean_index($table, $index) { 582 global $wpdb; 583 drop_index($table, $index); 584 $wpdb->query("ALTER TABLE `$table` ADD INDEX ( `$index` )"); 585 return true; 586 } 587 588 /** 589 ** maybe_add_column() 590 ** Add column to db table if it doesn't exist. 591 ** Returns: true if already exists or on successful completion 592 ** false on error 593 */ 594 function maybe_add_column($table_name, $column_name, $create_ddl) { 595 global $wpdb, $debug; 596 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 597 if ($debug) echo("checking $column == $column_name<br />"); 598 if ($column == $column_name) { 599 return true; 600 } 601 } 602 //didn't find it try to create it. 603 $q = $wpdb->query($create_ddl); 604 // we cannot directly tell that whether this succeeded! 605 foreach ($wpdb->get_col("DESC $table_name", 0) as $column ) { 606 if ($column == $column_name) { 607 return true; 608 } 609 } 610 return false; 611 } 612 613 614 // get_alloptions as it was for 1.2. 615 function get_alloptions_110() { 616 global $wpdb; 617 if ($options = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options")) { 618 foreach ($options as $option) { 619 // "When trying to design a foolproof system, 620 // never underestimate the ingenuity of the fools :)" -- Dougal 621 if ('siteurl' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); 622 if ('home' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); 623 if ('category_base' == $option->option_name) $option->option_value = preg_replace('|/+$|', '', $option->option_value); 624 $all_options->{$option->option_name} = stripslashes($option->option_value); 625 } 626 } 627 return $all_options; 628 } 629 630 // Version of get_option that is private to install/upgrade. 631 function __get_option($setting) { 632 global $wpdb; 633 634 $option = $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '$setting'"); 635 636 if ( 'home' == $setting && '' == $option ) 637 return __get_option('siteurl'); 638 639 if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting ) 640 $option = preg_replace('|/+$|', '', $option); 641 642 @ $kellogs = unserialize($option); 643 if ($kellogs !== FALSE) 644 return $kellogs; 645 else 646 return $option; 647 } 648 649 function deslash($content) { 650 // Note: \\\ inside a regex denotes a single backslash. 651 652 // Replace one or more backslashes followed by a single quote with 653 // a single quote. 654 $content = preg_replace("/\\\+'/", "'", $content); 655 656 // Replace one or more backslashes followed by a double quote with 657 // a double quote. 658 $content = preg_replace('/\\\+"/', '"', $content); 659 660 // Replace one or more backslashes with one backslash. 661 $content = preg_replace("/\\\+/", "\\", $content); 662 663 return $content; 664 } 665 666 function dbDelta($queries, $execute = true) { 667 global $wpdb; 668 669 // Seperate individual queries into an array 670 if( !is_array($queries) ) { 671 $queries = explode( ';', $queries ); 672 if('' == $queries[count($queries) - 1]) array_pop($queries); 673 } 674 675 $cqueries = array(); // Creation Queries 676 $iqueries = array(); // Insertion Queries 677 $for_update = array(); 678 679 // Create a tablename index for an array ($cqueries) of queries 680 foreach($queries as $qry) { 681 if(preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { 682 $cqueries[strtolower($matches[1])] = $qry; 683 $for_update[$matches[1]] = 'Created table '.$matches[1]; 684 } 685 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { 686 array_unshift($cqueries, $qry); 687 } 688 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { 689 $iqueries[] = $qry; 690 } 691 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { 692 $iqueries[] = $qry; 693 } 694 else { 695 // Unrecognized query type 696 } 697 } 698 699 // Check to see which tables and fields exist 700 if($tables = $wpdb->get_col('SHOW TABLES;')) { 701 // For every table in the database 702 foreach($tables as $table) { 703 // If a table query exists for the database table... 704 if( array_key_exists(strtolower($table), $cqueries) ) { 705 // Clear the field and index arrays 706 unset($cfields); 707 unset($indices); 708 // Get all of the field names in the query from between the parens 709 preg_match("|\((.*)\)|ms", $cqueries[strtolower($table)], $match2); 710 $qryline = trim($match2[1]); 711 712 // Separate field lines into an array 713 $flds = explode("\n", $qryline); 714 715 //echo "<hr/><pre>\n".print_r(strtolower($table), true).":\n".print_r($cqueries, true)."</pre><hr/>"; 716 717 // For every field line specified in the query 718 foreach($flds as $fld) { 719 // Extract the field name 720 preg_match("|^([^ ]*)|", trim($fld), $fvals); 721 $fieldname = $fvals[1]; 722 723 // Verify the found field name 724 $validfield = true; 725 switch(strtolower($fieldname)) 726 { 727 case '': 728 case 'primary': 729 case 'index': 730 case 'fulltext': 731 case 'unique': 732 case 'key': 733 $validfield = false; 734 $indices[] = trim(trim($fld), ", \n"); 735 break; 736 } 737 $fld = trim($fld); 738 739 // If it's a valid field, add it to the field array 740 if($validfield) { 741 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); 742 } 743 } 744 745 // Fetch the table column structure from the database 746 $tablefields = $wpdb->get_results("DESCRIBE {$table};"); 747 748 // For every field in the table 749 foreach($tablefields as $tablefield) { 750 // If the table field exists in the field array... 751 if(array_key_exists(strtolower($tablefield->Field), $cfields)) { 752 // Get the field type from the query 753 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); 754 $fieldtype = $matches[1]; 755 756 // Is actual field type different from the field type in query? 757 if($tablefield->Type != $fieldtype) { 758 // Add a query to change the column type 759 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; 760 $for_update[$table.'.'.$tablefield->Field] = "Changed type of {$table}.{$tablefield->Field} from {$tablefield->Type} to {$fieldtype}"; 761 } 762 763 // Get the default value from the array 764 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; 765 if(preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { 766 $default_value = $matches[1]; 767 if($tablefield->Default != $default_value) 768 { 769 // Add a query to change the column's default value 770 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; 771 $for_update[$table.'.'.$tablefield->Field] = "Changed default value of {$table}.{$tablefield->Field} from {$tablefield->Default} to {$default_value}"; 772 } 773 } 774 775 // Remove the field from the array (so it's not added) 776 unset($cfields[strtolower($tablefield->Field)]); 777 } 778 else { 779 // This field exists in the table, but not in the creation queries? 780 } 781 } 782 783 // For every remaining field specified for the table 784 foreach($cfields as $fieldname => $fielddef) { 785 // Push a query line into $cqueries that adds the field to that table 786 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; 787 $for_update[$table.'.'.$fieldname] = 'Added column '.$table.'.'.$fieldname; 788 } 789 790 // Index stuff goes here 791 // Fetch the table index structure from the database 792 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 793 794 if($tableindices) { 795 // Clear the index array 796 unset($index_ary); 797 798 // For every index in the table 799 foreach($tableindices as $tableindex) { 800 // Add the index to the index data array 801 $keyname = $tableindex->Key_name; 802 $index_ary[$keyname]['columns'][] = array('fieldname' => $tableindex->Column_name, 'subpart' => $tableindex->Sub_part); 803 $index_ary[$keyname]['unique'] = ($tableindex->Non_unique == 0)?true:false; 804 } 805 806 // For each actual index in the index array 807 foreach($index_ary as $index_name => $index_data) { 808 // Build a create string to compare to the query 809 $index_string = ''; 810 if($index_name == 'PRIMARY') { 811 $index_string .= 'PRIMARY '; 812 } 813 else if($index_data['unique']) { 814 $index_string .= 'UNIQUE '; 815 } 816 $index_string .= 'KEY '; 817 if($index_name != 'PRIMARY') { 818 $index_string .= $index_name; 819 } 820 $index_columns = ''; 821 // For each column in the index 822 foreach($index_data['columns'] as $column_data) { 823 if($index_columns != '') $index_columns .= ','; 824 // Add the field to the column list string 825 $index_columns .= $column_data['fieldname']; 826 if($column_data['subpart'] != '') { 827 $index_columns .= '('.$column_data['subpart'].')'; 828 } 829 } 830 // Add the column list to the index create string 831 $index_string .= ' ('.$index_columns.')'; 832 833 if(!(($aindex = array_search($index_string, $indices)) === false)) { 834 unset($indices[$aindex]); 835 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br/>Found index:".$index_string."</pre>\n"; 836 } 837 //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"; 838 } 839 } 840 841 // For every remaining index specified for the table 842 foreach($indices as $index) { 843 // Push a query line into $cqueries that adds the index to that table 844 $cqueries[] = "ALTER TABLE {$table} ADD $index"; 845 $for_update[$table.'.'.$fieldname] = 'Added index '.$table.' '.$index; 846 } 847 848 // Remove the original table creation query from processing 849 unset($cqueries[strtolower($table)]); 850 unset($for_update[strtolower($table)]); 851 } else { 852 // This table exists in the database, but not in the creation queries? 853 } 854 } 855 } 856 857 $allqueries = array_merge($cqueries, $iqueries); 858 if($execute) { 859 foreach($allqueries as $query) { 860 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; 861 $wpdb->query($query); 862 } 863 } 864 865 return $for_update; 866 } 867 868 function make_db_current() { 869 global $wp_queries; 870 871 $alterations = dbDelta($wp_queries); 872 echo "<ol>\n"; 873 foreach($alterations as $alteration) echo "<li>$alteration</li>\n"; 874 echo "</ol>\n"; 875 } 876 877 function make_db_current_silent() { 878 global $wp_queries; 879 880 $alterations = dbDelta($wp_queries); 881 } 882 883 function make_site_theme_from_oldschool($theme_name, $template) { 884 $home_path = get_home_path(); 885 $site_dir = ABSPATH . "wp-content/themes/$template"; 886 887 if (! file_exists("$home_path/index.php")) 888 return false; 889 890 // Copy files from the old locations to the site theme. 891 // TODO: This does not copy arbitarary include dependencies. Only the 892 // standard WP files are copied. 893 $files = array('index.php' => 'index.php', 'wp-layout.css' => 'style.css', 'wp-comments.php' => 'comments.php', 'wp-comments-popup.php' => 'comments-popup.php'); 894 895 foreach ($files as $oldfile => $newfile) { 896 if ($oldfile == 'index.php') 897 $oldpath = $home_path; 898 else 899 $oldpath = ABSPATH; 900 901 if ($oldfile == 'index.php') { // Check to make sure it's not a new index 902 $index = implode('', file("$oldpath/$oldfile")); 903 if ( strstr( $index, 'WP_USE_THEMES' ) ) { 904 if (! @copy(ABSPATH . 'wp-content/themes/default/index.php', "$site_dir/$newfile")) 905 return false; 906 continue; // Don't copy anything 907 } 908 } 909 910 if (! @copy("$oldpath/$oldfile", "$site_dir/$newfile")) 911 return false; 912 913 chmod("$site_dir/$newfile", 0777); 914 915 // Update the blog header include in each file. 916 $lines = explode("\n", implode('', file("$site_dir/$newfile"))); 917 if ($lines) { 918 $f = fopen("$site_dir/$newfile", 'w'); 919 920 foreach ($lines as $line) { 921 if (preg_match('/require.*wp-blog-header/', $line)) 922 $line = '//' . $line; 923 924 // Update stylesheet references. 925 $line = str_replace("<?php echo __get_option('siteurl'); ?>/wp-layout.css", "<?php bloginfo('stylesheet_url'); ?>", $line); 926 927 // Update comments template inclusion. 928 $line = str_replace("<?php include(ABSPATH . 'wp-comments.php'); ?>", "<?php comments_template(); ?>", $line); 929 930 fwrite($f, "{$line}\n"); 931 } 932 fclose($f); 933 } 934 } 935 936 // Add a theme header. 937 $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"; 938 939 $stylelines = file_get_contents("$site_dir/style.css"); 940 if ($stylelines) { 941 $f = fopen("$site_dir/style.css", 'w'); 942 943 fwrite($f, $header); 944 fwrite($f, $stylelines); 945 fclose($f); 946 } 947 948 return true; 949 } 950 951 function make_site_theme_from_default($theme_name, $template) { 952 $site_dir = ABSPATH . "wp-content/themes/$template"; 953 $default_dir = ABSPATH . 'wp-content/themes/default'; 954 955 // Copy files from the default theme to the site theme. 956 //$files = array('index.php', 'comments.php', 'comments-popup.php', 'footer.php', 'header.php', 'sidebar.php', 'style.css'); 957 958 $theme_dir = @ dir("$default_dir"); 959 if ($theme_dir) { 960 while(($theme_file = $theme_dir->read()) !== false) { 961 if (is_dir("$default_dir/$theme_file")) 962 continue; 963 if (! @copy("$default_dir/$theme_file", "$site_dir/$theme_file")) 964 return; 965 chmod("$site_dir/$theme_file", 0777); 966 } 967 } 968 969 // Rewrite the theme header. 970 $stylelines = explode("\n", implode('', file("$site_dir/style.css"))); 971 if ($stylelines) { 972 $f = fopen("$site_dir/style.css", 'w'); 973 974 foreach ($stylelines as $line) { 975 if (strstr($line, "Theme Name:")) $line = "Theme Name: $theme_name"; 976 elseif (strstr($line, "Theme URI:")) $line = "Theme URI: " . __get_option('siteurl'); 977 elseif (strstr($line, "Description:")) $line = "Description: Your theme"; 978 elseif (strstr($line, "Version:")) $line = "Version: 1"; 979 elseif (strstr($line, "Author:")) $line = "Author: You"; 980 fwrite($f, "{$line}\n"); 981 } 982 fclose($f); 983 } 984 985 // Copy the images. 986 umask(0); 987 if (! mkdir("$site_dir/images", 0777)) { 988 return false; 989 } 990 991 $images_dir = @ dir("$default_dir/images"); 992 if ($images_dir) { 993 while(($image = $images_dir->read()) !== false) { 994 if (is_dir("$default_dir/images/$image")) 995 continue; 996 if (! @copy("$default_dir/images/$image", "$site_dir/images/$image")) 997 return; 998 chmod("$site_dir/images/$image", 0777); 999 } 1000 } 1001 } 1002 1003 // Create a site theme from the default theme. 1004 function make_site_theme() { 1005 // Name the theme after the blog. 1006 $theme_name = __get_option('blogname'); 1007 $template = sanitize_title($theme_name); 1008 $site_dir = ABSPATH . "wp-content/themes/$template"; 1009 1010 // If the theme already exists, nothing to do. 1011 if ( is_dir($site_dir)) { 1012 return false; 1013 } 1014 1015 // We must be able to write to the themes dir. 1016 if (! is_writable(ABSPATH . "wp-content/themes")) { 1017 return false; 1018 } 1019 1020 umask(0); 1021 if (! mkdir($site_dir, 0777)) { 1022 return false; 1023 } 1024 1025 if (file_exists(ABSPATH . 'wp-layout.css')) { 1026 if (! make_site_theme_from_oldschool($theme_name, $template)) { 1027 // TODO: rm -rf the site theme directory. 1028 return false; 1029 } 1030 } else { 1031 if (! make_site_theme_from_default($theme_name, $template)) 1032 // TODO: rm -rf the site theme directory. 1033 return false; 1034 } 1035 1036 // Make the new site theme active. 1037 $current_template = __get_option('template'); 1038 if ($current_template == 'default') { 1039 update_option('template', $template); 1040 update_option('stylesheet', $template); 1041 } 1042 return $template; 1043 } 1044 1045 function translate_level_to_role($level) { 1046 switch ($level) { 1047 case 10: 1048 case 9: 1049 case 8: 1050 return 'administrator'; 1051 case 7: 1052 case 6: 1053 case 5: 1054 return 'editor'; 1055 case 4: 1056 case 3: 1057 case 2: 1058 return 'author'; 1059 case 1: 1060 return 'contributor'; 1061 case 0: 1062 return 'subscriber'; 1063 } 1064 } 1065 1066 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |