1
1
var mysql = require ( "mysql" ) ,
2
+ Step = require ( "step" ) ,
2
3
config = require ( "./config" ) ;
3
4
4
5
var db ,
5
- postsTable = "wp_" + ( config . siteId ? config . siteId + "_" : "" ) + "posts" ;
6
- postmetaTable = "wp_" + ( config . siteId ? config . siteId + "_" : "" ) + "postmeta" ;
7
-
6
+ postIds = { } ,
7
+ postsTable = table ( "posts" ) ;
8
+ postmetaTable = table ( "postmeta" ) ,
9
+ optionsTable = table ( "options" ) ;
8
10
11
+ function table ( name ) {
12
+ return "wp_" + ( config . siteId ? config . siteId + "_" : "" ) + name ;
13
+ }
9
14
10
15
function connect ( ) {
11
16
db = new mysql . createClient ( {
@@ -18,6 +23,12 @@ function connect() {
18
23
}
19
24
20
25
function getPostId ( name , fn ) {
26
+ if ( name in postIds ) {
27
+ return process . nextTick ( function ( ) {
28
+ fn ( null , postIds [ name ] ) ;
29
+ } ) ;
30
+ }
31
+
21
32
db . query ( "SELECT `ID` FROM `" + postsTable + "` WHERE `post_name` = ?" ,
22
33
[ name ] , function ( error , rows ) {
23
34
if ( error ) {
@@ -28,28 +39,34 @@ function getPostId( name, fn ) {
28
39
return fn ( null , null ) ;
29
40
}
30
41
42
+ postIds [ name ] = rows [ 0 ] . ID ;
31
43
fn ( null , rows [ 0 ] . ID ) ;
32
44
} ) ;
33
45
}
34
46
35
47
function createOrUpdatePost ( name , title , content , fn ) {
36
48
getPostId ( name , function ( error , id ) {
37
- if ( error ) {
38
- return fn ( error ) ;
39
- }
49
+ if ( error ) {
50
+ return fn ( error ) ;
51
+ }
40
52
41
- if ( ! id ) {
42
- // TODO: set post_type = "page"
43
- db . query ( "INSERT INTO `" + postsTable + "` " +
44
- "SET `post_name` = ?, `post_title` = ?, `post_content` = ?" ,
45
- [ name , title , content ] , fn ) ;
46
- } else {
47
- db . query ( "UPDATE `" + postsTable + "` " +
48
- "SET `post_content` = ? " +
49
- "WHERE `ID` = ?" ,
50
- [ content , id ] , fn ) ;
51
- }
52
- } ) ;
53
+ if ( ! id ) {
54
+ createPost ( name , title , content , fn ) ;
55
+ } else {
56
+ db . query ( "UPDATE `" + postsTable + "` " +
57
+ "SET `post_content` = ? " +
58
+ "WHERE `ID` = ?" ,
59
+ [ content , id ] , fn ) ;
60
+ }
61
+ } ) ;
62
+ }
63
+
64
+ function createPost ( name , title , content , fn ) {
65
+ // TODO: set all datetime fields
66
+ db . query ( "INSERT INTO `" + postsTable + "` " +
67
+ "SET `post_name` = ?, `post_title` = ?, `post_content` = ?, " +
68
+ "`post_type` = 'page'" ,
69
+ [ name , title , content ] , fn ) ;
53
70
}
54
71
55
72
function setMeta ( plugin , key , value , fn ) {
@@ -131,10 +148,10 @@ function auto( fn ) {
131
148
132
149
133
150
134
- module . exports = {
151
+ var wordpress = module . exports = {
135
152
addVersionedPlugin : auto ( function ( version , package , content , fn ) {
136
- var postName = package . name + "- " + package . version ;
137
- createOrUpdatePost ( postName , package . title , content , function ( error ) {
153
+ var postName = package . name + "/ " + package . version ;
154
+ createPost ( postName , package . title , content , function ( error ) {
138
155
if ( error ) {
139
156
return fn ( error ) ;
140
157
}
@@ -143,6 +160,62 @@ module.exports = {
143
160
} ) ;
144
161
} ) ,
145
162
163
+ getPendingVersions : auto ( function ( plugin , fn ) {
164
+ db . query ( "SELECT `ID`, `post_name` FROM `" + postsTable + "` WHERE `post_name` LIKE ?" ,
165
+ [ plugin + "/%" ] , function ( error , rows ) {
166
+ if ( error ) {
167
+ return fn ( error ) ;
168
+ }
169
+
170
+ fn ( null , rows . map ( function ( row ) {
171
+ return row . post_name . slice ( plugin . length + 1 ) ;
172
+ } ) ) ;
173
+ } ) ;
174
+ } ) ,
175
+
176
+ finalizePendingVersions : auto ( function ( plugin , fn ) {
177
+ Step (
178
+ function ( ) {
179
+ wordpress . getPendingVersions ( plugin , this ) ;
180
+ } ,
181
+
182
+ function ( error , versions ) {
183
+ if ( error ) {
184
+ return fn ( error ) ;
185
+ }
186
+
187
+ var group = this . group ( ) ;
188
+ versions . forEach ( function ( version ) {
189
+ wordpress . finalizePendingVersion ( plugin , version , group ( ) ) ;
190
+ } ) ;
191
+ } ,
192
+
193
+ function ( error ) {
194
+ if ( error ) {
195
+ return fn ( error ) ;
196
+ }
197
+
198
+ fn ( null ) ;
199
+ }
200
+ ) ;
201
+ } ) ,
202
+
203
+ finalizePendingVersion : auto ( function ( plugin , version , fn ) {
204
+ getPostId ( plugin , function ( error , id ) {
205
+ if ( error ) {
206
+ return fn ( error ) ;
207
+ }
208
+
209
+ if ( ! id ) {
210
+ return fn ( new Error ( "No page for " + plugin ) ) ;
211
+ }
212
+
213
+ db . query ( "UPDATE `" + postsTable + "` " +
214
+ "SET `post_name` = ?, `post_parent` = ? WHERE `post_name` = ?" ,
215
+ [ version , id , plugin + "/" + version ] , fn ) ;
216
+ } ) ;
217
+ } ) ,
218
+
146
219
getVersions : auto ( function ( plugin , fn ) {
147
220
getMeta ( plugin , "versions" , function ( error , versions ) {
148
221
if ( error ) {
@@ -158,7 +231,7 @@ module.exports = {
158
231
} ) ,
159
232
160
233
setVersions : auto ( function ( plugin , versions , latest , fn ) {
161
- var postName = plugin + "- " + latest ;
234
+ var postName = plugin + "/ " + latest ;
162
235
db . query ( "SELECT `post_title`, `post_content` FROM `" + postsTable + "` WHERE `post_name` = ?" ,
163
236
[ postName ] , function ( error , rows ) {
164
237
if ( error ) {
@@ -179,6 +252,10 @@ module.exports = {
179
252
} ) ;
180
253
} ) ,
181
254
255
+ flush : auto ( function ( fn ) {
256
+ db . query ( "DELETE FROM `" + optionsTable + "` WHERE `option_name` = 'rewrite_rules'" , fn ) ;
257
+ } ) ,
258
+
182
259
end : function ( ) {
183
260
if ( db ) {
184
261
db . end ( ) ;
0 commit comments