Skip to content

Commit b8527ef

Browse files
committed
Converted posts to pages and tracking more metadata.
1 parent 5f8c0f9 commit b8527ef

File tree

3 files changed

+124
-24
lines changed

3 files changed

+124
-24
lines changed

src/main.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,22 @@ function processPlugin( data, fn ) {
156156
});
157157
},
158158

159+
// flush redirect rules
159160
function( error ) {
160-
wordpress.end();
161+
if ( error ) {
162+
return fn( error );
163+
}
161164

165+
wordpress.flush( this );
166+
},
167+
168+
// close connection to WordPress
169+
function( error ) {
162170
if ( error ) {
163171
return fn( error );
164172
}
165173

174+
wordpress.end();
166175
fn( null );
167176
}
168177
);
@@ -322,6 +331,16 @@ function processPlugin( data, fn ) {
322331
wordpress.setVersions( plugin, filteredVersions, latest, this );
323332
},
324333

334+
// finalize/publish versioned pages
335+
function( error ) {
336+
if ( error ) {
337+
// TODO: log failure for retry
338+
return fn( error );
339+
}
340+
341+
wordpress.finalizePendingVersions( plugin, this );
342+
},
343+
325344
function( error ) {
326345
if ( error ) {
327346
// TODO: log failure for retry
@@ -345,6 +364,6 @@ processPlugin({
345364
}, function( error, data ) {
346365
// TODO: log error to file
347366
if ( error ) {
348-
console.log( error );
367+
console.log( error, error.stack );
349368
}
350369
});

src/wordpress.js

Lines changed: 99 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
var mysql = require( "mysql" ),
2+
Step = require( "step" ),
23
config = require( "./config" );
34

45
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" );
810

11+
function table( name ) {
12+
return "wp_" + (config.siteId ? config.siteId + "_" : "") + name;
13+
}
914

1015
function connect() {
1116
db = new mysql.createClient({
@@ -18,6 +23,12 @@ function connect() {
1823
}
1924

2025
function getPostId( name, fn ) {
26+
if ( name in postIds ) {
27+
return process.nextTick(function() {
28+
fn( null, postIds[ name ] );
29+
});
30+
}
31+
2132
db.query( "SELECT `ID` FROM `" + postsTable + "` WHERE `post_name` = ?",
2233
[ name ], function( error, rows ) {
2334
if ( error ) {
@@ -28,28 +39,34 @@ function getPostId( name, fn ) {
2839
return fn( null, null );
2940
}
3041

42+
postIds[ name ] = rows[ 0 ].ID;
3143
fn( null, rows[ 0 ].ID );
3244
});
3345
}
3446

3547
function createOrUpdatePost( name, title, content, fn ) {
3648
getPostId( name, function( error, id ) {
37-
if ( error ) {
38-
return fn( error );
39-
}
49+
if ( error ) {
50+
return fn( error );
51+
}
4052

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 );
5370
}
5471

5572
function setMeta( plugin, key, value, fn ) {
@@ -131,10 +148,10 @@ function auto( fn ) {
131148

132149

133150

134-
module.exports = {
151+
var wordpress = module.exports = {
135152
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 ) {
138155
if ( error ) {
139156
return fn( error );
140157
}
@@ -143,6 +160,62 @@ module.exports = {
143160
});
144161
}),
145162

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+
146219
getVersions: auto(function( plugin, fn ) {
147220
getMeta( plugin, "versions", function( error, versions ) {
148221
if ( error ) {
@@ -158,7 +231,7 @@ module.exports = {
158231
}),
159232

160233
setVersions: auto(function( plugin, versions, latest, fn ) {
161-
var postName = plugin + "-" + latest;
234+
var postName = plugin + "/" + latest;
162235
db.query( "SELECT `post_title`, `post_content` FROM `" + postsTable + "` WHERE `post_name` = ?",
163236
[ postName ], function( error, rows ) {
164237
if ( error ) {
@@ -179,6 +252,10 @@ module.exports = {
179252
});
180253
}),
181254

255+
flush: auto(function( fn ) {
256+
db.query( "DELETE FROM `" + optionsTable + "` WHERE `option_name` = 'rewrite_rules'", fn );
257+
}),
258+
182259
end: function() {
183260
if ( db ) {
184261
db.end();

template/page

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
{{allContributors}}
1515
</div>
1616
{{/if}}
17+
<div class="block">
18+
<h2>All versions</h2>
19+
[jq_plugin_versions]
20+
</div>
1721
</div>
1822
<div class="col col3-1">
1923
<div class="block">

0 commit comments

Comments
 (0)