Make WordPress Core

Ticket #4189: 4189b.dff

File 4189b.dff, 10.7 KB (added by majelbstoat, 19 years ago)

Second iteration, no taxonomy table, more functions.

Line 
1Index: wp-admin/upgrade-schema.php
2===================================================================
3--- wp-admin/upgrade-schema.php (revision 5485)
4+++ wp-admin/upgrade-schema.php (working copy)
5@@ -10,21 +10,30 @@
6 $charset_collate .= " COLLATE $wpdb->collate";
7 }
8
9-$wp_queries="CREATE TABLE $wpdb->categories (
10- cat_ID bigint(20) NOT NULL auto_increment,
11- cat_name varchar(55) NOT NULL default '',
12- category_nicename varchar(200) NOT NULL default '',
13- category_description longtext NOT NULL,
14- category_parent bigint(20) NOT NULL default '0',
15- category_count bigint(20) NOT NULL default '0',
16- link_count bigint(20) NOT NULL default '0',
17- tag_count bigint(20) NOT NULL default '0',
18- posts_private tinyint(1) NOT NULL default '0',
19- links_private tinyint(1) NOT NULL default '0',
20- type tinyint NOT NULL default '1',
21- PRIMARY KEY (cat_ID),
22- KEY category_nicename (category_nicename)
23+$wp_queries="CREATE TABLE $wpdb->terms (
24+ term_id bigint(20) NOT NULL auto_increment,
25+ term_name varchar(55) NOT NULL default '',
26+ term_slug varchar(200) NOT NULL default '',
27+ term_group bigint(10) NOT NULL default 0
28+ PRIMARY KEY (term_id),
29+ UNIQUE KEY term_slug (term_slug)
30 ) $charset_collate;
31+CREATE TABLE $wpdb->term_taxonomy (
32+ term_taxonomy_id bigint(20) NOT NULL auto_increment,
33+ term_id bigint(20) NOT NULL default 0,
34+ taxonomy varchar(32) NOT NULL default '',
35+ term_description longtext NOT NULL,
36+ parent bigint(20) NOT NULL default 0,
37+ count bigint(20) NOT NULL default 0,
38+ PRIMARY KEY (term_taxonomy_id),
39+ UNIQUE KEY (term_id, taxonomy)
40+) $charset_collate;
41+CREATE TABLE $wpdb->term_relationships (
42+ object_id bigint(20) NOT NULL default 0,
43+ term_taxonomy_id bigint(20) NOT NULL default 0,
44+ PRIMARY KEY (object_id),
45+ KEY (term_taxonomy_id)
46+) $charset_collate;
47 CREATE TABLE $wpdb->comments (
48 comment_ID bigint(20) unsigned NOT NULL auto_increment,
49 comment_post_ID int(11) NOT NULL default '0',
50@@ -45,13 +54,6 @@
51 KEY comment_approved (comment_approved),
52 KEY comment_post_ID (comment_post_ID)
53 ) $charset_collate;
54-CREATE TABLE $wpdb->link2cat (
55- rel_id bigint(20) NOT NULL auto_increment,
56- link_id bigint(20) NOT NULL default '0',
57- category_id bigint(20) NOT NULL default '0',
58- PRIMARY KEY (rel_id),
59- KEY link_id (link_id,category_id)
60-) $charset_collate;
61 CREATE TABLE $wpdb->links (
62 link_id bigint(20) NOT NULL auto_increment,
63 link_url varchar(255) NOT NULL default '',
64@@ -86,14 +88,6 @@
65 PRIMARY KEY (option_id,blog_id,option_name),
66 KEY option_name (option_name)
67 ) $charset_collate;
68-CREATE TABLE $wpdb->post2cat (
69- rel_id bigint(20) NOT NULL auto_increment,
70- post_id bigint(20) NOT NULL default '0',
71- category_id bigint(20) NOT NULL default '0',
72- rel_type varchar(64) NOT NULL default 'category',
73- PRIMARY KEY (rel_id),
74- KEY post_id (post_id,category_id)
75-) $charset_collate;
76 CREATE TABLE $wpdb->postmeta (
77 meta_id bigint(20) NOT NULL auto_increment,
78 post_id bigint(20) NOT NULL default '0',
79@@ -404,4 +398,4 @@
80 }
81 }
82
83-?>
84\ No newline at end of file
85+?>
86Index: wp-includes/taxonomy.php
87===================================================================
88--- wp-includes/taxonomy.php (revision 0)
89+++ wp-includes/taxonomy.php (revision 0)
90@@ -0,0 +1,195 @@
91+<?php
92+
93+class WPTaxonomy {
94+
95+ // This will look like (1 => 'post_category', 2 => 'link_category') etc.
96+ var $taxonomies;
97+
98+ function WPTaxonomy() {
99+ }
100+
101+ /**
102+ * Adds a new term to the database. Optionally marks it as an alias of an existing term.
103+ * @param string $term The term to add.
104+ * @param int|string $alias_of The id or slug of the new term's alias.
105+ */
106+ function add_term($term, $alias_of = '') {
107+ global $wpdb;
108+ $term_slug = sanitize_title($term);
109+ if ($alias_of) {
110+ $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM $wpdb->terms WHERE term_slug = '$alias_of'");
111+ if ($alias->term_group)
112+ // The alias we want is already in a group, so let's use that one.
113+ $term_group = $alias->term_group;
114+ } else {
115+ // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
116+ $term_group = $wpdb->get_var("SELECT MAX() term_group FROM $wpdb->terms GROUP BY term_group") + 1;
117+ $wpdb->query("UPDATE $wpdb->terms SET term_group = $term_group WHERE term_id = $alias->term_id");
118+ }
119+ } else {
120+ $term_group = 0;
121+ }
122+ // Now add or replace the term. This works because we have a UNIQUE key on term_slug.
123+ $wpdb->query("REPLACE INTO $wpdb->terms (term_name, term_slug, term_group) VALUES ($term, $term_slug, $term_group)");
124+ }
125+
126+ /**
127+ * Removes a term from the database.
128+ */
129+ function remove_term() {
130+
131+ }
132+
133+
134+ /**
135+ * Returns the index of a defined term, or 0 (false) if the term doesn't exist.
136+ */
137+ function is_defined_term($term) {
138+ global $wpdb;
139+ return $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE term_slug = '$term'");
140+ }
141+
142+ /**
143+ * Given an array of terms, returns those that are defined term slugs. Ignores integers.
144+ * @param array $terms The term slugs to check for a definition.
145+ */
146+ function get_defined_terms($terms) {
147+ global $wpdb;
148+ foreach ($terms as $term) {
149+ if (!is_int($term)) {
150+ $searches[] = $term;
151+ }
152+ }
153+ $terms = "'" . implode("', '", $searches) . "'";
154+ return $wpdb->get_col("SELECT term_slug FROM $wpdb->terms WHERE term_slug IN ($terms)");
155+ }
156+
157+ /**
158+ * Adds a term taxonomy entry or increments the count if it already exists.
159+ * @param int|string $term The term to add.
160+ * @param string $taxonomy The context.
161+ * @param string $description An optional description of the relationship.
162+ * @param int $parent The parent of the term in this context.
163+ */
164+ function add_term_taxonomy($term, $taxonomy, $description = '', $parent = 0) {
165+ $clause = (is_int($term)) ? "t.term_id = $term" : "t.term_slug = '$term'";
166+ if ($count = $wpdb->query("SELECT tt.count FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '$taxonomy' AND $clause")) {
167+ // We'll be updating whatever's there, and incrementing the count.
168+ $count++;
169+ $wpdb->query("UPDATE $wpdb->term_taxonomy SET count = $count");
170+ } else {
171+ // There was no existing entry for this term and taxonomy type, so add a new one, using the supplied description and parent, with a count of 1.
172+ $wpdb->query("INSERT INTO $wpdb->term_taxonomy (term_id, taxonomy, term_description, parent, count) SELECT term_id, '$taxonomy', '$description', $parent, 1 FROM $wpdb->terms AS t1 WHERE $clause");
173+ }
174+ }
175+
176+ /**
177+ * Removes a term from a particular taxonomy or set of taxonomies. Attaches orphaned children in one of various schemes.
178+ * @param int|string $term The slug or id of the term.
179+ * @param string|array $taxonomy The taxonomy or taxonomies to remove from.
180+ * @param string (delete|parent|root|ignore) $attach_children. Where to attach orphaned children.
181+ */
182+ function remove_term_taxonomy($term, $taxonomy, $attach_children = 'root') {
183+
184+ }
185+
186+ /**
187+ * Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy
188+ * relationship if it doesn't already exist. Creates a term if it doesn't exist (using the term_slug).
189+ * @param array|int|string $term The slug or id of the term.
190+ * @param int $object_id The object to relate to.
191+ * @param array|string $taxonomies The context(s) in which to relate the term to the object.
192+ */
193+ function add_term_relationship($terms, $object_id, $taxonomies) {
194+ global $wpdb;
195+
196+ if (!is_array($taxonomies)) {
197+ $taxonomies = array($taxonomies);
198+ }
199+
200+ if (!is_array($terms)) {
201+ $terms = array($terms);
202+ }
203+
204+ $defined_terms = $this->get_defined_terms($terms);
205+
206+ foreach ($terms as $term) {
207+ if (!is_int($term)) {
208+ if (!isset($defined_terms[$term])) {
209+ $new_terms[] = $term;
210+ }
211+ $term_slugs[] = $term;
212+ } else {
213+ $term_ids[] = $term;
214+ }
215+ }
216+
217+ if (isset($new_terms) {
218+ // At least one of the terms specified doesn't exist, so add any new ones, using the term_slug as the name.
219+ $this->add_terms($new_terms);
220+ }
221+
222+ $term_clause = (isset($term_ids)) 'tt.term_id IN (' . implode(', ', $term_ids) . ')' ? : '';
223+ if (isset($term_slugs)) {
224+ if ($term_clause) {
225+ $term_clause .= ' OR ';
226+ }
227+ $term_clause .= "t.term_slug IN ('" . implode("', '", $term_slugs) . "')";
228+ $term_join = "INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id";
229+ } else {
230+ $term_join = '';
231+ }
232+
233+ // Now add or increment the term taxonomy relationships. This is inefficient at the moment.
234+ foreach ($taxonomies as $taxonomy) {
235+ foreach ($terms as $term) {
236+ $this->add_term_taxonomy($term, $taxonomy);
237+ }
238+ }
239+
240+ $taxonomies = "'" . implode("', '", $taxonomies) . "'";
241+
242+ // Finally, relate the term and taxonomy to the object.
243+ $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)");
244+ }
245+
246+ /**
247+ * Returns the terms associated with the given object(s), in the supplied taxonomies.
248+ * @param int|array $object_id The id of the object(s)) to retrieve for.
249+ * @param string|array $taxonomies The taxonomies to retrieve terms from.
250+ * @return array The requested term data.
251+ */
252+ function get_object_terms($object_id, $taxonomy) {
253+ global $wpdb;
254+ $taxonomies = ($single_taxonomy = !is_array($taxonomy)) ? array($taxonomy) : $taxonomy;
255+ $object_ids = ($single_object = !is_array($object_id)) ? array($object_id) : $object_id;
256+
257+ $taxonomies = "'" . implode("', '", $taxonomies) . "'";
258+ $object_ids = implode(', ', $object_ids);
259+
260+ if ($taxonomy_data = $wpdb->get_results("SELECT tr.object_id, tt.taxonomy, 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)") {
261+ if ($single_taxonomy && $single_object) {
262+ // Just one kind of taxonomy for one object.
263+ return $taxonomy_data;
264+ } else {
265+ foreach ($taxonomy_data as $data) {
266+ if ($single_taxonomy) {
267+ // Many objects, one taxonomy type.
268+ $return[$data->object_id][] = $data;
269+ } elseif ($single_object) {
270+ // One object, many taxonomies.
271+ $return[$data->taxonomy][] = $data;
272+ } else {
273+ // Many objects, many taxonomies.
274+ $return[$data->object_id][$data->taxonomy][] = $data;
275+ }
276+ }
277+ return $return;
278+ }
279+ } else {
280+ return array();
281+ }
282+ }
283+}
284+
285+?>
286
287Property changes on: wp-includes\taxonomy.php
288___________________________________________________________________
289Name: svn:keywords
290 + "Date Author Revision"
291