Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created December 20, 2008 10:23
Show Gist options
  • Save ngerakines/38289 to your computer and use it in GitHub Desktop.
Save ngerakines/38289 to your computer and use it in GitHub Desktop.
A module used to dump MT blog entries to Jekyll files.
-module (mtdump).
-export([start/0]).
start() ->
mysql:start_link(mt, "localhost", "user", "password", "mt_database"),
mysql:prepare(posts, <<"SELECT entry_id, entry_basename, entry_text, entry_text_more, entry_created_on, entry_title FROM mt_entry where entry_blog_id = 3">>),
Posts = case mysql:execute(mt, posts, [], infinity) of
{data, MySQLRes} ->
Fields = [ F || {_, F, _, _} <- mysql:get_result_field_info(MySQLRes)],
Rows = mysql:get_result_rows(MySQLRes),
[lists:zip(Fields, Row) || Row <- Rows];
[] -> [];
Err -> io:format("[~p:~b] ~p~n", [?MODULE, ?LINE, Err]), false
end,
lists:foreach(fun(Data) ->
[
{<<"entry_id">>, EntryID},
{<<"entry_basename">>, BaseName},
{<<"entry_text">>, Body},
{<<"entry_text_more">>, BodyMore},
{<<"entry_created_on">>,{datetime,{{Year, Month, Day}, _}}},
{<<"entry_title">>, Title}
] = Data,
Filename = io_lib:format("/Users/ngerakines/dev/blog.socklabs.com/_posts/~p-~p-~p-~s.markdown", [Year, Month, Day, BaseName]),
{ok, FileRef} = file:open(Filename, [write]),
PageBody = case BodyMore of
undefined -> Body;
_ -> erlang:iolist_to_binary([Body, BodyMore])
end,
io:format(
FileRef,
"---~n"
"id: ~p~n"
"layout: post~n"
"title: >~n"
" ~s~n"
"---~n~n~s~n",
[EntryID, Title, PageBody]
),
file:close(FileRef)
end, Posts),
ok.
-module (mtdump2).
-export([start/0]).
start() ->
mysql:start_link(mt, "localhost", "user", "password", "mt_database"),
mysql:prepare(posts, <<"SELECT entry_basename, entry_created_on FROM mt_entry where entry_blog_id = 3">>),
Posts = case mysql:execute(mt, posts, [], infinity) of
{data, MySQLRes} ->
Fields = [ F || {_, F, _, _} <- mysql:get_result_field_info(MySQLRes)],
Rows = mysql:get_result_rows(MySQLRes),
[lists:zip(Fields, Row) || Row <- Rows];
[] -> [];
Err -> io:format("[~p:~b] ~p~n", [?MODULE, ?LINE, Err]), false
end,
Urls = lists:foldl(fun(Data, Acc) ->
[
{<<"entry_basename">>, BaseName},
{<<"entry_created_on">>,{datetime,{{Year, Month, Day}, _}}}
] = Data,
OldUrl = io_lib:format("/~p/~2.2.0w/~s", [Year, Month, BaseName]),
NewUrl = io_lib:format("/~p/~2.2.0w/~2.2.0w/~s.html", [Year, Month, Day, BaseName]),
[io_lib:format("\"^~s.*$\" => \"~s\",~n", [OldUrl, NewUrl]) | Acc]
end, [], Posts),
Filename = io_lib:format("redirect.list", []),
{ok, FileRef} = file:open(Filename, [write]),
lists:foreach(fun(Item) -> io:format(FileRef, "~s", [Item]) end, lists:usort(Urls)),
file:close(FileRef),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment