Make WordPress Core

Ticket #2659: comment_meta_002.diff

File comment_meta_002.diff, 12.6 KB (added by markjaquith, 17 years ago)

Take 2

  • wp-includes/version.php

     
    33// This just holds the version number, in a separate file so we can bump it without cluttering the SVN
    44
    55$wp_version = '2.1-alpha1';
    6 $wp_db_version = 3672;
     6$wp_db_version = 3673;
    77
    88?>
     9 No newline at end of file
  • wp-includes/functions-post.php

     
    574574
    575575        $wpdb->query("DELETE FROM $wpdb->posts WHERE ID = $postid");
    576576
     577        // A little extra effort here, but it saves us from having to store post_ids with comment meta
     578        $comment_ids = $wpdb->get_col("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = $postid");
     579        $comment_ids = implode(',', $comment_ids);
     580        $wpdb->query("DELETE FROM $wpdb->commentmeta WHERE comment_ID IN ($comment_ids)");
     581
    577582        $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_post_ID = $postid");
    578583
    579584        $wpdb->query("DELETE FROM $wpdb->post2cat WHERE post_id = $postid");
  • wp-includes/functions.php

     
    428428        return true;
    429429}
    430430
     431function setup_meta_vars($type = 'post') {
     432        global $wpdb;
     433        if ( $type != 'post' && $type != 'comment' )
     434                return false; // Garbage control
     435        $return = array();
     436        $return['table'] = $type . 'meta';
     437        $return['table'] = $wpdb->$return['table'];
     438        $return['id_col'] = $type . '_id';
     439        $return['cache'] = $type . '_meta_cache';
     440        return $return;
     441}
     442
     443function add_comment_meta($comment_id, $key, $value, $unique = false) {
     444        return wp_add_meta($comment_id, $key, $value, $unique, 'comment');
     445}
     446
    431447function add_post_meta($post_id, $key, $value, $unique = false) {
    432         global $wpdb, $post_meta_cache;
     448        return wp_add_meta($post_id, $key, $value, $unique, 'post');
     449}
    433450
     451function wp_add_meta($id, $key, $value, $unique = false, $type = 'post') {
     452        global $wpdb;
     453
     454        if ( $vars = setup_meta_vars($type) ) {
     455                extract($vars);
     456                global $$cache;
     457        } else {
     458                return false;
     459        }
     460
    434461        if ( $unique ) {
    435                 if ( $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
    436 = '$key' AND post_id = '$post_id'") ) {
     462                if ( $wpdb->get_var("SELECT meta_key FROM $table WHERE meta_key
     463= '$key' AND $id_col = '$id'") ) {
    437464                        return false;
    438465                }
    439466        }
     
    442469        if ( is_array($value) || is_object($value) )
    443470                $value = $wpdb->escape(serialize($value));
    444471
    445         $wpdb->query("INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value) VALUES ('$post_id','$key','$value')");
     472        $wpdb->query("INSERT INTO $table ($id_col,meta_key,meta_value) VALUES ('$id','$key','$value')");
    446473
    447         $post_meta_cache['$post_id'][$key][] = $original;
     474        ${$cache}['$id'][$key][] = $original;
    448475
    449476        return true;
    450477}
    451478
     479function delete_comment_meta($comment_id, $key, $value = '') {
     480        return wp_delete_meta($comment_id, $key, $value, 'comment');
     481}
     482
    452483function delete_post_meta($post_id, $key, $value = '') {
    453         global $wpdb, $post_meta_cache;
     484        return wp_delete_meta($post_id, $key, $value, 'post');
     485}
    454486
     487function wp_delete_meta($id, $key, $value = '', $type = 'post') {
     488        global $wpdb;
     489
     490        if ( $vars = setup_meta_vars($type) ) {
     491                extract($vars);
     492                global $$cache;
     493        } else {
     494                return false;
     495        }
     496
    455497        if ( empty($value) ) {
    456                 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
    457 post_id = '$post_id' AND meta_key = '$key'");
     498                $meta_id = $wpdb->get_var("SELECT meta_id FROM $table WHERE
     499$id_col = '$id' AND meta_key = '$key'");
    458500        } else {
    459                 $meta_id = $wpdb->get_var("SELECT meta_id FROM $wpdb->postmeta WHERE
    460 post_id = '$post_id' AND meta_key = '$key' AND meta_value = '$value'");
     501                $meta_id = $wpdb->get_var("SELECT meta_id FROM $table WHERE
     502$id_col = '$id' AND meta_key = '$key' AND meta_value = '$value'");
    461503        }
    462504
    463505        if ( !$meta_id )
    464506                return false;
    465507
    466508        if ( empty($value) ) {
    467                 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
     509                $wpdb->query("DELETE FROM $table WHERE $id_col = '$id'
    468510AND meta_key = '$key'");
    469                 unset($post_meta_cache['$post_id'][$key]);
     511                unset(${$cache}['$id'][$key]);
    470512        } else {
    471                 $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
     513                $wpdb->query("DELETE FROM $table WHERE $id_col = '$id'
    472514AND meta_key = '$key' AND meta_value = '$value'");
    473                 $cache_key = $post_meta_cache['$post_id'][$key];
     515                $cache_key = ${$cache}['$id'][$key];
    474516                if ($cache_key) foreach ( $cache_key as $index => $data )
    475517                        if ( $data == $value )
    476                                 unset($post_meta_cache['$post_id'][$key][$index]);
     518                                unset(${$cache}['$id'][$key][$index]);
    477519        }
    478520
    479         unset($post_meta_cache['$post_id'][$key]);
     521        unset(${$cache}['$id'][$key]);
    480522
    481523        return true;
    482524}
    483525
     526function get_comment_meta($comment_id, $key, $single = false) {
     527        return wp_get_meta($comment_id, $key, $single, 'comment');
     528}
     529
    484530function get_post_meta($post_id, $key, $single = false) {
    485         global $wpdb, $post_meta_cache;
     531        return wp_get_meta($post_id, $key, $single, 'post');
     532}
    486533
    487         if ( isset($post_meta_cache[$post_id][$key]) ) {
     534function wp_get_meta($id, $key, $single = false, $type = 'post') {
     535        global $wpdb;
     536
     537        if ( $vars = setup_meta_vars($type) ) {
     538                extract($vars);
     539                global $$cache;
     540        } else {
     541                return false;
     542        }
     543
     544        if ( isset(${$cache}[$id][$key]) ) {
    488545                if ( $single ) {
    489                         return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
     546                        return maybe_unserialize( ${$cache}[$id][$key][0] );
    490547                } else {
    491                         return maybe_unserialize( $post_meta_cache[$post_id][$key] );
     548                        return maybe_unserialize( ${$cache}[$id][$key] );
    492549                }
    493550        }
    494551
    495         $metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
     552        $metalist = $wpdb->get_results("SELECT meta_value FROM $table WHERE $id_col = '$id' AND meta_key = '$key'", ARRAY_N);
    496553
    497554        $values = array();
    498555        if ( $metalist ) {
     
    514571        return maybe_unserialize($return);
    515572}
    516573
     574function update_comment_meta($comment_id, $key, $value, $prev_value = '') {
     575        return wp_update_meta($comment_id, $key, $value, $prev_value, 'comment');
     576}
     577
    517578function update_post_meta($post_id, $key, $value, $prev_value = '') {
    518         global $wpdb, $post_meta_cache;
     579        return wp_update_meta($post_id, $key, $value, $prev_value, 'post');
     580}
    519581
     582function wp_update_meta($id, $key, $value, $prev_value = '', $type = 'post') {
     583        global $wpdb;
     584
     585        if ( $vars = setup_meta_vars($type) ) {
     586                extract($vars);
     587                global $$cache;
     588        } else {
     589                return false;
     590        }
     591
    520592        $original_value = $value;
    521593        if ( is_array($value) || is_object($value) )
    522594                $value = $wpdb->escape(serialize($value));
     
    525597        if ( is_array($prev_value) || is_object($prev_value) )
    526598                $prev_value = $wpdb->escape(serialize($prev_value));
    527599
    528         if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
    529 = '$key' AND post_id = '$post_id'") ) {
     600        if (! $wpdb->get_var("SELECT meta_key FROM $table WHERE meta_key
     601= '$key' AND $id_col = '$id'") ) {
    530602                return false;
    531603        }
    532604
    533605        if ( empty($prev_value) ) {
    534                 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
    535 meta_key = '$key' AND post_id = '$post_id'");
    536                 $cache_key = $post_meta_cache['$post_id'][$key];
     606                $wpdb->query("UPDATE $table SET meta_value = '$value' WHERE
     607meta_key = '$key' AND $id_col = '$id'");
     608                $cache_key = ${$cache}['$id'][$key];
    537609                if ( !empty($cache_key) )
    538610                        foreach ($cache_key as $index => $data)
    539                                 $post_meta_cache['$post_id'][$key][$index] = $original_value;
     611                                ${$cache}['$id'][$key][$index] = $original_value;
    540612        } else {
    541                 $wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
    542 meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
    543                 $cache_key = $post_meta_cache['$post_id'][$key];
     613                $wpdb->query("UPDATE $table SET meta_value = '$value' WHERE
     614meta_key = '$key' AND $id_col = '$id' AND meta_value = '$prev_value'");
     615                $cache_key = ${$cache}['$id'][$key];
    544616                if ( !empty($cache_key) )
    545617                        foreach ($cache_key as $index => $data)
    546618                                if ( $data == $original_prev )
    547                                         $post_meta_cache['$post_id'][$key][$index] = $original_value;
     619                                        ${$cache}['$id'][$key][$index] = $original_value;
    548620        }
    549621
    550622        return true;
  • wp-includes/comment-functions.php

     
    33// Template functions
    44
    55function comments_template( $file = '/comments.php' ) {
    6         global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $user_login, $user_ID, $user_identity;
     6        global $wp_query, $withcomments, $post, $wpdb, $id, $comment, $comment_meta_cache, $user_login, $user_ID, $user_identity;
    77
    88        if ( is_single() || is_page() || $withcomments ) :
    99                $req = get_settings('require_name_email');
     
    3434                $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_post_ID = '$post->ID' AND ( comment_approved = '1' OR ( comment_author = '$author_db' AND comment_author_email = '$email_db' AND comment_approved = '0' ) ) ORDER BY comment_date");
    3535        }
    3636
     37        // Comment Meta
     38        if ( $comments ) {
     39                $cid_list = array();
     40                foreach ( $comments as $c )
     41                        $cid_list[] =  $c->comment_ID;
     42                $cid_list = implode(',', $cid_list);
     43
     44                // Get comment-meta info
     45                if ( $meta_list = $wpdb->get_results("SELECT comment_id, meta_key, meta_value FROM $wpdb->commentmeta WHERE comment_id IN($cid_list) ORDER BY comment_id, meta_key", ARRAY_A) ) {
     46                        // Change from flat structure to hierarchical:
     47                        $comment_meta_cache = array();
     48                        foreach ($meta_list as $metarow) {
     49                                $mpid = $metarow['comment_id'];
     50                                $mkey = $metarow['meta_key'];
     51                                $mval = $metarow['meta_value'];
     52
     53                                // Force subkeys to be array type:
     54                                if ( !isset($comment_meta_cache[$mpid]) || !is_array($comment_meta_cache[$mpid]) )
     55                                        $comment_meta_cache[$mpid] = array();
     56                                if ( !isset($comment_meta_cache[$mpid]["$mkey"]) || !is_array($comment_meta_cache[$mpid]["$mkey"]) )
     57                                        $comment_meta_cache[$mpid]["$mkey"] = array();
     58
     59                                // Add a value to the current pid/key:
     60                                $comment_meta_cache[$mpid][$mkey][] = $mval;
     61                        }
     62                }
     63        }
     64
    3765        define('COMMENTS_TEMPLATE', true);
    3866        $include = apply_filters('comments_template', TEMPLATEPATH . $file );
    3967        if ( file_exists( $include ) )
     
    222250        if ( ! $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1") )
    223251                return false;
    224252
     253        $wpdb->query("DELETE FROM $wpdb->commentmeta WHERE comment_id = '$comment_id'");
     254
    225255        $post_id = $comment->comment_post_ID;
    226256        if ( $post_id && $comment->comment_approved == 1 )
    227257                $wpdb->query( "UPDATE $wpdb->posts SET comment_count = comment_count - 1 WHERE ID = '$post_id'" );
  • wp-content/plugins/wp-db-backup.php

     
    3131        var $backup_errors = array();
    3232        var $basename;
    3333        // Simple table name storage
    34         var     $wp_table_names = array('categories','comments','link2cat','links','options','post2cat','postmeta','posts','users','usermeta');
     34        var     $wp_table_names = array('categories','comments','commentmeta','link2cat','links','options','post2cat','postmeta','posts','users','usermeta');
    3535
    3636        function gzip() {
    3737                return function_exists('gzopen');
  • wp-settings.php

     
    7777$wpdb->categories       = $table_prefix . 'categories';
    7878$wpdb->post2cat         = $table_prefix . 'post2cat';
    7979$wpdb->comments         = $table_prefix . 'comments';
     80$wpdb->commentmeta      = $table_prefix . 'commentmeta';
    8081$wpdb->link2cat         = $table_prefix . 'link2cat';
    8182$wpdb->links            = $table_prefix . 'links';
    8283$wpdb->linkcategories   = $table_prefix . 'linkcategories';
  • wp-admin/upgrade-schema.php

     
    1414  PRIMARY KEY  (cat_ID),
    1515  KEY category_nicename (category_nicename)
    1616);
     17CREATE TABLE $wpdb->commentmeta (
     18  meta_id bigint(20) NOT NULL auto_increment,
     19  comment_id bigint(20) NOT NULL default '0',
     20  meta_key varchar(255) default NULL,
     21  meta_value longtext,
     22  PRIMARY KEY  (meta_id),
     23  KEY post_id (comment_id),
     24  KEY meta_key (meta_key)
     25);
    1726CREATE TABLE $wpdb->comments (
    1827  comment_ID bigint(20) unsigned NOT NULL auto_increment,
    1928  comment_post_ID int(11) NOT NULL default '0',