PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/textpattern/include/import/import_wp.php

https://bitbucket.org/Manfre/xpattern
PHP | 548 lines | 374 code | 120 blank | 54 comment | 28 complexity | cd6228b63c387911c999c2536311d289 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. function doImportWP($b2dblogin, $b2db, $b2dbpass, $b2dbhost, $wpdbprefix, $insert_into_section, $insert_with_status, $default_comment_invite)
  3. {
  4. global $txpcfg;
  5. $b2link = mysql_connect($b2dbhost, $b2dblogin, $b2dbpass, true);
  6. if (!$b2link)
  7. {
  8. return 'WordPress database values don&#8217;t work. Go back, replace them and try again.';
  9. }
  10. mysql_select_db($b2db, $b2link);
  11. // Keep some response on some part
  12. $results = array();
  13. $errors = array();
  14. $results[] = hed('Connected to WordPress database. Importing Data&#8230;', 1);
  15. /*
  16. export users
  17. */
  18. $users = array();
  19. $user_query = mysql_query("
  20. select
  21. ID as user_id,
  22. user_login as name,
  23. user_email as email,
  24. display_name as RealName
  25. from ".$wpdbprefix."users
  26. ", $b2link) or $errors[] = mysql_error();
  27. while ($user = mysql_fetch_array($user_query))
  28. {
  29. $user_privs_query = mysql_query("
  30. select
  31. meta_value
  32. from ".$wpdbprefix."usermeta
  33. where user_id = ".$user['user_id']." and meta_key = 'capabilities'
  34. ", $b2link) or $errors[] = mysql_error();
  35. $privs = unserialize(mysql_result($user_privs_query, 0));
  36. foreach ($privs as $key => $val)
  37. {
  38. // convert the built-in WordPress roles
  39. // to their Txp equivalent
  40. switch ($key)
  41. {
  42. // publisher
  43. case 'administrator':
  44. $user['privs'] = 1;
  45. break;
  46. // managing editor
  47. case 'editor':
  48. $user['privs'] = 2;
  49. break;
  50. // staff writer
  51. case 'author':
  52. $user['privs'] = 4;
  53. break;
  54. // freelancer
  55. case 'contributor':
  56. $user['privs'] = 5;
  57. break;
  58. // none
  59. case 'subscriber':
  60. default:
  61. $user['privs'] = 0;
  62. break;
  63. }
  64. }
  65. $users[] = $user;
  66. }
  67. /*
  68. export article and link categories
  69. */
  70. $categories = array();
  71. $category_query = mysql_query("
  72. select
  73. t.slug as name,
  74. t.name as title,
  75. tt.taxonomy as type,
  76. tt.parent as parent
  77. from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt
  78. on(t.term_id = tt.term_id)
  79. order by field(tt.taxonomy, 'category','post_tag','link_category'), tt.parent asc, t.name asc
  80. ", $b2link) or $errors[] = mysql_error();
  81. while ($category = mysql_fetch_array($category_query))
  82. {
  83. if ($category['parent'] != 0)
  84. {
  85. $category_parent_query = mysql_query("
  86. select
  87. slug as name
  88. from ".$wpdbprefix."terms
  89. where term_id = '".doSlash($category['parent'])."'
  90. ", $b2link) or $errors[] = mysql_error();
  91. while ($parent = mysql_fetch_array($category_parent_query))
  92. {
  93. $category['parent'] = $parent['name'];
  94. }
  95. }
  96. else
  97. {
  98. $category['parent'] = 'root';
  99. }
  100. switch ($category['type'])
  101. {
  102. case 'post_tag':
  103. case 'category':
  104. $category['type'] = 'article';
  105. break;
  106. case 'link_category':
  107. $category['type'] = 'link';
  108. break;
  109. }
  110. $categories[] = $category;
  111. }
  112. /*
  113. export articles
  114. */
  115. $article_query = mysql_query("
  116. select
  117. p.ID as ID,
  118. p.post_status as Status,
  119. p.post_date as Posted,
  120. p.post_modified as LastMod,
  121. p.post_title as Title,
  122. p.post_content as Body,
  123. p.comment_status as Annotate,
  124. p.comment_count as comments_count,
  125. p.post_name as url_title,
  126. u.user_login as AuthorID
  127. from ".$wpdbprefix."posts as p left join ".$wpdbprefix."users as u
  128. on u.ID = p.post_author
  129. order by p.ID asc
  130. ", $b2link) or $errors[] = mysql_error();
  131. while ($article = mysql_fetch_array($article_query))
  132. {
  133. // convert WP article status to Txp equivalent
  134. switch ($article['Status'])
  135. {
  136. case 'draft':
  137. $article['Status'] = 1;
  138. break;
  139. // hidden
  140. case 'private':
  141. $article['Status'] = 2;
  142. break;
  143. case 'pending':
  144. $article['Status'] = 3;
  145. break;
  146. // live
  147. case 'publish':
  148. $article['Status'] = 4;
  149. break;
  150. default:
  151. $article['Status'] = $insert_with_status;
  152. break;
  153. }
  154. // convert WP comment status to Txp equivalent
  155. switch ($article['Annotate'])
  156. {
  157. // on
  158. case 'open':
  159. $article['Annotate'] = 1;
  160. break;
  161. // off
  162. case 'closed':
  163. case 'registered_only':
  164. $article['Annotate'] = 0;
  165. break;
  166. }
  167. // article commments
  168. $comments = array();
  169. $comment_query = mysql_query("
  170. select
  171. comment_author_IP as ip,
  172. comment_author as name,
  173. comment_author_email as email,
  174. comment_author_url as web,
  175. comment_content as message,
  176. comment_date as posted
  177. from ".$wpdbprefix."comments
  178. where comment_post_ID = '".$article['ID']."'
  179. order by comment_ID asc
  180. ", $b2link) or $errors[]= mysql_error();
  181. while ($comment = mysql_fetch_assoc($comment_query))
  182. {
  183. $comments[] = $comment;
  184. }
  185. $article['comments'] = $comments;
  186. // article categories
  187. $article_categories = array();
  188. $article_category_query = mysql_query("
  189. select
  190. t.name as title,
  191. t.slug as name
  192. from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt
  193. on(t.term_id = tt.term_id)
  194. inner join ".$wpdbprefix."term_relationships as tr
  195. on(tt.term_taxonomy_id = tr.term_taxonomy_id)
  196. where tr.object_id = '".$article['ID']."' and tt.taxonomy in('post_tag', 'category')
  197. order by tr.object_id asc, t.name asc
  198. limit 2;
  199. ", $b2link) or $errors[] = mysql_error();
  200. while ($category = mysql_fetch_array($article_category_query))
  201. {
  202. $article_categories[] = $category;
  203. }
  204. $article['Category1'] = !empty($article_categories[0]) ? $article_categories[0]['name'] : '';
  205. $article['Category2'] = !empty($article_categories[1]) ? $article_categories[1]['name'] : '';
  206. $articles[] = $article;
  207. }
  208. /*
  209. export links
  210. */
  211. $links = array();
  212. $link_query = mysql_query("
  213. select
  214. link_id as id,
  215. link_name as linkname,
  216. link_description as description,
  217. link_updated as date,
  218. link_url as url
  219. from ".$wpdbprefix."links
  220. order by link_id asc
  221. ", $b2link) or $errors[] = mysql_error();
  222. while ($link = mysql_fetch_array($link_query))
  223. {
  224. // link categories
  225. $link_categories = array();
  226. $link_category_query = mysql_query("
  227. select
  228. t.name as title,
  229. t.slug as name
  230. from ".$wpdbprefix."terms as t inner join ".$wpdbprefix."term_taxonomy as tt
  231. on(t.term_id = tt.term_id)
  232. inner join ".$wpdbprefix."term_relationships as tr
  233. on(tt.term_taxonomy_id = tr.term_taxonomy_id)
  234. where tr.object_id = '".$link['id']."' and tt.taxonomy = 'link_category'
  235. order by tr.object_id asc, t.name asc
  236. ", $b2link) or $errors[] = mysql_error();
  237. while ($category = mysql_fetch_array($link_category_query))
  238. {
  239. $link['category'] = $category['name'];
  240. }
  241. $links[] = $link;
  242. }
  243. mysql_close($b2link);
  244. /*
  245. begin import
  246. */
  247. // keep a handy copy of txpdb values, and do not alter Dean code
  248. // for now! ;-)
  249. $txpdb = $txpcfg['db'];
  250. $txpdblogin = $txpcfg['user'];
  251. $txpdbpass = $txpcfg['pass'];
  252. $txpdbhost = $txpcfg['host'];
  253. // Yes, we have to make a new connection
  254. // otherwise doArray complains
  255. $DB = new DB;
  256. $txplink = &$DB->link;
  257. mysql_select_db($txpdb, $txplink);
  258. /*
  259. import users
  260. */
  261. if ($users)
  262. {
  263. include_once txpath.'/lib/txplib_admin.php';
  264. $results[] = hed('Imported Users:', 2).
  265. n.graf('Because WordPress uses a different password mechanism than Textpattern, you will need to reset each user&#8217;s password from <a href="index.php?event=admin">the Users tab</a>.').
  266. n.'<ul>';
  267. foreach ($users as $user)
  268. {
  269. extract($user);
  270. if (!safe_row('user_id', 'txp_users', "name = '".doSlash($name)."'"))
  271. {
  272. $pass = doSlash(generate_password(6));
  273. $nonce = doSlash(md5(uniqid(mt_rand(), TRUE)));
  274. $rs = mysql_query("
  275. insert into ".safe_pfx('txp_users')." set
  276. name = '".doSlash($name)."',
  277. pass = '".doSlash($pass)."',
  278. email = '".doSlash($email)."',
  279. RealName = '".doSlash($RealName)."',
  280. privs = ".$privs.",
  281. nonce = '".doSlash($nonce)."'
  282. ", $txplink) or $errors[] = mysql_error();
  283. if (mysql_insert_id())
  284. {
  285. $results[] = '<li>'.$name.' ('.$RealName.')</li>';
  286. }
  287. }
  288. }
  289. $results[] = '</ul>';
  290. }
  291. /*
  292. import categories
  293. */
  294. if ($categories)
  295. {
  296. $results[] = hed('Imported Categories:', 2).n.'<ul>';
  297. foreach ($categories as $category)
  298. {
  299. extract($category);
  300. if (!safe_row('id', 'txp_category', "name = '".doSlash($name)."' and type = '".doSlash($type)."' and parent = '".doSlash($parent)."'"))
  301. {
  302. $rs = mysql_query("
  303. insert into ".safe_pfx('txp_category')." set
  304. name = '".doSlash($name)."',
  305. title = '".doSlash($title)."',
  306. type = '".doSlash($type)."',
  307. parent = '".doSlash($parent)."'
  308. ", $txplink) or $errors[] = mysql_error();
  309. if (mysql_insert_id())
  310. {
  311. $results[] = '<li>'.$title.' ('.$type.')</li>';
  312. }
  313. }
  314. }
  315. rebuild_tree_full('article');
  316. rebuild_tree_full('link');
  317. $results[] = '</ul>';
  318. }
  319. /*
  320. import articles
  321. */
  322. if ($articles)
  323. {
  324. $results[] = hed('Imported Articles and Comments:', 2).n.'<ul>';
  325. include txpath.'/lib/classTextile.php';
  326. $textile = new Textile;
  327. foreach ($articles as $article)
  328. {
  329. extract($article);
  330. // Ugly, really ugly way to workaround the slashes WP gotcha
  331. $Body = str_replace('<!--more-->', '', $Body);
  332. $Body_html = $textile->textileThis($Body);
  333. // can not use array slash due to way on which comments are selected
  334. $rs = mysql_query("
  335. insert into ".safe_pfx('textpattern')." set
  336. Posted = '".doSlash($Posted)."',
  337. LastMod = '".doSlash($LastMod)."',
  338. Title = '".doSlash($textile->TextileThis($Title, 1))."',
  339. url_title = '".doSlash($url_title)."',
  340. Body = '".doSlash($Body)."',
  341. Body_html = '".doSlash($Body_html)."',
  342. AuthorID = '".doSlash($AuthorID)."',
  343. Category1 = '".doSlash($Category1)."',
  344. Category2 = '".doSlash($Category2)."',
  345. Section = '$insert_into_section',
  346. uid = '".md5(uniqid(rand(), true))."',
  347. feed_time = '".substr($Posted, 0, 10)."',
  348. Annotate = '".doSlash($Annotate)."',
  349. AnnotateInvite = '$default_comment_invite',
  350. Status = '".doSlash($Status)."'
  351. ", $txplink) or $errors[] = mysql_error();
  352. if ((int)$insert_id = mysql_insert_id())
  353. {
  354. $results[] = '<li>'.$Title.'</li>';
  355. if (!empty($comments))
  356. {
  357. $inserted_comments = 0;
  358. foreach ($comments as $comment)
  359. {
  360. extract(array_slash($comment));
  361. // The ugly workaroud again
  362. $message = nl2br($message);
  363. $rs = mysql_query("
  364. insert into ".safe_pfx('txp_discuss')." set
  365. parentid = '$insert_id',
  366. name = '".doSlash($name)."',
  367. email = '".doSlash($email)."',
  368. web = '".doSlash($web)."',
  369. ip = '".doSlash($ip)."',
  370. posted = '".doSlash($posted)."',
  371. message = '".doSlash($message)."',
  372. visible = 1
  373. ", $txplink) or $results[] = mysql_error();
  374. if (mysql_insert_id())
  375. {
  376. $inserted_comments++;
  377. }
  378. }
  379. $results[] = '<li>- '.$inserted_comments.' of '.$comments_count.' comment(s)</li>';
  380. }
  381. }
  382. }
  383. $results[] = '</ul>';
  384. }
  385. /*
  386. import links
  387. */
  388. if ($links)
  389. {
  390. $results[] = hed('Imported Links:', 2).n.'<ul>';
  391. foreach ($links as $link)
  392. {
  393. extract($link);
  394. $rs = mysql_query("
  395. insert into ".safe_pfx('txp_link')." set
  396. linkname = '".doSlash($linkname)."',
  397. linksort = '".doSlash($linkname)."',
  398. description = '".doSlash($description)."',
  399. category = '".doSlash($category)."',
  400. date = '".doSlash($date)."',
  401. url = '".doSlash($url)."'
  402. ", $txplink) or $errors[] = mysql_error();
  403. if (mysql_insert_id())
  404. {
  405. $results[] = '<li>'.$linkname.'</li>';
  406. }
  407. }
  408. $results[] = '</ul>';
  409. }
  410. /*
  411. show any errors we encountered
  412. */
  413. if ($errors)
  414. {
  415. $results[] = hed('Errors Encountered:', 2).n.'<ul>';
  416. foreach ($errors as $error)
  417. {
  418. $results[] = '<li>'.$error.'</li>';
  419. }
  420. $results[] = '</ul>';
  421. }
  422. return join(n, $results);
  423. }
  424. function undoSlash($in)
  425. {
  426. return doArray($in, 'stripslashes');
  427. }
  428. ?>