PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/blog/kwa-to-bxb-import.php

https://github.com/chregu/fluxcms
PHP | 368 lines | 267 code | 79 blank | 22 comment | 19 complexity | e964cdc77bd333cd9469f14a4375860f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. #!/usr/local/bin/php5
  2. <?php
  3. /**
  4. * vars - please adjust to your needs.
  5. */
  6. /* docroot of BXB */
  7. define("BXB_DOCROOT", '');
  8. /* tidy */
  9. define("TIDY_BIN", '/usr/bin/tidy -q');
  10. /* unimportant ;) */
  11. define("MSG_PREFX", '[kwa2bxb]: ');
  12. /* tmp-dir */
  13. define("TMP_DIR", "/tmp");
  14. /* dsn for kaywa-blog db (thatdb) */
  15. $dsn_kwa = 'mysql://user:pass@localhost/kwa';
  16. /* dsn for bxb-db (thisdb) */
  17. $dsn_bxb = 'mysql://user:pass@localhost/etoy';
  18. /* you shouldn't need to change something below this line */
  19. if (is_dir(BXB_DOCROOT)) {
  20. ini_set("include_path", ini_get("include_path") . ":" . BXB_DOCROOT."/inc");
  21. }
  22. include_once 'MDB2.php';
  23. include_once '../conf/config.inc.php';
  24. include_once 'SQL/Tree.php';
  25. /* KWA db */
  26. $thatdb = MDB2::connect($dsn_kwa);
  27. if (MDB2::isError($thatdb)) {
  28. print MSG_PREFIX." db connect with kwa-dsn: $dsn_kwa failed!\n";
  29. exit(1);
  30. }
  31. /* BXB db */
  32. $thisdb = MDB2::connect($dsn_bxb);
  33. if (MDB2::isError($thisdb)) {
  34. print MSG_PREFIX." db connect with bxb-dsn: $dsn_bxb failed!\n";
  35. exit(1);
  36. }
  37. $moblogTable = "image";
  38. $tables = array(/* Posts */
  39. 'posts' => array('thistablename' => 'blogposts',
  40. 'thattablename' => 'posts',
  41. 'fields' => array('id' => 'id', // mdb2 lowercases ID
  42. 'post_author' => 'post_author',
  43. 'post_date' => 'post_date',
  44. 'post_content' => 'post_content',
  45. 'post_title' => 'post_title',
  46. 'post_category' => 'post_category',
  47. 'post_karma' => '',
  48. 'post_uri' => 'post_uri',
  49. 'changed' => 'post_modified',
  50. 'post_comment_mode'=> '',
  51. 'post_status' => 'status'
  52. ),
  53. 'callbacks' => array('post_content' => array('utf2entities', 'tidy'),
  54. 'post_title' => array('utf2entities', 'tidy'),
  55. 'post_uri' => array('makePostUri')
  56. )
  57. ),
  58. /* Users */
  59. 'users' => array('thistablename' => 'users',
  60. 'thattablename' => 'users',
  61. 'fields' => array('id' => 'id',
  62. 'user_login' => 'user_login',
  63. 'user_pass' => 'user_pass',
  64. 'user_fullname' => '',
  65. // 'user_nick' => 'user_nickname',
  66. 'user_email' => 'user_email'
  67. ),
  68. 'callbacks' => array()
  69. ),
  70. /* Categories */
  71. 'cats' => array('thistablename' => 'blogcategories',
  72. 'thattablename' => 'categories',
  73. 'fields' => array('id' => 'cat_id',
  74. 'name' => 'cat_name',
  75. 'uri' => 'cat_name'),
  76. 'callbacks' => array('uri' => array('makeUri'),
  77. 'id' => array('plusOne')),
  78. 'defaults' => array(array('id' => 1,
  79. 'name' => 'All',
  80. 'uri' => 'root',
  81. 'parentid' => 0)
  82. )
  83. ),
  84. /* Blogroll */
  85. 'links' => array('thistablename' => 'bloglinks',
  86. 'thattablename' => 'blogroll',
  87. 'fields' => array('id' => 'id',
  88. 'link' => 'url',
  89. 'text' => 'name',
  90. 'bloglinkscategories' => 'cat'
  91. ),
  92. 'callbacks' => array()
  93. ),
  94. /* Blogrollcats */
  95. 'linkcat'=>array('thistablename' => 'bloglinkscategories',
  96. 'thattablename' => 'blogroll_cats',
  97. 'fields' => array('id' => 'id',
  98. 'name' => 'name',
  99. 'rang' => 'rang'
  100. ),
  101. 'callbacks' => array()
  102. ),
  103. /* comments */
  104. 'comms' => array('thistablename' => 'blogcomments',
  105. 'thattablename' => 'comments',
  106. 'fields' => array('id' => 'comment_id',
  107. 'comment_posts_id' => 'comment_post_id',
  108. 'comment_author' => 'comment_author',
  109. 'comment_author_email' => 'comment_author_email',
  110. 'comment_author_url' => 'comment_author_url',
  111. 'comment_author_ip' => 'comment_author_ip',
  112. 'comment_date' => 'comment_date',
  113. 'comment_content' => 'comment_content'
  114. ),
  115. 'callbacks' => array()
  116. )
  117. );
  118. importTables($tables, $thisdb, $thatdb, 'id');
  119. importTableTree('blogcategories', 'id', 'parentid', 'fulluri', 'fullname', 'uri', 'name',
  120. array('name','uri','fulluri'), $thisdb
  121. );
  122. function importTables($tables, &$thisdb, &$thatdb, $thisIdField=NULL) {
  123. foreach($tables as $table) {
  124. importTable($table, $thisdb, $thatdb);
  125. }
  126. }
  127. // import blogposts2categories
  128. $thisdb->query( "delete from blogposts2categories");
  129. $thisdb->query( "insert into blogposts2categories (blogposts_id, blogcategories_id) select id, post_category + 1 from blogposts;");
  130. // update authorid 2 authorname
  131. $thisdb->query("ALTER TABLE `blogposts` ADD `post_authorid` INT NOT NULL AFTER `post_author` ;");
  132. $thisdb->query("update blogposts set post_authorid = post_author ;");
  133. $thisdb->query("ALTER TABLE `blogposts` CHANGE `post_author` `post_author` VARCHAR( 40 ) DEFAULT '' NOT NULL ;");
  134. $thisdb->query("ALTER TABLE `blogposts` ADD INDEX ( `post_author` ) ;");
  135. $thisdb->query("update blogposts, users set post_author = users.user_login where users.ID = blogposts.post_authorid ;");
  136. $thisdb->query("ALTER TABLE `blogposts` DROP `post_authorid` ;");
  137. importMoblogs($moblogTable,$tables,$thisdb,$thatdb);
  138. function importTable($table, &$thisdb, &$thatdb, $thisIdField=NULL) {
  139. $thatquery = sprintf("SELECT * FROM %s", $table['thattablename']);
  140. if (is_object($thatdb) && is_object($thisdb)) {
  141. if (importDeleteTable($table['thistablename'], $thisdb)) {
  142. $thatres = $thatdb->query($thatquery);
  143. if (!MDB2::isError($thatres)) {
  144. print MSG_PREFX."start importing from ".$table['thattablename']." to ". $table['thistablename']."\n";
  145. if (isset($table['defaults'])) {
  146. foreach($table['defaults'] as $i => $values) {
  147. $thisquery = importPrepareQuery($table['thistablename'], $values);
  148. if ($thisquery) {
  149. $thisinsert = $thisdb->query($thisquery);
  150. }
  151. }
  152. }
  153. while($thatrow = $thatres->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  154. $thisrow = importPrepareValues($table['fields'], $thatrow, $table['callbacks'], $thisdb);
  155. if (is_array($thisrow)) {
  156. $thisquery = importPrepareQuery($table['thistablename'], $thisrow);
  157. if ($thisquery) {
  158. $thisinsert = $thisdb->query($thisquery);
  159. if (MDB2::isError($thisinsert)) {
  160. echo $thisinsert->getMessage()."\n";
  161. echo $thisquery."\n\n";
  162. }
  163. }
  164. }
  165. }
  166. } else {
  167. echo MSG_PREFX.$thatres->getMessage()."\n";
  168. }
  169. }
  170. }
  171. }
  172. function importPrepareValues($fields, $values, $callbacks=array(), &$db=null) {
  173. $thisresults = array();
  174. foreach($values as $field => $value) {
  175. // thiskeys: keys of thisresults to which value of values[field] applies
  176. $thiskeys = array_keys( $fields, $field, 0);
  177. foreach($thiskeys as $thiskey) {
  178. if (is_array($callbacks[$thiskey])) {
  179. foreach($callbacks[$thiskey] as $c => $callback) {
  180. if (function_exists($callback)) {
  181. $value = call_user_func($callback, $value, $values);
  182. }
  183. }
  184. }
  185. $thisresults[$thiskey]= addslashes(trim($value));
  186. }
  187. }
  188. return $thisresults;
  189. }
  190. function importPrepareQuery($table, $input) {
  191. $fields = sprintf("(%s)", implode(",", array_keys($input)) );
  192. $values = sprintf("('%s')", implode('\',\'', array_values($input)));
  193. return sprintf("INSERT INTO %s%s VALUES%s", $table, $fields, $values);
  194. }
  195. function importDeleteTable($table, &$db) {
  196. print (sprintf("DELETE FROM %s", $table));
  197. if (!MDB2::isError($db->query(sprintf("DELETE FROM %s", $table)))) {
  198. if (!MDB2::isError($db->query(sprintf("ALTER TABLE %s auto_increment=0", $table)))) {
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. function tidy($input) {
  205. if (is_executable(TIDY_BIN)) {
  206. $tmpfile = sprintf("%s/%s", TMP_DIR, uniqid('BXB'));
  207. if (file_put_contents($tmpfile, $input) > 0) {
  208. $tidycmd = escapeshellcmd(TIDY_BIN." -wrap 0 --indent-spaces 0 --indent 1 --force-output y -q -asxhtml --numeric-entities yes --show-body-only y ".$tmpfile);
  209. $out = `$tidycmd`;
  210. unlink($tmpfile);
  211. return $out;
  212. }
  213. }
  214. return $input;
  215. }
  216. function utf2entities($input) {
  217. return $input;
  218. }
  219. function plusOne($input = 0) {
  220. print $input."\n";
  221. return $input + 1;
  222. }
  223. function makeUri($title) {
  224. $title = trim($title);
  225. $newValue= strtolower(utf8_encode(preg_replace("/[-;: \.!,?+'$£\"*ç%&\/\(\)=]/","-",$title)));
  226. $newValue= preg_replace("/-{2,}/","-",$newValue);
  227. $newValue= preg_replace("/[öÖ]/u","oe",$newValue);
  228. $newValue= preg_replace("/[üÜ]/u","ue",$newValue);
  229. $newValue= preg_replace("/[äÄ]/u","ae",$newValue);
  230. $newValue= preg_replace("/[éè]/u","e",$newValue);
  231. $newValue= preg_replace("/[ï]/u","i",$newValue);
  232. $newValue= preg_replace("/[ñ]/u","n",$newValue);
  233. $newValue= preg_replace("/[à]/u","a",$newValue);
  234. //numbers at the end are nasty for bxcms
  235. $newValue= preg_replace("/—+$/","",$newValue);
  236. $newValue= preg_replace("/^-/","",$newValue);
  237. return $newValue;
  238. }
  239. function makePostUri($uri, $fields){
  240. if (trim($uri)) {
  241. return $uri;
  242. } else if (trim($fields['post_title']) && $fields['post_title'] != "..." ) {
  243. return makeUri($fields['post_title']);
  244. } else {
  245. return 'p'.$fields['id'];
  246. }
  247. }
  248. function importTableTree($tablename, $idField, $refField, $fullPath, $fullTitlePath, $path, $title, $data, &$db ) {
  249. $tree = new SQL_Tree($db);
  250. $tree->idField = $idField;
  251. $tree->referenceField = $refField;
  252. $tree->tablename = $tablename;
  253. $tree->FullPath = $fullPath;
  254. $tree->FullTitlePath = $fullTitlePath;
  255. $tree->Path = $path;
  256. $tree->Title = $title;
  257. $tree->fullnameSeparator = " :: ";
  258. $query = $tree->children_query(1,$data,True);
  259. $tree->importTree(1,true,"name");
  260. return null;
  261. }
  262. function importMoblogs ($moblogTable, $tables, &$thisdb, &$thatdb) {
  263. $query = "select image_posts_ID, image_name, image_alt from $moblogTable where image_inline = 0";
  264. $res = $thatdb->query($query);
  265. while ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
  266. $query = " update blogposts set post_content = concat(post_content, '\n<p><a href=\"/files/images/moblogs/";
  267. $query .= $row['image_name']."\"><img src=\"/dynimages/180/files/images/moblogs/".$row['image_name']."\"/></a><br/>\n";
  268. $query .= "</p>\n') where id = ".$row['image_posts_id'];
  269. print $query ."\n";
  270. $res2 = $thisdb->query($query);
  271. if (MDB2::isError($res2)) {
  272. echo $res2->getMessage()."\n";
  273. echo $res2."\n\n";
  274. }
  275. }
  276. }
  277. ?>