@@ -4,9 +4,12 @@ var mysql = require( "mysql" ),
4
4
5
5
var db ,
6
6
postIds = { } ,
7
- postsTable = table ( "posts " ) ;
7
+ optionsTable = table ( "options " ) ,
8
8
postmetaTable = table ( "postmeta" ) ,
9
- optionsTable = table ( "options" ) ;
9
+ postsTable = table ( "posts" ) ,
10
+ termsTable = table ( "terms" ) ,
11
+ termRelationshipsTable = table ( "term_relationships" ) ,
12
+ termTaxonomyTable = table ( "term_taxonomy" ) ;
10
13
11
14
function table ( name ) {
12
15
return "wp_" + ( config . siteId ? config . siteId + "_" : "" ) + name ;
@@ -152,13 +155,27 @@ function auto( fn ) {
152
155
var wordpress = module . exports = {
153
156
addVersionedPlugin : auto ( function ( version , package , content , fn ) {
154
157
var postName = package . name + "/" + package . version ;
155
- createPost ( postName , package . title , content , function ( error ) {
156
- if ( error ) {
157
- return fn ( error ) ;
158
- }
158
+ Step (
159
+ function ( ) {
160
+ createPost ( postName , package . title , content , this ) ;
161
+ } ,
159
162
160
- setMeta ( postName , "package_json" , JSON . stringify ( package ) , fn ) ;
161
- } ) ;
163
+ function ( error ) {
164
+ var parallel = this . parallel ;
165
+ setMeta ( postName , "package_json" , JSON . stringify ( package ) , parallel ( ) ) ;
166
+ ( package . keywords || [ ] ) . forEach ( function ( keyword ) {
167
+ wordpress . setTerm ( postName , keyword , parallel ( ) ) ;
168
+ } ) ;
169
+ } ,
170
+
171
+ function ( error ) {
172
+ if ( error ) {
173
+ return fn ( error ) ;
174
+ }
175
+
176
+ fn ( null ) ;
177
+ }
178
+ ) ;
162
179
} ) ,
163
180
164
181
getPendingVersions : auto ( function ( plugin , fn ) {
@@ -273,6 +290,62 @@ var wordpress = module.exports = {
273
290
) ;
274
291
} ) ,
275
292
293
+ createTerm : auto ( function ( term , fn ) {
294
+ Step (
295
+ function ( ) {
296
+ db . query ( "INSERT INTO `" + termsTable + "` SET `name` = ?, `slug` = ? " +
297
+ "ON DUPLICATE KEY UPDATE `term_id` = LAST_INSERT_ID(`term_id`)" ,
298
+ [ term , term ] , this ) ;
299
+ } ,
300
+
301
+ function ( error , info ) {
302
+ if ( error ) {
303
+ return fn ( error ) ;
304
+ }
305
+
306
+ db . query ( "INSERT INTO `" + termTaxonomyTable + "` " +
307
+ "SET `term_id` = ?, `taxonomy` = 'post_tag' " +
308
+ "ON DUPLICATE KEY UPDATE `term_taxonomy_id` = LAST_INSERT_ID(`term_taxonomy_id`)" ,
309
+ [ info . insertId ] , this ) ;
310
+ } ,
311
+
312
+ function ( error , info ) {
313
+ if ( error ) {
314
+ return fn ( error ) ;
315
+ }
316
+
317
+ fn ( null , info . insertId ) ;
318
+ }
319
+ ) ;
320
+ } ) ,
321
+
322
+ setTerm : auto ( function ( post , term , fn ) {
323
+ Step (
324
+ function ( ) {
325
+ getPostId ( post , this . parallel ( ) ) ;
326
+ wordpress . createTerm ( term , this . parallel ( ) ) ;
327
+ } ,
328
+
329
+ function ( error , postId , termId ) {
330
+ if ( error ) {
331
+ return fn ( error ) ;
332
+ }
333
+
334
+ db . query ( "INSERT INTO `" + termRelationshipsTable + "` " +
335
+ "SET `object_id` = ?, `term_taxonomy_id` = ?" ,
336
+ [ postId , termId ] , this ) ;
337
+ } ,
338
+
339
+ function ( error , termId ) {
340
+ if ( error ) {
341
+ return fn ( error ) ;
342
+ }
343
+
344
+ fn ( null ) ;
345
+ }
346
+ ) ;
347
+ } ) ,
348
+
276
349
flush : auto ( function ( fn ) {
277
350
db . query ( "DELETE FROM `" + optionsTable + "` WHERE `option_name` = 'rewrite_rules'" , fn ) ;
278
351
} ) ,
@@ -287,9 +360,11 @@ var wordpress = module.exports = {
287
360
_reset : auto ( function ( fn ) {
288
361
Step (
289
362
function ( ) {
290
- db . query ( "TRUNCATE TABLE `" + postsTable + "`" , this . parallel ( ) ) ;
291
- db . query ( "TRUNCATE TABLE `" + postmetaTable + "`" , this . parallel ( ) ) ;
292
- wordpress . flush ( this . parallel ( ) ) ;
363
+ var parallel = this . parallel ;
364
+ [ postmetaTable , postsTable , termsTable , termRelationshipsTable , termTaxonomyTable ] . forEach ( function ( table ) {
365
+ db . query ( "TRUNCATE TABLE `" + table + "`" , parallel ( ) ) ;
366
+ } ) ;
367
+ wordpress . flush ( parallel ( ) ) ;
293
368
} ,
294
369
295
370
function ( error ) {
0 commit comments