Make WordPress Core

Ticket #3723: tagging.diff

File tagging.diff, 8.1 KB (added by ryan, 18 years ago)

Draft tag schema and API

  • wp-includes/wp-db.php

     
    3434        var $optiongroups;
    3535        var $optiongroup_options;
    3636        var $postmeta;
     37        var $tags;
     38        var $tagged;
    3739
    3840        /**
    3941         * Connects to the database server and selects a database
  • wp-includes/tag.php

     
     1<?php
     2
     3// TODO: Split these across tag.php, tag-template.php, and admin-db.php
     4
     5
     6function get_post_tags ( $post_id = false ) {
     7        global $post_tag_cache, $wpdb, $post;
     8
     9        if ( empty($post_id) )
     10                $post_id = $post->ID;
     11
     12        if ( isset ($post_tag_cache[$post_id] ) )
     13                return $post_tag_cache[$post_id];
     14
     15        $post_tag_cache[$post_id] = $wpdb->get_results("SELECT * FROM $wpdb->tagged RIGHT JOIN $wpdb->tags ON ($wpdb->tags.tag_id = $wpdb->tagged.tag_id) WHERE post_id = '$post_id'");
     16       
     17        return $post_tag_cache[$post_id];
     18}
     19
     20function &get_tag(&$tag, $output = OBJECT) {
     21        global $wpdb;
     22
     23        if ( empty($tag) )
     24                return null;
     25
     26        if ( is_object($tag) ) {
     27                wp_cache_add($tag->tag_id, $tag, 'tag');
     28                $_tag = $tag;
     29        } else {
     30                if ( ! $_tag = wp_cache_get($tag, 'tag') ) {
     31                        $_tag = $wpdb->get_row("SELECT * FROM $wpdb->tags WHERE tag_id = '$tag' LIMIT 1");
     32                        wp_cache_set($tag, $_tag, 'tag');
     33                }
     34        }
     35
     36        $_tag = apply_filters('get_tag', $_tag);
     37
     38        if ( $output == OBJECT ) {
     39                return $_tag;
     40        } elseif ( $output == ARRAY_A ) {
     41                return get_object_vars($_tag);
     42        } elseif ( $output == ARRAY_N ) {
     43                return array_values(get_object_vars($_tag));
     44        } else {
     45                return $_tag;
     46        }
     47}
     48
     49function get_tag_id($tag_name) {
     50        global $wpdb;
     51
     52        $tag_id = $wpdb->get_var("SELECT tag_id FROM $wpdb->tags WHERE raw_tag = '$tag_name'");
     53
     54        return $tag_id;
     55}
     56
     57function get_tag_by_name() {
     58
     59}
     60
     61function get_tag_name($tag_id) {
     62        $tag_id = (int) $tag_id;
     63        $tag = &get_tag($tag_id);
     64        return $tag->raw_tag;
     65}
     66
     67function get_tag_link() {
     68       
     69}
     70
     71// Template funcs
     72
     73function get_the_tag_list() {
     74       
     75}
     76
     77function the_tags() {
     78       
     79}
     80
     81function wp_list_tags() {
     82       
     83}
     84
     85// Admin funcs
     86
     87function wp_create_tag( $tag ) {
     88        global $wpdb;
     89        $raw_tag = $tag;
     90        $tag = sanitize_title( $tag );
     91        if ( empty( $tag ) )
     92                return false;
     93        if ( $exists = $wpdb->get_var("SELECT tag_id FROM $wpdb->tags WHERE tag = '$tag'") )
     94                return $exists;
     95
     96        $wpdb->query("INSERT INTO $wpdb->tags ( tag, raw_tag ) VALUES ( '$tag', '$raw_tag' )");
     97        $tag_id = $wpdb->insert_id;
     98        do_action('wp_tag_created', $raw_tag, $tag_id);
     99        return $tag_id;
     100}
     101
     102function wp_set_post_tags($post_ID = 0, $post_tags = array()) {
     103        global $wpdb;
     104        if (!is_array($post_tags) || 0 == count($post_tags) || empty($post_tags))
     105                $post_tags = array();
     106
     107        $post_tags = array_unique($post_tags);
     108
     109        // First the old tags
     110        $old_tags = $wpdb->get_col("
     111                SELECT tag_id
     112                FROM $wpdb->tagged
     113                WHERE post_id = $post_ID");
     114
     115        if (!$old_tags) {
     116                $old_tags = array();
     117        } else {
     118                $old_tags = array_unique($old_tags);
     119        }
     120
     121        // Delete any?
     122        $delete_tags = array_diff($old_tags,$post_tags);
     123
     124        if ($delete_tags) {
     125                foreach ($delete_tags as $del) {
     126                        $wpdb->query("
     127                                DELETE FROM $wpdb->tagged
     128                                WHERE tag_id = $del
     129                                        AND post_id = $post_ID
     130                                ");
     131                }
     132        }
     133
     134        // Add any?
     135        $add_tags = array_diff($post_tags, $old_tags);
     136
     137        if ($add_tags) {
     138                foreach ($add_tags as $new_tag) {
     139                        if ( !empty($new_tag) )
     140                                $wpdb->query("
     141                                        INSERT INTO $wpdb->taggewd (post_id, tag_id)
     142                                        VALUES ($post_ID, $new_tag)");
     143                }
     144        }
     145
     146        // Update tag counts.
     147        $all_affected_tags = array_unique(array_merge($post_tags, $old_tags));
     148        foreach ( $all_affected_tags as $tag_id ) {
     149                $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->tagged, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->tagged.post_id AND post_status = 'publish' AND post_type = 'post' AND tag_id = '$tag_id'");
     150                $wpdb->query("UPDATE $wpdb->tags SET tag_count = '$count' WHERE tag_id = '$tag_id'");
     151                clean_tag_cache($tag_id);
     152                do_action('edit_tag', $tag_id);
     153        }
     154}
     155
     156?>
  • wp-includes/post.php

     
    522522        }
    523523        $post_cat = $post_category[0];
    524524
     525        if ( empty($post_tags) )
     526                $post_tags = array();
     527
    525528        if ( empty($post_author) )
    526529                $post_author = $user_ID;
    527530
     
    646649        }
    647650
    648651        wp_set_post_categories($post_ID, $post_category);
     652        wp_set_post_tags($post_ID, $post_tags);
    649653
    650654        if ( 'page' == $post_type ) {
    651655                clean_page_cache($post_ID);
  • 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.2-bleeding';
    6 $wp_db_version = 4772;
     6$wp_db_version = 4847;
    77
    88?>
  • wp-settings.php

     
    116116$wpdb->options        = $wpdb->prefix . 'options';
    117117$wpdb->postmeta       = $wpdb->prefix . 'postmeta';
    118118$wpdb->usermeta       = $wpdb->prefix . 'usermeta';
     119$wpdb->tags           = $wpdb->prefix . 'tags';
     120$wpdb->tagged         = $wpdb->prefix . 'tagged';
    119121
    120122if ( defined('CUSTOM_USER_TABLE') )
    121123        $wpdb->users = CUSTOM_USER_TABLE;
  • wp-admin/edit-form-advanced.php

     
    7575<ul id="categorychecklist"><?php dropdown_categories(); ?></ul></div>
    7676</fieldset>
    7777
     78<fieldset id="tagdiv" class="dbx-box">
     79<h3 class="dbx-handle"><?php _e('Tags') ?></h3>
     80<div class="dbx-content">
     81<p id="jaxtag"></p>
     82<ul id="tagchecklist"></ul></div>
     83</fieldset>
     84
    7885<fieldset id="commentstatusdiv" class="dbx-box">
    7986<h3 class="dbx-handle"><?php _e('Discussion') ?></h3>
    8087<div class="dbx-content">
  • wp-admin/cat-js.php

     
    1212        $('newcat').onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','jaxcat');", e); };
    1313        $('catadd').onclick = function() { catList.ajaxAdder('category', 'jaxcat'); };
    1414}
     15addLoadEvent(function(){tagList=new listMan('tagchecklist');tagList.ajaxRespEl='jaxtag';tagList.topAdder=1;tagList.alt=0;tagList.showLink=0;});
     16addLoadEvent(newtagAddIn);
     17function newtagAddIn() {
     18        var jaxtag = $('jaxtag');
     19        if ( !jaxtag )
     20                return false;
     21        Element.update(jaxtag,'<span id="ajaxtag"><input type="text" name="newtag" id="newtag" size="16" autocomplete="off"/><input type="button" name="Button" id="tagadd" value="<?php echo js_escape(__('Add')); ?>"/><span id="howto"><?php echo js_escape(__('Separate multiple tags with commas.')); ?></span></span>');
     22        $('newtag').onkeypress = function(e) { return killSubmit("tagList.ajaxAdder('tag','jaxtag');", e); };
     23        $('tagadd').onclick = function() { tagList.ajaxAdder('tag', 'jaxtag'); };
     24}
  • wp-admin/upgrade-schema.php

     
    120120  KEY post_name (post_name),
    121121  KEY type_status_date (post_type,post_status,post_date,ID)
    122122);
     123CREATE TABLE $wpdb->tags (
     124  tag_id bigint(20) unsigned NOT NULL auto_increment,
     125  tag varchar(30) NOT NULL default '',
     126  raw_tag varchar(50) NOT NULL default '',
     127  tag_count bigint(20) unsigned NOT NULL default '0',
     128  PRIMARY KEY  (tag_id)
     129);
     130CREATE TABLE $wpdb->tagged (
     131  tag_id bigint(20) unsigned NOT NULL default '0',
     132  post_id bigint(20) unsigned NOT NULL default '0',
     133  PRIMARY KEY  (tag_id,post_id),
     134  KEY tag_id_index (tag_id),
     135  KEY post_id_index (post_id)
     136);
    123137CREATE TABLE $wpdb->users (
    124138  ID bigint(20) unsigned NOT NULL auto_increment,
    125139  user_login varchar(60) NOT NULL default '',