Make WordPress Core

Ticket #4189: terms.2.diff

File terms.2.diff, 15.4 KB (added by ryan, 18 years ago)
  • wp-includes/wp-db.php

     
    3434        var $optiongroups;
    3535        var $optiongroup_options;
    3636        var $postmeta;
     37        var $terms;
     38        var $term_taxonomy;
     39        var $term_relationships;
    3740
    3841        var $charset;
    3942        var $collate;
  • wp-includes/taxonomy.php

     
     1<?php
     2
     3/**
     4 * Adds a new term to the database.  Optionally marks it as an alias of an existing term.
     5 * @param string $term The term to add.
     6 * @param string $taxonomy The taxonomy to which to add the term
     7 * @param int|string $alias_of The id or slug of the new term's alias.
     8 */
     9function add_term( $term, $taxonomy, $args = array() ) {
     10        global $wpdb;
     11        $term_slug = sanitize_title($term);
     12        $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0);
     13        $args = wp_parse_args($args, $defaults);
     14        extract($args);
     15
     16        $term_group = 0;       
     17        if ($alias_of) {
     18                $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE term_slug = '$alias_of'");
     19                if ($alias->term_group) {
     20                        // The alias we want is already in a group, so let's use that one.
     21                        $term_group = $alias->term_group;
     22                } else {
     23                        // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     24                        $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1;
     25                        $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id");
     26                }
     27        }
     28
     29        // Now add or replace the term.  This works because we have a UNIQUE key on term_slug.
     30        $wpdb->query("REPLACE INTO $wpdb->terms (term_name, term_slug, term_group) VALUES ('$term', '$term_slug', '$term_group')");
     31        $term_id = (int) $wpdb->insert_id;
     32       
     33        $tt_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND t.term_id = $term_id");
     34
     35        if ( ! empty($tt_id) )
     36                return $tt_id;
     37                       
     38        $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, term_description, parent, count) VALUES ('$term_id', '$taxonomy', '$description', '$parent', '0')");
     39}
     40
     41/**
     42 * Removes a term from the database.
     43 */
     44function remove_term() {}
     45       
     46       
     47/**
     48 * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
     49 */
     50function is_term($term, $taxonomy = '') {
     51        global $wpdb;
     52        $term = sanitize_title($term);
     53        return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_slug = '$term'");
     54}
     55       
     56/**
     57 * Given an array of terms, returns those that are defined term slugs.  Ignores integers.
     58 * @param array $terms The term slugs to check for a definition.
     59 */
     60function get_defined_terms($terms) {
     61        global $wpdb;
     62
     63        foreach ($terms as $term) {
     64                if (!is_int($term)) {
     65                        $searches[] = $term;
     66                }
     67        }
     68        $terms = "'" . implode("', '", $searches) . "'";
     69        return $wpdb->get_col("SELECT term_slug FROM $wpdb->terms WHERE term_slug IN ($terms)");
     70}
     71       
     72/**
     73 * Relates an object (post, link etc) to a term and taxonomy type.  Creates the term and taxonomy
     74 * relationship if it doesn't already exist.  Creates a term if it doesn't exist (using the term_slug).
     75 * @param array|int|string $term The slug or id of the term.
     76 * @param int $object_id The object to relate to.
     77 * @param array|string $taxonomies The context(s) in which to relate the term to the object.
     78 */
     79function add_term_relationship($terms, $object_id, $taxonomies) {
     80        global $wpdb;
     81               
     82        if (!is_array($taxonomies)) {
     83                $taxonomies = array($taxonomies);
     84        }
     85       
     86        if (!is_array($terms)) {
     87                $terms = array($terms);
     88        }
     89               
     90        $defined_terms = get_defined_terms($terms);
     91
     92        foreach ($terms as $term) {
     93                if (!is_int($term)) {
     94                        if (!isset($defined_terms[$term])) {
     95                                $new_terms[] = $term;
     96                        }
     97                        $term_slugs[] = $term;
     98                } else {
     99                        $term_ids[] = $term;
     100                }
     101        }
     102
     103        $term_clause = isset($term_ids) ? 'tt.term_id IN (' . implode(', ', $term_ids) . ')' : '';
     104        if (isset($term_slugs)) {
     105                if ($term_clause) {
     106                        $term_clause .= ' OR ';
     107                }
     108                $term_clause .= "t.term_slug IN ('" . implode("', '", $term_slugs) . "')";
     109                $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id";
     110        } else {
     111                $term_join = '';
     112        }
     113               
     114        // Now add or increment the term taxonomy relationships.  This is inefficient at the moment.
     115        foreach ($taxonomies as $taxonomy) {
     116                foreach ($terms as $term) {
     117                        add_term($term, $taxonomy);
     118                }
     119        }
     120               
     121        $taxonomies = "'" . implode("', '", $taxonomies) . "'";
     122               
     123        // Finally, relate the term and taxonomy to the object.
     124        $wpdb->query("INSERT INTO $wpdb->term_relationships(object_id, term_taxonomy_id) SELECT '$object_id', term_taxonomy_id FROM $wpdb->term_taxonomy AS tt $term_join WHERE ($term_clause) AND tt.taxonomy IN ($taxonomies)");
     125}
     126       
     127/**
     128 * Returns the terms associated with the given object(s), in the supplied taxonomies.
     129 * @param int|array $object_id The id of the object(s)) to retrieve for.
     130 * @param string|array $taxonomies The taxonomies to retrieve terms from.
     131 * @return array The requested term data.                       
     132 */
     133function get_object_terms($object_id, $taxonomy) {
     134        global $wpdb;
     135        $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
     136        $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
     137
     138        $taxonomies = "'" . implode("', '", $taxonomies) . "'";         
     139        $object_ids = implode(', ', $object_ids);               
     140
     141        if ( $taxonomy_data = $wpdb->get_results("SELECT t.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids)") ) {
     142                if ($single_taxonomy && $single_object) {
     143                        // Just one kind of taxonomy for one object.
     144                        return $taxonomy_data;
     145                } else {
     146                        foreach ($taxonomy_data as $data) {
     147                                if ($single_taxonomy) {
     148                                        // Many objects, one taxonomy type.
     149                                        $return[$data->object_id][] = $data;
     150                                } elseif ($single_object) {
     151                                        // One object, many taxonomies.
     152                                        $return[$data->taxonomy][] = $data;
     153                                } else {
     154                                        // Many objects, many taxonomies.
     155                                        $return[$data->object_id][$data->taxonomy][] = $data;
     156                                }
     157                        }
     158                        return $return;                 
     159                }
     160        } else {
     161                return array();
     162        }               
     163}       
     164
     165?>
  • wp-includes/version.php

     
    33// This holds the version number in a separate file so we can bump it without cluttering the SVN
    44
    55$wp_version = '2.3-alpha';
    6 $wp_db_version = 5200;
     6$wp_db_version = 5495;
    77
    88?>
  • wp-includes/post.php

     
    459459
    460460        $post_id = (int) $post_id;
    461461       
    462         if ( !isset( $tag_cache[$blog_id][$post_id] ) )
    463                 update_post_category_cache( $post_id ); // loads $tag_cache
    464 
    465         return $tag_cache[$blog_id][$post_id];
     462        $tags = get_object_terms($post_id, 'post_tag');
     463        return $tags;
    466464}
    467465
    468466function wp_get_recent_posts($num = 10) {
     
    792790       
    793791        if ( !$post_id )
    794792                return false;
    795        
    796         // prevent warnings for unintialized variables
    797         $tag_ids = array();
    798793
    799794        if ( empty($tags) )
    800795                $tags = array();
    801796        $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
    802        
    803         foreach ( $tags as $tag ) {
    804                 $tag = trim( $tag );
    805                 if ( !$tag_slug = sanitize_title( $tag ) )
    806                         continue; // discard
    807                 if ( !$tag_id = tag_exists( $tag ) )
    808                         $tag_id = wp_create_tag( $tag );
    809                 $tag_ids[] = $tag_id;
    810         }
    811 
    812         if ( empty($tag_ids) && ( !empty($tags) || $append ) )
    813                 return false;
    814        
    815         $tag_ids = array_unique( $tag_ids );
    816        
    817         // First the old tags
    818         $old_tags = $wpdb->get_col("
    819                 SELECT category_id
    820                 FROM $wpdb->post2cat
    821                 WHERE post_id = '$post_id' AND rel_type = 'tag'");
    822        
    823         if ( !$old_tags ) {
    824                 $old_tags = array();
    825         } else {
    826                 $old_tags = array_unique( $old_tags );
    827         }
    828        
    829         // Delete any?
    830         $delete_tags = array_diff( $old_tags, $tag_ids);
    831         if ( $delete_tags && !$append ) {
    832                 foreach ( $delete_tags as $del ) {
    833                         $wpdb->query("
    834                                 DELETE FROM $wpdb->post2cat
    835                                 WHERE category_id = '$del'
    836                                         AND post_id = '$post_id'
    837                                         AND rel_type = 'tag'
    838                                 ");
    839                 }
    840         }
    841        
    842         // Add any?
    843         $add_tags = array_diff( $tag_ids, $old_tags );
    844         if ( $add_tags ) {
    845                 foreach ( $add_tags as $new_tag ) {
    846                         $new_tag = (int) $new_tag;
    847                         if ( !empty($new_tag) )
    848                                 $wpdb->query("
    849                                         INSERT INTO $wpdb->post2cat (post_id, category_id, rel_type)
    850                                         VALUES ('$post_id', '$new_tag', 'tag')");
    851                 }
    852         }
    853        
    854         // Update category counts.
    855         $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) );
    856         foreach ( $all_affected_tags as $tag_id ) {
    857                 $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$tag_id' AND rel_type = 'tag'" );
    858                 $wpdb->query( "UPDATE $wpdb->categories SET tag_count = '$count', type = type | " . TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" );
    859                 if ( $count == 0 )
    860                         $wpdb->query( "UPDATE $wpdb->categories SET type = type & ~". TAXONOMY_TAG . " WHERE cat_ID = '$tag_id'" );
    861                 clean_category_cache( $tag_id );
    862                 do_action( 'edit_category', $tag_id );
    863                 do_action( 'edit_tag', $tag_id );
    864         }
     797        add_term_relationship($tags, $post_id, 'post_tag');
    865798}
    866799
    867800function wp_set_post_categories($post_ID = 0, $post_categories = array()) {
  • wp-settings.php

     
    116116$wpdb->options        = $wpdb->prefix . 'options';
    117117$wpdb->postmeta       = $wpdb->prefix . 'postmeta';
    118118$wpdb->usermeta       = $wpdb->prefix . 'usermeta';
     119$wpdb->terms          = $wpdb->prefix . 'terms';
     120$wpdb->term_taxonomy  = $wpdb->prefix . 'term_taxonomy';
     121$wpdb->term_relationships = $wpdb->prefix . 'term_relationships';
    119122
    120123if ( defined('CUSTOM_USER_TABLE') )
    121124        $wpdb->users = CUSTOM_USER_TABLE;
     
    168171require (ABSPATH . WPINC . '/version.php');
    169172require (ABSPATH . WPINC . '/deprecated.php');
    170173require (ABSPATH . WPINC . '/script-loader.php');
     174require (ABSPATH . WPINC . '/taxonomy.php');
    171175
    172176if (strpos($_SERVER['PHP_SELF'], 'install.php') === false) {
    173177    // Used to guarantee unique hash cookies
  • wp-admin/admin-functions.php

     
    664664        if ( !$post_id )
    665665                return false;
    666666
    667         $tags = $wpdb->get_results( "
    668                      SELECT category_id, cat_name
    669                      FROM $wpdb->categories, $wpdb->post2cat
    670                      WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$post_id' AND rel_type = 'tag'
    671                      " );
     667        $tags = wp_get_post_tags($post_id);
     668
    672669        if ( !$tags )
    673670                return false;
    674671
    675672        foreach ( $tags as $tag )
    676                 $tag_names[] = $tag->cat_name;
     673                $tag_names[] = $tag->term_name;
    677674        $tags_to_edit = join( ', ', $tag_names );
    678675        $tags_to_edit = attribute_escape( $tags_to_edit );
    679676        $tags_to_edit = apply_filters( 'tags_to_edit', $tags_to_edit );
  • wp-admin/admin-db.php

     
    286286        if (! $tag_nicename = sanitize_title($tag_name))
    287287                return 0;
    288288
    289         return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '$tag_nicename' AND ( type & " . TAXONOMY_TAG .  " != 0 )");
     289        return is_term($tag_name, 'post_tag');
    290290}
    291291
    292292function wp_create_tag($tag_name) {
    293293        if ( $id = tag_exists($tag_name) )
    294294                return $id;
    295         $tag_array = array('cat_name' => $tag_name, 'type' => TAXONOMY_TAG);
    296295
    297         if ( $id = category_object_exists($tag_name) ) {
    298                 $category = get_category($id);
    299                 $tag_array['type'] = $category->type | $tag_array['type'];
    300                 $tag_array['cat_ID'] = $id;
    301                 $id = wp_update_category($tag_array);
    302                 return $id;
    303         } else {
    304                 return wp_insert_category($tag_array);
    305         }
     296        $tag_id = add_term($tag_name, 'post_tag');     
    306297}
    307298
    308299function wp_delete_user($id, $reassign = 'novalue') {
  • wp-admin/upgrade-schema.php

     
    1010                $charset_collate .= " COLLATE $wpdb->collate";
    1111}
    1212
    13 $wp_queries="CREATE TABLE $wpdb->categories (
    14   cat_ID bigint(20) NOT NULL auto_increment,
    15   cat_name varchar(55) NOT NULL default '',
    16   category_nicename varchar(200) NOT NULL default '',
    17   category_description longtext NOT NULL,
    18   category_parent bigint(20) NOT NULL default '0',
    19   category_count bigint(20) NOT NULL default '0',
    20   link_count bigint(20) NOT NULL default '0',
    21   tag_count bigint(20) NOT NULL default '0',
    22   posts_private tinyint(1) NOT NULL default '0',
    23   links_private tinyint(1) NOT NULL default '0',
    24   type tinyint NOT NULL default '1',
    25   PRIMARY KEY  (cat_ID),
    26   KEY category_nicename (category_nicename)
     13$wp_queries="CREATE TABLE $wpdb->terms (
     14 term_id bigint(20) NOT NULL auto_increment,
     15 term_name varchar(55) NOT NULL default '',
     16 term_slug varchar(200) NOT NULL default '',
     17 term_group bigint(10) NOT NULL default 0,
     18 PRIMARY KEY  (term_id),
     19 UNIQUE KEY term_slug (term_slug)
    2720) $charset_collate;
     21CREATE TABLE $wpdb->term_taxonomy (
     22 term_taxonomy_id bigint(20) NOT NULL auto_increment,
     23 term_id bigint(20) NOT NULL default 0,
     24 taxonomy varchar(32) NOT NULL default '',
     25 term_description longtext NOT NULL,
     26 parent bigint(20) NOT NULL default 0,
     27 count bigint(20) NOT NULL default 0,
     28 PRIMARY KEY (term_taxonomy_id),
     29 UNIQUE KEY (term_id, taxonomy)
     30) $charset_collate;
     31CREATE TABLE $wpdb->term_relationships (
     32 object_id bigint(20) NOT NULL default 0,
     33 term_taxonomy_id bigint(20) NOT NULL default 0,
     34 PRIMARY KEY  (object_id),
     35 KEY (term_taxonomy_id)
     36) $charset_collate;
    2837CREATE TABLE $wpdb->comments (
    2938  comment_ID bigint(20) unsigned NOT NULL auto_increment,
    3039  comment_post_ID int(11) NOT NULL default '0',
     
    4554  KEY comment_approved (comment_approved),
    4655  KEY comment_post_ID (comment_post_ID)
    4756) $charset_collate;
    48 CREATE TABLE $wpdb->link2cat (
    49   rel_id bigint(20) NOT NULL auto_increment,
    50   link_id bigint(20) NOT NULL default '0',
    51   category_id bigint(20) NOT NULL default '0',
    52   PRIMARY KEY  (rel_id),
    53   KEY link_id (link_id,category_id)
    54 ) $charset_collate;
    5557CREATE TABLE $wpdb->links (
    5658  link_id bigint(20) NOT NULL auto_increment,
    5759  link_url varchar(255) NOT NULL default '',
     
    8688  PRIMARY KEY  (option_id,blog_id,option_name),
    8789  KEY option_name (option_name)
    8890) $charset_collate;
    89 CREATE TABLE $wpdb->post2cat (
    90   rel_id bigint(20) NOT NULL auto_increment,
    91   post_id bigint(20) NOT NULL default '0',
    92   category_id bigint(20) NOT NULL default '0',
    93   rel_type varchar(64) NOT NULL default 'category',
    94   PRIMARY KEY  (rel_id),
    95   KEY post_id (post_id,category_id)
    96 ) $charset_collate;
    9791CREATE TABLE $wpdb->postmeta (
    9892  meta_id bigint(20) NOT NULL auto_increment,
    9993  post_id bigint(20) NOT NULL default '0',
     
    404398        }
    405399}
    406400
    407 ?>
    408  No newline at end of file
     401?>