Menu

[r249]: / cms / trunk / wp-import.cpp  Maximize  Restore  History

Download this file

115 lines (100 with data), 2.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <dbi/dbixx.h>
#include <iostream>
using namespace dbixx;
using namespace std;
void remove_tabs(string &s)
{
unsigned i;
for(i=0;i<s.size();i++){
if(s[i]=='\t') s[i]=' ';
}
}
void prepare_post(string &a,string &c)
{
string tmp=c;
size_t s;
if((s=tmp.find("<!--more-->"))==string::npos) {
a=tmp;
c="";
}
else {
a=tmp.substr(0,s);
c=tmp.substr(s+11);
}
remove_tabs(a);
remove_tabs(c);
}
int main()
{
try {
session wp("mysql");
wp.param("dbname","wordpress");
wp.param("username","root");
wp.param("password","root");
wp.connect();
session sql("mysql");
sql.param("dbname","cppcms");
sql.param("username","root");
sql.param("password","root");
sql.connect();
sql<<"begin",exec();
row r;
string val;
wp<<"select option_value from wp_options where option_name='blogname'";
wp.single(r);
r>>val;
sql<<"insert into options values(0,?)",val,exec();
wp<<"select option_value from wp_options where option_name='blogdescription'";
wp.single(r);
r>>val;
sql<<"insert into options values(1,?)",val,exec();
result res;
wp<<"select ID,display_name from wp_users",res;
while(res.next(r)) {
int id;
string name,pass;
r>>id>>name;
cout<<"Insert password for user "<<name<<":";
cin>>pass;
sql<<"insert into users(id,username,password) values(?,?,?)",
id,name,pass,exec();
}
wp<< "select ID,post_author,post_date,post_title,post_content,post_status "
"from wp_posts where post_type='post'";
wp.fetch(res);
while(res.next(r)) {
int id;
int uid;
std::tm pub;
string title;
string content;
string status;
r>>id>>uid>>pub>>title>>content>>status;
string abstract;
prepare_post(abstract,content);
sql<<"Insert into posts(id,author_id,title,abstract,content,publish,is_open) "
"values(?,?,?,?,?,?,?)",
id,uid,title,abstract,content,pub,(status=="publish" ? 1 : 0);
sql.exec();
}
wp<<"select comment_ID,comment_post_ID,comment_author,comment_author_email,"
"comment_author_url,comment_date,comment_content from "
"wp_comments where comment_approved=1;";
wp.fetch(res);
while(res.next(r)) {
int id,pid;
std::tm pub;
string a,e,u,cont;
r>>id>>pid>>a>>e>>u>>pub>>cont;
remove_tabs(cont);
sql<<"insert into comments(id,post_id,author,email,url,publish_time,content) "
"values(?,?,?,?,?,?,?)",
id,pid,a,e,u,pub,cont;
sql.exec();
}
sql<<"commit",exec();
}
catch(dbixx_error const &e){
cerr<<e.what()<<endl;
}
}
MongoDB Logo MongoDB